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;
}
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;
}
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);
}
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));
}
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;
}
Aggregations