use of io.fabric8.kubernetes.api.model.extensions.ReplicaSetBuilder 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();
}
}
Aggregations