Skip to content

Commit

Permalink
check both sts name and UID when handling pod deletion (#4238)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
  • Loading branch information
zhangzujian authored Jul 2, 2024
1 parent 41755bf commit 2b71a56
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ func (c *Controller) markAndCleanLSP() error {
}
ipMap := strset.NewWithSize(len(pods) + len(nodes))
for _, pod := range pods {
if isStsPod, sts := isStatefulSetPod(pod); isStsPod {
if isStatefulSetPodToDel(c.config.KubeClient, pod, sts) {
if isStsPod, stsName, stsUID := isStatefulSetPod(pod); isStsPod {
if isStatefulSetPodToDel(c.config.KubeClient, pod, stsName, stsUID) {
continue
}
} else if !isPodAlive(pod) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (c *Controller) InitIPAM() error {
}

isAlive := isPodAlive(pod)
isStsPod, _ := isStatefulSetPod(pod)
isStsPod, _, _ := isStatefulSetPod(pod)
if !isAlive && !isStsPod {
continue
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ func (c *Controller) enqueueAddPod(obj interface{}) {
}

if !isPodAlive(p) {
isStateful, statefulSetName := isStatefulSetPod(p)
isStateful, statefulSetName, statefulSetUID := isStatefulSetPod(p)
isVMPod, vmName := isVMPod(p)
if isStateful || (isVMPod && c.config.EnableKeepVMIP) {
if isStateful && isStatefulSetPodToDel(c.config.KubeClient, p, statefulSetName) {
if isStateful && isStatefulSetPodToDel(c.config.KubeClient, p, statefulSetName, statefulSetUID) {
klog.V(3).Infof("enqueue delete pod %s", key)
c.deletingPodObjMap.Store(key, p)
c.deletePodQueue.Add(key)
Expand Down Expand Up @@ -322,7 +322,7 @@ func (c *Controller) enqueueUpdatePod(oldObj, newObj interface{}) {
return
}

isStateful, statefulSetName := isStatefulSetPod(newPod)
isStateful, statefulSetName, statefulSetUID := isStatefulSetPod(newPod)
isVMPod, vmName := isVMPod(newPod)
if !isPodStatusPhaseAlive(newPod) && !isStateful && !isVMPod {
klog.V(3).Infof("enqueue delete pod %s", key)
Expand Down Expand Up @@ -353,7 +353,7 @@ func (c *Controller) enqueueUpdatePod(oldObj, newObj interface{}) {
}

// do not delete statefulset pod unless ownerReferences is deleted
if isStateful && isStatefulSetPodToDel(c.config.KubeClient, newPod, statefulSetName) {
if isStateful && isStatefulSetPodToDel(c.config.KubeClient, newPod, statefulSetName, statefulSetUID) {
go func() {
klog.V(3).Infof("enqueue delete pod %s after %v", key, delay)
c.deletingPodObjMap.Store(key, newPod)
Expand Down Expand Up @@ -1009,17 +1009,17 @@ func (c *Controller) handleDeletePod(key string) error {
podKey := fmt.Sprintf("%s/%s", pod.Namespace, podName)

var keepIPCR bool
if ok, sts := isStatefulSetPod(pod); ok {
if ok, stsName, stsUID := isStatefulSetPod(pod); ok {
if pod.DeletionTimestamp != nil {
klog.Infof("handle deletion of sts pod %s", podName)
toDel := isStatefulSetPodToDel(c.config.KubeClient, pod, sts)
toDel := isStatefulSetPodToDel(c.config.KubeClient, pod, stsName, stsUID)
if !toDel {
klog.Infof("try keep ip for sts pod %s", podKey)
keepIPCR = true
}
}
if keepIPCR {
isDelete, err := appendCheckPodToDel(c, pod, sts, util.StatefulSet)
isDelete, err := appendCheckPodToDel(c, pod, stsName, util.StatefulSet)
if err != nil {
klog.Error(err)
return err
Expand Down Expand Up @@ -1322,20 +1322,20 @@ func (c *Controller) syncKubeOvnNet(cachedPod, pod *v1.Pod, podNets []*kubeovnNe
return patchedPod.DeepCopy(), nil
}

func isStatefulSetPod(pod *v1.Pod) (bool, string) {
func isStatefulSetPod(pod *v1.Pod) (bool, string, types.UID) {
for _, owner := range pod.OwnerReferences {
if owner.Kind == util.StatefulSet && strings.HasPrefix(owner.APIVersion, "apps/") {
if strings.HasPrefix(pod.Name, owner.Name) {
return true, owner.Name
return true, owner.Name, owner.UID
}
}
}
return false, ""
return false, "", ""
}

func isStatefulSetPodToDel(c kubernetes.Interface, pod *v1.Pod, statefulSetName string) bool {
func isStatefulSetPodToDel(c kubernetes.Interface, pod *v1.Pod, statefulSetName string, statefulSetUID types.UID) bool {
// only delete statefulset pod lsp when statefulset deleted or down scaled
ss, err := c.AppsV1().StatefulSets(pod.Namespace).Get(context.Background(), statefulSetName, metav1.GetOptions{})
sts, err := c.AppsV1().StatefulSets(pod.Namespace).Get(context.Background(), statefulSetName, metav1.GetOptions{})
if err != nil {
// statefulset is deleted
if k8serrors.IsNotFound(err) {
Expand All @@ -1345,8 +1345,8 @@ func isStatefulSetPodToDel(c kubernetes.Interface, pod *v1.Pod, statefulSetName
return false
}

// statefulset is deleting
if !ss.DeletionTimestamp.IsZero() {
// statefulset is being deleted, or it's a newly created one
if !sts.DeletionTimestamp.IsZero() || sts.UID != statefulSetUID {
return true
}

Expand All @@ -1359,7 +1359,7 @@ func isStatefulSetPodToDel(c kubernetes.Interface, pod *v1.Pod, statefulSetName
return false
}
// down scaled
return index >= int64(*ss.Spec.Replicas)
return index >= int64(*sts.Spec.Replicas)
}

func getNodeTunlIP(node *v1.Node) ([]net.IP, error) {
Expand Down Expand Up @@ -1684,7 +1684,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
key := fmt.Sprintf("%s/%s", pod.Namespace, podName)

var checkVMPod bool
isStsPod, _ := isStatefulSetPod(pod)
isStsPod, _, _ := isStatefulSetPod(pod)
// if pod has static vip
vipName := pod.Annotations[util.VipAnnotation]
if vipName != "" {
Expand Down Expand Up @@ -2061,7 +2061,7 @@ func (c *Controller) getNsAvailableSubnets(pod *v1.Pod, podNet *kubeovnNet) ([]*
}

func getPodType(pod *v1.Pod) string {
if ok, _ := isStatefulSetPod(pod); ok {
if ok, _, _ := isStatefulSetPod(pod); ok {
return util.StatefulSet
}

Expand Down

0 comments on commit 2b71a56

Please sign in to comment.