Search in sources :

Example 36 with Account

use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.

the class KubernetesV1ProviderUtils method kubectlAccountCommand.

private static List<String> kubectlAccountCommand(AccountDeploymentDetails<KubernetesAccount> details) {
    KubernetesAccount account = details.getAccount();
    List<String> command = new ArrayList<>();
    command.add("kubectl");
    String context = account.getContext();
    if (context != null && !context.isEmpty()) {
        command.add("--context");
        command.add(context);
    }
    String cluster = account.getCluster();
    if (cluster != null && !cluster.isEmpty()) {
        command.add("--cluster");
        command.add(cluster);
    }
    String user = account.getUser();
    if (user != null && !user.isEmpty()) {
        command.add("--user");
        command.add(user);
    }
    String kubeconfig = account.getKubeconfigFile();
    if (kubeconfig != null && !kubeconfig.isEmpty()) {
        command.add("--kubeconfig");
        command.add(kubeconfig);
    }
    return command;
}
Also used : KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount)

Example 37 with Account

use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.

the class KubernetesV1ProviderUtils method openProxy.

static Proxy openProxy(JobExecutor jobExecutor, AccountDeploymentDetails<KubernetesAccount> details) {
    KubernetesAccount account = details.getAccount();
    Proxy proxy = proxyMap.getOrDefault(Proxy.buildKey(details.getDeploymentName()), new Proxy());
    String jobId = proxy.jobId;
    if (StringUtils.isEmpty(jobId) || !jobExecutor.jobExists(jobId)) {
        DaemonTaskHandler.newStage("Connecting to the Kubernetes cluster in account \"" + account.getName() + "\"");
        List<String> command = kubectlAccountCommand(details);
        command.add("proxy");
        // select a random port
        command.add("--port=0");
        JobRequest request = new JobRequest().setTokenizedCommand(command);
        proxy.jobId = jobExecutor.startJob(request);
        JobStatus status = jobExecutor.updateJob(proxy.jobId);
        while (status == null) {
            DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(2));
            status = jobExecutor.updateJob(proxy.jobId);
        }
        // This should be a long-running job.
        if (status.getState() == JobStatus.State.COMPLETED) {
            throw new HalException(Severity.FATAL, "Unable to establish a proxy against account " + account.getName() + ":\n" + status.getStdOut() + "\n" + status.getStdErr());
        }
        String connectionMessage = status.getStdOut();
        Pattern portPattern = Pattern.compile(":(\\d+)");
        Matcher matcher = portPattern.matcher(connectionMessage);
        if (matcher.find()) {
            proxy.setPort(Integer.valueOf(matcher.group(1)));
            proxyMap.put(Proxy.buildKey(details.getDeploymentName()), proxy);
            DaemonTaskHandler.message("Connected to kubernetes cluster for account " + account.getName() + " on port " + proxy.getPort());
            DaemonTaskHandler.message("View the kube ui on http://localhost:" + proxy.getPort() + "/ui/");
        } else {
            throw new HalException(Severity.FATAL, "Could not parse connection information from:\n" + connectionMessage + "(" + status.getStdErr() + ")");
        }
    }
    return proxy;
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) Pattern(java.util.regex.Pattern) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) Matcher(java.util.regex.Matcher) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException)

Example 38 with Account

use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.

the class KubernetesV1ProviderUtils method getClient.

static KubernetesClient getClient(AccountDeploymentDetails<KubernetesAccount> details) {
    KubernetesAccount account = details.getAccount();
    Config config = KubernetesConfigParser.parse(account.getKubeconfigFile(), account.getContext(), account.getCluster(), account.getUser(), account.getNamespaces(), false);
    return new DefaultKubernetesClient(config);
}
Also used : Config(io.fabric8.kubernetes.client.Config) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 39 with Account

use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.

the class KubernetesV2Service method connectCommand.

default String connectCommand(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
    ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
    KubernetesAccount account = details.getAccount();
    String namespace = settings.getLocation();
    String name = getServiceName();
    int port = settings.getPort();
    String podNameCommand = String.join(" ", KubernetesV2Utils.kubectlPodServiceCommand(account, namespace, name));
    return String.join(" ", KubernetesV2Utils.kubectlConnectPodCommand(account, namespace, "$(" + podNameCommand + ")", port));
}
Also used : KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) HasServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings) KubernetesSharedServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.KubernetesSharedServiceSettings) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)

Example 40 with Account

use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.

