Skip to content

Commit 0194bc1

Browse files
committed
Default AWSMachine in admission controller
1 parent ac96235 commit 0194bc1

File tree

8 files changed

+94
-49
lines changed

8 files changed

+94
-49
lines changed

api/v1beta2/awsmachine_webhook.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,11 @@ func (*awsMachineWebhook) Default(_ context.Context, obj runtime.Object) error {
460460
r.Spec.Ignition.Version = DefaultIgnitionVersion
461461
}
462462

463+
if r.Spec.InstanceMetadataOptions == nil {
464+
r.Spec.InstanceMetadataOptions = &InstanceMetadataOptions{}
465+
}
466+
r.Spec.InstanceMetadataOptions.SetDefaults()
467+
463468
return nil
464469
}
465470

api/v1beta2/awsmachine_webhook_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func TestMachineDefault(t *testing.T) {
3838
err := (&awsMachineWebhook{}).Default(context.Background(), machine)
3939
g.Expect(err).NotTo(HaveOccurred())
4040
g.Expect(machine.Spec.CloudInit.SecureSecretsBackend).To(Equal(SecretBackendSecretsManager))
41+
g.Expect(machine.Spec.InstanceMetadataOptions).NotTo(BeNil())
42+
g.Expect(machine.Spec.InstanceMetadataOptions.HTTPEndpoint).To(Equal(InstanceMetadataEndpointStateEnabled))
43+
g.Expect(machine.Spec.InstanceMetadataOptions.HTTPPutResponseHopLimit).To(Equal(int64(1)))
44+
g.Expect(machine.Spec.InstanceMetadataOptions.HTTPTokens).To(Equal(HTTPTokensStateOptional))
45+
g.Expect(machine.Spec.InstanceMetadataOptions.InstanceMetadataTags).To(Equal(InstanceMetadataEndpointStateDisabled))
4146
}
4247

4348
func TestAWSMachineCreate(t *testing.T) {

controllers/awsmachine_controller.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ func (r *AWSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
197197
return ctrl.Result{}, nil
198198
}
199199

200-
infrav1.SetDefaults_AWSMachineSpec(&awsMachine.Spec)
201-
202200
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, r.Client, cluster, awsMachine); err != nil || isPaused || conditionChanged {
203201
return ctrl.Result{}, err
204202
}
@@ -723,12 +721,6 @@ func (r *AWSMachineReconciler) reconcileOperationalState(ec2svc services.EC2Inte
723721
}
724722
v1beta1conditions.MarkTrue(machineScope.AWSMachine, infrav1.SecurityGroupsReadyCondition)
725723

726-
err = r.ensureInstanceMetadataOptions(ec2svc, instance, machineScope.AWSMachine)
727-
if err != nil {
728-
machineScope.Error(err, "failed to ensure instance metadata options")
729-
return err
730-
}
731-
732724
return nil
733725
}
734726

@@ -1322,11 +1314,3 @@ func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, i
13221314
}
13231315
}
13241316
}
1325-
1326-
func (r *AWSMachineReconciler) ensureInstanceMetadataOptions(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine) error {
1327-
if cmp.Equal(machine.Spec.InstanceMetadataOptions, instance.InstanceMetadataOptions) {
1328-
return nil
1329-
}
1330-
1331-
return ec2svc.ModifyInstanceMetadataOptions(instance.ID, machine.Spec.InstanceMetadataOptions)
1332-
}

controllers/awsmachine_controller_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,12 @@ func getAWSMachine() *infrav1.AWSMachine {
585585
},
586586
InstanceType: "test",
587587
Subnet: &infrav1.AWSResourceReference{ID: aws.String("subnet-1")},
588+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
589+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
590+
HTTPPutResponseHopLimit: 1,
591+
HTTPTokens: infrav1.HTTPTokensStateOptional,
592+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
593+
},
588594
},
589595
}
590596
}

controllers/awsmachine_controller_unit_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ func TestAWSMachineReconciler(t *testing.T) {
8888
}
8989
klog.SetOutput(GinkgoWriter)
9090

