constellation/operators/constellation-node-operator/internal/patch/annotations.go
Thomas Tendyck bd63aa3c6b add license headers
sed -i '1i/*\nCopyright (c) Edgeless Systems GmbH\n\nSPDX-License-Identifier: AGPL-3.0-only\n*/\n' `grep -rL --include='*.go' 'DO NOT EDIT'`
gofumpt -w .
2022-09-05 09:17:25 +02:00

33 lines
1.2 KiB
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package patch
import "sigs.k8s.io/controller-runtime/pkg/client"
// SetAnnotations creates a patch for a client.Object by merging annotations with existing annotations.
func SetAnnotations(original, patched client.Object, annotations map[string]string) client.Patch {
mergedAnnotations := patched.GetAnnotations()
if mergedAnnotations == nil {
mergedAnnotations = make(map[string]string, len(annotations))
}
for annotationKey, annotationValue := range annotations {
mergedAnnotations[annotationKey] = annotationValue
}
patched.SetAnnotations(mergedAnnotations)
return client.StrategicMergeFrom(original, client.MergeFromWithOptimisticLock{})
}
// UnsetAnnotations creates a patch for a client.Object by deleting annotations from the object.
func UnsetAnnotations(original, patched client.Object, annotationKeys []string) client.Patch {
mergedAnnotations := patched.GetAnnotations()
for _, annotationKey := range annotationKeys {
delete(mergedAnnotations, annotationKey)
}
patched.SetAnnotations(mergedAnnotations)
return client.StrategicMergeFrom(original, client.MergeFromWithOptimisticLock{})
}