Search in sources :

Example 1 with StrimziPodSetStatus

use of io.strimzi.api.kafka.model.status.StrimziPodSetStatus in project strimzi by strimzi.

the class StrimziPodSetController method reconcile.

/**
 * The main reconciliation logic which handles the reconciliations.
 *
 * @param reconciliation    Reconciliation identifier used for logging
 */
private void reconcile(Reconciliation reconciliation) {
    String name = reconciliation.name();
    String namespace = reconciliation.namespace();
    StrimziPodSet podSet = strimziPodSetLister.namespace(namespace).get(name);
    if (podSet == null) {
        LOGGER.debugCr(reconciliation, "StrimziPodSet is null => nothing to do");
    } else if (!matchesCrSelector(podSet)) {
        LOGGER.debugCr(reconciliation, "StrimziPodSet doesn't match the selector => nothing to do");
    } else if (isDeleting(podSet)) {
        // When the PodSet is deleted, the pod deletion is done by Kubernetes Garbage Collection. When the PodSet
        // deletion is non-cascading, Kubernetes will remove the owner references. In order to avoid setting the
        // owner reference again, we need to check if the PodSet is being deleted and if it is, we leave it to
        // Kubernetes.
        LOGGER.infoCr(reconciliation, "StrimziPodSet is deleting => nothing to do");
    } else {
        LOGGER.infoCr(reconciliation, "StrimziPodSet will be reconciled");
        StrimziPodSetStatus status = new StrimziPodSetStatus();
        status.setObservedGeneration(podSet.getMetadata().getGeneration());
        try {
            // This has to:
            // 1) Create missing pods
            // 2) Modify changed pods if needed (patch owner reference)
            // 3) Delete scaled down pods
            // Will be used later to find out if any pod needs to be deleted
            Set<String> desiredPods = new HashSet<>(podSet.getSpec().getPods().size());
            PodCounter podCounter = new PodCounter();
            podCounter.pods = podSet.getSpec().getPods().size();
            for (Map<String, Object> desiredPod : podSet.getSpec().getPods()) {
                Pod pod = PodSetUtils.mapToPod(desiredPod);
                desiredPods.add(pod.getMetadata().getName());
                maybeCreateOrPatchPod(reconciliation, pod, ModelUtils.createOwnerReference(podSet), podCounter);
            }
            // Check if any pods needs to be deleted
            removeDeletedPods(reconciliation, podSet.getSpec().getSelector(), desiredPods, podCounter);
            status.setPods(podCounter.pods);
            status.setReadyPods(podCounter.readyPods);
            status.setCurrentPods(podCounter.currentPods);
        } catch (Exception e) {
            LOGGER.errorCr(reconciliation, "StrimziPodSet {} in namespace {} reconciliation failed", reconciliation.name(), reconciliation.namespace(), e);
            status.addCondition(StatusUtils.buildConditionFromException("Error", "true", e));
        } finally {
            maybeUpdateStatus(reconciliation, podSet, status);
            LOGGER.infoCr(reconciliation, "reconciled");
        }
    }
}
Also used : StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) Pod(io.fabric8.kubernetes.api.model.Pod) StrimziPodSetStatus(io.strimzi.api.kafka.model.status.StrimziPodSetStatus) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) HashSet(java.util.HashSet)

Example 2 with StrimziPodSetStatus

use of io.strimzi.api.kafka.model.status.StrimziPodSetStatus in project strimzi by strimzi.

the class StrimziPodSetUtils method waitForAllStrimziPodSetAndPodsReady.

/**
 * Wait until the SPS is ready and all of its Pods are also ready with custom timeout.
 *
 * @param namespaceName Namespace name
 * @param spsName The name of the StrimziPodSet
 * @param expectPods The number of pods expected.
 */
public static void waitForAllStrimziPodSetAndPodsReady(String namespaceName, String spsName, int expectPods, long timeout) {
    String resourceName = spsName.contains("-kafka") ? spsName.replace("-kafka", "") : spsName.replace("-zookeeper", "");
    LabelSelector labelSelector = KafkaResource.getLabelSelector(resourceName, spsName);
    LOGGER.info("Waiting for StrimziPodSet {} to be ready", spsName);
    TestUtils.waitFor("StrimziPodSet " + spsName + " to be ready", Constants.POLL_INTERVAL_FOR_RESOURCE_READINESS, timeout, () -> {
        StrimziPodSetStatus podSetStatus = StrimziPodSetResource.strimziPodSetClient().inNamespace(namespaceName).withName(spsName).get().getStatus();
        return podSetStatus.getPods() == podSetStatus.getReadyPods();
    }, () -> ResourceManager.logCurrentResourceStatus(KafkaResource.kafkaClient().inNamespace(namespaceName).withName(resourceName).get()));
    LOGGER.info("Waiting for {} Pod(s) of StrimziPodSet {} to be ready", expectPods, spsName);
    PodUtils.waitForPodsReady(namespaceName, labelSelector, expectPods, true, () -> ResourceManager.logCurrentResourceStatus(KafkaResource.kafkaClient().inNamespace(namespaceName).withName(resourceName).get()));
    LOGGER.info("StrimziPodSet {} is ready", spsName);
}
Also used : StrimziPodSetStatus(io.strimzi.api.kafka.model.status.StrimziPodSetStatus) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector)

Example 3 with StrimziPodSetStatus

use of io.strimzi.api.kafka.model.status.StrimziPodSetStatus in project strimzi-kafka-operator by strimzi.

