From 63de5cbdf3f4b90c4d07b009da1610946d2b5816 Mon Sep 17 00:00:00 2001 From: XsWack Date: Wed, 15 May 2019 09:24:17 +0800 Subject: [PATCH] fix repeat delete service and pod (#998) --- pkg/control/pod_control.go | 12 ++++++++++++ pkg/control/service_control.go | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/pkg/control/pod_control.go b/pkg/control/pod_control.go index 995f0794ba..0d482ee0ad 100644 --- a/pkg/control/pod_control.go +++ b/pkg/control/pod_control.go @@ -24,6 +24,7 @@ import ( "github.com/golang/glog" "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -154,6 +155,17 @@ func (r RealPodControl) DeletePod(namespace string, podID string, object runtime if err != nil { return fmt.Errorf("object does not have ObjectMeta, %v", err) } + pod, err := r.KubeClient.CoreV1().Pods(namespace).Get(podID, metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + if pod.DeletionTimestamp != nil { + glog.V(3).Infof("pod %s/%s is terminating, skip deleting", pod.Namespace, pod.Name) + return nil + } glog.V(2).Infof("Controller %v deleting pod %v/%v", accessor.GetName(), namespace, podID) if err := r.KubeClient.CoreV1().Pods(namespace).Delete(podID, nil); err != nil { r.Recorder.Eventf(object, v1.EventTypeWarning, FailedDeletePodReason, "Error deleting: %v", err) diff --git a/pkg/control/service_control.go b/pkg/control/service_control.go index 1162565d00..981093634b 100644 --- a/pkg/control/service_control.go +++ b/pkg/control/service_control.go @@ -20,6 +20,7 @@ import ( log "github.com/sirupsen/logrus" "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -123,6 +124,17 @@ func (r RealServiceControl) DeleteService(namespace, serviceID string, object ru if err != nil { return fmt.Errorf("object does not have ObjectMeta, %v", err) } + service, err := r.KubeClient.CoreV1().Services(namespace).Get(serviceID, metav1.GetOptions{}) + if err != nil { + if errors.IsNotFound(err) { + return nil + } + return err + } + if service.DeletionTimestamp != nil { + log.Infof("service %s/%s is terminating, skip deleting", service.Namespace, service.Name) + return nil + } log.Infof("Controller %v deleting service %v/%v", accessor.GetName(), namespace, serviceID) if err := r.KubeClient.CoreV1().Services(namespace).Delete(serviceID, nil); err != nil { r.Recorder.Eventf(object, v1.EventTypeWarning, FailedDeleteServiceReason, "Error deleting: %v", err)