use of io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder in project curiostack by curioswitch.
the class DeployPodTask method exec.
@TaskAction
public void exec() {
ImmutableDeploymentExtension config = getProject().getExtensions().getByType(DeploymentExtension.class);
final ImmutableDeploymentConfiguration deploymentConfig = config.getTypes().getByName(type);
ImmutableGcloudExtension gcloud = getProject().getRootProject().getExtensions().getByType(GcloudExtension.class);
ImmutableList.Builder<EnvVar> envVars = ImmutableList.<EnvVar>builder().addAll(deploymentConfig.envVars().entrySet().stream().map((entry) -> new EnvVar(entry.getKey(), entry.getValue(), null))::iterator).addAll(deploymentConfig.secretEnvVars().entrySet().stream().map((entry) -> new EnvVar(entry.getKey(), null, new EnvVarSourceBuilder().withSecretKeyRef(new SecretKeySelectorBuilder().withName(entry.getValue().get(0)).withKey(entry.getValue().get(1)).build()).build()))::iterator);
if (!deploymentConfig.envVars().containsKey("JAVA_OPTS")) {
int heapSize = deploymentConfig.jvmHeapMb();
StringBuilder javaOpts = new StringBuilder();
javaOpts.append("--add-opens java.base/jdk.internal.misc=ALL-UNNAMED ").append("--add-opens jdk.unsupported/sun.misc=ALL-UNNAMED ").append("-Xms").append(heapSize).append("m ").append("-Xmx").append(heapSize).append("m ").append("-Dconfig.resource=application-").append(type).append(".conf ").append("-Dmonitoring.stackdriverProjectId=").append(gcloud.clusterProject()).append(" ").append("-Dmonitoring.serverName=").append(deploymentConfig.deploymentName()).append(" ");
if (!deploymentConfig.request()) {
int numCpus = (int) Math.ceil(Double.parseDouble(deploymentConfig.cpu()));
int numWorkers = numCpus * 2;
javaOpts.append("-XX:ParallelGCThreads=").append(numCpus).append(" ").append("-Dcom.linecorp.armeria.numCommonWorkers=").append(numWorkers).append(" ").append("-Dio.netty.availableProcessors=").append(numCpus).append(" ");
}
if (!type.equals("prod")) {
javaOpts.append("-Dcom.linecorp.armeria.verboseExceptions=true ");
}
envVars.add(new EnvVar("JAVA_OPTS", javaOpts.toString(), null));
}
Map<String, Quantity> resources = ImmutableMap.of("cpu", new Quantity(deploymentConfig.cpu()), "memory", new Quantity(deploymentConfig.memoryMb() + "Mi"));
Deployment deployment = new DeploymentBuilder().withMetadata(new ObjectMetaBuilder().withNamespace(deploymentConfig.namespace()).withName(deploymentConfig.deploymentName()).build()).withSpec(new DeploymentSpecBuilder().withReplicas(deploymentConfig.replicas()).withStrategy(new DeploymentStrategyBuilder().withType("RollingUpdate").withRollingUpdate(new RollingUpdateDeploymentBuilder().withNewMaxUnavailable(0).build()).build()).withSelector(new LabelSelectorBuilder().withMatchLabels(ImmutableMap.of("name", deploymentConfig.deploymentName())).build()).withTemplate(new PodTemplateSpecBuilder().withMetadata(new ObjectMetaBuilder().withLabels(ImmutableMap.of("name", deploymentConfig.deploymentName(), "revision", System.getenv().getOrDefault("REVISION_ID", "none"))).withAnnotations(ImmutableMap.<String, String>builder().put("prometheus.io/scrape", "true").put("prometheus.io/scheme", "https").put("prometheus.io/path", "/internal/metrics").put("prometheus.io/port", String.valueOf(deploymentConfig.containerPort())).build()).build()).withSpec(new PodSpecBuilder().withContainers(new ContainerBuilder().withResources(new ResourceRequirementsBuilder().withLimits(!deploymentConfig.request() ? resources : ImmutableMap.of()).withRequests(deploymentConfig.request() ? resources : ImmutableMap.of()).build()).withImage(deploymentConfig.image()).withName(deploymentConfig.deploymentName()).withEnv(envVars.build()).withImagePullPolicy("Always").withReadinessProbe(createProbe(deploymentConfig, Duration.ofSeconds(5))).withLivenessProbe(createProbe(deploymentConfig, Duration.ofSeconds(15))).withPorts(ImmutableList.of(new ContainerPortBuilder().withContainerPort(deploymentConfig.containerPort()).withName("http").build())).withVolumeMounts(new VolumeMountBuilder().withName("tls").withMountPath("/etc/tls").withReadOnly(true).build(), new VolumeMountBuilder().withName("rpcacls").withMountPath("/etc/rpcacls").withReadOnly(true).build()).build()).withVolumes(new VolumeBuilder().withName("tls").withSecret(new SecretVolumeSourceBuilder().withSecretName("server-tls").build()).build(), new VolumeBuilder().withName("rpcacls").withConfigMap(new ConfigMapVolumeSourceBuilder().withName("rpcacls").build()).build()).build()).build()).build()).build();
KubernetesClient client = new DefaultKubernetesClient();
Service service = new ServiceBuilder().withMetadata(new ObjectMetaBuilder().withName(deploymentConfig.deploymentName()).withNamespace(deploymentConfig.namespace()).withAnnotations(ImmutableMap.<String, String>builder().put("service.alpha.kubernetes.io/app-protocols", "{\"https\":\"HTTPS\"}").put("prometheus.io/scrape", "true").put("prometheus.io/scheme", "https").put("prometheus.io/path", "/internal/metrics").put("prometheus.io/port", String.valueOf(deploymentConfig.containerPort())).put("prometheus.io/probe", "true").build()).build()).withSpec(createServiceSpec(deploymentConfig)).build();
Map<String, Service> additionalServices = new HashMap<>();
for (String path : deploymentConfig.additionalServicePaths()) {
String sanitizedPath = path;
if (sanitizedPath.endsWith("/*")) {
sanitizedPath = sanitizedPath.substring(0, path.length() - 2);
}
String serviceName = deploymentConfig.deploymentName() + sanitizedPath.replace('/', '-');
additionalServices.put(path, new ServiceBuilder().withMetadata(new ObjectMetaBuilder().withName(serviceName).withNamespace(deploymentConfig.namespace()).withAnnotations(ImmutableMap.of("service.alpha.kubernetes.io/app-protocols", "{\"https\":\"HTTPS\"}")).build()).withSpec(createServiceSpec(deploymentConfig)).build());
}
client.resource(deployment).createOrReplace();
deployService(service, client);
additionalServices.values().forEach(s -> deployService(s, client));
if (deploymentConfig.externalHost() != null) {
List<HTTPIngressPath> ingressPaths = new ArrayList<>();
additionalServices.forEach((path, s) -> ingressPaths.add(createIngressPath(path, s.getMetadata().getName(), deploymentConfig)));
ingressPaths.add(createIngressPath("/*", deploymentConfig.deploymentName(), deploymentConfig));
Ingress ingress = new IngressBuilder().withMetadata(new ObjectMetaBuilder().withNamespace(deploymentConfig.namespace()).withName(deploymentConfig.deploymentName()).withAnnotations(ImmutableMap.of("kubernetes.io/tls-acme", "true", "kubernetes.io/ingress.class", "gce")).build()).withSpec(new IngressSpecBuilder().withTls(new IngressTLSBuilder().withSecretName(deploymentConfig.deploymentName() + "-tls").withHosts(deploymentConfig.externalHost()).build()).withRules(new IngressRuleBuilder().withHost(deploymentConfig.externalHost()).withHttp(new HTTPIngressRuleValueBuilder().withPaths(ingressPaths).build()).build()).build()).build();
client.resource(ingress).createOrReplace();
}
}
use of io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder 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