From 97a2fe5bb37f2de030ac545f6519f1dca799563d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B5=D0=B2?= =?UTF-8?q?=D0=B8=D1=87?= Date: Thu, 4 Jul 2024 09:59:43 +0300 Subject: [PATCH 1/2] fix: correct work of DaemonSetIgnore --- .../client/extended/kubectl/KubectlDrain.java | 194 +++++++++--------- 1 file changed, 100 insertions(+), 94 deletions(-) diff --git a/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java b/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java index 22fec7de0d..99af28e2fc 100644 --- a/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java +++ b/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java @@ -19,109 +19,115 @@ import io.kubernetes.client.openapi.models.V1OwnerReference; import io.kubernetes.client.openapi.models.V1Pod; import io.kubernetes.client.openapi.models.V1PodList; + import java.io.IOException; import java.net.HttpURLConnection; import java.util.List; public class KubectlDrain extends KubectlCordon { - private int gracePeriodSeconds; - private boolean force; - private boolean ignoreDaemonSets; - - KubectlDrain() { - super(true); - gracePeriodSeconds = 30; - } - - public KubectlDrain gracePeriod(int gracePeriodSeconds) { - this.gracePeriodSeconds = gracePeriodSeconds; - return this; - } - - public KubectlDrain force() { - this.force = true; - return this; - } - - public KubectlDrain ignoreDaemonSets() { - this.ignoreDaemonSets = true; - return this; - } - - @Override - public V1Node execute() throws KubectlException { - try { - refreshDiscovery(); - return doDrain(); - } catch (ApiException | IOException ex) { - throw new KubectlException(ex); + private int gracePeriodSeconds; + private boolean force; + private boolean ignoreDaemonSets; + + KubectlDrain() { + super(true); + gracePeriodSeconds = 30; + } + + public KubectlDrain gracePeriod(int gracePeriodSeconds) { + this.gracePeriodSeconds = gracePeriodSeconds; + return this; + } + + public KubectlDrain force() { + this.force = true; + return this; + } + + public KubectlDrain ignoreDaemonSets() { + this.ignoreDaemonSets = true; + return this; } - } - - private V1Node doDrain() throws KubectlException, ApiException, IOException { - CoreV1Api api = new CoreV1Api(apiClient); - V1Node node = performCordon(); - - V1PodList allPods = - api.listPodForAllNamespaces() - .fieldSelector("spec.nodeName=" + node.getMetadata().getName()) - .execute(); - - validatePods(allPods.getItems()); - - for (V1Pod pod : allPods.getItems()) { - // at this point we know, that we have to ignore daemon set pods - if (pod.getMetadata().getOwnerReferences() != null) { - for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { - if (ref.getKind().equals("DaemonSet")) { - continue; - } + + @Override + public V1Node execute() throws KubectlException { + try { + refreshDiscovery(); + return doDrain(); + } catch (ApiException | IOException ex) { + throw new KubectlException(ex); } - } - deletePod(api, pod.getMetadata().getName(), pod.getMetadata().getNamespace()); } - return node; - } - - private void validatePods(List pods) throws KubectlException { - // Throw if there are any unmanaged pods and force is false - for (V1Pod pod : pods) { - if (pod.getMetadata().getOwnerReferences() == null) continue; - - if (!force && pod.getMetadata().getOwnerReferences().size() == 0) { - throw new KubectlException("Pods unmanaged by a controller are present on the node"); - } - // Throw exception if there are daemon set pods and ignore daemon set is false - if (!ignoreDaemonSets) { - for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { - if (ref.getKind().equals("DaemonSet")) { - throw new KubectlException("Pod managed by DaemonSet found"); - } + + private V1Node doDrain() throws KubectlException, ApiException, IOException { + CoreV1Api api = new CoreV1Api(apiClient); + V1Node node = performCordon(); + + V1PodList allPods = + api.listPodForAllNamespaces() + .fieldSelector("spec.nodeName=" + node.getMetadata().getName()) + .execute(); + + validatePods(allPods.getItems()); + + boolean isDaemonSetPod; + for (V1Pod pod : allPods.getItems()) { + isDaemonSetPod = false; + // at this point we know, that we have to ignore daemon set pods + if (pod.getMetadata().getOwnerReferences() != null) { + for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { + if (ref.getKind().equals("DaemonSet")) { + isDaemonSetPod = true; + break; + } + } + } + if (!isDaemonSetPod) { + deletePod(api, pod.getMetadata().getName(), pod.getMetadata().getNamespace()); + } } - } + return node; } - } - - private void deletePod(CoreV1Api api, String name, String namespace) - throws ApiException, IOException, KubectlException { - api.deleteNamespacedPod(name, namespace).gracePeriodSeconds(gracePeriodSeconds).execute(); - waitForPodDelete(api, name, namespace); - } - - private void waitForPodDelete(CoreV1Api api, String name, String namespace) - throws KubectlException { - long start = System.currentTimeMillis(); - do { - try { - api.readNamespacedPod(name, namespace).execute(); - } catch (ApiException ex) { - if (ex.getCode() == HttpURLConnection.HTTP_NOT_FOUND) { - return; + + private void validatePods(List pods) throws KubectlException { + // Throw if there are any unmanaged pods and force is false + for (V1Pod pod : pods) { + if (pod.getMetadata().getOwnerReferences() == null) continue; + + if (!force && pod.getMetadata().getOwnerReferences().size() == 0) { + throw new KubectlException("Pods unmanaged by a controller are present on the node"); + } + // Throw exception if there are daemon set pods and ignore daemon set is false + if (!ignoreDaemonSets) { + for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { + if (ref.getKind().equals("DaemonSet")) { + throw new KubectlException("Pod managed by DaemonSet found"); + } + } + } } - throw new KubectlException(ex); - } - // add 10 seconds to gracePeriod to allow the force deletion of the pod to finish - } while (System.currentTimeMillis() - start < (this.gracePeriodSeconds + 10) * 1000); - throw new KubectlException("Timed out waiting for Pod delete."); - } + } + + private void deletePod(CoreV1Api api, String name, String namespace) + throws ApiException, IOException, KubectlException { + api.deleteNamespacedPod(name, namespace).gracePeriodSeconds(gracePeriodSeconds).execute(); + waitForPodDelete(api, name, namespace); + } + + private void waitForPodDelete(CoreV1Api api, String name, String namespace) + throws KubectlException { + long start = System.currentTimeMillis(); + do { + try { + api.readNamespacedPod(name, namespace).execute(); + } catch (ApiException ex) { + if (ex.getCode() == HttpURLConnection.HTTP_NOT_FOUND) { + return; + } + throw new KubectlException(ex); + } + // add 10 seconds to gracePeriod to allow the force deletion of the pod to finish + } while (System.currentTimeMillis() - start < (this.gracePeriodSeconds + 10) * 1000); + throw new KubectlException("Timed out waiting for Pod delete."); + } } From 376248fa7946cb34be9d290fd5ce5987c79f18b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B5=D0=B2?= =?UTF-8?q?=D0=B8=D1=87?= Date: Mon, 8 Jul 2024 23:03:14 +0300 Subject: [PATCH 2/2] fix: correct work of DaemonSetIgnore --- .../client/extended/kubectl/KubectlDrain.java | 199 +++++++++--------- 1 file changed, 99 insertions(+), 100 deletions(-) diff --git a/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java b/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java index 99af28e2fc..fe84a9f474 100644 --- a/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java +++ b/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDrain.java @@ -19,115 +19,114 @@ import io.kubernetes.client.openapi.models.V1OwnerReference; import io.kubernetes.client.openapi.models.V1Pod; import io.kubernetes.client.openapi.models.V1PodList; - import java.io.IOException; import java.net.HttpURLConnection; import java.util.List; public class KubectlDrain extends KubectlCordon { - private int gracePeriodSeconds; - private boolean force; - private boolean ignoreDaemonSets; - - KubectlDrain() { - super(true); - gracePeriodSeconds = 30; - } - - public KubectlDrain gracePeriod(int gracePeriodSeconds) { - this.gracePeriodSeconds = gracePeriodSeconds; - return this; - } - - public KubectlDrain force() { - this.force = true; - return this; - } - - public KubectlDrain ignoreDaemonSets() { - this.ignoreDaemonSets = true; - return this; + private int gracePeriodSeconds; + private boolean force; + private boolean ignoreDaemonSets; + + KubectlDrain() { + super(true); + gracePeriodSeconds = 30; + } + + public KubectlDrain gracePeriod(int gracePeriodSeconds) { + this.gracePeriodSeconds = gracePeriodSeconds; + return this; + } + + public KubectlDrain force() { + this.force = true; + return this; + } + + public KubectlDrain ignoreDaemonSets() { + this.ignoreDaemonSets = true; + return this; + } + + @Override + public V1Node execute() throws KubectlException { + try { + refreshDiscovery(); + return doDrain(); + } catch (ApiException | IOException ex) { + throw new KubectlException(ex); } - - @Override - public V1Node execute() throws KubectlException { - try { - refreshDiscovery(); - return doDrain(); - } catch (ApiException | IOException ex) { - throw new KubectlException(ex); + } + + private V1Node doDrain() throws KubectlException, ApiException, IOException { + CoreV1Api api = new CoreV1Api(apiClient); + V1Node node = performCordon(); + + V1PodList allPods = + api.listPodForAllNamespaces() + .fieldSelector("spec.nodeName=" + node.getMetadata().getName()) + .execute(); + + validatePods(allPods.getItems()); + + boolean isDaemonSetPod; + for (V1Pod pod : allPods.getItems()) { + isDaemonSetPod = false; + // at this point we know, that we have to ignore daemon set pods + if (pod.getMetadata().getOwnerReferences() != null) { + for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { + if (ref.getKind().equals("DaemonSet")) { + isDaemonSetPod = true; + break; + } } + } + if (!isDaemonSetPod) { + deletePod(api, pod.getMetadata().getName(), pod.getMetadata().getNamespace()); + } } - - private V1Node doDrain() throws KubectlException, ApiException, IOException { - CoreV1Api api = new CoreV1Api(apiClient); - V1Node node = performCordon(); - - V1PodList allPods = - api.listPodForAllNamespaces() - .fieldSelector("spec.nodeName=" + node.getMetadata().getName()) - .execute(); - - validatePods(allPods.getItems()); - - boolean isDaemonSetPod; - for (V1Pod pod : allPods.getItems()) { - isDaemonSetPod = false; - // at this point we know, that we have to ignore daemon set pods - if (pod.getMetadata().getOwnerReferences() != null) { - for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { - if (ref.getKind().equals("DaemonSet")) { - isDaemonSetPod = true; - break; - } - } - } - if (!isDaemonSetPod) { - deletePod(api, pod.getMetadata().getName(), pod.getMetadata().getNamespace()); - } + return node; + } + + private void validatePods(List pods) throws KubectlException { + // Throw if there are any unmanaged pods and force is false + for (V1Pod pod : pods) { + if (pod.getMetadata().getOwnerReferences() == null) continue; + + if (!force && pod.getMetadata().getOwnerReferences().size() == 0) { + throw new KubectlException("Pods unmanaged by a controller are present on the node"); + } + // Throw exception if there are daemon set pods and ignore daemon set is false + if (!ignoreDaemonSets) { + for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { + if (ref.getKind().equals("DaemonSet")) { + throw new KubectlException("Pod managed by DaemonSet found"); + } } - return node; + } } - - private void validatePods(List pods) throws KubectlException { - // Throw if there are any unmanaged pods and force is false - for (V1Pod pod : pods) { - if (pod.getMetadata().getOwnerReferences() == null) continue; - - if (!force && pod.getMetadata().getOwnerReferences().size() == 0) { - throw new KubectlException("Pods unmanaged by a controller are present on the node"); - } - // Throw exception if there are daemon set pods and ignore daemon set is false - if (!ignoreDaemonSets) { - for (V1OwnerReference ref : pod.getMetadata().getOwnerReferences()) { - if (ref.getKind().equals("DaemonSet")) { - throw new KubectlException("Pod managed by DaemonSet found"); - } - } - } + } + + private void deletePod(CoreV1Api api, String name, String namespace) + throws ApiException, IOException, KubectlException { + api.deleteNamespacedPod(name, namespace).gracePeriodSeconds(gracePeriodSeconds).execute(); + waitForPodDelete(api, name, namespace); + } + + private void waitForPodDelete(CoreV1Api api, String name, String namespace) + throws KubectlException { + long start = System.currentTimeMillis(); + do { + try { + api.readNamespacedPod(name, namespace).execute(); + } catch (ApiException ex) { + if (ex.getCode() == HttpURLConnection.HTTP_NOT_FOUND) { + return; } - } - - private void deletePod(CoreV1Api api, String name, String namespace) - throws ApiException, IOException, KubectlException { - api.deleteNamespacedPod(name, namespace).gracePeriodSeconds(gracePeriodSeconds).execute(); - waitForPodDelete(api, name, namespace); - } - - private void waitForPodDelete(CoreV1Api api, String name, String namespace) - throws KubectlException { - long start = System.currentTimeMillis(); - do { - try { - api.readNamespacedPod(name, namespace).execute(); - } catch (ApiException ex) { - if (ex.getCode() == HttpURLConnection.HTTP_NOT_FOUND) { - return; - } - throw new KubectlException(ex); - } - // add 10 seconds to gracePeriod to allow the force deletion of the pod to finish - } while (System.currentTimeMillis() - start < (this.gracePeriodSeconds + 10) * 1000); - throw new KubectlException("Timed out waiting for Pod delete."); - } + throw new KubectlException(ex); + } + // add 10 seconds to gracePeriod to allow the force deletion of the pod to finish + } while (System.currentTimeMillis() - start < (this.gracePeriodSeconds + 10) * 1000); + throw new KubectlException("Timed out waiting for Pod delete."); + } }