Skip to content

Commit

Permalink
Nest spec within container template, allows for future metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <medgar@redhat.com>
  • Loading branch information
MikeEdgar committed Jan 15, 2025
1 parent c235232 commit e019b19
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Buildable
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ContainerTemplate {
public class ContainerSpec {

@JsonPropertyDescription("Container image to be used for the container")
private String image;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.streamshub.console.api.v1alpha1.spec.containers;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;

import io.sundr.builder.annotations.Buildable;

@Buildable
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ContainerTemplateSpec {

@JsonPropertyDescription("Specification to be applied to the resulting container.")
ContainerSpec spec;

public ContainerSpec getSpec() {
return spec;
}

public void setSpec(ContainerSpec spec) {
this.spec = spec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ public class Containers {

@JsonPropertyDescription("Template for the Console API server container. " +
"The template allows users to specify how the Kubernetes resources are generated.")
ContainerTemplate api;
ContainerTemplateSpec api;

@JsonPropertyDescription("Template for the Console UI server container. " +
"The template allows users to specify how the Kubernetes resources are generated.")
ContainerTemplate ui;
ContainerTemplateSpec ui;

public ContainerTemplate getApi() {
public ContainerTemplateSpec getApi() {
return api;
}

public void setApi(ContainerTemplate api) {
public void setApi(ContainerTemplateSpec api) {
this.api = api;
}

public ContainerTemplate getUi() {
public ContainerTemplateSpec getUi() {
return ui;
}

public void setUi(ContainerTemplate ui) {
public void setUi(ContainerTemplateSpec ui) {
this.ui = ui;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.streamshub.console.dependents;

import java.util.Collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -13,7 +13,8 @@

import com.github.streamshub.console.api.v1alpha1.Console;
import com.github.streamshub.console.api.v1alpha1.spec.Images;
import com.github.streamshub.console.api.v1alpha1.spec.containers.ContainerTemplate;
import com.github.streamshub.console.api.v1alpha1.spec.containers.ContainerSpec;
import com.github.streamshub.console.api.v1alpha1.spec.containers.ContainerTemplateSpec;
import com.github.streamshub.console.api.v1alpha1.spec.containers.Containers;
import com.github.streamshub.console.dependents.discriminators.ConsoleLabelDiscriminator;

Expand Down Expand Up @@ -64,27 +65,27 @@ protected Deployment desired(Console primary, Context<Console> context) {
String configSecretName = secret.instanceName(primary);

var containers = Optional.ofNullable(primary.getSpec().getContainers());
var templateAPI = containers.map(Containers::getApi);
var templateUI = containers.map(Containers::getUi);
var templateAPI = containers.map(Containers::getApi).map(ContainerTemplateSpec::getSpec);
var templateUI = containers.map(Containers::getUi).map(ContainerTemplateSpec::getSpec);
// deprecated
var images = Optional.ofNullable(primary.getSpec().getImages());

String imageAPI = templateAPI.map(ContainerTemplate::getImage)
String imageAPI = templateAPI.map(ContainerSpec::getImage)
.or(() -> images.map(Images::getApi))
.orElse(defaultAPIImage);
String imageUI = templateUI.map(ContainerTemplate::getImage)
String imageUI = templateUI.map(ContainerSpec::getImage)
.or(() -> images.map(Images::getUi))
.orElse(defaultUIImage);

var trustResources = getTrustResources("TrustStoreResources", context);
List<EnvVar> envVars = new ArrayList<>();
envVars.addAll(coalesce(primary.getSpec().getEnv(), Collections::emptyList));
envVars.addAll(templateAPI.map(ContainerTemplate::getEnv).orElseGet(Collections::emptyList));
envVars.addAll(templateAPI.map(ContainerSpec::getEnv).orElseGet(Collections::emptyList));
envVars.addAll(getResourcesByType(trustResources, EnvVar.class));

var trustResourcesUI = getTrustResources("TrustStoreResourcesUI", context);
List<EnvVar> envVarsUI = new ArrayList<>();
envVarsUI.addAll(templateUI.map(ContainerTemplate::getEnv).orElseGet(Collections::emptyList));
envVarsUI.addAll(templateUI.map(ContainerSpec::getEnv).orElseGet(Collections::emptyList));
envVarsUI.addAll(getResourcesByType(trustResourcesUI, EnvVar.class));

return desired.edit()
Expand Down Expand Up @@ -116,15 +117,15 @@ protected Deployment desired(Console primary, Context<Console> context) {
.editMatchingContainer(c -> "console-api".equals(c.getName()))
.withImage(imageAPI)
.withImagePullPolicy(pullPolicy(imageAPI))
.withResources(templateAPI.map(ContainerTemplate::getResources).orElse(null))
.withResources(templateAPI.map(ContainerSpec::getResources).orElse(null))
.addAllToVolumeMounts(getResourcesByType(trustResources, VolumeMount.class))
.addAllToEnv(envVars)
.endContainer()
// Set UI container image options
.editMatchingContainer(c -> "console-ui".equals(c.getName()))
.withImage(imageUI)
.withImagePullPolicy(pullPolicy(imageUI))
.withResources(templateUI.map(ContainerTemplate::getResources).orElse(null))
.withResources(templateUI.map(ContainerSpec::getResources).orElse(null))
.editMatchingEnv(env -> "NEXTAUTH_URL".equals(env.getName()))
.withValue(getAttribute(context, ConsoleIngress.NAME + ".url", String.class))
.endEnv()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,30 @@ void testConsoleReconciliationWithContainerOverrides() {
.build())
.withNewContainers()
.withNewApi()
.withImage("custom-api-image")
.withResources(new ResourceRequirementsBuilder()
.withRequests(Map.of("cpu", Quantity.parse("250m")))
.withLimits(Map.of("cpu", Quantity.parse("500m")))
.build())
.addToEnv(new EnvVarBuilder()
.withName("CUSTOM_API_VAR")
.withValue("value1")
.build())
.withNewSpec()
.withImage("custom-api-image")
.withResources(new ResourceRequirementsBuilder()
.withRequests(Map.of("cpu", Quantity.parse("250m")))
.withLimits(Map.of("cpu", Quantity.parse("500m")))
.build())
.addToEnv(new EnvVarBuilder()
.withName("CUSTOM_API_VAR")
.withValue("value1")
.build())
.endSpec()
.endApi()
.withNewUi()
.withImage("custom-ui-image")
.withResources(new ResourceRequirementsBuilder()
.withRequests(Map.of("cpu", Quantity.parse("100m")))
.withLimits(Map.of("cpu", Quantity.parse("200m")))
.build())
.addToEnv(new EnvVarBuilder()
.withName("CUSTOM_UI_VAR")
.withValue("value2")
.build())
.withNewSpec()
.withImage("custom-ui-image")
.withResources(new ResourceRequirementsBuilder()
.withRequests(Map.of("cpu", Quantity.parse("100m")))
.withLimits(Map.of("cpu", Quantity.parse("200m")))
.build())
.addToEnv(new EnvVarBuilder()
.withName("CUSTOM_UI_VAR")
.withValue("value2")
.build())
.endSpec()
.endUi()
.endContainers()
.addNewKafkaCluster()
Expand Down

0 comments on commit e019b19

Please sign in to comment.