Skip to content

Commit

Permalink
Merge pull request kubernetes#122998 from MikeSpreitzer/add-deletion-…
Browse files Browse the repository at this point in the history
…handling

Add DeletionHandlingObjectToName
  • Loading branch information
k8s-ci-robot committed Mar 5, 2024
2 parents b0ee334 + d60a25b commit 6efef79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions staging/src/k8s.io/client-go/tools/cache/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ func DeletionHandlingMetaNamespaceKeyFunc(obj interface{}) (string, error) {
return MetaNamespaceKeyFunc(obj)
}

// DeletionHandlingObjectToName checks for
// DeletedFinalStateUnknown objects before calling
// ObjectToName.
func DeletionHandlingObjectToName(obj interface{}) (ObjectName, error) {
if d, ok := obj.(DeletedFinalStateUnknown); ok {
return ParseObjectName(d.Key)
}
return ObjectToName(obj)
}

// NewInformer returns a Store and a controller for populating the store
// while also providing event notifications. You should only used the returned
// Store for Get/List operations; Add/Modify/Deletes will cause the event
Expand Down
28 changes: 28 additions & 0 deletions staging/src/k8s.io/client-go/tools/cache/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,31 @@ func TestTransformingInformer(t *testing.T) {

close(stopCh)
}

func TestDeletionHandlingObjectToName(t *testing.T) {
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "testname",
Namespace: "testnamespace",
},
}
stringKey, err := MetaNamespaceKeyFunc(cm)
if err != nil {
t.Error(err)
}
deleted := DeletedFinalStateUnknown{
Key: stringKey,
Obj: cm,
}
expected, err := ObjectToName(cm)
if err != nil {
t.Error(err)
}
actual, err := DeletionHandlingObjectToName(deleted)
if err != nil {
t.Error(err)
}
if expected != actual {
t.Errorf("Expected %#v, got %#v", expected, actual)
}
}

0 comments on commit 6efef79

Please sign in to comment.