Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/v1beta2/awsmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,11 @@ func (*awsMachineWebhook) Default(_ context.Context, obj runtime.Object) error {
r.Spec.Ignition.Version = DefaultIgnitionVersion
}

if r.Spec.InstanceMetadataOptions == nil {
r.Spec.InstanceMetadataOptions = &InstanceMetadataOptions{}
}
r.Spec.InstanceMetadataOptions.SetDefaults()

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions api/v1beta2/awsmachine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func TestMachineDefault(t *testing.T) {
err := (&awsMachineWebhook{}).Default(context.Background(), machine)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(machine.Spec.CloudInit.SecureSecretsBackend).To(Equal(SecretBackendSecretsManager))
g.Expect(machine.Spec.InstanceMetadataOptions).NotTo(BeNil())
g.Expect(machine.Spec.InstanceMetadataOptions.HTTPEndpoint).To(Equal(InstanceMetadataEndpointStateEnabled))
g.Expect(machine.Spec.InstanceMetadataOptions.HTTPPutResponseHopLimit).To(Equal(int64(1)))
g.Expect(machine.Spec.InstanceMetadataOptions.HTTPTokens).To(Equal(HTTPTokensStateOptional))
g.Expect(machine.Spec.InstanceMetadataOptions.InstanceMetadataTags).To(Equal(InstanceMetadataEndpointStateDisabled))
}

func TestAWSMachineCreate(t *testing.T) {
Expand Down
16 changes: 0 additions & 16 deletions controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ func (r *AWSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

infrav1.SetDefaults_AWSMachineSpec(&awsMachine.Spec)

if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, r.Client, cluster, awsMachine); err != nil || isPaused || conditionChanged {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -719,12 +717,6 @@ func (r *AWSMachineReconciler) reconcileOperationalState(ec2svc services.EC2Inte
}
conditions.MarkTrue(machineScope.AWSMachine, infrav1.SecurityGroupsReadyCondition)

err = r.ensureInstanceMetadataOptions(ec2svc, instance, machineScope.AWSMachine)
if err != nil {
machineScope.Error(err, "failed to ensure instance metadata options")
return err
}

return nil
}

Expand Down Expand Up @@ -1318,11 +1310,3 @@ func (r *AWSMachineReconciler) ensureStorageTags(ec2svc services.EC2Interface, i
}
}
}

func (r *AWSMachineReconciler) ensureInstanceMetadataOptions(ec2svc services.EC2Interface, instance *infrav1.Instance, machine *infrav1.AWSMachine) error {
if cmp.Equal(machine.Spec.InstanceMetadataOptions, instance.InstanceMetadataOptions) {
return nil
}

return ec2svc.ModifyInstanceMetadataOptions(instance.ID, machine.Spec.InstanceMetadataOptions)
}
6 changes: 6 additions & 0 deletions controllers/awsmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,12 @@ func getAWSMachine() *infrav1.AWSMachine {
},
InstanceType: "test",
Subnet: &infrav1.AWSResourceReference{ID: aws.String("subnet-1")},
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
},
}
}
Expand Down
78 changes: 78 additions & 0 deletions controllers/awsmachine_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func TestAWSMachineReconciler(t *testing.T) {
}
klog.SetOutput(GinkgoWriter)

