Skip to content

Commit

Permalink
feat: add prefix to envFrom configMap and secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev authored and iocanel committed Mar 27, 2024
1 parent 5f4acbd commit 71e5654
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static ServiceBindingConfigBuilder newBuilder(ServiceBinding instance) {
.toArray(ServiceConfig[]::new),
instance.envVarPrefix(), instance.detectBindingResources(), instance.bindAsFiles(), instance.mountPath(),
Arrays.stream(instance.customEnvVar())
.map(c -> new Env(c.name(), c.value(), c.secret(), c.configmap(), c.field(), c.resourceField()))
.map(c -> new Env(c.name(), c.value(), c.secret(), c.configmap(), c.field(), c.resourceField(), c.prefix()))
.toArray(Env[]::new),
new BindingPathConfig(null, null, bindingPath.containerPath(), bindingPath.secretPath())));
}
Expand Down Expand Up @@ -114,7 +114,8 @@ private static ServiceConfig getServiceConfig(Map i) {
private static Env getCustomEnvVarConfig(Map i) {
return new Env((String) i.getOrDefault("name", null), (String) i.getOrDefault("value", null),
(String) i.getOrDefault("secret", null), (String) i.getOrDefault("configmap", null),
(String) i.getOrDefault("field", null), (String) i.getOrDefault("resourceField", null));
(String) i.getOrDefault("field", null), (String) i.getOrDefault("resourceField", null),
(String) i.getOrDefault("prefix", null));
}

private static BindingPathConfig getBindingPathConfig(Map i) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/io/dekorate/kubernetes/annotation/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@
String field() default "";

String resourceField() default "";

String prefix() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ private void populateFromSecret(ContainerBuilder builder) {
builder.addNewEnv().withName(env.getName()).withNewValueFrom()
.withNewSecretKeyRef(env.getValue(), env.getSecret(), null).endValueFrom().endEnv();
} else {
builder.addNewEnvFrom().withNewSecretRef(env.getSecret(), null).endEnvFrom();
builder.addNewEnvFrom().withNewSecretRef(env.getSecret(), null)
.withPrefix(env.getPrefix()).endEnvFrom();
}
}

Expand All @@ -119,7 +120,8 @@ private void populateFromConfigMap(ContainerBuilder builder) {
builder.addNewEnv().withName(env.getName()).withNewValueFrom()
.withNewConfigMapKeyRef(env.getValue(), env.getConfigmap(), null).endValueFrom().endEnv();
} else {
builder.addNewEnvFrom().withNewConfigMapRef(env.getConfigmap(), null).endEnvFrom();
builder.addNewEnvFrom().withNewConfigMapRef(env.getConfigmap(), null)
.withPrefix(env.getPrefix()).endEnvFrom();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.dekorate.kubernetes.decorator;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import io.dekorate.kubernetes.config.EditableEnv;
import io.dekorate.kubernetes.config.EnvBuilder;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.EnvFromSource;

class AddEnvVarDecoratorTest {

@Test
void shouldAddPrefixToContainerWhenUsingConfigMapRef() {

EditableEnv editableEnv = new EnvBuilder()
.withConfigmap("configs")
.withPrefix("ALPHA")
.build();

AddEnvVarDecorator addEnvVarDecorator = new AddEnvVarDecorator("jdeployment", "jcontainer", editableEnv);

ContainerBuilder containerBuilder = new ContainerBuilder();

addEnvVarDecorator.andThenVisit(containerBuilder);

List<EnvFromSource> envFromSources = containerBuilder.buildEnvFrom();

Assertions.assertThat(envFromSources.size()).isOne();

Assertions.assertThat(envFromSources)
.filteredOn("prefix", "ALPHA")
.extracting("configMapRef.name")
.contains("configs")
.isNotEmpty();

}

@Test
void shouldAddPrefixToContainerWhenUsingSecretRef() {

EditableEnv editableEnv = new EnvBuilder()
.withSecret("secrets")
.withPrefix("ALPHA")
.build();

AddEnvVarDecorator addEnvVarDecorator = new AddEnvVarDecorator("jdeployment", "jcontainer", editableEnv);

ContainerBuilder containerBuilder = new ContainerBuilder();

addEnvVarDecorator.andThenVisit(containerBuilder);

List<EnvFromSource> envFromSources = containerBuilder.buildEnvFrom();

Assertions.assertThat(envFromSources.size()).isOne();

Assertions.assertThat(envFromSources)
.filteredOn("prefix", "ALPHA")
.extracting("secretRef.name")
.contains("secrets")
.isNotEmpty();
}
}

0 comments on commit 71e5654

Please sign in to comment.