-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: decouple ui container from backend
- Loading branch information
1 parent
dfec951
commit ce962cf
Showing
15 changed files
with
440 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
src/main/java/org/trustify/operator/cdrs/v2alpha1/ui/UIDeployment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
package org.trustify.operator.cdrs.v2alpha1.ui; | ||
|
||
import io.fabric8.kubernetes.api.model.*; | ||
import io.fabric8.kubernetes.api.model.apps.*; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; | ||
import io.javaoperatorsdk.operator.processing.dependent.Matcher; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; | ||
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.trustify.operator.Config; | ||
import org.trustify.operator.Constants; | ||
import org.trustify.operator.cdrs.v2alpha1.Trustify; | ||
import org.trustify.operator.cdrs.v2alpha1.TrustifySpec; | ||
import org.trustify.operator.cdrs.v2alpha1.server.ServerDeployment; | ||
import org.trustify.operator.cdrs.v2alpha1.server.ServerService; | ||
import org.trustify.operator.utils.CRDUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@KubernetesDependent(labelSelector = UIDeployment.LABEL_SELECTOR, resourceDiscriminator = UIDeploymentDiscriminator.class) | ||
@ApplicationScoped | ||
public class UIDeployment extends CRUDKubernetesDependentResource<Deployment, Trustify> | ||
implements Matcher<Deployment, Trustify>, Condition<Deployment, Trustify> { | ||
|
||
public static final String LABEL_SELECTOR = "app.kubernetes.io/managed-by=trustify-operator,component=ui"; | ||
|
||
@Inject | ||
Config config; | ||
|
||
public UIDeployment() { | ||
super(Deployment.class); | ||
} | ||
|
||
@Override | ||
protected Deployment desired(Trustify cr, Context<Trustify> context) { | ||
return newDeployment(cr, context); | ||
} | ||
|
||
@Override | ||
public Result<Deployment> match(Deployment actual, Trustify cr, Context<Trustify> context) { | ||
final var container = actual.getSpec() | ||
.getTemplate().getSpec().getContainers() | ||
.stream() | ||
.findFirst(); | ||
|
||
return Result.nonComputed(container | ||
.map(c -> c.getImage() != null) | ||
.orElse(false) | ||
); | ||
} | ||
|
||
@Override | ||
public boolean isMet(DependentResource<Deployment, Trustify> dependentResource, Trustify primary, Context<Trustify> context) { | ||
return context.getSecondaryResource(Deployment.class, new UIDeploymentDiscriminator()) | ||
.map(deployment -> { | ||
final var status = deployment.getStatus(); | ||
if (status != null) { | ||
final var readyReplicas = status.getReadyReplicas(); | ||
return readyReplicas != null && readyReplicas >= 1; | ||
} | ||
return false; | ||
}) | ||
.orElse(false); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Deployment newDeployment(Trustify cr, Context<Trustify> context) { | ||
final var contextLabels = (Map<String, String>) context.managedDependentResourceContext() | ||
.getMandatory(Constants.CONTEXT_LABELS_KEY, Map.class); | ||
|
||
return new DeploymentBuilder() | ||
.withNewMetadata() | ||
.withName(getDeploymentName(cr)) | ||
.withNamespace(cr.getMetadata().getNamespace()) | ||
.withLabels(contextLabels) | ||
.addToLabels("component", "ui") | ||
.addToLabels(Map.of( | ||
"app.openshift.io/runtime", "nodejs" | ||
)) | ||
.withAnnotations(Map.of("app.openshift.io/connects-to", """ | ||
[{"apiVersion": "apps/v1", "kind":"Deployment", "name": "%s"}] | ||
""".formatted(ServerDeployment.getDeploymentName(cr)) | ||
)) | ||
.withOwnerReferences(CRDUtils.getOwnerReference(cr)) | ||
.endMetadata() | ||
.withSpec(getDeploymentSpec(cr, context)) | ||
.build(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private DeploymentSpec getDeploymentSpec(Trustify cr, Context<Trustify> context) { | ||
final var contextLabels = (Map<String, String>) context.managedDependentResourceContext() | ||
.getMandatory(Constants.CONTEXT_LABELS_KEY, Map.class); | ||
|
||
Map<String, String> selectorLabels = Constants.UI_SELECTOR_LABELS; | ||
String image = Optional.ofNullable(cr.getSpec().uiImage()).orElse(config.uiImage()); | ||
String imagePullPolicy = Optional.ofNullable(cr.getSpec().imagePullPolicy()).orElse(config.imagePullPolicy()); | ||
|
||
TrustifySpec.ResourcesLimitSpec resourcesLimitSpec = CRDUtils.getValueFromSubSpec(cr.getSpec(), TrustifySpec::uiResourceLimitSpec) | ||
.orElse(null); | ||
|
||
return new DeploymentSpecBuilder() | ||
.withStrategy(new DeploymentStrategyBuilder() | ||
.withType("Recreate") | ||
.build() | ||
) | ||
.withReplicas(1) | ||
.withSelector(new LabelSelectorBuilder() | ||
.withMatchLabels(selectorLabels) | ||
.build() | ||
) | ||
.withTemplate(new PodTemplateSpecBuilder() | ||
.withNewMetadata() | ||
.withLabels(Stream | ||
.concat(contextLabels.entrySet().stream(), selectorLabels.entrySet().stream()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) | ||
) | ||
.endMetadata() | ||
.withSpec(new PodSpecBuilder() | ||
.withRestartPolicy("Always") | ||
.withTerminationGracePeriodSeconds(60L) | ||
.withImagePullSecrets(cr.getSpec().imagePullSecrets()) | ||
.withContainers(new ContainerBuilder() | ||
.withName(Constants.TRUSTI_UI_NAME) | ||
.withImage(image) | ||
.withImagePullPolicy(imagePullPolicy) | ||
.withEnv(getEnvVars(cr)) | ||
.withPorts( | ||
new ContainerPortBuilder() | ||
.withName("http") | ||
.withProtocol("TCP") | ||
.withContainerPort(Constants.HTTP_PORT) | ||
.build() | ||
) | ||
.withLivenessProbe(new ProbeBuilder() | ||
.withExec(new ExecActionBuilder() | ||
.withCommand( | ||
"/bin/sh", | ||
"-c", | ||
"ps -A | grep node" | ||
) | ||
.build() | ||
) | ||
.withInitialDelaySeconds(10) | ||
.withTimeoutSeconds(1) | ||
.withPeriodSeconds(5) | ||
.withSuccessThreshold(1) | ||
.withFailureThreshold(3) | ||
.build() | ||
) | ||
.withReadinessProbe(new ProbeBuilder() | ||
.withHttpGet(new HTTPGetActionBuilder() | ||
.withPath("/") | ||
.withNewPort(Constants.HTTP_PORT) | ||
.withScheme("HTTP") | ||
.build() | ||
) | ||
.withInitialDelaySeconds(10) | ||
.withTimeoutSeconds(1) | ||
.withPeriodSeconds(5) | ||
.withSuccessThreshold(1) | ||
.withFailureThreshold(3) | ||
.build() | ||
) | ||
.withResources(new ResourceRequirementsBuilder() | ||
.withRequests(Map.of( | ||
"cpu", new Quantity(CRDUtils.getValueFromSubSpec(resourcesLimitSpec, TrustifySpec.ResourcesLimitSpec::cpuRequest).orElse("100m")), | ||
"memory", new Quantity(CRDUtils.getValueFromSubSpec(resourcesLimitSpec, TrustifySpec.ResourcesLimitSpec::memoryRequest).orElse("350Mi")) | ||
)) | ||
.withLimits(Map.of( | ||
"cpu", new Quantity(CRDUtils.getValueFromSubSpec(resourcesLimitSpec, TrustifySpec.ResourcesLimitSpec::cpuLimit).orElse("500m")), | ||
"memory", new Quantity(CRDUtils.getValueFromSubSpec(resourcesLimitSpec, TrustifySpec.ResourcesLimitSpec::memoryLimit).orElse("800Mi")) | ||
)) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build(); | ||
} | ||
|
||
private List<EnvVar> getEnvVars(Trustify cr) { | ||
return Arrays.asList( | ||
new EnvVarBuilder() | ||
.withName("AUTH_REQUIRED") | ||
.withValue("false") | ||
.build(), | ||
new EnvVarBuilder() | ||
.withName("ANALYTICS_ENABLED") | ||
.withValue("false") | ||
.build(), | ||
new EnvVarBuilder() | ||
.withName("TRUSTIFY_API_URL") | ||
.withValue(ServerService.getServiceName(cr)) | ||
.build(), | ||
new EnvVarBuilder() | ||
.withName("UI_INGRESS_PROXY_BODY_SIZE") | ||
.withValue("50m") | ||
.build(), | ||
new EnvVarBuilder() | ||
.withName("NODE_EXTRA_CA_CERTS") | ||
.withValue("/opt/app-root/src/ca.crt") | ||
.build() | ||
); | ||
} | ||
|
||
public static String getDeploymentName(Trustify cr) { | ||
return cr.getMetadata().getName() + Constants.UI_DEPLOYMENT_SUFFIX; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/trustify/operator/cdrs/v2alpha1/ui/UIDeploymentDiscriminator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.trustify.operator.cdrs.v2alpha1.ui; | ||
|
||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator; | ||
import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; | ||
import org.trustify.operator.cdrs.v2alpha1.Trustify; | ||
import org.trustify.operator.controllers.TrustifyReconciler; | ||
|
||
import java.util.Optional; | ||
|
||
public class UIDeploymentDiscriminator implements ResourceDiscriminator<Deployment, Trustify> { | ||
@Override | ||
public Optional<Deployment> distinguish(Class<Deployment> resource, Trustify cr, Context<Trustify> context) { | ||
String deploymentName = UIDeployment.getDeploymentName(cr); | ||
ResourceID resourceID = new ResourceID(deploymentName, cr.getMetadata().getNamespace()); | ||
var informerEventSource = (InformerEventSource<Deployment, Trustify>) context.eventSourceRetriever().getResourceEventSourceFor(Deployment.class, TrustifyReconciler.DEPLOYMENT_EVENT_SOURCE); | ||
return informerEventSource.get(resourceID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.