Search in sources :

Example 1 with NamedContext

use of io.fabric8.kubernetes.api.model.NamedContext in project halyard by spinnaker.

the class KubernetesAccount method contextOptions.

protected List<String> contextOptions(ConfigProblemSetBuilder psBuilder) {
    Config kubeconfig;
    try {
        File kubeconfigFileOpen = new File(getKubeconfigFile());
        kubeconfig = KubeConfigUtils.parseConfig(kubeconfigFileOpen);
    } catch (IOException e) {
        psBuilder.addProblem(ERROR, e.getMessage());
        return null;
    }
    return kubeconfig.getContexts().stream().map(NamedContext::getName).collect(Collectors.toList());
}
Also used : ArtifactSourcesConfig(com.netflix.spinnaker.halyard.config.config.v1.ArtifactSourcesConfig) Config(io.fabric8.kubernetes.api.model.Config) IOException(java.io.IOException) LocalFile(com.netflix.spinnaker.halyard.config.model.v1.node.LocalFile) File(java.io.File)

Example 2 with NamedContext

use of io.fabric8.kubernetes.api.model.NamedContext in project halyard by spinnaker.

the class KubernetesAccountValidator method validateKubeconfig.

private void validateKubeconfig(ConfigProblemSetBuilder psBuilder, KubernetesAccount account) {
    io.fabric8.kubernetes.api.model.Config kubeconfig;
    String context = account.getContext();
    String kubeconfigFile = account.getKubeconfigFile();
    String cluster = account.getCluster();
    String user = account.getUser();
    List<String> namespaces = account.getNamespaces();
    List<String> omitNamespaces = account.getOmitNamespaces();
    // This indicates if a first pass at the config looks OK. If we don't see any serious problems, we'll do one last check
    // against the requested kubernetes cluster to ensure that we can run spinnaker.
    boolean smoketest = true;
    boolean namespacesProvided = namespaces != null && !namespaces.isEmpty();
    boolean omitNamespacesProvided = omitNamespaces != null && !omitNamespaces.isEmpty();
    if (namespacesProvided && omitNamespacesProvided) {
        psBuilder.addProblem(ERROR, "At most one of \"namespaces\" and \"omitNamespaces\" can be supplied.");
        smoketest = false;
    }
    // TODO(lwander) find a good resource / list of resources for generating kubeconfig files to link to here.
    try {
        if (ValidatingFileReader.contents(psBuilder, kubeconfigFile) == null) {
            return;
        }
        File kubeconfigFileOpen = new File(kubeconfigFile);
        kubeconfig = KubeConfigUtils.parseConfig(kubeconfigFileOpen);
    } catch (IOException e) {
        psBuilder.addProblem(ERROR, e.getMessage());
        return;
    }
    System.out.println(context);
    if (context != null && !context.isEmpty()) {
        Optional<NamedContext> namedContext = kubeconfig.getContexts().stream().filter(c -> c.getName().equals(context)).findFirst();
        if (!namedContext.isPresent()) {
            psBuilder.addProblem(ERROR, "Context \"" + context + "\" not found in kubeconfig \"" + kubeconfigFile + "\".", "context").setRemediation("Either add this context to your kubeconfig, rely on the default context, or pick another kubeconfig file.");
            smoketest = false;
        }
    } else {
        String currentContext = kubeconfig.getCurrentContext();
        if (StringUtils.isEmpty(currentContext)) {
            psBuilder.addProblem(ERROR, "You have not specified a Kubernetes context, and your kubeconfig \"" + kubeconfigFile + "\" has no current-context.", "context").setRemediation("Either specify a context in your halconfig, or set a current-context in your kubeconfig.");
            smoketest = false;
        } else {
            psBuilder.addProblem(WARNING, "You have not specified a Kubernetes context in your halconfig, Spinnaker will use \"" + currentContext + "\" instead.", "context").setRemediation("We recommend explicitly setting a context in your halconfig, to ensure changes to your kubeconfig won't break your deployment.");
        }
    }
    if (smoketest) {
        Config config = KubernetesConfigParser.parse(kubeconfigFile, context, cluster, user, namespaces, false);
        try {
            KubernetesClient client = new DefaultKubernetesClient(config);
            client.namespaces().list();
        } catch (Exception e) {
            ConfigProblemBuilder pb = psBuilder.addProblem(ERROR, "Unable to communicate with your Kubernetes cluster: " + e.getMessage() + ".");
            if (e.getMessage().contains("Token may have expired")) {
                pb.setRemediation("If you downloaded these keys with gcloud, it's possible they are in the wrong format. To fix this, run \n\n" + "gcloud config set container/use_client_certificate true\n\ngcloud container clusters get-credentials $CLUSTERNAME");
            } else {
                pb.setRemediation("Unable to authenticate with your Kubernetes cluster. Try using kubectl to verify your credentials.");
            }
        }
    }
}
Also used : KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) StringUtils(org.apache.commons.lang3.StringUtils) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) WARNING(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.WARNING) CollectionUtils(org.apache.commons.collections.CollectionUtils) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) KubernetesConfigParser(com.netflix.spinnaker.clouddriver.kubernetes.v1.security.KubernetesConfigParser) Validator(com.netflix.spinnaker.halyard.config.model.v1.node.Validator) KubeConfigUtils(io.fabric8.kubernetes.client.internal.KubeConfigUtils) NamedContext(io.fabric8.kubernetes.api.model.NamedContext) Provider(com.netflix.spinnaker.halyard.config.model.v1.node.Provider) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DockerRegistryReference(com.netflix.spinnaker.halyard.config.model.v1.providers.containers.DockerRegistryReference) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) ERROR(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.ERROR) IOException(java.io.IOException) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ConfigProblemBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder) Config(io.fabric8.kubernetes.client.Config) Component(org.springframework.stereotype.Component) List(java.util.List) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) DockerRegistryReferenceValidation.validateDockerRegistries(com.netflix.spinnaker.halyard.config.validate.v1.providers.dockerRegistry.DockerRegistryReferenceValidation.validateDockerRegistries) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Optional(java.util.Optional) ValidatingFileReader(com.netflix.spinnaker.halyard.config.validate.v1.util.ValidatingFileReader) DaemonTaskInterrupted(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskInterrupted) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) Collections(java.util.Collections) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) NamedContext(io.fabric8.kubernetes.api.model.NamedContext) Config(io.fabric8.kubernetes.client.Config) IOException(java.io.IOException) IOException(java.io.IOException) ConfigProblemBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 KubernetesConfigParser (com.netflix.spinnaker.clouddriver.kubernetes.v1.security.KubernetesConfigParser)1 ArtifactSourcesConfig (com.netflix.spinnaker.halyard.config.config.v1.ArtifactSourcesConfig)1 DeploymentConfiguration (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)1 LocalFile (com.netflix.spinnaker.halyard.config.model.v1.node.LocalFile)1 Node (com.netflix.spinnaker.halyard.config.model.v1.node.Node)1 Provider (com.netflix.spinnaker.halyard.config.model.v1.node.Provider)1 Validator (com.netflix.spinnaker.halyard.config.model.v1.node.Validator)1 DockerRegistryReference (com.netflix.spinnaker.halyard.config.model.v1.providers.containers.DockerRegistryReference)1 KubernetesAccount (com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount)1 ConfigProblemBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder)1 ConfigProblemSetBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder)1 DockerRegistryReferenceValidation.validateDockerRegistries (com.netflix.spinnaker.halyard.config.validate.v1.providers.dockerRegistry.DockerRegistryReferenceValidation.validateDockerRegistries)1 ValidatingFileReader (com.netflix.spinnaker.halyard.config.validate.v1.util.ValidatingFileReader)1 JobExecutor (com.netflix.spinnaker.halyard.core.job.v1.JobExecutor)1 JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)1 JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)1 ERROR (com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.ERROR)1 FATAL (com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL)1