Refine search
public KubernetesClientTimeoutException(HasMetadata resource, long amount, TimeUnit timeUnit) { super(String.format(RESOURCE_FORMAT, timeUnit.toMillis(amount), resource.getKind(), resource.getMetadata().getName(), resource.getMetadata().getNamespace())); this.resourcesNotReady = Collections.unmodifiableList(Arrays.asList(resource)); }
/** * Extracts apiGroupName from apiGroupVersion when in resource for apiGroupName/apiGroupVersion combination * @param item resource which is being used * @param apiGroup apiGroupName present if any * @return Just the apiGroupName part without apiGroupVersion */ public static <T> String apiGroup(T item, String apiGroup) { if (item instanceof HasMetadata && Utils.isNotNullOrEmpty(((HasMetadata) item).getApiVersion())) { return trimGroupOrNull(((HasMetadata) item).getApiVersion()); } else if (apiGroup != null && !apiGroup.isEmpty()) { return trimGroup(apiGroup); } return null; }
public static ObjectMeta getOrCreateMetadata(HasMetadata entity) { ObjectMeta metadata = entity.getMetadata(); if (metadata == null) { metadata = new ObjectMeta(); entity.setMetadata(metadata); } return metadata; }
@Override public void run() { LOG.debug("Only {}/{} pod(s) ready for {}: {} in namespace: {} seconds so waiting...", podCount.get(), requiredPodCount, obj.getKind(), obj.getMetadata().getName(), namespace); } }, 0, loggingIntervalMillis, TimeUnit.MILLISECONDS);
T rollUpdate(T oldObj, T newObj) { try { String namespace = oldObj.getMetadata().getNamespace(); String oldName = oldObj.getMetadata().getName(); String newName = newObj.getMetadata().getName(); if (newName == null || newName.equals(oldName)) { newName = newName + "-" + newDeploymentHash; while (getReplicas(createdObj) < getReplicas(newObj)) { int newReplicas = getReplicas(createdObj) + 1; resources().inNamespace(namespace).withName(createdObj.getMetadata().getName()).scale(newReplicas, true); waitUntilPodsAreReady(createdObj, namespace, newReplicas); createdObj = setReplicas(createdObj, newReplicas); if (Objects.equals(oldName, newObj.getMetadata().getName())) { createdObj.getMetadata().setResourceVersion(null); createdObj.getMetadata().setName(oldName); editable = resources().inNamespace(namespace).withName(createdObj.getMetadata().getName()).cascading(false).edit(); removeDeploymentKey(editable); createdObj = editable.done(); throw new KubernetesClientException("Could not calculate MD5 of RC", e);
private static String display(HasMetadata item) { StringBuilder sb = new StringBuilder(); sb.append("[ "); if (Utils.isNotNullOrEmpty(item.getKind())) { sb.append("Kind:").append(item.getKind()); } if (Utils.isNotNullOrEmpty(item.getMetadata().getName())) { sb.append("Name:").append(item.getMetadata().getName()); } if (item.getMetadata().getLabels()!=null && !item.getMetadata().getLabels().isEmpty()) { sb.append("Lables: [ "); for (Map.Entry<String,String> entry : item.getMetadata().getLabels().entrySet()) { sb.append(entry.getKey()).append(":").append(entry.getValue()).append(" "); } sb.append("]"); } sb.append(" ]"); return sb.toString(); } }
public static String getNamespace(HasMetadata entity) { if (entity != null) { return entity.getMetadata() != null ? entity.getMetadata().getNamespace() : null; } else { return null; } }
@Override public void handle(Void v) { EventBuilder evtb = new EventBuilder().withApiVersion("v1"); if (involvedObject != null) { evtb.withNewInvolvedObject() .withKind(involvedObject.getKind()) .withName(involvedObject.getMetadata().getName()) .withApiVersion(involvedObject.getApiVersion()) .withNamespace(involvedObject.getMetadata().getNamespace()) .withUid(involvedObject.getMetadata().getUid()) .endInvolvedObject(); } evtb.withType(eventType.name) .withMessage(message) .withNewMetadata().withLabels(resourcePredicate.labels()).withGenerateName("topic-operator").withNamespace(namespace).endMetadata() .withNewSource() .withComponent(TopicOperator.class.getName()) .endSource(); io.fabric8.kubernetes.api.model.Event event = evtb.build(); switch (eventType) { case INFO: LOGGER.info("{}", message); break; case WARNING: LOGGER.warn("{}", message); break; } k8s.createEvent(event, handler); }
@Override protected <T> String checkNamespace(T item) { String operationNs = getNamespace(); String itemNs = (item instanceof HasMetadata && ((HasMetadata)item).getMetadata() != null) ? ((HasMetadata) item).getMetadata().getNamespace() : null; if (Utils.isNullOrEmpty(operationNs) && Utils.isNullOrEmpty(itemNs)) { return null; } else if (Utils.isNullOrEmpty(itemNs)) { return operationNs; } else if (Utils.isNullOrEmpty(operationNs)) { return itemNs; } else if (itemNs.equals(operationNs)) { return itemNs; } throw new KubernetesClientException("Namespace mismatch. Item namespace:" + itemNs + ". Operation namespace:" + operationNs + "."); }
protected <T> String checkName(T item) { String operationName = getName(); String itemName = item instanceof HasMetadata ? ((HasMetadata) item).getMetadata().getName() : null; if (Utils.isNullOrEmpty(operationName) && Utils.isNullOrEmpty(itemName)) { return null; } else if (Utils.isNullOrEmpty(itemName)) { return operationName; } else if (Utils.isNullOrEmpty(operationName)) { return itemName; } else if (itemName.equals(operationName)) { return itemName; } throw new KubernetesClientException("Name mismatch. Item name:" + itemName + ". Operation name:" + operationName + "."); }
@Override public T createOrReplace(T... items) { T item = getItem(); if (items.length > 1) { throw new IllegalArgumentException("Too many items to create."); } else if (items.length == 1) { item = items[0]; } if (item == null) { throw new IllegalArgumentException("Nothing to create."); } if (Utils.isNullOrEmpty(name) && item instanceof HasMetadata) { return withName(((HasMetadata)item).getMetadata().getName()).createOrReplace(item); } if (fromServer().get() == null) { return create(item); } else { return replace(item); } }
@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; } } } }
public void run() { try { T t = get(); //If the resource is gone, we shouldn't wait. if (t == null) { if (count == 0) { queue.put(true); } else { queue.put(new IllegalStateException("Can't wait for " + getType().getSimpleName() + ": " +name + " in namespace: " + namespace + " to scale. Resource is no longer available.")); } return; } int currentReplicas = getCurrentReplicas(t); int desiredReplicas = getDesiredReplicas(t); replicasRef.set(currentReplicas); long generation = t.getMetadata().getGeneration() != null ? t.getMetadata().getGeneration() : -1; long observedGeneration = getObservedGeneration(t); if (observedGeneration >= generation && Objects.equals(desiredReplicas, currentReplicas)) { queue.put(true); } Log.debug("Only {}/{} replicas scheduled for {}: {} in namespace: {} seconds so waiting...", currentReplicas, desiredReplicas, t.getKind(), t.getMetadata().getName(), namespace); } catch (Throwable t) { Log.error("Error while waiting for resource to be scaled.", t); } } };
for (HasMetadata meta : acceptVisitors(asHasMetadata(item, true), visitors)) { ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta); HasMetadata r = h.reload(client, config, meta.getMetadata().getNamespace(), meta); String namespaceToUse = meta.getMetadata().getNamespace(); Boolean deleted = h.delete(client, config, namespaceToUse, meta); if (!deleted) { throw new KubernetesClientException("Failed to delete existing item:" + meta);
private void setOwnership(HasMetadata owned, HasMetadata ...owners) { ArrayList<OwnerReference> ownerReferences = new ArrayList<>(owners.length); for (HasMetadata owner: owners) { ownerReferences.add(new OwnerReferenceBuilder() .withApiVersion(owner.getApiVersion()) .withKind(owner.getKind()) .withBlockOwnerDeletion(false) .withName(owner.getMetadata().getName()) .withUid(owner.getMetadata().getUid()) .build()); } owned.getMetadata().setOwnerReferences(ownerReferences); }
/** * Returns the name and falls back to the item name. * @param item The item. * @param name The name to check. * @param <T> * @return */ private static <T> String name(T item, String name) { if (name != null && !name.isEmpty()) { return name; } else if (item instanceof HasMetadata) { HasMetadata h = (HasMetadata) item; return h.getMetadata() != null ? h.getMetadata().getName() : null; } return null; }
/** * Asynchronously create or update the given {@code resource} depending on whether it already exists, * returning a future for the outcome. * If the resource with that name already exists the future completes successfully. * @param resource The resource to create. */ public Future<ReconcileResult<T>> createOrUpdate(T resource) { if (resource == null) { throw new NullPointerException(); } return reconcile(resource.getMetadata().getNamespace(), resource.getMetadata().getName(), resource); }
} catch (KubernetesClientException e) { String message = e.getCause() == null ? e.getMessage() : e.getCause().getMessage(); if (message.contains("\n")) { Map<String, Secret> secrets = new HashMap<>(); for (HasMetadata object : list) { checkNotNull(object.getKind(), "Environment contains object without specified kind field"); checkNotNull(object.getMetadata(), "%s metadata must not be null", object.getKind()); checkNotNull(object.getMetadata().getName(), "%s name must not be null", object.getKind()); } else if (object instanceof Pod) { Pod pod = (Pod) object; pods.put(pod.getMetadata().getName(), pod); } else if (object instanceof Deployment) { Deployment deployment = (Deployment) object; deployments.put(deployment.getMetadata().getName(), deployment); } else if (object instanceof Service) { Service service = (Service) object; format( "Found unknown object type in recipe -- name: '%s', kind: '%s'", object.getMetadata().getName(), object.getKind()));
protected static void mergeMetadata(HasMetadata item1, HasMetadata item2) { if (item1 != null && item2 != null) { ObjectMeta metadata1 = item1.getMetadata(); ObjectMeta metadata2 = item2.getMetadata(); if (metadata1 == null) { item1.setMetadata(metadata2); } else if (metadata2 != null) { metadata1.setAnnotations(mergeMapsAndRemoveEmptyStrings(metadata2.getAnnotations(), metadata1.getAnnotations())); metadata1.setLabels(mergeMapsAndRemoveEmptyStrings(metadata2.getLabels(), metadata1.getLabels())); } } }
/** * Returns the value of the {@code strimzi.io/name} label of the given {@code resource}. */ public static String name(HasMetadata resource) { return resource.getMetadata().getLabels().get(Labels.STRIMZI_NAME_LABEL); }