sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/aso/tags.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package aso
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
    23  	"github.com/pkg/errors"
    24  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    25  	"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
    26  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
    27  	"sigs.k8s.io/cluster-api-provider-azure/util/maps"
    28  )
    29  
    30  // tagsLastAppliedAnnotation is the key for the annotation which tracks the AdditionalTags.
    31  // See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
    32  // for annotation formatting rules.
    33  const tagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags"
    34  
    35  // reconcileTags modifies parameters in place to update its tags and its last-applied annotation.
    36  func reconcileTags[T genruntime.MetaObject](t TagsGetterSetter[T], existing T, resourceExists bool, parameters T) error {
    37  	var existingTags infrav1.Tags
    38  	lastAppliedTags := map[string]interface{}{}
    39  
    40  	if resourceExists {
    41  		lastAppliedTagsJSON := existing.GetAnnotations()[tagsLastAppliedAnnotation]
    42  		if lastAppliedTagsJSON != "" {
    43  			err := json.Unmarshal([]byte(lastAppliedTagsJSON), &lastAppliedTags)
    44  			if err != nil {
    45  				return errors.Wrapf(err, "failed to unmarshal JSON from %s annotation", tagsLastAppliedAnnotation)
    46  			}
    47  		}
    48  
    49  		existingTags = t.GetDesiredTags(existing)
    50  	}
    51  
    52  	existingTagsMap := converters.TagsToMap(existingTags)
    53  	_, createdOrUpdated, deleted, newAnnotation := tags.TagsChanged(lastAppliedTags, t.GetAdditionalTags(), existingTagsMap)
    54  	newTags := maps.Merge(maps.Merge(existingTags, t.GetDesiredTags(parameters)), createdOrUpdated)
    55  	for k := range deleted {
    56  		delete(newTags, k)
    57  	}
    58  	if len(newTags) == 0 {
    59  		newTags = nil
    60  	}
    61  	t.SetTags(parameters, newTags)
    62  
    63  	// We also need to update the annotation even if nothing changed to
    64  	// ensure it's set immediately following resource creation.
    65  	newAnnotationJSON, err := json.Marshal(newAnnotation)
    66  	if err != nil {
    67  		return errors.Wrapf(err, "failed to marshal JSON to %s annotation", tagsLastAppliedAnnotation)
    68  	}
    69  	parameters.SetAnnotations(maps.Merge(parameters.GetAnnotations(), map[string]string{
    70  		tagsLastAppliedAnnotation: string(newAnnotationJSON),
    71  	}))
    72  
    73  	return nil
    74  }