the class KubernetesV2Service method stageConfig.

default List<ConfigSource> stageConfig(AccountDeploymentDetails<KubernetesAccount> details, GenerateService.ResolvedConfiguration resolvedConfiguration) {
    Map<String, Profile> profiles = resolvedConfiguration.getProfilesForService(getService().getType());
    String stagingPath = getSpinnakerStagingPath(details.getDeploymentName());
    Map<String, Set<Profile>> profilesByDirectory = new HashMap<>();
    List<String> requiredFiles = new ArrayList<>();
    List<ConfigSource> configSources = new ArrayList<>();
    String secretNamePrefix = getServiceName() + "-files";
    String namespace = getNamespace(resolvedConfiguration.getServiceSettings(getService()));
    KubernetesAccount account = details.getAccount();
    for (Entry<String, Profile> entry : profiles.entrySet()) {
        Profile profile = entry.getValue();
        String outputFile = profile.getOutputFile();
        String mountPoint = Paths.get(outputFile).getParent().toString();
        Set<Profile> profilesInDirectory = profilesByDirectory.getOrDefault(mountPoint, new HashSet<>());
        profilesInDirectory.add(profile);
        requiredFiles.addAll(profile.getRequiredFiles());
        profilesByDirectory.put(mountPoint, profilesInDirectory);
    }
    for (Entry<String, Set<Profile>> entry : profilesByDirectory.entrySet()) {
        Set<Profile> profilesInDirectory = entry.getValue();
        String mountPath = entry.getKey();
        List<SecretMountPair> files = profilesInDirectory.stream().map(p -> {
            File input = new File(p.getStagedFile(stagingPath));
            File output = new File(p.getOutputFile());
            return new SecretMountPair(input, output);
        }).collect(Collectors.toList());
        Map<String, String> env = profilesInDirectory.stream().map(Profile::getEnv).map(Map::entrySet).flatMap(Collection::stream).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
        String name = KubernetesV2Utils.createSecret(account, namespace, secretNamePrefix, files);
        configSources.add(new ConfigSource().setId(name).setMountPath(mountPath).setEnv(env));
    }
    if (!requiredFiles.isEmpty()) {
        List<SecretMountPair> files = requiredFiles.stream().map(File::new).map(SecretMountPair::new).collect(Collectors.toList());
        String name = KubernetesV2Utils.createSecret(account, namespace, secretNamePrefix, files);
        configSources.add(new ConfigSource().setId(name).setMountPath(files.get(0).getContents().getParent()));
    }
    return configSources;
}
Also used : Arrays(java.util.Arrays) KubernetesUtil(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.KubernetesUtil) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) SecretMountPair(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.v2.KubernetesV2Utils.SecretMountPair) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) Map(java.util.Map) KubernetesImageDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesImageDescription) JinjaJarResource(com.netflix.spinnaker.halyard.core.resource.v1.JinjaJarResource) Strings(io.fabric8.utils.Strings) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration) Collectors(java.util.stream.Collectors) File(java.io.File) HasServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.HasServiceSettings) List(java.util.List) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) KubernetesSharedServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.KubernetesSharedServiceSettings) Entry(java.util.Map.Entry) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Collections(java.util.Collections) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) SecretMountPair(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.kubernetes.v2.KubernetesV2Utils.SecretMountPair) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Account (com.netflix.spinnaker.halyard.config.model.v1.node.Account)19 List (java.util.List)13 Provider (com.netflix.spinnaker.halyard.config.model.v1.node.Provider)11 KubernetesAccount (com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount)11 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)11 Collectors (java.util.stream.Collectors)10 DeploymentConfiguration (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)9 ConfigProblemBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder)8 ArrayList (java.util.ArrayList)8 AbstractCanaryAccount (com.netflix.spinnaker.halyard.config.model.v1.canary.AbstractCanaryAccount)7 Path (java.nio.file.Path)7 OperationHandler (com.netflix.spinnaker.halyard.cli.services.v1.OperationHandler)6 ArtifactAccount (com.netflix.spinnaker.halyard.config.model.v1.node.ArtifactAccount)6 DockerRegistryReference (com.netflix.spinnaker.halyard.config.model.v1.providers.containers.DockerRegistryReference)6 GoogleAccount (com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleAccount)6 UpdateRequestBuilder (com.netflix.spinnaker.halyard.core.DaemonResponse.UpdateRequestBuilder)6 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)6 IOException (java.io.IOException)5 Collections (java.util.Collections)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5