Java Code Examples for io.fabric8.kubernetes.client.utils.Utils#getSystemPropertyOrEnvVar()

The following examples show how to use io.fabric8.kubernetes.client.utils.Utils#getSystemPropertyOrEnvVar() . 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: Config.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
private static boolean tryKubeConfig(Config config, String context) {
  LOGGER.debug("Trying to configure client from Kubernetes config...");
  if (!Utils.getSystemPropertyOrEnvVar(KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, true)) {
    return false;
  }
  File kubeConfigFile = new File(getKubeconfigFilename());
  if (!kubeConfigFile.isFile()) {
    LOGGER.debug("Did not find Kubernetes config at: [{}]. Ignoring.", kubeConfigFile.getPath());
    return false;
  }
  LOGGER.debug("Found for Kubernetes config at: [{}].", kubeConfigFile.getPath());
  String kubeconfigContents = getKubeconfigContents(kubeConfigFile);
  if (kubeconfigContents == null) {
    return false;
  }
  loadFromKubeconfig(config, context, kubeconfigContents, kubeConfigFile.getPath());
  return true;
}
 
Example 2
Source File: Config.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
private static boolean tryNamespaceFromPath(Config config) {
  LOGGER.debug("Trying to configure client namespace from Kubernetes service account namespace path...");
  if (Utils.getSystemPropertyOrEnvVar(KUBERNETES_TRYNAMESPACE_PATH_SYSTEM_PROPERTY, true)) {
    String serviceAccountNamespace = Utils.getSystemPropertyOrEnvVar(KUBERNETES_NAMESPACE_FILE, KUBERNETES_NAMESPACE_PATH);
    boolean serviceAccountNamespaceExists = Files.isRegularFile(new File(serviceAccountNamespace).toPath());
    if (serviceAccountNamespaceExists) {
      LOGGER.debug("Found service account namespace at: [" + serviceAccountNamespace + "].");
      try {
        String namespace = new String(Files.readAllBytes(new File(serviceAccountNamespace).toPath()));
        config.setNamespace(namespace.replace(System.lineSeparator(), ""));
        return true;
      } catch (IOException e) {
        LOGGER.error("Error reading service account namespace from: [" + serviceAccountNamespace + "].", e);
      }
    } else {
      LOGGER.debug("Did not find service account namespace at: [" + serviceAccountNamespace + "]. Ignoring.");
    }
  }
  return false;
}
 
Example 3
Source File: Config.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public static String getKeyAlgorithm(String clientKeyFile, String clientKeyData) {
  // Check if any system property is set
  if(Utils.getSystemPropertyOrEnvVar(KUBERNETES_CLIENT_KEY_ALGO_SYSTEM_PROPERTY) != null) {
    return Utils.getSystemPropertyOrEnvVar(KUBERNETES_CLIENT_KEY_ALGO_SYSTEM_PROPERTY);
  }

  // Detect algorithm
  try {
    InputStream keyInputStream = CertUtils.getInputStreamFromDataOrFile(clientKeyData, clientKeyFile);
    if(keyInputStream != null) {
      return getKeyAlgorithm(keyInputStream);
    }
  } catch(IOException exception) {
    LOGGER.debug("Failure in determining private key algorithm type, defaulting to RSA ", exception.getMessage());
  }
  return null;
}
 
Example 4
Source File: Config.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated use {@link #autoConfigure(String)} or {@link ConfigBuilder} instead
 */
@Deprecated
public Config() {
  if (!Utils.getSystemPropertyOrEnvVar(KUBERNETES_DISABLE_AUTO_CONFIG_SYSTEM_PROPERTY, false)) {
    autoConfigure(this, null);
  }
}
 
Example 5
Source File: Config.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static boolean tryServiceAccount(Config config) {
  LOGGER.debug("Trying to configure client from service account...");
  String masterHost = Utils.getSystemPropertyOrEnvVar(KUBERNETES_SERVICE_HOST_PROPERTY, (String) null);
  String masterPort = Utils.getSystemPropertyOrEnvVar(KUBERNETES_SERVICE_PORT_PROPERTY, (String) null);
  if (masterHost != null && masterPort != null) {
    String hostPort = joinHostPort(masterHost, masterPort);
    LOGGER.debug("Found service account host and port: " + hostPort);
    config.setMasterUrl("https://" + hostPort);
  }
  if (Utils.getSystemPropertyOrEnvVar(KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, true)) {
    boolean serviceAccountCaCertExists = Files.isRegularFile(new File(KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH).toPath());
    if (serviceAccountCaCertExists) {
      LOGGER.debug("Found service account ca cert at: ["+KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH+"].");
      config.setCaCertFile(KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH);
    } else {
      LOGGER.debug("Did not find service account ca cert at: ["+KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH+"].");
    }
    try {
      String serviceTokenCandidate = new String(Files.readAllBytes(new File(KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH).toPath()));
      LOGGER.debug("Found service account token at: ["+KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH+"].");
      config.setOauthToken(serviceTokenCandidate);
      String txt = "Configured service account doesn't have access. Service account may have been revoked.";
      config.getErrorMessages().put(401, "Unauthorized! " + txt);
      config.getErrorMessages().put(403, "Forbidden!" + txt);
      return true;
    } catch (IOException e) {
      // No service account token available...
      LOGGER.warn("Error reading service account token from: [{}]. Ignoring.", KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH);
    }
  }
  return false;
}
 
Example 6
Source File: Config.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static String getKubeconfigFilename() {
  String fileName = Utils.getSystemPropertyOrEnvVar(KUBERNETES_KUBECONFIG_FILE, new File(getHomeDir(), ".kube" + File.separator + "config").toString());

  // if system property/env var contains multiple files take the first one based on the environment
  // we are running in (eg. : for Linux, ; for Windows)
  String[] fileNames = fileName.split(File.pathSeparator);

  if (fileNames.length > 1) {
    LOGGER.warn("Found multiple Kubernetes config files [{}], using the first one: [{}]. If not desired file, please change it by doing `export KUBECONFIG=/path/to/kubeconfig` on Unix systems or `$Env:KUBECONFIG=/path/to/kubeconfig` on Windows.", fileNames, fileNames[0]);
    fileName = fileNames[0];
  }
  return fileName;
}
 
Example 7
Source File: OpenShiftConfig.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static String getDefaultOpenShiftUrl(Config config) {
  String openshiftUrl = Utils.getSystemPropertyOrEnvVar(OPENSHIFT_URL_SYSTEM_PROPERTY);
  if (openshiftUrl != null) {
    // The OPENSHIFT_URL environment variable may be set to the root url (i.e. without the '/oapi/version' path) in some configurations
    if (isRootURL(openshiftUrl)) {
      openshiftUrl = URLUtils.join(openshiftUrl, "oapi", getDefaultOapiVersion(config));
    }
    return openshiftUrl;
  } else {
    return URLUtils.join(config.getMasterUrl(), "oapi", getDefaultOapiVersion(config));
  }
}
 
Example 8
Source File: OpenShiftConfig.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
private static String getDefaultOapiVersion(Config config) {
  return Utils.getSystemPropertyOrEnvVar(KUBERNETES_OAPI_VERSION_SYSTEM_PROPERTY, config.getApiVersion());
}