the class StrimziPodSetController method reconcile.

/**
 * The main reconciliation logic which handles the reconciliations.
 *
 * @param reconciliation    Reconciliation identifier used for logging
 */
private void reconcile(Reconciliation reconciliation) {
    String name = reconciliation.name();
    String namespace = reconciliation.namespace();
    StrimziPodSet podSet = strimziPodSetLister.namespace(namespace).get(name);
    if (podSet == null) {
        LOGGER.debugCr(reconciliation, "StrimziPodSet is null => nothing to do");
    } else if (!matchesCrSelector(podSet)) {
        LOGGER.debugCr(reconciliation, "StrimziPodSet doesn't match the selector => nothing to do");
    } else if (isDeleting(podSet)) {
        // When the PodSet is deleted, the pod deletion is done by Kubernetes Garbage Collection. When the PodSet
        // deletion is non-cascading, Kubernetes will remove the owner references. In order to avoid setting the
        // owner reference again, we need to check if the PodSet is being deleted and if it is, we leave it to
        // Kubernetes.
        LOGGER.infoCr(reconciliation, "StrimziPodSet is deleting => nothing to do");
    } else {
        LOGGER.infoCr(reconciliation, "StrimziPodSet will be reconciled");
        StrimziPodSetStatus status = new StrimziPodSetStatus();
        status.setObservedGeneration(podSet.getMetadata().getGeneration());
        try {
            // This has to:
            // 1) Create missing pods
            // 2) Modify changed pods if needed (patch owner reference)
            // 3) Delete scaled down pods
            // Will be used later to find out if any pod needs to be deleted
            Set<String> desiredPods = new HashSet<>(podSet.getSpec().getPods().size());
            PodCounter podCounter = new PodCounter();
            podCounter.pods = podSet.getSpec().getPods().size();
            for (Map<String, Object> desiredPod : podSet.getSpec().getPods()) {
                Pod pod = PodSetUtils.mapToPod(desiredPod);
                desiredPods.add(pod.getMetadata().getName());
                maybeCreateOrPatchPod(reconciliation, pod, ModelUtils.createOwnerReference(podSet), podCounter);
            }
            // Check if any pods needs to be deleted
            removeDeletedPods(reconciliation, podSet.getSpec().getSelector(), desiredPods, podCounter);
            status.setPods(podCounter.pods);
            status.setReadyPods(podCounter.readyPods);
            status.setCurrentPods(podCounter.currentPods);
        } catch (Exception e) {
            LOGGER.errorCr(reconciliation, "StrimziPodSet {} in namespace {} reconciliation failed", reconciliation.name(), reconciliation.namespace(), e);
            status.addCondition(StatusUtils.buildConditionFromException("Error", "true", e));
        } finally {
            maybeUpdateStatus(reconciliation, podSet, status);
            LOGGER.infoCr(reconciliation, "reconciled");
        }
    }
}
Also used : StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) Pod(io.fabric8.kubernetes.api.model.Pod) StrimziPodSetStatus(io.strimzi.api.kafka.model.status.StrimziPodSetStatus) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) HashSet(java.util.HashSet)

Example 4 with StrimziPodSetStatus

use of io.strimzi.api.kafka.model.status.StrimziPodSetStatus in project strimzi-kafka-operator by strimzi.

the class StrimziPodSetUtils method waitForAllStrimziPodSetAndPodsReady.

/**
 * Wait until the SPS is ready and all of its Pods are also ready with custom timeout.
 *
 * @param namespaceName Namespace name
 * @param spsName The name of the StrimziPodSet
 * @param expectPods The number of pods expected.
 */
public static void waitForAllStrimziPodSetAndPodsReady(String namespaceName, String spsName, int expectPods, long timeout) {
    String resourceName = spsName.contains("-kafka") ? spsName.replace("-kafka", "") : spsName.replace("-zookeeper", "");
    LabelSelector labelSelector = KafkaResource.getLabelSelector(resourceName, spsName);
    LOGGER.info("Waiting for StrimziPodSet {} to be ready", spsName);
    TestUtils.waitFor("StrimziPodSet " + spsName + " to be ready", Constants.POLL_INTERVAL_FOR_RESOURCE_READINESS, timeout, () -> {
        StrimziPodSetStatus podSetStatus = StrimziPodSetResource.strimziPodSetClient().inNamespace(namespaceName).withName(spsName).get().getStatus();
        return podSetStatus.getPods() == podSetStatus.getReadyPods();
    }, () -> ResourceManager.logCurrentResourceStatus(KafkaResource.kafkaClient().inNamespace(namespaceName).withName(resourceName).get()));
    LOGGER.info("Waiting for {} Pod(s) of StrimziPodSet {} to be ready", expectPods, spsName);
    PodUtils.waitForPodsReady(namespaceName, labelSelector, expectPods, true, () -> ResourceManager.logCurrentResourceStatus(KafkaResource.kafkaClient().inNamespace(namespaceName).withName(resourceName).get()));
    LOGGER.info("StrimziPodSet {} is ready", spsName);
}
Also used : StrimziPodSetStatus(io.strimzi.api.kafka.model.status.StrimziPodSetStatus) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector)

Aggregations

StrimziPodSetStatus (io.strimzi.api.kafka.model.status.StrimziPodSetStatus)4 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 StrimziPodSet (io.strimzi.api.kafka.model.StrimziPodSet)2 HashSet (java.util.HashSet)2