3636 logger = ctrl .Log .WithName ("k8s" )
3737)
3838
39- type K8sClient interface {
40- // Sync ensures that the object is up to date in the cluster.
41- // Object is created if it does not exist and updated if it exists but is different.
42- // Returns nil if object is in sync.
43- Sync (ctx context.Context , blueprint client.Object , owner metav1.Object , diffOpts ... cmp.Option ) error
44- // Create creates object.
45- // Returns nil if object is created otherwise returns error.
46- Create (ctx context.Context , blueprint client.Object , owner metav1.Object , opts ... client.CreateOption ) error
47- // GetIgnoreNotFound gets object.
48- // Returns true if object exists otherwise returns false.
49- // Returns nil if object is retrieved or not found otherwise returns error.
50- GetIgnoreNotFound (ctx context.Context , key client.ObjectKey , objectMeta client.Object , opts ... client.GetOption ) (bool , error )
51- // DeleteByKeyIgnoreNotFound deletes object by key.
52- // Returns nil if object is deleted or not found otherwise returns error.
53- DeleteByKeyIgnoreNotFound (ctx context.Context , key client.ObjectKey , objectMeta client.Object , opts ... client.DeleteOption ) error
54- // List returns list of runtime objects.
55- // Returns nil if list is retrieved otherwise returns error.
56- List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) ([]runtime.Object , error )
57- }
58-
5939func NewK8sClient (cli client.Client , scheme * runtime.Scheme ) * K8sClientWrapper {
6040 return & K8sClientWrapper {cli : cli , scheme : scheme }
6141}
@@ -69,7 +49,7 @@ func (k K8sClientWrapper) Sync(
6949 ctx context.Context ,
7050 obj client.Object ,
7151 owner metav1.Object ,
72- diffOpts ... cmp. Option ,
52+ opts ... SyncOption ,
7353) error {
7454 defer func () {
7555 // ensure GVK is set (for original object) when function returns
@@ -94,9 +74,9 @@ func (k K8sClientWrapper) Sync(
9474 Namespace : obj .GetNamespace (),
9575 }
9676 if exists , err := k .doGetIgnoreNotFound (ctx , key , actual .(client.Object )); exists {
97- return k .doSync (ctx , actual .(client.Object ), obj , diffOpts ... )
77+ return k .doSync (ctx , actual .(client.Object ), obj , opts ... )
9878 } else if err == nil {
99- return k .doSync (ctx , nil , obj , diffOpts ... )
79+ return k .doSync (ctx , nil , obj , opts ... )
10080 } else {
10181 return err
10282 }
@@ -256,16 +236,19 @@ func (k K8sClientWrapper) doSync(
256236 ctx context.Context ,
257237 actual client.Object ,
258238 obj client.Object ,
259- diffOpts ... cmp. Option ,
239+ opts ... SyncOption ,
260240) error {
261241 if actual == nil {
262242 return k .doCreate (ctx , obj , false )
263243 }
264244
265- diff := cmp .Diff (actual , obj , diffOpts ... )
245+ syncOptions := SyncOptions {}
246+ syncOptions .ApplyOptions (opts )
247+
248+ diff := cmp .Diff (actual , obj , syncOptions .DiffOpts ... )
266249 if len (diff ) > 0 {
267- // don't print difference if there are no diffOpts mainly to avoid huge output
268- if len (diffOpts ) != 0 {
250+ // don't print difference if there are no DiffOpts mainly to avoid huge output
251+ if ! syncOptions . SuppressDiff && len (syncOptions . DiffOpts ) != 0 {
269252 fmt .Printf ("Difference:\n %s" , diff )
270253 }
271254
@@ -279,6 +262,30 @@ func (k K8sClientWrapper) doSync(
279262 // to be able to update, we need to set the resource version of the object that we know of
280263 obj .(metav1.Object ).SetResourceVersion (actual .GetResourceVersion ())
281264
265+ if syncOptions .MergeLabels {
266+ if obj .GetLabels () == nil {
267+ obj .SetLabels (map [string ]string {})
268+ }
269+
270+ for k , v := range actual .GetLabels () {
271+ if _ , exists := obj .GetLabels ()[k ]; ! exists {
272+ obj .GetLabels ()[k ] = v
273+ }
274+ }
275+ }
276+
277+ if syncOptions .MergeAnnotations {
278+ if obj .GetAnnotations () == nil {
279+ obj .SetAnnotations (map [string ]string {})
280+ }
281+
282+ for k , v := range actual .GetAnnotations () {
283+ if _ , exists := obj .GetAnnotations ()[k ]; ! exists {
284+ obj .GetAnnotations ()[k ] = v
285+ }
286+ }
287+ }
288+
282289 err := k .cli .Update (ctx , obj )
283290 if err == nil {
284291 logger .Info ("Object updated" , "namespace" , actual .GetNamespace (), "kind" , GetObjectType (actual ), "name" , actual .GetName ())
0 commit comments