Skip to content

Commit

Permalink
Use uptional for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal committed Jan 22, 2025
1 parent fc4f132 commit 2aa97e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.apicurio.registry.operator.Constants.DEFAULT_REPLICAS;
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;
Expand All @@ -28,8 +28,6 @@

public class ResourceFactory {

private static final Logger log = LoggerFactory.getLogger(ResourceFactory.class);

public static final ResourceFactory INSTANCE = new ResourceFactory();

public static final String COMPONENT_APP = "app";
Expand All @@ -41,8 +39,11 @@ public class ResourceFactory {
public static final String RESOURCE_TYPE_INGRESS = "ingress";

public Deployment getDefaultAppDeployment(ApicurioRegistry3 primary) {
var r = initDefaultDeployment(primary, COMPONENT_APP, 1, ofNullable(primary.getSpec())
.map(ApicurioRegistry3Spec::getApp).map(AppSpec::getPodTemplateSpec).orElse(null)); // TODO:
var r = initDefaultDeployment(primary, COMPONENT_APP,
Optional.ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getApp)
.map(AppSpec::getReplicas).orElse(DEFAULT_REPLICAS),
ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getApp)
.map(AppSpec::getPodTemplateSpec).orElse(null)); // TODO:
// Replicas
mergeDeploymentPodTemplateSpec(
// spotless:off
Expand All @@ -63,8 +64,11 @@ public Deployment getDefaultAppDeployment(ApicurioRegistry3 primary) {
}

public Deployment getDefaultUIDeployment(ApicurioRegistry3 primary) {
var r = initDefaultDeployment(primary, COMPONENT_UI, 1, ofNullable(primary.getSpec())
.map(ApicurioRegistry3Spec::getUi).map(UiSpec::getPodTemplateSpec).orElse(null)); // TODO:
var r = initDefaultDeployment(primary, COMPONENT_UI,
Optional.ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getUi)
.map(UiSpec::getReplicas).orElse(DEFAULT_REPLICAS),
ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getUi)
.map(UiSpec::getPodTemplateSpec).orElse(null)); // TODO:
// Replicas
mergeDeploymentPodTemplateSpec(
// spotless:off
Expand All @@ -85,9 +89,12 @@ public Deployment getDefaultUIDeployment(ApicurioRegistry3 primary) {
}

public Deployment getDefaultStudioUIDeployment(ApicurioRegistry3 primary) {
var r = initDefaultDeployment(primary, COMPONENT_STUDIO_UI, 1, ofNullable(primary.getSpec())
.map(ApicurioRegistry3Spec::getStudioUi).map(StudioUiSpec::getPodTemplateSpec).orElse(null)); // TODO:
// Replicas
var r = initDefaultDeployment(primary, COMPONENT_STUDIO_UI,
Optional.ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getStudioUi)
.map(StudioUiSpec::getReplicas).orElse(DEFAULT_REPLICAS),
ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getStudioUi)
.map(StudioUiSpec::getPodTemplateSpec).orElse(null)); // TODO:
// Replicas
mergeDeploymentPodTemplateSpec(
// spotless:off
r.getSpec().getTemplate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,15 @@ void testService() {
client.resource(registry).create();

// Wait for Services
checkServiceExists(registry, COMPONENT_APP);
checkServiceExists(registry, COMPONENT_UI);
await().ignoreExceptions().until(() -> {
assertThat(client.services().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-service").get().getSpec()
.getClusterIP()).isNotBlank();
assertThat(client.services().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-service").get().getSpec()
.getClusterIP()).isNotBlank();
return true;
});

int appServicePort = portForwardManager
.startPortForward(registry.getMetadata().getName() + "-app-service", 8080);
Expand Down Expand Up @@ -148,8 +155,12 @@ void testIngress() {
client.resource(registry).create();

// Wait for Ingresses
checkIngressExists(registry, COMPONENT_APP);
checkIngressExists(registry, COMPONENT_UI);
await().untilAsserted(() -> {
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-ingress").get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-ingress").get()).isNotNull();
});

await().ignoreExceptions().until(() -> {
ingressManager.startHttpRequest(registry.getMetadata().getName() + "-app-ingress")
Expand All @@ -176,8 +187,12 @@ void testEmptyHostDisablesIngress() {
client.resource(registry).create();

// Wait for Ingresses
checkIngressExists(registry, COMPONENT_APP);
checkIngressExists(registry, COMPONENT_UI);
await().untilAsserted(() -> {
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-ingress").get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-ingress").get()).isNotNull();
});

// Check that REGISTRY_API_URL is set
await().ignoreExceptions().untilAsserted(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public abstract class ComponentSpec {
@JsonSetter(nulls = Nulls.SKIP)
private String host;

/**
* Number of replicas for the component
*/
@JsonProperty("replicas")
@JsonPropertyDescription("Number of replicas for the component")
@JsonSetter(nulls = Nulls.SKIP)
private Integer replicas;

public IngressSpec withIngress() {
if (ingress == null) {
ingress = new IngressSpec();
Expand Down

0 comments on commit 2aa97e1

Please sign in to comment.