Skip to content

Commit 9b10597

Browse files
authored
Merge pull request #4535 from Ankitasw/cleanup-volume-tags
[release-2.2] cherry-pick: Fix cluster-api-provider-last-applied-tags-on-volumes to recalculate the value on each reconcile
2 parents 6876872 + 409d73c commit 9b10597

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

controllers/awsmachine_controller.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
ignTypes "github.com/flatcar/ignition/config/v2_3/types"
2727
"github.com/go-logr/logr"
2828
"github.com/google/go-cmp/cmp"
29+
"github.com/google/go-cmp/cmp/cmpopts"
2930
"github.com/pkg/errors"
3031
corev1 "k8s.io/api/core/v1"
3132
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -589,7 +590,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
589590
}
590591

591592
if instance != nil {
592-
r.ensureStorageTags(ec2svc, instance, machineScope.AWSMachine)
593+
r.ensureStorageTags(ec2svc, instance, machineScope.AWSMachine, machineScope.AdditionalTags())
593594
}
594595

595596
if err := r.reconcileLBAttachment(machineScope, elbScope, instance); err != nil {
@@ -802,7 +803,7 @@ func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(machineScope *s
802803
return nil
803804
}
804805

805-
// If bootstrap data has not been populated yet, we cannot determine it's format, so there is probably nothing to do.
806+
// If bootstrap data has not been populated yet, we cannot determine its format, so there is probably nothing to do.
806807
if machineScope.Machine.Spec.Bootstrap.DataSecretName == nil {
807808
return nil
808809
}
@@ -1111,14 +1112,15 @@ func (r *AWSMachineReconciler) indexAWSMachineByInstanceID(o client.Object) []st
11111112
return nil
11121113
}
11131114

1114-
func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine) {
1115-
annotations, err := r.machineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation)
1115+
func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine, additionalTags map[string]string) {
1116+
prevAnnotations, err := r.machineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation)
11161117
if err != nil {
11171118
r.Log.Error(err, "Failed to fetch the annotations for volume tags")
11181119
}
1120+
annotations := make(map[string]interface{}, len(instance.VolumeIDs))
11191121
for _, volumeID := range instance.VolumeIDs {
11201122
if subAnnotation, ok := annotations[volumeID].(map[string]interface{}); ok {
1121-
newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), subAnnotation, machine.Spec.AdditionalTags)
1123+
newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), subAnnotation, additionalTags)
11221124
if err != nil {
11231125
r.Log.Error(err, "Failed to fetch the changed volume tags in EC2 instance")
11241126
}
@@ -1130,7 +1132,9 @@ func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, i
11301132
}
11311133
annotations[volumeID] = newAnnotation
11321134
}
1135+
}
11331136

1137+
if !cmp.Equal(prevAnnotations, annotations, cmpopts.EquateEmpty()) {
11341138
// We also need to update the annotation if anything changed.
11351139
err = r.updateMachineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation, annotations)
11361140
if err != nil {

0 commit comments

Comments
 (0)