Skip to content

Commit a0e5849

Browse files
authored
Merge pull request #4512 from philjb/pjb-4511-additionaltags-root-volumes
fix: propagate AdditionalTags from AWSCluster to storage volumes
2 parents b90c18c + b805082 commit a0e5849

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

controllers/awsmachine_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
589589
}
590590

591591
if instance != nil {
592-
r.ensureStorageTags(ec2svc, instance, machineScope.AWSMachine)
592+
r.ensureStorageTags(ec2svc, instance, machineScope.AWSMachine, machineScope.AdditionalTags())
593593
}
594594

595595
if err := r.reconcileLBAttachment(machineScope, elbScope, instance); err != nil {
@@ -1111,20 +1111,20 @@ func (r *AWSMachineReconciler) indexAWSMachineByInstanceID(o client.Object) []st
11111111
return nil
11121112
}
11131113

1114-
func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine) {
1114+
func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine, additionalTags map[string]string) {
11151115
annotations, err := r.machineAnnotationJSON(machine, VolumeTagsLastAppliedAnnotation)
11161116
if err != nil {
11171117
r.Log.Error(err, "Failed to fetch the annotations for volume tags")
11181118
}
11191119
for _, volumeID := range instance.VolumeIDs {
11201120
if subAnnotation, ok := annotations[volumeID].(map[string]interface{}); ok {
1121-
newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), subAnnotation, machine.Spec.AdditionalTags)
1121+
newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), subAnnotation, additionalTags)
11221122
if err != nil {
11231123
r.Log.Error(err, "Failed to fetch the changed volume tags in EC2 instance")
11241124
}
11251125
annotations[volumeID] = newAnnotation
11261126
} else {
1127-
newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), make(map[string]interface{}), machine.Spec.AdditionalTags)
1127+
newAnnotation, err := r.ensureVolumeTags(ec2svc, aws.String(volumeID), make(map[string]interface{}), additionalTags)
11281128
if err != nil {
11291129
r.Log.Error(err, "Failed to fetch the changed volume tags in EC2 instance")
11301130
}

controllers/awsmachine_controller_unit_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -481,34 +481,41 @@ func TestAWSMachineReconciler(t *testing.T) {
481481
}
482482
})
483483

484-
t.Run("should tag instances from machine and cluster tags", func(t *testing.T) {
484+
t.Run("should tag instances and volumes with machine and cluster tags", func(t *testing.T) {
485485
g := NewWithT(t)
486486
awsMachine := getAWSMachine()
487487
setup(t, g, awsMachine)
488488
defer teardown(t, g)
489489
instanceCreate(t, g)
490490
getCoreSecurityGroups(t, g)
491491

492-
ms.AWSMachine.Spec.AdditionalTags = infrav1.Tags{"kind": "alicorn"}
493-
cs.AWSCluster.Spec.AdditionalTags = infrav1.Tags{"colour": "lavender"}
492+
ms.AWSMachine.Spec.AdditionalTags = infrav1.Tags{"kind": "alicorn", "colour": "pink"} // takes precedence
493+
cs.AWSCluster.Spec.AdditionalTags = infrav1.Tags{"colour": "lavender", "shape": "round"}
494494

495495
ec2Svc.EXPECT().GetAdditionalSecurityGroupsIDs(gomock.Any()).Return(nil, nil)
496+
497+
// expect one call first to tag the instance and two calls for tagging each of two volumes
498+
// the volumes get the tags from the AWSCluster _and_ the AWSMachine
499+
496500
ec2Svc.EXPECT().UpdateResourceTags(
497-
gomock.Any(),
501+
PointsTo("myMachine"),
498502
map[string]string{
499-
"kind": "alicorn",
503+
"colour": "pink",
504+
"shape": "round",
505+
"kind": "alicorn",
500506
},
501507
map[string]string{},
502-
).Return(nil).Times(2)
508+
).Return(nil)
503509

504510
ec2Svc.EXPECT().UpdateResourceTags(
505-
PointsTo("myMachine"),
511+
gomock.Any(),
506512
map[string]string{
507-
"colour": "lavender",
513+
"colour": "pink",
514+
"shape": "round",
508515
"kind": "alicorn",
509516
},
510517
map[string]string{},
511-
).Return(nil)
518+
).Return(nil).Times(2)
512519

513520
_, err := reconciler.reconcileNormal(context.Background(), ms, cs, cs, cs, cs)
514521
g.Expect(err).To(BeNil())

0 commit comments

Comments
 (0)