Skip to content

Commit

Permalink
Fix undo function of cluster annotation setter wiping annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnn committed Dec 20, 2024
1 parent 7bf8cd5 commit 4bb6c7c
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions pkg/admission/validatingwebhook/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4bb6c7c

Please sign in to comment.