Fix handling of pods having DeletionTimestamp set (#7119) (#7131)

Signed-off-by: Bartosz Borkowski <bartebor@wp.pl>
Co-authored-by: Bartosz Borkowski <bartosz.borkowski@grupawp.pl>
This commit is contained in:
Bartosz Borkowski 2025-03-24 15:31:24 +01:00 committed by GitHub
parent 77516a6bc2
commit 7c76d534d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 76 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package kubernetes
import ( import (
"fmt" "fmt"
"testing" "testing"
"time"
"github.com/coredns/coredns/plugin/kubernetes/object" "github.com/coredns/coredns/plugin/kubernetes/object"
@ -118,3 +119,75 @@ func testProcessor(t *testing.T, processor cache.ProcessFunc, idx cache.Indexer)
t.Fatal("tombstone deleted object found in index") t.Fatal("tombstone deleted object found in index")
} }
} }
func TestDefaultProcessorWithPod(t *testing.T) {
pbuild := object.DefaultProcessor(object.ToPod, nil)
reh := cache.ResourceEventHandlerFuncs{}
idx := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{})
processor := pbuild(idx, reh)
testProcessorWithPod(t, processor, idx)
}
func testProcessorWithPod(t *testing.T, processor cache.ProcessFunc, idx cache.Indexer) {
obj := &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "test1"},
Spec: api.PodSpec{
Containers: []api.Container{
api.Container{},
},
},
}
obj2 := &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "test1", DeletionTimestamp: &metav1.Time{Time: time.Now()}},
Spec: api.PodSpec{
Containers: []api.Container{
api.Container{},
},
},
}
// Add the pod
err := processor(cache.Deltas{
{
Type: cache.Added,
Object: obj.DeepCopy(),
},
}, false)
if err != nil {
t.Fatalf("add pod failed: %v", err)
}
got, exists, err := idx.Get(obj)
if err != nil {
t.Fatalf("get added pod failed: %v", err)
}
if !exists {
t.Fatal("added pod not found in index")
}
_, ok := got.(*object.Pod)
if !ok {
t.Fatal("pod in index was incorrect type")
}
// Update and delete pod having DeletionTimestamp set in one batch
err = processor(cache.Deltas{
{
Type: cache.Updated,
Object: obj2.DeepCopy(),
},
{
Type: cache.Deleted,
Object: obj2.DeepCopy(),
},
}, false)
if err != nil {
t.Fatalf("update or delete failed: %v", err)
}
_, exists, err = idx.Get(obj)
if err != nil {
t.Fatalf("get pod failed: %v", err)
}
if exists {
t.Fatal("deleted pod still exists in the index")
}
}

View file

@ -40,6 +40,9 @@ func DefaultProcessor(convert ToFunc, recordLatency *EndpointLatencyRecorder) Pr
case cache.Sync, cache.Added, cache.Updated: case cache.Sync, cache.Added, cache.Updated:
obj, err := convert(d.Object.(meta.Object)) obj, err := convert(d.Object.(meta.Object))
if err != nil { if err != nil {
if err == errPodTerminating {
continue
}
return err return err
} }
if old, exists, err := clientState.Get(obj); err == nil && exists { if old, exists, err := clientState.Get(obj); err == nil && exists {