Skip to content

Commit

Permalink
Provide a way to override operand image, env, and resources in CRD
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Dec 19, 2024
1 parent 3a81758 commit 86863ae
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.github.streamshub.console.api.v1alpha1.spec.containers.Containers;
import com.github.streamshub.console.api.v1alpha1.spec.metrics.MetricsSource;

import io.fabric8.generator.annotation.Required;
Expand All @@ -26,6 +28,16 @@ public class ConsoleSpec {
@Required
String hostname;

@JsonPropertyDescription("""
Templates for Console instance containers. The templates allow \
users to specify how the Kubernetes resources are generated.
""")
Containers containers;

@JsonPropertyDescription("""
DEPRECATED: Image overrides to be used for the API and UI servers. \
Use `containers` property instead.
""")
Images images;

List<MetricsSource> metricsSources;
Expand All @@ -34,6 +46,10 @@ public class ConsoleSpec {

List<KafkaCluster> kafkaClusters = new ArrayList<>();

@JsonPropertyDescription("""
DEPRECATED: Environment variables which should be applied to the API container. \
Use `containers` property instead.
""")
List<EnvVar> env;

public String getHostname() {
Expand All @@ -44,6 +60,14 @@ public void setHostname(String hostname) {
this.hostname = hostname;
}

public Containers getContainers() {
return containers;
}

public void setContainers(Containers containers) {
this.containers = containers;
}

public Images getImages() {
return images;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.streamshub.console.api.v1alpha1.spec.containers;

import java.util.List;

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

import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.ResourceRequirements;
import io.sundr.builder.annotations.Buildable;

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

@JsonPropertyDescription("Container image to be used for the container")
private String image;

@JsonPropertyDescription("CPU and memory resources to reserve.")
private ResourceRequirements resources;

@JsonPropertyDescription("Environment variables which should be applied to the container.")
private List<EnvVar> env;

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public ResourceRequirements getResources() {
return resources;
}

public void setResources(ResourceRequirements resources) {
this.resources = resources;
}

public List<EnvVar> getEnv() {
return env;
}

public void setEnv(List<EnvVar> env) {
this.env = env;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 Containers {

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

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

public ContainerTemplate getApi() {
return api;
}

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

public ContainerTemplate getUi() {
return ui;
}

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -13,6 +12,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.Containers;
import com.github.streamshub.console.dependents.discriminators.ConsoleLabelDiscriminator;

import io.fabric8.kubernetes.api.model.EnvVar;
Expand Down Expand Up @@ -61,14 +62,13 @@ protected Deployment desired(Console primary, Context<Console> context) {
String name = instanceName(primary);
String configSecretName = secret.instanceName(primary);

var imagesSpec = Optional.ofNullable(primary.getSpec().getImages());
String imageAPI = imagesSpec.map(Images::getApi).orElse(defaultAPIImage);
String imageUI = imagesSpec.map(Images::getUi).orElse(defaultUIImage);

var envVars = new ArrayList<>(coalesce(primary.getSpec().getEnv(), Collections::emptyList));

var containers = Optional.ofNullable(primary.getSpec().getContainers());
var templateAPI = containers.map(Containers::getApi);
var templateUI = containers.map(Containers::getUi);
var trustResources = getTrustResources(context);
envVars.addAll(getResourcesByType(trustResources, EnvVar.class));

// deprecated
var images = Optional.ofNullable(primary.getSpec().getImages());

return desired.edit()
.editMetadata()
Expand All @@ -95,13 +95,26 @@ protected Deployment desired(Console primary, Context<Console> context) {
.endSecret()
.endVolume()
.addAllToVolumes(getResourcesByType(trustResources, Volume.class))
// Set API container image options
.editMatchingContainer(c -> "console-api".equals(c.getName()))
.withImage(imageAPI)
.withImage(templateAPI.map(ContainerTemplate::getImage)
.or(() -> images.map(Images::getApi))
.orElse(defaultAPIImage))
.withResources(templateAPI.map(ContainerTemplate::getResources).orElse(null))
.addAllToVolumeMounts(getResourcesByType(trustResources, VolumeMount.class))
.addAllToEnv(envVars)
// deprecated env list
.addAllToEnv(coalesce(primary.getSpec().getEnv(), Collections::emptyList))
// Env from template
.addAllToEnv(templateAPI.map(ContainerTemplate::getEnv).orElseGet(Collections::emptyList))
// Env for truststores
.addAllToEnv(getResourcesByType(trustResources, EnvVar.class))
.endContainer()
// Set UI container image options
.editMatchingContainer(c -> "console-ui".equals(c.getName()))
.withImage(imageUI)
.withImage(templateUI.map(ContainerTemplate::getImage)
.or(() -> images.map(Images::getUi))
.orElse(defaultUIImage))
.withResources(templateUI.map(ContainerTemplate::getResources).orElse(null))
.editMatchingEnv(env -> "NEXTAUTH_URL".equals(env.getName()))
.withValue(getAttribute(context, ConsoleIngress.NAME + ".url", String.class))
.endEnv()
Expand All @@ -112,6 +125,7 @@ protected Deployment desired(Console primary, Context<Console> context) {
.endSecretKeyRef()
.endValueFrom()
.endEnv()
.addAllToEnv(templateUI.map(ContainerTemplate::getEnv).orElseGet(Collections::emptyList))
.endContainer()
.endSpec()
.endTemplate()
Expand Down
Loading

0 comments on commit 86863ae

Please sign in to comment.