From 4bb6c7ca0278875d76e5f5a2a53c06da9dc8ad4b Mon Sep 17 00:00:00 2001 From: "Nelo-T. Wallus" Date: Fri, 20 Dec 2024 13:15:42 +0100 Subject: [PATCH] Fix undo function of cluster annotation setter wiping annotations --- pkg/admission/validatingwebhook/plugin.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/pkg/admission/validatingwebhook/plugin.go b/pkg/admission/validatingwebhook/plugin.go index 5c19d9f3fe8..ef0c8a996c8 100644 --- a/pkg/admission/validatingwebhook/plugin.go +++ b/pkg/admission/validatingwebhook/plugin.go @@ -203,28 +203,25 @@ func (p *Plugin) SetKcpInformers(local, global kcpinformers.SharedInformerFactor // SetClusterAnnotation sets the cluster annotation on the given object to the given clusterName, // returning an undo function that can be used to revert the change. -func SetClusterAnnotation(obj metav1.Object, clusterName logicalcluster.Name) (undoFn func()) { +func SetClusterAnnotation(obj metav1.Object, clusterName logicalcluster.Name) func() { + undoFn := func() { + anns := obj.GetAnnotations() + delete(anns, logicalcluster.AnnotationKey) + obj.SetAnnotations(anns) + } + anns := obj.GetAnnotations() if anns == nil { obj.SetAnnotations(map[string]string{logicalcluster.AnnotationKey: clusterName.String()}) - return func() { obj.SetAnnotations(nil) } + return undoFn } old, ok := anns[logicalcluster.AnnotationKey] - if old == clusterName.String() { + if ok && old == clusterName.String() { return nil } anns[logicalcluster.AnnotationKey] = clusterName.String() obj.SetAnnotations(anns) - if ok { - return func() { - anns[logicalcluster.AnnotationKey] = old - obj.SetAnnotations(anns) - } - } - return func() { - delete(anns, logicalcluster.AnnotationKey) - obj.SetAnnotations(anns) - } + return undoFn }