@Override public Collection<CertComponent> listComponents(String uuid) { List<HasMetadata> components = new ArrayList<>(); components.addAll(client.apps().deployments().inNamespace(namespace).withLabel(LabelKeys.INFRA_UUID, uuid).list().getItems()); components.addAll(client.apps().statefulSets().inNamespace(namespace).withLabel(LabelKeys.INFRA_UUID, uuid).list().getItems()); return components.stream() .filter(object -> object.getMetadata().getAnnotations() != null && object.getMetadata().getAnnotations().containsKey(AnnotationKeys.CERT_SECRET_NAME)) .map(object -> { Map<String, String> annotations = object.getMetadata().getAnnotations(); String cn = annotations.getOrDefault(AnnotationKeys.CERT_CN, object.getMetadata().getName()); return new CertComponent(cn, uuid, annotations.get(AnnotationKeys.CERT_SECRET_NAME)); }) .collect(Collectors.toList()); }
deployment = client.apps().deployments().inNamespace("thisisatest").create(deployment); log("Created deployment", deployment); 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();
protected void doListDeploymentsByLabels(Exchange exchange, String operation) throws Exception { DeploymentList deploymentList = null; Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_DEPLOYMENTS_LABELS, Map.class); NonNamespaceOperation<Deployment, DeploymentList, DoneableDeployment, ScalableResource<Deployment, DoneableDeployment>> deployments = getEndpoint().getKubernetesClient() .apps().deployments(); for (Map.Entry<String, String> entry : labels.entrySet()) { deployments.withLabel(entry.getKey(), entry.getValue()); } deploymentList = deployments.list(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(deploymentList.getItems()); }
@Override protected MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> operation() { return client.apps().statefulSets(); }
protected void doGetDeployment(Exchange exchange, String operation) throws Exception { Deployment deployment = null; String deploymentName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_DEPLOYMENT_NAME, String.class); if (ObjectHelper.isEmpty(deploymentName)) { LOG.error("Get a specific Deployment require specify a Deployment name"); throw new IllegalArgumentException("Get a specific Deployment require specify a Deployment name"); } deployment = getEndpoint().getKubernetesClient().apps().deployments().withName(deploymentName).get(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(deployment); }
@Override public void scaleStatefulSet(String name, int numReplicas) { log.info("Scaling stateful set with id {} and {} replicas", name, numReplicas); client.apps().statefulSets().withName(name).scale(numReplicas); }
@Override public List<BrokerCluster> listClusters() { Map<String, List<HasMetadata>> resourceMap = new HashMap<>(); // Add other resources part of a destination cluster List<HasMetadata> objects = new ArrayList<>(); objects.addAll(client.apps().deployments().withLabel(LabelKeys.INFRA_UUID, infraUuid).list().getItems()); objects.addAll(client.apps().statefulSets().withLabel(LabelKeys.INFRA_UUID, infraUuid).list().getItems()); objects.addAll(client.persistentVolumeClaims().withLabel(LabelKeys.INFRA_UUID, infraUuid).list().getItems()); objects.addAll(client.configMaps().withLabel(LabelKeys.INFRA_UUID, infraUuid).withLabelNotIn("type", "address-config", "address-space", "address-space-plan", "address-plan").list().getItems()); objects.addAll(client.services().withLabel(LabelKeys.INFRA_UUID, infraUuid).list().getItems()); for (HasMetadata config : objects) { Map<String, String> annotations = config.getMetadata().getAnnotations(); if (annotations != null && annotations.containsKey(AnnotationKeys.CLUSTER_ID)) { String groupId = annotations.get(AnnotationKeys.CLUSTER_ID); Map<String, String> labels = config.getMetadata().getLabels(); if (labels != null) { if (!resourceMap.containsKey(groupId)) { resourceMap.put(groupId, new ArrayList<>()); } resourceMap.get(groupId).add(config); } } } return resourceMap.entrySet().stream() .map(entry -> { KubernetesList list = new KubernetesList(); list.setItems(entry.getValue()); return new BrokerCluster(entry.getKey(), list); }).collect(Collectors.toList()); }
/*** * 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); }
@Override public Set<StatefulSet> getReadyStatefulSets(AddressSpace addressSpace) { String infraUuid = addressSpace.getAnnotation(AnnotationKeys.INFRA_UUID); return client.apps().statefulSets().inNamespace(namespace).withLabel(LabelKeys.INFRA_UUID, infraUuid).list().getItems().stream() .filter(KubernetesHelper::isReady) .collect(Collectors.toSet()); }
@Override public void deleteResourcesNotIn(String [] uuids) { client.apps().statefulSets().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.secrets().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.configMaps().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.apps().deployments().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.serviceAccounts().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.services().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.persistentVolumeClaims().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); if (isOpenShift) { client.roleBindings().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.deploymentConfigs().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); client.routes().withLabel(LabelKeys.INFRA_TYPE).withLabelNotIn(LabelKeys.INFRA_UUID, uuids).delete(); } }
protected void doDeleteDeployment(Exchange exchange, String operation) { String deploymentName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_DEPLOYMENT_NAME, String.class); String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class); if (ObjectHelper.isEmpty(deploymentName)) { LOG.error("Delete a specific deployment require specify a deployment name"); throw new IllegalArgumentException("Delete a specific deployment require specify a deployment name"); } if (ObjectHelper.isEmpty(namespaceName)) { LOG.error("Delete a specific deployment require specify a namespace name"); throw new IllegalArgumentException("Delete a specific deployment require specify a namespace name"); } Boolean deployment = getEndpoint().getKubernetesClient().apps().deployments().inNamespace(namespaceName).withName(deploymentName).delete(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(deployment); }
private void deleteStatefulSet(Map<String, String> labels) { FilterWatchListDeletable<StatefulSet, StatefulSetList, Boolean, Watch, Watcher<StatefulSet>> ssToDelete = client.apps().statefulSets().withLabels(labels); if (ssToDelete != null && ssToDelete.list().getItems() != null) { boolean ssDeleted = ssToDelete.delete(); logger.debug(String.format("StatefulSet deleted for: %s - %b", labels, ssDeleted)); } }
@Override public void apply(KubernetesList resources, boolean patchPersistentVolumeClaims) { for (HasMetadata resource : resources.getItems()) { try { if (resource instanceof ConfigMap) { client.configMaps().withName(resource.getMetadata().getName()).patch((ConfigMap) resource); } else if (resource instanceof Secret) { client.secrets().withName(resource.getMetadata().getName()).patch((Secret) resource); } else if (resource instanceof Deployment) { client.apps().deployments().withName(resource.getMetadata().getName()).patch((Deployment) resource); } else if (resource instanceof StatefulSet) { client.apps().statefulSets().withName(resource.getMetadata().getName()).cascading(false).patch((StatefulSet) resource); } else if (resource instanceof Service) { client.services().withName(resource.getMetadata().getName()).patch((Service) resource); } else if (resource instanceof ServiceAccount) { client.serviceAccounts().withName(resource.getMetadata().getName()).patch((ServiceAccount) resource); } else if (resource instanceof PersistentVolumeClaim && patchPersistentVolumeClaims) { client.persistentVolumeClaims().withName(resource.getMetadata().getName()).patch((PersistentVolumeClaim) resource); } } catch (KubernetesClientException e) { if (e.getCode() == 404) { // Create it if it does not exist client.resource(resource).createOrReplace(); } else { throw e; } } } }
protected void doList(Exchange exchange, String operation) throws Exception { DeploymentList deploymentsList = getEndpoint().getKubernetesClient().apps().deployments().list(); MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true); exchange.getOut().setBody(deploymentsList.getItems()); }
public void setStatefulSetReplicas(String name, int numReplicas) { client.apps().statefulSets().inNamespace(globalNamespace).withName(name).scale(numReplicas, true); }
client.secrets().withName(resource.getMetadata().getName()).patch((Secret) resource); } else if (resource instanceof Deployment) { client.apps().deployments().withName(resource.getMetadata().getName()).patch((Deployment) resource); } else if (resource instanceof StatefulSet) { client.apps().statefulSets().withName(resource.getMetadata().getName()).cascading(false).patch((StatefulSet) resource); } else if (resource instanceof Service) { client.services().withName(resource.getMetadata().getName()).patch((Service) resource);
@Override public Set<Deployment> getReadyDeployments(AddressSpace addressSpace) { String infraUuid = addressSpace.getAnnotation(AnnotationKeys.INFRA_UUID); return client.apps().deployments().inNamespace(namespace).withLabel(LabelKeys.INFRA_UUID, infraUuid).list().getItems().stream() .filter(KubernetesHelper::isReady) .collect(Collectors.toSet()); }
public List<StatefulSet> listStatefulSets(Map<String, String> labels) { return client.apps().statefulSets().inNamespace(globalNamespace).withLabels(labels).list().getItems(); }
private void deleteDeployment(Map<String, String> labels) { FilterWatchListDeletable<Deployment, DeploymentList, Boolean, Watch, Watcher<Deployment>> deploymentsToDelete = client.apps().deployments().withLabels(labels); if (deploymentsToDelete != null && deploymentsToDelete.list().getItems() != null) { boolean deploymentsDeleted = deploymentsToDelete.delete(); logger.debug(String.format("Deployment deleted for: %s - %b", labels, deploymentsDeleted)); } }
@Override public RouterCluster getRouterCluster() throws IOException { StatefulSet s = client.apps().statefulSets().withName("qdrouterd-" + infraUuid).get(); StandardInfraConfig infraConfig = null; if (s.getMetadata().getAnnotations() != null && s.getMetadata().getAnnotations().get(AnnotationKeys.APPLIED_INFRA_CONFIG) != null) { infraConfig = mapper.readValue(s.getMetadata().getAnnotations().get(AnnotationKeys.APPLIED_INFRA_CONFIG), StandardInfraConfig.class); } return new RouterCluster(s.getMetadata().getName(), s.getSpec().getReplicas(), infraConfig); }