Skip to content

Commit

Permalink
refactor ASO label/annotation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nojnhuh committed Jul 17, 2023
1 parent 87345f7 commit 04570e0
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions azure/services/aso/aso.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/maps"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -150,8 +149,14 @@ func (s *Service) CreateOrUpdateResource(ctx context.Context, spec azure.ASOReso
parameters.SetName(resourceName)
parameters.SetNamespace(resourceNamespace)

labels := make(map[string]string)
annotations := make(map[string]string)
labels := parameters.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
annotations := parameters.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}

if existing == nil {
labels[infrav1.OwnedByClusterLabelKey] = s.clusterName
Expand All @@ -170,12 +175,14 @@ func (s *Service) CreateOrUpdateResource(ctx context.Context, spec azure.ASOReso
annotations[ReconcilePolicyAnnotation] = ReconcilePolicyManage
}

if len(labels) > 0 {
parameters.SetLabels(maps.Merge(parameters.GetLabels(), labels))
if len(labels) == 0 {
labels = nil
}
if len(annotations) > 0 {
parameters.SetAnnotations(maps.Merge(parameters.GetAnnotations(), annotations))
parameters.SetLabels(labels)
if len(annotations) == 0 {
annotations = nil
}
parameters.SetAnnotations(annotations)

diff := cmp.Diff(existing, parameters)
if diff == "" {
Expand Down

0 comments on commit 04570e0

Please sign in to comment.