[node operator] patching util functions

Signed-off-by: Malte Poll <mp@edgeless.systems>
This commit is contained in:
Malte Poll 2022-06-28 09:55:05 +02:00 committed by Malte Poll
parent 614447495d
commit 3932581f2a
6 changed files with 212 additions and 5 deletions

View file

@ -0,0 +1,18 @@
package patch
import (
"sigs.k8s.io/controller-runtime/pkg/client"
)
// SetLabels creates a patch for a client.Object by merging labels with existing labels.
func SetLabels(original, patched client.Object, labels map[string]string) client.Patch {
mergedLabels := patched.GetLabels()
if mergedLabels == nil {
mergedLabels = make(map[string]string, len(labels))
}
for labelKey, labelValue := range labels {
mergedLabels[labelKey] = labelValue
}
patched.SetLabels(mergedLabels)
return client.StrategicMergeFrom(original, client.MergeFromWithOptimisticLock{})
}