diff --git a/v2/internal/reconcilers/arm/azure_generic_arm_reconciler_instance.go b/v2/internal/reconcilers/arm/azure_generic_arm_reconciler_instance.go index 26d9915ca78..d74dc4bb665 100644 --- a/v2/internal/reconcilers/arm/azure_generic_arm_reconciler_instance.go +++ b/v2/internal/reconcilers/arm/azure_generic_arm_reconciler_instance.go @@ -26,6 +26,7 @@ import ( "github.com/Azure/azure-service-operator/v2/internal/reconcilers" "github.com/Azure/azure-service-operator/v2/internal/reflecthelpers" "github.com/Azure/azure-service-operator/v2/internal/resolver" + "github.com/Azure/azure-service-operator/v2/pkg/common/labels" "github.com/Azure/azure-service-operator/v2/pkg/genruntime" "github.com/Azure/azure-service-operator/v2/pkg/genruntime/conditions" "github.com/Azure/azure-service-operator/v2/pkg/genruntime/core" @@ -162,6 +163,8 @@ func (r *azureDeploymentReconcilerInstance) AddInitialResourceState(ctx context. return err } genruntime.SetResourceID(r.Obj, armResource.GetID()) + labels.SetOwnerNameLabel(r.Obj) + labels.SetOwnerGroupKindLabel(r.Obj) return nil } diff --git a/v2/pkg/common/labels/labels.go b/v2/pkg/common/labels/labels.go new file mode 100644 index 00000000000..5f6b4b883a6 --- /dev/null +++ b/v2/pkg/common/labels/labels.go @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package labels + +import ( + "github.com/Azure/azure-service-operator/v2/pkg/genruntime" +) + +const ( + OwnerNameLabel = "serviceoperator.azure.com/owner-name" + OwnerGroupKindLabel = "serviceoperator.azure.com/owner-group-kind" +) + +func SetOwnerNameLabel(obj genruntime.ARMMetaObject) { + if obj.Owner() != nil && obj.Owner().Name != "" { + genruntime.AddLabel(obj, OwnerNameLabel, obj.Owner().Name) + } +} + +func SetOwnerGroupKindLabel(obj genruntime.ARMMetaObject) { + if obj.Owner() != nil && obj.Owner().IsKubernetesReference() { + genruntime.AddLabel(obj, OwnerGroupKindLabel, obj.Owner().GroupKind().String()) + } +} diff --git a/v2/pkg/genruntime/base_types.go b/v2/pkg/genruntime/base_types.go index eadc7a2a35e..206fbb573d1 100644 --- a/v2/pkg/genruntime/base_types.go +++ b/v2/pkg/genruntime/base_types.go @@ -84,21 +84,40 @@ type ARMOwnedMetaObject interface { // of empty string will result in the removal of that annotation. func AddAnnotation(obj MetaObject, k string, v string) { annotations := obj.GetAnnotations() - if annotations == nil { - annotations = map[string]string{} + annotations = AddToMap(annotations, k, v) + obj.SetAnnotations(annotations) +} + +// RemoveAnnotation removes the specified annotation from the object +func RemoveAnnotation(obj MetaObject, k string) { + AddAnnotation(obj, k, "") +} + +// AddLabel adds the specified label to the object. +// Empty string labels are not allowed. Attempting to add a label with a value +// of empty string will result in the removal of that label. +func AddLabel(obj MetaObject, k string, v string) { + labels := obj.GetLabels() + labels = AddToMap(labels, k, v) + obj.SetLabels(labels) +} + +func AddToMap(m map[string]string, k string, v string) map[string]string { + if m == nil { + m = map[string]string{} } // I think this is the behavior we want... if v == "" { - delete(annotations, k) + delete(m, k) } else { - annotations[k] = v + m[k] = v } - obj.SetAnnotations(annotations) + return m } -// RemoveAnnotation removes the specified annotation from the object -func RemoveAnnotation(obj MetaObject, k string) { - AddAnnotation(obj, k, "") +// RemoveLabel removes the specified label from the object +func RemoveLabel(obj MetaObject, k string) { + AddLabel(obj, k, "") } // ARMResourceSpec is an ARM resource specification. This interface contains