Java Code Examples for io.fabric8.kubernetes.api.model.EnvVar#getValue()

The following examples show how to use io.fabric8.kubernetes.api.model.EnvVar#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean setEnvVar(List<EnvVar> envVarList, String name, String value) {
    for (EnvVar envVar : envVarList) {
        String envVarName = envVar.getName();
        if (Objects.equals(name, envVarName)) {
            String oldValue = envVar.getValue();
            if (Objects.equals(value, oldValue)) {
                return false;
            } else {
                envVar.setValue(value);
                return true;
            }
        }
    }
    EnvVar env = new EnvVarBuilder().withName(name).withValue(value).build();
    envVarList.add(env);
    return true;
}
 
Example 2
Source File: ModelUtils.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * @param pod The StatefulSet
 * @param containerName The name of the container whoes environment variables are to be retrieved
 * @param envVarName Name of the environment variable which we should get
 * @return The environment of the Kafka container in the sts.
 */
public static String getPodEnv(Pod pod, String containerName, String envVarName) {
    if (pod != null) {
        for (Container container : pod.getSpec().getContainers()) {
            if (containerName.equals(container.getName())) {
                if (container.getEnv() != null) {
                    for (EnvVar envVar : container.getEnv()) {
                        if (envVarName.equals(envVar.getName()))    {
                            return envVar.getValue();
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 3
Source File: EnvVars.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Looks at the value of the provided environment variable and returns a set of environment
 * variable references in the Kubernetes convention of {@literal $(VAR_NAME)}.
 *
 * <p>See <a
 * href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#envvar-v1-core">API
 * docs</a> and/or <a
 * href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config">documentation</a>.
 *
 * @param var the environment variable to analyze
 * @return a set of variable references, never null
 */
public static Set<String> extractReferencedVariables(EnvVar var) {
  String val = var.getValue();

  Matcher matcher = REFERENCE_PATTERN.matcher(val);

  // let's just keep the initial size small, because usually there are not that many references
  // present.
  Set<String> ret = new HashSet<>(2);

  while (matcher.find()) {
    int start = matcher.start();

    // the variable reference can be escaped using a double $, e.g. $$(VAR) is not a reference
    if (start > 0 && val.charAt(start - 1) == '$') {
      continue;
    }

    // extract the variable name out of the reference $(NAME) -> NAME
    String refName = matcher.group().substring(2, matcher.group().length() - 1);
    ret.add(refName);
  }

  return ret;
}
 
Example 4
Source File: ImageEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private String getEnvValue(List<EnvVar> envVars, String name) {
    for (EnvVar var : envVars) {
        if (var.getName().equals(name)) {
            return var.getValue();
        }
    }
    return "(not found)";
}
 
Example 5
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static String getEnvVar(List<EnvVar> envVarList, String name, String defaultValue) {
    String answer = defaultValue;
    if (envVarList != null) {
        for (EnvVar envVar : envVarList) {
            String envVarName = envVar.getName();
            if (Objects.equals(name, envVarName)) {
                String value = envVar.getValue();
                if (StringUtils.isNotBlank(value)) {
                    return value;
                }
            }
        }
    }
    return answer;
}
 
Example 6
Source File: EnvVarsTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
private EnvVar copy(EnvVar envVar) {
  return new EnvVar(envVar.getName(), envVar.getValue(), envVar.getValueFrom());
}