Skip to content

Commit

Permalink
refactor(operator): update CRD field structure and some example files
Browse files Browse the repository at this point in the history
  • Loading branch information
jsenko committed Jan 21, 2025
1 parent 88dae10 commit 0e6b933
Show file tree
Hide file tree
Showing 61 changed files with 1,401 additions and 592 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ metadata:
name: example-registry-with-studio
spec:
app:
host: example-registry-with-studio-app.apps.yourcluster.example
host: example-registry-with-studio-app.apps.cluster.example
ui:
host: example-registry-with-studio-ui.apps.yourcluster.example
host: example-registry-with-studio-ui.apps.cluster.example
studioUi:
enabled: true
host: example-registry-with-studio-studio-ui.apps.yourcluster.example
host: example-registry-with-studio-studio-ui.apps.cluster.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
apiVersion: registry.apicur.io/v1
kind: ApicurioRegistry3
metadata:
name: simple-apicurioregistry3
name: simple
spec:
app:
host: simple-apicurioregistry3-app.apps.yourcluster.example
ingress:
host: simple-app.apps.cluster.example
ui:
host: simple-apicurioregistry3-ui.apps.yourcluster.example
ingress:
host: simple-ui.apps.cluster.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.apicurio.registry.operator;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.resource.ActivationConditions.UIIngressActivationCondition;
import io.apicurio.registry.operator.resource.LabelDiscriminators.AppDeploymentDiscriminator;
import io.apicurio.registry.operator.resource.app.AppDeploymentResource;
import io.apicurio.registry.operator.resource.app.AppIngressResource;
Expand All @@ -12,6 +11,9 @@
import io.apicurio.registry.operator.resource.ui.UIDeploymentResource;
import io.apicurio.registry.operator.resource.ui.UIIngressResource;
import io.apicurio.registry.operator.resource.ui.UIServiceResource;
import io.apicurio.registry.operator.updater.IngressCRUpdater;
import io.apicurio.registry.operator.updater.KafkaSqlCRUpdater;
import io.apicurio.registry.operator.updater.SqlCRUpdater;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
Expand Down Expand Up @@ -86,6 +88,17 @@ public UpdateControl<ApicurioRegistry3> reconcile(ApicurioRegistry3 primary,
Context<ApicurioRegistry3> context) {

log.info("Reconciling Apicurio Registry: {}", primary);

// Some of the fields in the CR have been deprecated and another fields should be used instead.
// Operator will attempt to update the CR to use the newer fields if possible.
// This has to be done first, so subsequent functionality can deal with new fields only.
var update = IngressCRUpdater.update(primary);
update = SqlCRUpdater.update(primary) || update;
update = KafkaSqlCRUpdater.update(primary) || update;
if (update) {
return UpdateControl.updateResource(primary);
}

var statusUpdater = new StatusUpdater(primary);

return context.getSecondaryResource(Deployment.class, AppDeploymentDiscriminator.INSTANCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Status;
import io.apicurio.registry.operator.api.v1.status.Condition;
import io.apicurio.registry.operator.api.v1.status.ConditionStatus;
import io.apicurio.registry.operator.api.v1.status.Conditions;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,13 +28,13 @@ public StatusUpdater(ApicurioRegistry3 registry) {
this.registry = registry;
}

private Conditions defaultCondition() {
var conditions = new Conditions();
conditions.setStatus(ConditionStatus.TRUE);
conditions.setObservedGeneration(
private Condition defaultCondition() {
var condition = new Condition();
condition.setStatus(ConditionStatus.TRUE);
condition.setObservedGeneration(
registry.getMetadata() == null ? null : registry.getMetadata().getGeneration());
conditions.setLastTransitionTime(Instant.now());
return conditions;
condition.setLastTransitionTime(Instant.now());
return condition;
}

public ApicurioRegistry3Status errorStatus(Exception e) {
Expand All @@ -56,7 +56,7 @@ public ApicurioRegistry3Status next(Deployment deployment) {
if (deployment.getStatus().getConditions().stream()
.anyMatch(condition -> condition.getStatus().equalsIgnoreCase(
ConditionStatus.TRUE.getValue()) && condition.getType().equals("Available"))
&& !registry.getStatus().getConditions().stream()
&& !registry.withStatus().getConditions().stream()
.anyMatch(condition -> condition.getType().equals(READY_TYPE))) {
var readyCondition = defaultCondition();
readyCondition.setType(READY_TYPE);
Expand All @@ -66,7 +66,7 @@ public ApicurioRegistry3Status next(Deployment deployment) {
conditions.add(readyCondition);
return registry.getStatus();
} else if (deployment.getStatus().getConditions().size() > 0
&& !registry.getStatus().getConditions().stream()
&& !registry.withStatus().getConditions().stream()
.anyMatch(condition -> condition.getStatus().getValue().equals(STARTED_TYPE))) {
var generation = registry.getMetadata() == null ? null
: registry.getMetadata().getGeneration();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package io.apicurio.registry.operator.feat;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Spec;
import io.apicurio.registry.operator.api.v1.spec.AppSpec;
import io.apicurio.registry.operator.api.v1.spec.StorageSpec;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;

import java.util.Map;

import static io.apicurio.registry.operator.resource.app.AppDeploymentResource.addEnvVar;
import static io.apicurio.registry.operator.utils.Utils.isBlank;
import static java.util.Optional.ofNullable;

public class KafkaSql {

public static String ENV_STORAGE_KIND = "APICURIO_STORAGE_KIND";
public static String ENV_KAFKASQL_BOOTSTRAP_SERVERS = "APICURIO_KAFKASQL_BOOTSTRAP_SERVERS";

public static boolean configureKafkaSQL(ApicurioRegistry3 primary, Map<String, EnvVar> env) {
if (primary.getSpec().getApp().getKafkasql() != null
&& !isBlank(primary.getSpec().getApp().getKafkasql().getBootstrapServers())) {
addEnvVar(env, new EnvVarBuilder().withName(ENV_STORAGE_KIND).withValue("kafkasql").build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_KAFKASQL_BOOTSTRAP_SERVERS)
.withValue(primary.getSpec().getApp().getKafkasql().getBootstrapServers()).build());
return true;
}
return false;
public static void configureKafkaSQL(ApicurioRegistry3 primary, Map<String, EnvVar> env) {
ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getApp).map(AppSpec::getStorage)
.map(StorageSpec::getKafkasql).ifPresent(kafkasql -> {
if (!isBlank(kafkasql.getBootstrapServers())) {
addEnvVar(env,
new EnvVarBuilder().withName(ENV_STORAGE_KIND).withValue("kafkasql").build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_KAFKASQL_BOOTSTRAP_SERVERS)
.withValue(kafkasql.getBootstrapServers()).build());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.apicurio.registry.operator.feat;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Spec;
import io.apicurio.registry.operator.api.v1.spec.AppSpec;
import io.apicurio.registry.operator.api.v1.spec.SqlSpec;
import io.apicurio.registry.operator.api.v1.spec.StorageSpec;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;

import java.util.Map;

import static io.apicurio.registry.operator.resource.app.AppDeploymentResource.addEnvVar;
import static java.util.Optional.ofNullable;

public class PostgresSql {

Expand All @@ -16,23 +21,22 @@ public class PostgresSql {
public static final String ENV_APICURIO_DATASOURCE_USERNAME = "APICURIO_DATASOURCE_USERNAME";
public static final String ENV_APICURIO_DATASOURCE_PASSWORD = "APICURIO_DATASOURCE_PASSWORD";

public static boolean configureDatasource(ApicurioRegistry3 primary, Map<String, EnvVar> env) {
if (primary.getSpec().getApp().getSql() != null
&& primary.getSpec().getApp().getSql().getDataSource() != null) {
var datasource = primary.getSpec().getApp().getSql().getDataSource();

addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_STORAGE_KIND).withValue("sql").build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_STORAGE_SQL_KIND).withValue("postgresql")
.build());

addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_URL)
.withValue(datasource.getUrl()).build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_USERNAME)
.withValue(datasource.getUsername()).build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_PASSWORD)
.withValue(datasource.getPassword()).build());
return true;
}
return false;
public static void configureDatasource(ApicurioRegistry3 primary, Map<String, EnvVar> env) {
ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getApp).map(AppSpec::getStorage)
.map(StorageSpec::getSql).map(SqlSpec::getDataSource).ifPresent(dataSource -> {

// TODO: Validation
addEnvVar(env,
new EnvVarBuilder().withName(ENV_APICURIO_STORAGE_KIND).withValue("sql").build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_STORAGE_SQL_KIND)
.withValue("postgresql").build());

addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_URL)
.withValue(dataSource.getUrl()).build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_USERNAME)
.withValue(dataSource.getUsername()).build());
addEnvVar(env, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_PASSWORD)
.withValue(dataSource.getPassword()).build());
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.apicurio.registry.operator.resource;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Spec;
import io.apicurio.registry.operator.api.v1.spec.AppSpec;
import io.apicurio.registry.operator.api.v1.spec.IngressSpec;
import io.apicurio.registry.operator.api.v1.spec.StudioUiSpec;
import io.apicurio.registry.operator.api.v1.spec.UiSpec;
import io.apicurio.registry.operator.resource.app.AppIngressResource;
import io.apicurio.registry.operator.resource.studioui.StudioUIDeploymentResource;
import io.apicurio.registry.operator.resource.studioui.StudioUIIngressResource;
Expand All @@ -27,11 +32,13 @@ public static class AppIngressActivationCondition implements Condition<Ingress,
public boolean isMet(DependentResource<Ingress, ApicurioRegistry3> resource,
ApicurioRegistry3 primary, Context<ApicurioRegistry3> context) {

var disabled = isBlank(primary.getSpec().getApp().getHost());
if (disabled) {
var enabled = ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getApp)
.map(AppSpec::getIngress).map(IngressSpec::getHost).map(host -> !isBlank(host))
.orElse(false);
if (!enabled) {
((AppIngressResource) resource).delete(primary, context);
}
return !disabled;
return enabled;
}
}

Expand All @@ -43,11 +50,13 @@ public static class UIIngressActivationCondition implements Condition<Ingress, A
public boolean isMet(DependentResource<Ingress, ApicurioRegistry3> resource,
ApicurioRegistry3 primary, Context<ApicurioRegistry3> context) {

var disabled = isBlank(primary.getSpec().getUi().getHost());
if (disabled) {
var enabled = ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getUi)
.map(UiSpec::getIngress).map(IngressSpec::getHost).map(host -> !isBlank(host))
.orElse(false);
if (!enabled) {
((UIIngressResource) resource).delete(primary, context);
}
return !disabled;
return enabled;
}
}

Expand All @@ -60,7 +69,8 @@ public static class StudioUIDeploymentActivationCondition
public boolean isMet(DependentResource<Deployment, ApicurioRegistry3> resource,
ApicurioRegistry3 primary, Context<ApicurioRegistry3> context) {

var enabled = ofNullable(primary.getSpec().getStudioUi().getEnabled()).orElse(false);
var enabled = ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getStudioUi)
.map(StudioUiSpec::getEnabled).orElse(false);
if (!enabled) {
((StudioUIDeploymentResource) resource).delete(primary, context);
}
Expand All @@ -74,11 +84,13 @@ public static class StudioUIIngressActivationCondition implements Condition<Ingr
public boolean isMet(DependentResource<Ingress, ApicurioRegistry3> resource,
ApicurioRegistry3 primary, Context<ApicurioRegistry3> context) {

var disabled = isBlank(primary.getSpec().getStudioUi().getHost());
if (disabled) {
var enabled = ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getStudioUi)
.map(StudioUiSpec::getIngress).map(IngressSpec::getHost).map(host -> !isBlank(host))
.orElse(false);
if (!enabled) {
((StudioUIIngressResource) resource).delete(primary, context);
}
return !disabled;
return enabled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import io.apicurio.registry.operator.Configuration;
import io.apicurio.registry.operator.OperatorException;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Spec;
import io.apicurio.registry.operator.api.v1.spec.AppSpec;
import io.apicurio.registry.operator.api.v1.spec.StudioUiSpec;
import io.apicurio.registry.operator.api.v1.spec.UiSpec;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
Expand All @@ -16,9 +20,11 @@
import java.util.List;
import java.util.Map;

import static io.apicurio.registry.operator.api.v1.ContainerNames.*;
import static io.apicurio.registry.operator.resource.app.AppDeploymentResource.getContainerFromPodTemplateSpec;
import static io.apicurio.registry.operator.utils.Mapper.YAML_MAPPER;
import static io.apicurio.registry.operator.utils.Utils.isBlank;
import static java.util.Optional.ofNullable;

public class ResourceFactory {

Expand All @@ -34,17 +40,14 @@ public class ResourceFactory {
public static final String RESOURCE_TYPE_SERVICE = "service";
public static final String RESOURCE_TYPE_INGRESS = "ingress";

public static final String APP_CONTAINER_NAME = "apicurio-registry-app";
public static final String UI_CONTAINER_NAME = "apicurio-registry-ui";
public static final String STUDIO_UI_CONTAINER_NAME = "apicurio-studio-ui";

public Deployment getDefaultAppDeployment(ApicurioRegistry3 primary) {
var r = initDefaultDeployment(primary, COMPONENT_APP, 1,
primary.getSpec().getApp().getPodTemplateSpec()); // TODO: Replicas
var r = initDefaultDeployment(primary, COMPONENT_APP, 1, ofNullable(primary.getSpec())
.map(ApicurioRegistry3Spec::getApp).map(AppSpec::getPodTemplateSpec).orElse(null)); // TODO:
// Replicas
mergeDeploymentPodTemplateSpec(
// spotless:off
r.getSpec().getTemplate(),
APP_CONTAINER_NAME,
REGISTRY_APP_CONTAINER_NAME,
Configuration.getAppImage(),
List.of(new ContainerPortBuilder().withName("http").withProtocol("TCP").withContainerPort(8080).build()),
new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/health/ready").withPort(new IntOrString(8080)).withScheme("HTTP").build()).build(),
Expand All @@ -60,12 +63,13 @@ public Deployment getDefaultAppDeployment(ApicurioRegistry3 primary) {
}

public Deployment getDefaultUIDeployment(ApicurioRegistry3 primary) {
var r = initDefaultDeployment(primary, COMPONENT_UI, 1,
primary.getSpec().getUi().getPodTemplateSpec()); // TODO: Replicas
var r = initDefaultDeployment(primary, COMPONENT_UI, 1, ofNullable(primary.getSpec())
.map(ApicurioRegistry3Spec::getUi).map(UiSpec::getPodTemplateSpec).orElse(null)); // TODO:
// Replicas
mergeDeploymentPodTemplateSpec(
// spotless:off
r.getSpec().getTemplate(),
UI_CONTAINER_NAME,
REGISTRY_UI_CONTAINER_NAME,
Configuration.getUIImage(),
List.of(new ContainerPortBuilder().withName("http").withProtocol("TCP").withContainerPort(8080).build()),
new ProbeBuilder().withHttpGet(new HTTPGetActionBuilder().withPath("/config.js").withPort(new IntOrString(8080)).withScheme("HTTP").build()).build(),
Expand All @@ -81,8 +85,9 @@ public Deployment getDefaultUIDeployment(ApicurioRegistry3 primary) {
}

public Deployment getDefaultStudioUIDeployment(ApicurioRegistry3 primary) {
var r = initDefaultDeployment(primary, COMPONENT_STUDIO_UI, 1,
primary.getSpec().getStudioUi().getPodTemplateSpec()); // TODO: Replicas
var r = initDefaultDeployment(primary, COMPONENT_STUDIO_UI, 1, ofNullable(primary.getSpec())
.map(ApicurioRegistry3Spec::getStudioUi).map(StudioUiSpec::getPodTemplateSpec).orElse(null)); // TODO:
// Replicas
mergeDeploymentPodTemplateSpec(
// spotless:off
r.getSpec().getTemplate(),
Expand Down
Loading

0 comments on commit 0e6b933

Please sign in to comment.