This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new c36c68d3 [horus] Add node drain eviction pods (#339)
c36c68d3 is described below

commit c36c68d3ee25cae47a6bcd8877473b65500e421e
Author: mfordjody <[email protected]>
AuthorDate: Fri Sep 13 11:57:56 2024 +0800

    [horus] Add node drain eviction pods (#339)
---
 app/horus/core/horuser/action.go | 42 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/app/horus/core/horuser/action.go b/app/horus/core/horuser/action.go
index 1bd252b8..01dced5a 100644
--- a/app/horus/core/horuser/action.go
+++ b/app/horus/core/horuser/action.go
@@ -16,6 +16,7 @@
 package horuser
 
 import (
+       "fmt"
        v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/klog/v2"
 )
@@ -49,7 +50,46 @@ func (h *Horuser) Cordon(nodeName, clusterName string) (err 
error) {
        return nil
 }
 
-func (h *Horuser) Drain() error {
+func (h *Horuser) Drain(nodeName, clusterName string) (err error) {
+       kubeClient := h.kubeClientMap[clusterName]
+       if kubeClient == nil {
+               klog.Errorf("node Drain kubeClient by clusterName empty.")
+               klog.Infof("nodeName:%v,clusterName:%v", nodeName, clusterName)
+               return err
+       }
+
+       ctxFirst, cancelFirst := h.GetK8sContext()
+       defer cancelFirst()
+       listOpts := v1.ListOptions{FieldSelector: fmt.Sprintf("nodeName=%s", 
nodeName)}
+       var podNamespace string
+       pod, err := kubeClient.CoreV1().Pods(podNamespace).List(ctxFirst, 
listOpts)
+       if err != nil {
+               klog.Errorf("node Drain err:%v nodeName:%v clusterName:%v", 
err, nodeName, clusterName)
+               return err
+       }
+       if len(pod.Items) == 0 {
+               klog.Errorf("Cannot find pod on node.")
+               klog.Infof("nodeName:%v,clusterName:%v", nodeName, clusterName)
+       }
+       count := len(pod.Items)
+       for items, pods := range pod.Items {
+               ds := false
+               for _, owner := range pods.OwnerReferences {
+                       if owner.Kind == "Daemonset" {
+                               ds = true
+                               break
+                       }
+               }
+               klog.Errorf("node Drain evict pod result items:%d count:%v 
nodeName:%v clusterName:%v podName:%v podNamespace:%v", items+1, count, 
nodeName, clusterName, pods.Name, pods.Namespace)
+               if ds {
+                       continue
+               }
+               err := h.Evict(pods.Name, pods.Namespace, clusterName)
+               if err != nil {
+                       klog.Errorf("node Drain evict pod err:%v items:%d 
count:%v nodeName:%v clusterName:%v podName:%v podNamespace:%v", err, items+1, 
count, nodeName, clusterName, pods.Name, pods.Namespace)
+                       return err
+               }
+       }
        return nil
 }
 

Reply via email to