// Ensure InstanceMetadataOptions defaults are set (webhook sets these normally, but not in unit tests)
if awsMachine.Spec.InstanceMetadataOptions == nil {
awsMachine.Spec.InstanceMetadataOptions = &infrav1.InstanceMetadataOptions{}
awsMachine.Spec.InstanceMetadataOptions.SetDefaults()
}

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "bootstrap-data",
Expand Down Expand Up @@ -346,6 +352,12 @@ func TestAWSMachineReconciler(t *testing.T) {
instance = &infrav1.Instance{
ID: "myMachine",
VolumeIDs: []string{"volume-1", "volume-2"},
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}
instance.State = infrav1.InstanceStatePending

Expand Down Expand Up @@ -752,6 +764,12 @@ func TestAWSMachineReconciler(t *testing.T) {
ID: "myMachine",
VolumeIDs: []string{"volume-1", "volume-2"},
AvailabilityZone: "us-east-1",
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}
instance.State = infrav1.InstanceStatePending
}
Expand Down Expand Up @@ -1008,6 +1026,12 @@ func TestAWSMachineReconciler(t *testing.T) {
instance = &infrav1.Instance{
ID: "myMachine",
State: infrav1.InstanceStatePending,
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}

ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil).AnyTimes()
Expand Down Expand Up @@ -1045,6 +1069,12 @@ func TestAWSMachineReconciler(t *testing.T) {
instance = &infrav1.Instance{
ID: "myMachine",
State: infrav1.InstanceStatePending,
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}

ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(nil, nil).AnyTimes()
Expand All @@ -1069,6 +1099,12 @@ func TestAWSMachineReconciler(t *testing.T) {

instance = &infrav1.Instance{
ID: "myMachine",
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}

ms.Machine.Status.NodeRef = &corev1.ObjectReference{
Expand Down Expand Up @@ -1205,6 +1241,12 @@ func TestAWSMachineReconciler(t *testing.T) {

instance = &infrav1.Instance{
ID: "myMachine",
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}

ms.AWSMachine.Spec.CloudInit = infrav1.CloudInit{
Expand Down Expand Up @@ -1302,6 +1344,12 @@ func TestAWSMachineReconciler(t *testing.T) {

instance = &infrav1.Instance{
ID: "myMachine",
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}
instance.State = infrav1.InstanceStatePending
secretSvc.EXPECT().Create(gomock.Any(), gomock.Any()).Return(secretPrefix, int32(1), nil).Times(1)
Expand Down Expand Up @@ -1354,6 +1402,12 @@ func TestAWSMachineReconciler(t *testing.T) {
instance = &infrav1.Instance{
ID: "myMachine",
State: infrav1.InstanceStatePending,
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}
fakeS3URL := "s3://foo"

Expand Down Expand Up @@ -1387,6 +1441,12 @@ func TestAWSMachineReconciler(t *testing.T) {
instance = &infrav1.Instance{
ID: "myMachine",
State: infrav1.InstanceStatePending,
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}

//nolint:gosec
Expand Down Expand Up @@ -1414,6 +1474,12 @@ func TestAWSMachineReconciler(t *testing.T) {

instance = &infrav1.Instance{
ID: "myMachine",
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}

ms.Machine.Status.NodeRef = &corev1.ObjectReference{
Expand Down Expand Up @@ -1497,6 +1563,12 @@ func TestAWSMachineReconciler(t *testing.T) {

instance = &infrav1.Instance{
ID: "myMachine",
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}
ec2Svc.EXPECT().GetRunningInstanceByTags(gomock.Any()).Return(instance, nil).AnyTimes()
}
Expand Down Expand Up @@ -1606,6 +1678,12 @@ func TestAWSMachineReconciler(t *testing.T) {
instance = &infrav1.Instance{
ID: "myMachine",
State: infrav1.InstanceStatePending,
InstanceMetadataOptions: &infrav1.InstanceMetadataOptions{
HTTPEndpoint: infrav1.InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: infrav1.HTTPTokensStateOptional,
InstanceMetadataTags: infrav1.InstanceMetadataEndpointStateDisabled,
},
}
fakeS3URL := "s3://foo"

Expand Down
18 changes: 0 additions & 18 deletions pkg/cloud/services/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,24 +1122,6 @@ func (s *Service) checkRootVolume(rootVolume *infrav1.Volume, imageID string) (*
return rootDeviceName, nil
}

// ModifyInstanceMetadataOptions modifies the metadata options of the given EC2 instance.
func (s *Service) ModifyInstanceMetadataOptions(instanceID string, options *infrav1.InstanceMetadataOptions) error {
input := &ec2.ModifyInstanceMetadataOptionsInput{
HttpEndpoint: types.InstanceMetadataEndpointState(string(options.HTTPEndpoint)),
HttpPutResponseHopLimit: utils.ToInt32Pointer(&options.HTTPPutResponseHopLimit),
HttpTokens: types.HttpTokensState(string(options.HTTPTokens)),
InstanceMetadataTags: types.InstanceMetadataTagsState(string(options.InstanceMetadataTags)),
InstanceId: aws.String(instanceID),
}

s.scope.Info("Updating instance metadata options", "instance id", instanceID, "options", input)
if _, err := s.EC2Client.ModifyInstanceMetadataOptions(context.TODO(), input); err != nil {
return err
}

return nil
}

// GetDHCPOptionSetDomainName returns the domain DNS name for the VPC from the DHCP Options.
func (s *Service) GetDHCPOptionSetDomainName(ec2client common.EC2API, vpcID *string) *string {
log := s.scope.GetLogger()
Expand Down
1 change: 0 additions & 1 deletion pkg/cloud/services/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ type EC2Interface interface {
GetInstanceSecurityGroups(instanceID string) (map[string][]string, error)
UpdateInstanceSecurityGroups(id string, securityGroups []string) error
UpdateResourceTags(resourceID *string, create, remove map[string]string) error
ModifyInstanceMetadataOptions(instanceID string, options *infrav1.InstanceMetadataOptions) error

TerminateInstanceAndWait(instanceID string) error
DetachSecurityGroupsFromNetworkInterface(groups []string, interfaceID string) error
Expand Down
14 changes: 0 additions & 14 deletions pkg/cloud/services/mock_services/ec2_interface_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.