Skip to content

Commit 0c5e997

Browse files
authored
Merge pull request #4531 from cnmcavoy/cnmcavoy/cleanup-volume-last-applied-tags
Fix cluster-api-provider-last-applied-tags-on-volumes to recalculate the value on each reconcile
2 parents b5835c0 + 83c75a3 commit 0c5e997

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

controllers/awsmachine_controller.go

Lines changed: 6 additions & 2 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"
@@ -1112,12 +1113,13 @@ func (r *AWSMachineReconciler) indexAWSMachineByInstanceID(o client.Object) []st
11121113
}
11131114

11141115
func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine, additionalTags map[string]string) {
1115-
annotations, err := r.machineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation)
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 {
1120-
if subAnnotation, ok := annotations[volumeID].(map[string]interface{}); ok {
1122+
if subAnnotation, ok := prevAnnotations[volumeID].(map[string]interface{}); ok {
11211123
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")
@@ -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)