use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService in project halyard by spinnaker.
the class KubernetesV1DistributedService method stageProfiles.
default List<ConfigSource> stageProfiles(AccountDeploymentDetails<KubernetesAccount> details, GenerateService.ResolvedConfiguration resolvedConfiguration) {
SpinnakerService thisService = getService();
ServiceSettings thisServiceSettings = resolvedConfiguration.getServiceSettings(thisService);
SpinnakerRuntimeSettings runtimeSettings = resolvedConfiguration.getRuntimeSettings();
Integer version = getRunningServiceDetails(details, runtimeSettings).getLatestEnabledVersion();
if (version == null) {
version = 0;
} else {
version++;
}
String namespace = getNamespace(thisServiceSettings);
KubernetesV1ProviderUtils.createNamespace(details, namespace);
String name = getServiceName();
Map<String, String> env = new HashMap<>();
List<ConfigSource> configSources = new ArrayList<>();
Map<String, Profile> serviceProfiles = resolvedConfiguration.getProfilesForService(thisService.getType());
Set<String> requiredFiles = new HashSet<>();
for (SidecarService sidecarService : getSidecars(runtimeSettings)) {
for (Profile profile : sidecarService.getSidecarProfiles(resolvedConfiguration, thisService)) {
if (profile == null) {
throw new HalException(Problem.Severity.FATAL, "Service " + sidecarService.getService().getCanonicalName() + " is required but was not supplied for deployment.");
}
serviceProfiles.put(profile.getName(), profile);
requiredFiles.addAll(profile.getRequiredFiles());
}
}
Map<String, Set<Profile>> collapseByDirectory = new HashMap<>();
for (Map.Entry<String, Profile> entry : serviceProfiles.entrySet()) {
Profile profile = entry.getValue();
String mountPoint = Paths.get(profile.getOutputFile()).getParent().toString();
Set<Profile> profiles = collapseByDirectory.getOrDefault(mountPoint, new HashSet<>());
profiles.add(profile);
requiredFiles.addAll(profile.getRequiredFiles());
collapseByDirectory.put(mountPoint, profiles);
}
String stagingPath = getSpinnakerStagingPath(details.getDeploymentName());
if (!requiredFiles.isEmpty()) {
String secretName = KubernetesV1ProviderUtils.componentDependencies(name, version);
String mountPoint = null;
for (String file : requiredFiles) {
String nextMountPoint = Paths.get(file).getParent().toString();
if (mountPoint == null) {
mountPoint = nextMountPoint;
}
assert (mountPoint.equals(nextMountPoint));
}
Set<Pair<File, String>> pairs = requiredFiles.stream().map(f -> {
return new ImmutablePair<>(new File(f), new File(f).getName());
}).collect(Collectors.toSet());
KubernetesV1ProviderUtils.upsertSecret(details, pairs, secretName, namespace);
configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
}
int ind = 0;
for (Map.Entry<String, Set<Profile>> entry : collapseByDirectory.entrySet()) {
env.clear();
String mountPoint = entry.getKey();
Set<Profile> profiles = entry.getValue();
env.putAll(profiles.stream().reduce(new HashMap<>(), (acc, profile) -> {
acc.putAll(profile.getEnv());
return acc;
}, (a, b) -> {
a.putAll(b);
return a;
}));
String secretName = KubernetesV1ProviderUtils.componentSecret(name + ind, version);
ind += 1;
Set<Pair<File, String>> pairs = profiles.stream().map(p -> {
return new ImmutablePair<>(new File(stagingPath, p.getName()), new File(p.getOutputFile()).getName());
}).collect(Collectors.toSet());
KubernetesV1ProviderUtils.upsertSecret(details, pairs, secretName, namespace);
configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint).setEnv(env));
}
return configSources;
}
use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService in project halyard by spinnaker.
the class KubernetesV1DistributedService method ensureRunning.
default void ensureRunning(AccountDeploymentDetails<KubernetesAccount> details, GenerateService.ResolvedConfiguration resolvedConfiguration, List<ConfigSource> configSources, boolean recreate) {
ServiceSettings settings = resolvedConfiguration.getServiceSettings(getService());
SpinnakerRuntimeSettings runtimeSettings = resolvedConfiguration.getRuntimeSettings();
String namespace = getNamespace(settings);
String serviceName = getServiceName();
String replicaSetName = serviceName + "-v000";
int port = settings.getPort();
SpinnakerMonitoringDaemonService monitoringService = getMonitoringDaemonService();
ServiceSettings monitoringSettings = runtimeSettings.getServiceSettings(monitoringService);
KubernetesClient client = KubernetesV1ProviderUtils.getClient(details);
KubernetesV1ProviderUtils.createNamespace(details, namespace);
Map<String, String> serviceSelector = new HashMap<>();
serviceSelector.put("load-balancer-" + serviceName, "true");
Map<String, String> replicaSetSelector = new HashMap<>();
replicaSetSelector.put("replication-controller", replicaSetName);
Map<String, String> podLabels = new HashMap<>();
podLabels.putAll(replicaSetSelector);
podLabels.putAll(serviceSelector);
Map<String, String> serviceLabels = new HashMap<>();
serviceLabels.put("app", "spin");
serviceLabels.put("stack", getCanonicalName());
ServiceBuilder serviceBuilder = new ServiceBuilder();
serviceBuilder = serviceBuilder.withNewMetadata().withName(serviceName).withNamespace(namespace).withLabels(serviceLabels).endMetadata().withNewSpec().withSelector(serviceSelector).withPorts(new ServicePortBuilder().withPort(port).withName("http").build(), new ServicePortBuilder().withPort(monitoringSettings.getPort()).withName("monitoring").build()).endSpec();
boolean create = true;
if (client.services().inNamespace(namespace).withName(serviceName).get() != null) {
if (recreate) {
client.services().inNamespace(namespace).withName(serviceName).delete();
} else {
create = false;
}
}
if (create) {
client.services().inNamespace(namespace).create(serviceBuilder.build());
}
List<Container> containers = new ArrayList<>();
DeploymentEnvironment deploymentEnvironment = details.getDeploymentConfiguration().getDeploymentEnvironment();
containers.add(ResourceBuilder.buildContainer(serviceName, settings, configSources, deploymentEnvironment));
for (SidecarService sidecarService : getSidecars(runtimeSettings)) {
String sidecarName = sidecarService.getService().getServiceName();
ServiceSettings sidecarSettings = resolvedConfiguration.getServiceSettings(sidecarService.getService());
containers.add(ResourceBuilder.buildContainer(sidecarName, sidecarSettings, configSources, deploymentEnvironment));
}
List<Volume> volumes = configSources.stream().map(c -> {
return new VolumeBuilder().withName(c.getId()).withSecret(new SecretVolumeSourceBuilder().withSecretName(c.getId()).build()).build();
}).collect(Collectors.toList());
ReplicaSetBuilder replicaSetBuilder = new ReplicaSetBuilder();
List<LocalObjectReference> imagePullSecrets = getImagePullSecrets(settings);
Map componentSizing = deploymentEnvironment.getCustomSizing().get(serviceName);
replicaSetBuilder = replicaSetBuilder.withNewMetadata().withName(replicaSetName).withNamespace(namespace).endMetadata().withNewSpec().withReplicas(retrieveKubernetesTargetSize(componentSizing)).withNewSelector().withMatchLabels(replicaSetSelector).endSelector().withNewTemplate().withNewMetadata().withAnnotations(settings.getKubernetes().getPodAnnotations()).withLabels(podLabels).endMetadata().withNewSpec().withContainers(containers).withTerminationGracePeriodSeconds(5L).withVolumes(volumes).withImagePullSecrets(imagePullSecrets).endSpec().endTemplate().endSpec();
create = true;
if (client.extensions().replicaSets().inNamespace(namespace).withName(replicaSetName).get() != null) {
if (recreate) {
client.extensions().replicaSets().inNamespace(namespace).withName(replicaSetName).delete();
RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
while (runningServiceDetails.getLatestEnabledVersion() != null) {
DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
}
} else {
create = false;
}
}
if (create) {
client.extensions().replicaSets().inNamespace(namespace).create(replicaSetBuilder.build());
}
RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
Integer version = runningServiceDetails.getLatestEnabledVersion();
while (version == null || runningServiceDetails.getInstances().get(version).stream().anyMatch(i -> !(i.isHealthy() && i.isRunning()))) {
DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
version = runningServiceDetails.getLatestEnabledVersion();
}
}
use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService in project halyard by spinnaker.
the class DistributedService method getSidecars.
default List<SidecarService> getSidecars(SpinnakerRuntimeSettings runtimeSettings) {
SpinnakerMonitoringDaemonService monitoringService = getMonitoringDaemonService();
ServiceSettings monitoringSettings = runtimeSettings.getServiceSettings(monitoringService);
ServiceSettings thisSettings = runtimeSettings.getServiceSettings(getService());
List<SidecarService> result = new ArrayList<>();
if (monitoringSettings.getEnabled() && thisSettings.getMonitored()) {
result.add(monitoringService);
}
return result;
}
use of com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService in project halyard by spinnaker.
the class GoogleDistributedService method stageProfiles.
@Override
default List<ConfigSource> stageProfiles(AccountDeploymentDetails<GoogleAccount> details, ResolvedConfiguration resolvedConfiguration) {
String deploymentName = details.getDeploymentName();
SpinnakerRuntimeSettings runtimeSettings = resolvedConfiguration.getRuntimeSettings();
SpinnakerService thisService = getService();
ServiceSettings thisServiceSettings = resolvedConfiguration.getServiceSettings(thisService);
Map<String, String> env = new HashMap<>();
Integer version = getRunningServiceDetails(details, runtimeSettings).getLatestEnabledVersion();
if (version == null) {
version = 0;
} else {
version++;
}
List<ConfigSource> configSources = new ArrayList<>();
String stagingPath = getSpinnakerStagingPath(deploymentName);
GoogleVaultServerService vaultService = getVaultServerService();
VaultServerService.Vault vault = vaultService.connectToPrimaryService(details, runtimeSettings);
for (SidecarService sidecarService : getSidecars(runtimeSettings)) {
for (Profile profile : sidecarService.getSidecarProfiles(resolvedConfiguration, thisService)) {
if (profile == null) {
throw new HalException(Problem.Severity.FATAL, "Service " + sidecarService.getService().getCanonicalName() + " is required but was not supplied for deployment.");
}
String secretName = secretName(profile.getName(), version);
String mountPoint = Paths.get(profile.getOutputFile()).toString();
Path stagedFile = Paths.get(profile.getStagedFile(stagingPath));
VaultConfigMount vaultConfigMount = VaultConfigMount.fromLocalFile(stagedFile.toFile(), mountPoint);
secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
}
}
Map<String, Profile> serviceProfiles = resolvedConfiguration.getProfilesForService(thisService.getType());
Set<String> requiredFiles = new HashSet<>();
for (Map.Entry<String, Profile> entry : serviceProfiles.entrySet()) {
Profile profile = entry.getValue();
requiredFiles.addAll(profile.getRequiredFiles());
env.putAll(profile.getEnv());
String mountPoint = profile.getOutputFile();
String secretName = secretName("profile-" + profile.getName(), version);
Path stagedFile = Paths.get(profile.getStagedFile(stagingPath));
VaultConfigMount vaultConfigMount = VaultConfigMount.fromLocalFile(stagedFile.toFile(), mountPoint);
secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
}
for (String file : requiredFiles) {
String mountPoint = Paths.get(file).toString();
String secretName = secretName("dependencies-" + file, version);
VaultConfigMount vaultConfigMount = VaultConfigMount.fromLocalFile(Paths.get(file).toFile(), mountPoint);
secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
}
env.putAll(thisServiceSettings.getEnv());
String envSourceFile = env.entrySet().stream().reduce("", (s, e) -> String.format("%s\n%s=%s", s, e.getKey(), e.getValue()), (s1, s2) -> String.join("\n", s1, s2));
String mountPoint = getEnvFile();
String secretName = secretName("env", version);
VaultConfigMount vaultConfigMount = VaultConfigMount.fromString(envSourceFile, mountPoint);
secretName = vaultService.writeVaultConfig(deploymentName, vault, secretName, vaultConfigMount);
configSources.add(new ConfigSource().setId(secretName).setMountPath(mountPoint));
return configSources;
}
Aggregations