use of io.kubernetes.client.openapi.models.V1EnvVarSource in project heron by twitter.
the class V1ControllerTest method testConfigureContainerEnvVars.
@Test
public void testConfigureContainerEnvVars() {
final List<V1EnvVar> heronEnvVars = Collections.unmodifiableList(V1Controller.getExecutorEnvVars());
final V1EnvVar additionEnvVar = new V1EnvVar().name("env-variable-to-be-kept").valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-variable-was-kept")));
final List<V1EnvVar> inputEnvVars = Arrays.asList(new V1EnvVar().name(KubernetesConstants.ENV_HOST).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-host-to-be-replaced"))), new V1EnvVar().name(KubernetesConstants.ENV_POD_NAME).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("pod-name-to-be-replaced"))), additionEnvVar);
// Null env vars. This is the default case.
V1Container containerWithNullEnvVars = new V1ContainerBuilder().build();
v1ControllerWithPodTemplate.configureContainerEnvVars(containerWithNullEnvVars);
Assert.assertTrue("ENV_HOST & ENV_POD_NAME in container with null Env Vars should match", CollectionUtils.containsAll(containerWithNullEnvVars.getEnv(), heronEnvVars));
// Empty env vars.
V1Container containerWithEmptyEnvVars = new V1ContainerBuilder().withEnv(new LinkedList<>()).build();
v1ControllerWithPodTemplate.configureContainerEnvVars(containerWithEmptyEnvVars);
Assert.assertTrue("ENV_HOST & ENV_POD_NAME in container with empty Env Vars should match", CollectionUtils.containsAll(containerWithEmptyEnvVars.getEnv(), heronEnvVars));
// Env Var overriding.
final List<V1EnvVar> expectedOverriding = new LinkedList<>(heronEnvVars);
expectedOverriding.add(additionEnvVar);
V1Container containerWithEnvVars = new V1ContainerBuilder().withEnv(inputEnvVars).build();
v1ControllerWithPodTemplate.configureContainerEnvVars(containerWithEnvVars);
Assert.assertTrue("ENV_HOST & ENV_POD_NAME in container with Env Vars should be overridden", CollectionUtils.containsAll(containerWithEnvVars.getEnv(), expectedOverriding));
}
use of io.kubernetes.client.openapi.models.V1EnvVarSource in project heron by twitter.
the class V1Controller method getExecutorEnvVars.
/**
* Generates a list of <code>Environment Variables</code> required by Heron to function.
* @return A list of configured <code>Environment Variables</code> required by Heron to function.
*/
@VisibleForTesting
protected static List<V1EnvVar> getExecutorEnvVars() {
final V1EnvVar envVarHost = new V1EnvVar();
envVarHost.name(KubernetesConstants.ENV_HOST).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath(KubernetesConstants.POD_IP)));
final V1EnvVar envVarPodName = new V1EnvVar();
envVarPodName.name(KubernetesConstants.ENV_POD_NAME).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath(KubernetesConstants.POD_NAME)));
return Arrays.asList(envVarHost, envVarPodName);
}
use of io.kubernetes.client.openapi.models.V1EnvVarSource in project heron by twitter.
the class V1Controller method setSecretKeyRefs.
/**
* Adds <code>Secret Key</code> references to a <code>container</code>.
* @param container <code>container</code> to be configured.
*/
private void setSecretKeyRefs(V1Container container) {
final Config config = getConfiguration();
final Map<String, String> podSecretKeyRefs = KubernetesContext.getPodSecretKeyRefs(config);
for (Map.Entry<String, String> secret : podSecretKeyRefs.entrySet()) {
final String[] keyRefParts = secret.getValue().split(":");
if (keyRefParts.length != 2) {
LOG.log(Level.SEVERE, "SecretKeyRef must be in the form name:key. <" + secret.getValue() + ">");
throw new TopologyRuntimeManagementException("SecretKeyRef must be in the form name:key. <" + secret.getValue() + ">");
}
String name = keyRefParts[0];
String key = keyRefParts[1];
final V1EnvVar envVar = new V1EnvVar().name(secret.getKey()).valueFrom(new V1EnvVarSource().secretKeyRef(new V1SecretKeySelector().key(key).name(name)));
container.addEnvItem(envVar);
}
}
use of io.kubernetes.client.openapi.models.V1EnvVarSource in project heron by twitter.
the class KubernetesUtilsTest method testMergeListsDedupe.
@Test
public void testMergeListsDedupe() {
final String description = "Pod Template Environment Variables";
final List<V1EnvVar> heronEnvVars = Collections.unmodifiableList(V1Controller.getExecutorEnvVars());
final V1EnvVar additionEnvVar = new V1EnvVar().name("env-variable-to-be-kept").valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-variable-was-kept")));
final List<V1EnvVar> expectedEnvVars = Collections.unmodifiableList(new LinkedList<V1EnvVar>(V1Controller.getExecutorEnvVars()) {
{
add(additionEnvVar);
}
});
final List<V1EnvVar> inputEnvVars = Arrays.asList(new V1EnvVar().name(KubernetesConstants.ENV_HOST).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-host-to-be-replaced"))), new V1EnvVar().name(KubernetesConstants.ENV_POD_NAME).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("pod-name-to-be-replaced"))), additionEnvVar);
KubernetesUtils.V1ControllerUtils<V1EnvVar> v1ControllerUtils = new KubernetesUtils.V1ControllerUtils<>();
// Both input lists are null.
Assert.assertNull("Both input lists are <null>", v1ControllerUtils.mergeListsDedupe(null, null, Comparator.comparing(V1EnvVar::getName), description));
// <primaryList> is <null>.
Assert.assertEquals("<primaryList> is null and <secondaryList> should be returned", inputEnvVars, v1ControllerUtils.mergeListsDedupe(null, inputEnvVars, Comparator.comparing(V1EnvVar::getName), description));
// <primaryList> is empty.
Assert.assertEquals("<primaryList> is empty and <secondaryList> should be returned", inputEnvVars, v1ControllerUtils.mergeListsDedupe(new LinkedList<>(), inputEnvVars, Comparator.comparing(V1EnvVar::getName), description));
// <secondaryList> is <null>.
Assert.assertEquals("<secondaryList> is null and <primaryList> should be returned", heronEnvVars, v1ControllerUtils.mergeListsDedupe(heronEnvVars, null, Comparator.comparing(V1EnvVar::getName), description));
// <secondaryList> is empty.
Assert.assertEquals("<secondaryList> is empty and <primaryList> should be returned", heronEnvVars, v1ControllerUtils.mergeListsDedupe(heronEnvVars, new LinkedList<>(), Comparator.comparing(V1EnvVar::getName), description));
// Merge both lists.
Assert.assertTrue("<primaryList> and <secondaryList> merged and deduplicated", expectedEnvVars.containsAll(v1ControllerUtils.mergeListsDedupe(heronEnvVars, inputEnvVars, Comparator.comparing(V1EnvVar::getName), description)));
// Expect thrown error.
String errorMessage = "";
try {
v1ControllerUtils.mergeListsDedupe(heronEnvVars, Collections.singletonList(new V1EnvVar()), Comparator.comparing(V1EnvVar::getName), description);
} catch (TopologySubmissionException e) {
errorMessage = e.getMessage();
}
Assert.assertTrue("Expecting error to be thrown for null deduplication key", errorMessage.contains(description));
}
Aggregations