public void run() { try { Deployment deployment = get(); //If the deployment is gone, we shouldn't wait. if (deployment == null) { if (count == 0) { queue.put(true); return; } else { queue.put(new IllegalStateException("Can't wait for Deployment: " + checkName(getItem()) + " in namespace: " + checkName(getItem()) + " to scale. Resource is no longer available.")); return; } } replicasRef.set(deployment.getStatus().getReplicas()); int currentReplicas = deployment.getStatus().getReplicas() != null ? deployment.getStatus().getReplicas() : 0; long generation = deployment.getMetadata().getGeneration() != null ? deployment.getMetadata().getGeneration() : 0; long observedGeneration = deployment.getStatus() != null && deployment.getStatus().getObservedGeneration() != null ? deployment.getStatus().getObservedGeneration() : -1; if (observedGeneration >= generation && Objects.equals(deployment.getSpec().getReplicas(), currentReplicas)) { queue.put(true); } else { LOG.debug("Only {}/{} pods scheduled for Deployment: {} in namespace: {} seconds so waiting...", deployment.getStatus().getReplicas(), deployment.getSpec().getReplicas(), deployment.getMetadata().getName(), namespace); } } catch (Throwable t) { LOG.error("Error while waiting for Deployment to be scaled.", t); } } };
@Override public boolean reap() { Deployment deployment = oper.cascading(false).edit().editSpec().withReplicas(0).endSpec().done(); //TODO: These checks shouldn't be used as they are not realistic. We just use them to support mock/crud tests. Need to find a cleaner way to do so. if (deployment.getStatus() != null) { waitForObservedGeneration(deployment.getStatus().getObservedGeneration()); } if (deployment.getSpec().getSelector() != null) { reapMatchingReplicaSets(deployment.getSpec().getSelector()); } return false; }
@Override protected Integer currentScale(String namespace, String name) { Deployment deployment = get(namespace, name); if (deployment != null) { return deployment.getSpec().getReplicas(); } else { return null; } }
@Override public void adapt(KubernetesListBuilder builder) { super.adapt(builder); List<HasMetadata> items = builder.getItems(); for (HasMetadata item : items) { if (item instanceof Deployment) { Deployment deployment = (Deployment) item; ObjectMeta metadata = deployment.getMetadata(); DeploymentSpec spec = deployment.getSpec(); if (metadata != null && spec != null) { PodTemplateSpec template = spec.getTemplate(); if (template != null) { ObjectMeta templateMetadata = template.getMetadata(); if (templateMetadata == null) { templateMetadata = new ObjectMeta(); template.setMetadata(templateMetadata); } templateMetadata.setAnnotations(MapUtil.mergeMaps(templateMetadata.getAnnotations(), metadata.getAnnotations())); } } } } builder.withItems(items); } }
log("Created deployment", deployment); System.err.println("Scaling up:" + deployment.getMetadata().getName()); client.apps().deployments().inNamespace("thisisatest").withName("nginx").scale(2, true); log("Created replica sets:", client.apps().replicaSets().inNamespace("thisisatest").list().getItems()); System.err.println("Deleting:" + deployment.getMetadata().getName()); client.resource(deployment).delete();
public void run() { Deployment deployment = oper.getMandatory(); if (observedGeneration <= deployment.getStatus().getObservedGeneration()) { countDownLatch.countDown(); } } };
private int getDeploymentCaCertGeneration(Deployment dep, Ca ca) { int caCertGeneration = 0; if (dep != null) { caCertGeneration = Annotations.intAnnotation( dep.getSpec().getTemplate(), getCaCertAnnotation(ca), 0); } return caCertGeneration; }
Deployment resource = (Deployment) item; DeploymentConfigBuilder builder = new DeploymentConfigBuilder(); builder.withMetadata(resource.getMetadata()); DeploymentSpec spec = resource.getSpec(); if (spec != null) { DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder = builder.withNewSpec();
public static boolean isDeploymentReady(Deployment d) { Utils.checkNotNull(d, "Deployment can't be null."); DeploymentSpec spec = d.getSpec(); DeploymentStatus status = d.getStatus(); if (status == null || status.getReplicas() == null || status.getAvailableReplicas() == null) { return false; } //Can be true in testing, so handle it to make test writing easier. if (spec == null || spec.getReplicas() == null) { return false; } return spec.getReplicas().intValue() == status.getReplicas() && spec.getReplicas().intValue() <= status.getAvailableReplicas(); }
/*** * Check if deployment exists * @param namespace kuberntes namespace name * @param appName name of deployment * @return true if deployment exists */ public boolean deploymentExists(String namespace, String appName) { return client.apps().deployments().inNamespace(namespace).list().getItems().stream() .map(deployment -> deployment.getMetadata().getName()).collect(Collectors.toList()).contains(appName); }
private static boolean isReady(Deployment deployment) { // TODO: Assuming at least one replica is ok Integer readyReplicas = deployment.getStatus().getReadyReplicas(); return readyReplicas != null && readyReplicas >= 1; }
private int findReplicas(List<HasMetadata> items) { for (HasMetadata item : items) { if (item instanceof StatefulSet) { return ((StatefulSet)item).getSpec().getReplicas(); } else if (item instanceof Deployment) { return ((Deployment)item).getSpec().getReplicas(); } } return 0; }
private void checkDeploymentsReady(AddressSpace addressSpace, List<HasMetadata> requiredResources) { Set<String> readyDeployments = kubernetes.getReadyDeployments(addressSpace).stream() .map(deployment -> deployment.getMetadata().getName()) .collect(Collectors.toSet()); Set<String> requiredDeployments = requiredResources.stream() .filter(KubernetesHelper::isDeployment) .map(item -> item.getMetadata().getName()) .collect(Collectors.toSet()); boolean isReady = readyDeployments.containsAll(requiredDeployments); if (!isReady) { Set<String> missing = new HashSet<>(requiredDeployments); missing.removeAll(readyDeployments); addressSpace.getStatus().setReady(false); addressSpace.getStatus().appendMessage("The following deployments are not ready: " + missing); } }
private int findReadyReplicas(List<HasMetadata> items) { for (HasMetadata item : items) { if (item instanceof StatefulSet) { return Optional.ofNullable(((StatefulSet)item).getStatus()).map(StatefulSetStatus::getReadyReplicas).orElse(0); } else if (item instanceof Deployment) { return Optional.ofNullable(((Deployment)item).getStatus()).map(DeploymentStatus::getReadyReplicas).orElse(0); } } return 0; }
this.toDeployment = topicOperator.generateDeployment(isOpenShift); this.toMetricsAndLogsConfigMap = logAndMetricsConfigMap; Annotations.annotations(this.toDeployment.getSpec().getTemplate()).put( ANNO_STRIMZI_IO_LOGGING, this.toMetricsAndLogsConfigMap.getData().get("log4j2.properties"));
/*** * Creates application from resources * @param namespace * @param resources * @return String name of application * @throws Exception */ public String createDeploymentFromResource(String namespace, Deployment resources) throws Exception { Deployment depRes = client.apps().deployments().inNamespace(namespace).create(resources); Deployment result = client.apps().deployments().inNamespace(namespace) .withName(depRes.getMetadata().getName()).waitUntilReady(2, TimeUnit.MINUTES); log.info("Deployment {} created", result.getMetadata().getName()); return result.getMetadata().getName(); }
protected void doScaleDeployment(Exchange exchange, String operation) throws Exception { String deploymentName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_DEPLOYMENT_NAME, String.class); String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); Integer replicasNumber = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_DEPLOYMENT_REPLICAS, Integer.class); if (ObjectHelper.isEmpty(deploymentName)) { LOG.error("Scale a specific deployment require specify a deployment name"); throw new IllegalArgumentException("Scale a specific deployment require specify a deployment name"); } if (ObjectHelper.isEmpty(namespaceName)) { LOG.error("Scale a specific deployment require specify a namespace name"); throw new IllegalArgumentException("Scale a specific deployment require specify a namespace name"); } if (ObjectHelper.isEmpty(replicasNumber)) { LOG.error("Scale a specific deployment require specify a replicas number"); throw new IllegalArgumentException("Scale a specific deployment require specify a replicas number"); } Deployment deploymentScaled = getEndpoint().getKubernetesClient().apps().deployments().inNamespace(namespaceName).withName(deploymentName).scale(replicasNumber, false); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(deploymentScaled.getStatus().getReplicas()); } }
Future<ReconciliationState> entityOperatorDeployment(Supplier<Date> dateSupplier) { if (this.entityOperator != null) { Future<Deployment> future = deploymentOperations.getAsync(namespace, this.entityOperator.getName()); if (future != null) { return future.compose(dep -> { // getting the current cluster CA generation from the current deployment, if exists int clusterCaCertGeneration = getDeploymentCaCertGeneration(dep, this.clusterCa); int clientsCaCertGeneration = getDeploymentCaCertGeneration(dep, this.clientsCa); // if maintenance windows are satisfied, the cluster CA generation could be changed // and EO needs a rolling update updating the related annotation boolean isSatisfiedBy = isMaintenanceTimeWindowsSatisfied(dateSupplier); if (isSatisfiedBy) { clusterCaCertGeneration = getCaCertGeneration(this.clusterCa); clientsCaCertGeneration = getCaCertGeneration(this.clientsCa); } Annotations.annotations(eoDeployment.getSpec().getTemplate()).put( Ca.ANNO_STRIMZI_IO_CLUSTER_CA_CERT_GENERATION, String.valueOf(clusterCaCertGeneration)); Annotations.annotations(eoDeployment.getSpec().getTemplate()).put( Ca.ANNO_STRIMZI_IO_CLIENTS_CA_CERT_GENERATION, String.valueOf(clientsCaCertGeneration)); return withVoid(deploymentOperations.reconcile(namespace, EntityOperator.entityOperatorName(name), eoDeployment)); }).map(i -> this); } } return Future.succeededFuture(this); }
} else if (object instanceof Deployment) { Deployment deployment = (Deployment) object; deployments.put(deployment.getMetadata().getName(), deployment); } else if (object instanceof Service) { Service service = (Service) object;