Skip terminal Pods with a deletion timestamp from the Daemonset sync (#118716)

* Skip terminal Pods with a deletion timestamp from the Daemonset sync

Change-Id: I64a347a87c02ee2bd48be10e6fff380c8c81f742

* Review comments and fix integration test

Change-Id: I3eb5ec62bce8b4b150726a1e9b2b517c4e993713

* Include deleted terminal pods in history

Change-Id: I8b921157e6be1c809dd59f8035ec259ea4d96301
This commit is contained in:
Aldo Culquicondor
2023-06-27 11:56:33 -04:00
committed by GitHub
parent 6dbb1c6cf0
commit a4519665fe
6 changed files with 188 additions and 84 deletions

View File

@@ -18,6 +18,7 @@ package util
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
@@ -27,6 +28,7 @@ import (
policy "k8s.io/api/policy/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/admission"
@@ -229,17 +231,33 @@ func CleanupTest(t *testing.T, testCtx *TestContext) {
testCtx.CloseFn()
}
func RemovePodFinalizersInNamespace(ctx context.Context, cs clientset.Interface, t *testing.T, ns string) {
t.Helper()
pods, err := cs.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})
if err != nil {
t.Fatalf("Failed obtaining list of pods: %v", err)
}
RemovePodFinalizers(ctx, cs, t, pods.Items...)
}
// RemovePodFinalizers removes pod finalizers for the pods
func RemovePodFinalizers(cs clientset.Interface, t *testing.T, pods []*v1.Pod) {
func RemovePodFinalizers(ctx context.Context, cs clientset.Interface, t *testing.T, pods ...v1.Pod) {
t.Helper()
for _, p := range pods {
pod, err := cs.CoreV1().Pods(p.Namespace).Get(context.TODO(), p.Name, metav1.GetOptions{})
pod, err := cs.CoreV1().Pods(p.Namespace).Get(ctx, p.Name, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
t.Errorf("error while removing pod finalizers for %v: %v", klog.KObj(p), err)
} else if pod != nil {
pod.ObjectMeta.Finalizers = nil
_, err = cs.CoreV1().Pods(pod.Namespace).Update(context.TODO(), pod, metav1.UpdateOptions{})
t.Errorf("error while removing pod finalizers for %v: %v", klog.KObj(&p), err)
} else if pod != nil && len(pod.Finalizers) > 0 {
// Use Patch to remove finalizer, instead of Update, to avoid transient
// conflicts.
patchBytes, _ := json.Marshal(map[string]interface{}{
"metadata": map[string]interface{}{
"$deleteFromPrimitiveList/finalizers": pod.Finalizers,
},
})
_, err = cs.CoreV1().Pods(pod.Namespace).Patch(ctx, pod.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
if err != nil {
t.Errorf("error while updating pod status for %v: %v", klog.KObj(p), err)
t.Errorf("error while updating pod status for %v: %v", klog.KObj(&p), err)
}
}
}