diff --git a/controllers/awsmachine_controller.go b/controllers/awsmachine_controller.go index 4021a38c49..c544854697 100644 --- a/controllers/awsmachine_controller.go +++ b/controllers/awsmachine_controller.go @@ -26,6 +26,7 @@ import ( ignTypes "github.com/flatcar/ignition/config/v2_3/types" "github.com/go-logr/logr" "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -589,7 +590,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope * } if instance != nil { - r.ensureStorageTags(ec2svc, instance, machineScope.AWSMachine) + r.ensureStorageTags(ec2svc, instance, machineScope.AWSMachine, machineScope.AdditionalTags()) } if err := r.reconcileLBAttachment(machineScope, elbScope, instance); err != nil { @@ -802,7 +803,7 @@ func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(machineScope *s return nil } - // If bootstrap data has not been populated yet, we cannot determine it's format, so there is probably nothing to do. + // If bootstrap data has not been populated yet, we cannot determine its format, so there is probably nothing to do. if machineScope.Machine.Spec.Bootstrap.DataSecretName == nil { return nil } @@ -1111,14 +1112,15 @@ func (r *AWSMachineReconciler) indexAWSMachineByInstanceID(o client.Object) []st return nil } -func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine) { - annotations, err := r.machineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation) +func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine, additionalTags map[string]string) { + prevAnnotations, err := r.machineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation) if err != nil { r.Log.Error(err, "Failed to fetch the annotations for volume tags") } + annotations := make(map[string]interface{}, len(instance.VolumeIDs)) for _, volumeID := range instance.VolumeIDs { if subAnnotation, ok := annotations[volumeID].(map[string]interface{}); ok { - newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), subAnnotation, machine.Spec.AdditionalTags) + newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), subAnnotation, additionalTags) if err != nil { r.Log.Error(err, "Failed to fetch the changed volume tags in EC2 instance") } @@ -1130,7 +1132,9 @@ func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, i } annotations[volumeID] = newAnnotation } + } + if !cmp.Equal(prevAnnotations, annotations, cmpopts.EquateEmpty()) { // We also need to update the annotation if anything changed. err = r.updateMachineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation, annotations) if err != nil {