91+
// Ensure InstanceMetadataOptions defaults are set (webhook sets these normally, but not in unit tests)
92+
if awsMachine.Spec.InstanceMetadataOptions == nil {
93+
awsMachine.Spec.InstanceMetadataOptions = &infrav1.InstanceMetadataOptions{}
94+
awsMachine.Spec.InstanceMetadataOptions.SetDefaults()
95+
}
96+
9197
secret := &corev1.Secret{
9298
ObjectMeta: metav1.ObjectMeta{
9399
Name: "bootstrap-data",
@@ -361,6 +367,12 @@ func TestAWSMachineReconciler(t *testing.T) {
361367
instance = &infrav1.Instance{
362368
ID: "myMachine",
363369
VolumeIDs: []string{"volume-1", "volume-2"},
370+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
371+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
372+
HTTPPutResponseHopLimit: 1,
373+
HTTPTokens: infrav1.HTTPTokensStateOptional,
374+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
375+
},
364376
}
365377
instance.State = infrav1.InstanceStatePending
366378

@@ -766,6 +778,12 @@ func TestAWSMachineReconciler(t *testing.T) {
766778
ID: "myMachine",
767779
VolumeIDs: []string{"volume-1", "volume-2"},
768780
AvailabilityZone: "us-east-1",
781+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
782+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
783+
HTTPPutResponseHopLimit: 1,
784+
HTTPTokens: infrav1.HTTPTokensStateOptional,
785+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
786+
},
769787
}
770788
instance.State = infrav1.InstanceStatePending
771789
}
@@ -1022,6 +1040,12 @@ func TestAWSMachineReconciler(t *testing.T) {
10221040
instance = &infrav1.Instance{
10231041
ID: "myMachine",
10241042
State: infrav1.InstanceStatePending,
1043+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1044+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1045+
HTTPPutResponseHopLimit: 1,
1046+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1047+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1048+
},
10251049
}
10261050

10271051
ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil).AnyTimes()
@@ -1059,6 +1083,12 @@ func TestAWSMachineReconciler(t *testing.T) {
10591083
instance = &infrav1.Instance{
10601084
ID: "myMachine",
10611085
State: infrav1.InstanceStatePending,
1086+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1087+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1088+
HTTPPutResponseHopLimit: 1,
1089+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1090+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1091+
},
10621092
}
10631093

10641094
ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil).AnyTimes()
@@ -1083,6 +1113,12 @@ func TestAWSMachineReconciler(t *testing.T) {
10831113

10841114
instance = &infrav1.Instance{
10851115
ID: "myMachine",
1116+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1117+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1118+
HTTPPutResponseHopLimit: 1,
1119+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1120+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1121+
},
10861122
}
10871123

