EnvVars { public void apply(PodData podData, List<? extends Env> env) { Stream.concat( podData.getSpec().getInitContainers().stream(), podData.getSpec().getContainers().stream()) .forEach(c -> apply(c, env)); }  void apply(PodData podData, List<? extends Env> env); void apply(Container container, List<? extends Env> toApply); static Set<String> extractReferencedVariables(EnvVar var);  }
@Test public void shouldProvisionEnvIfContainersDoeNotHaveEnvAtAll() throws Exception { PodData pod = new PodData( new PodBuilder() .withNewMetadata() .withName("pod") .endMetadata() .withNewSpec() .withInitContainers(new ContainerBuilder().withName("initContainer").build()) .withContainers(new ContainerBuilder().withName("container").build()) .endSpec() .build()); envVars.apply(pod, singletonList(new EnvImpl("TEST_ENV", "anyValue"))); List<EnvVar> initCEnv = pod.getSpec().getInitContainers().get(0).getEnv(); assertEquals(initCEnv.size(), 1); assertEquals(initCEnv.get(0), new EnvVar("TEST_ENV", "anyValue", null)); List<EnvVar> containerEnv = pod.getSpec().getContainers().get(0).getEnv(); assertEquals(containerEnv.size(), 1); assertEquals(containerEnv.get(0), new EnvVar("TEST_ENV", "anyValue", null)); }
@Test public void shouldOverrideEnvOnApplyingEnvVarIfContainersAlreadyHaveSomeEnvVars() throws Exception { EnvVar existingInitCEnvVar = new EnvVar("TEST_ENV", "value", null); EnvVar existingCEnvVar = new EnvVar("TEST_ENV", null, new EnvVarSource()); PodData pod = new PodData( new PodBuilder() .withNewMetadata() .withName("pod") .endMetadata() .withNewSpec() .withInitContainers( new ContainerBuilder() .withName("initContainer") .withEnv(copy(existingInitCEnvVar)) .build()) .withContainers( new ContainerBuilder() .withName("container") .withEnv(copy(existingCEnvVar)) .build()) .endSpec() .build()); envVars.apply(pod, singletonList(new EnvImpl("TEST_ENV", "anyValue"))); List<EnvVar> initCEnv = pod.getSpec().getInitContainers().get(0).getEnv(); assertEquals(initCEnv.size(), 1); assertEquals(initCEnv.get(0), new EnvVar("TEST_ENV", "anyValue", null)); List<EnvVar> containerEnv = pod.getSpec().getContainers().get(0).getEnv(); assertEquals(containerEnv.size(), 1); assertEquals(containerEnv.get(0), new EnvVar("TEST_ENV", "anyValue", null)); }
@Test public void shouldProvisionEnvIntoK8SListIfContainerAlreadyHasSomeEnvVars() throws Exception { EnvVar existingInitCEnvVar = new EnvVar("ENV", "value", null); EnvVar existingCEnvVar = new EnvVar("ENV", null, new EnvVarSource()); PodData pod = new PodData( new PodBuilder() .withNewMetadata() .withName("pod") .endMetadata() .withNewSpec() .withInitContainers( new ContainerBuilder() .withName("initContainer") .withEnv(copy(existingInitCEnvVar)) .build()) .withContainers( new ContainerBuilder() .withName("container") .withEnv(copy(existingCEnvVar)) .build()) .endSpec() .build()); envVars.apply(pod, singletonList(new EnvImpl("TEST_ENV", "anyValue"))); List<EnvVar> initCEnv = pod.getSpec().getInitContainers().get(0).getEnv(); assertEquals(initCEnv.size(), 2); assertEquals(initCEnv.get(0), existingInitCEnvVar); assertEquals(initCEnv.get(1), new EnvVar("TEST_ENV", "anyValue", null)); List<EnvVar> containerEnv = pod.getSpec().getContainers().get(0).getEnv(); assertEquals(containerEnv.size(), 2); assertEquals(containerEnv.get(0), existingCEnvVar); assertEquals(containerEnv.get(1), new EnvVar("TEST_ENV", "anyValue", null)); }