Merge pull request #122998 from MikeSpreitzer/add-deletion-handling

Add DeletionHandlingObjectToName
This commit is contained in:
Kubernetes Prow Robot 2024-03-05 12:41:45 -08:00 committed by GitHub
commit 6efef796f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 0 deletions

View File

@ -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

View File

@ -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)
}
}