10881124
ms.Machine.Status.NodeRef = clusterv1.MachineNodeReference{
@@ -1217,6 +1253,12 @@ func TestAWSMachineReconciler(t *testing.T) {
12171253

12181254
instance = &infrav1.Instance{
12191255
ID: "myMachine",
1256+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1257+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1258+
HTTPPutResponseHopLimit: 1,
1259+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1260+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1261+
},
12201262
}
12211263

12221264
ms.AWSMachine.Spec.CloudInit = infrav1.CloudInit{
@@ -1314,6 +1356,12 @@ func TestAWSMachineReconciler(t *testing.T) {
13141356

13151357
instance = &infrav1.Instance{
13161358
ID: "myMachine",
1359+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1360+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1361+
HTTPPutResponseHopLimit: 1,
1362+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1363+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1364+
},
13171365
}
13181366
instance.State = infrav1.InstanceStatePending
13191367
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return(secretPrefix, int32(1), nil).Times(1)
@@ -1366,6 +1414,12 @@ func TestAWSMachineReconciler(t *testing.T) {
13661414
instance = &infrav1.Instance{
13671415
ID: "myMachine",
13681416
State: infrav1.InstanceStatePending,
1417+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1418+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1419+
HTTPPutResponseHopLimit: 1,
1420+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1421+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1422+
},
13691423
}
13701424
fakeS3URL := "s3://foo"
13711425

@@ -1399,6 +1453,12 @@ func TestAWSMachineReconciler(t *testing.T) {
13991453
instance = &infrav1.Instance{
14001454
ID: "myMachine",
14011455
State: infrav1.InstanceStatePending,
1456+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1457+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1458+
HTTPPutResponseHopLimit: 1,
1459+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1460+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1461+
},
14021462
}
14031463

14041464
//nolint:gosec
@@ -1426,6 +1486,12 @@ func TestAWSMachineReconciler(t *testing.T) {
14261486

14271487
instance = &infrav1.Instance{
14281488
ID: "myMachine",
1489+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1490+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1491+
HTTPPutResponseHopLimit: 1,
1492+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1493+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1494+
},
14291495
}
14301496

14311497
ms.Machine.Status.NodeRef = clusterv1.MachineNodeReference{
@@ -1507,6 +1573,12 @@ func TestAWSMachineReconciler(t *testing.T) {
15071573

15081574
instance = &infrav1.Instance{
15091575
ID: "myMachine",
1576+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1577+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1578+
HTTPPutResponseHopLimit: 1,
1579+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1580+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1581+
},
15101582
}
15111583
ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(instance, nil).AnyTimes()
15121584
}
@@ -1616,6 +1688,12 @@ func TestAWSMachineReconciler(t *testing.T) {
16161688
instance = &infrav1.Instance{
16171689
ID: "myMachine",
16181690
State: infrav1.InstanceStatePending,
1691+
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
1692+
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
1693+
HTTPPutResponseHopLimit: 1,
1694+
HTTPTokens: infrav1.HTTPTokensStateOptional,
1695+
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
1696+
},
16191697
}
16201698
fakeS3URL := "s3://foo"
16211699

pkg/cloud/services/ec2/instances.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,24 +1122,6 @@ func (s *Service) checkRootVolume(rootVolume *infrav1.Volume, imageID string) (*
11221122
return rootDeviceName, nil
11231123
}
11241124

1125-
// ModifyInstanceMetadataOptions modifies the metadata options of the given EC2 instance.
1126-
func (s *Service) ModifyInstanceMetadataOptions(instanceID string, options *infrav1.InstanceMetadataOptions) error {
1127-
input := &ec2.ModifyInstanceMetadataOptionsInput{
1128-
HttpEndpoint: types.InstanceMetadataEndpointState(string(options.HTTPEndpoint)),
1129-
HttpPutResponseHopLimit: utils.ToInt32Pointer(&options.HTTPPutResponseHopLimit),
1130-
HttpTokens: types.HttpTokensState(string(options.HTTPTokens)),
1131-
InstanceMetadataTags: types.InstanceMetadataTagsState(string(options.InstanceMetadataTags)),
1132-
InstanceId: aws.String(instanceID),
1133-
}
1134-
1135-
s.scope.Info("Updating instance metadata options", "instance id", instanceID, "options", input)
1136-
if _, err := s.EC2Client.ModifyInstanceMetadataOptions(context.TODO(), input); err != nil {
1137-
return err
1138-
}
1139-
1140-
return nil
1141-
}
1142-
11431125
// GetDHCPOptionSetDomainName returns the domain DNS name for the VPC from the DHCP Options.
11441126
func (s *Service) GetDHCPOptionSetDomainName(ec2client common.EC2API, vpcID *string) *string {
11451127
log := s.scope.GetLogger()

pkg/cloud/services/interfaces.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ type EC2Interface interface {
7373
GetInstanceSecurityGroups(instanceID string) (map[string][]string, error)
7474
UpdateInstanceSecurityGroups(id string, securityGroups []string) error
7575
UpdateResourceTags(resourceID *string, create, remove map[string]string) error
76-
ModifyInstanceMetadataOptions(instanceID string, options *infrav1.InstanceMetadataOptions) error
7776

7877
TerminateInstanceAndWait(instanceID string) error
7978
DetachSecurityGroupsFromNetworkInterface(groups []string, interfaceID string) error

pkg/cloud/services/mock_services/ec2_interface_mock.go

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)