Skip to content

Commit

Permalink
Merge pull request #4147 from muraee/fix-metadata-options-defaults
Browse files Browse the repository at this point in the history
Fix InstanceMetadataOptions defaults
  • Loading branch information
k8s-ci-robot authored Mar 25, 2023
2 parents f1779d0 + 395cf4d commit 7887778
Show file tree
Hide file tree
Showing 5 changed files with 628 additions and 455 deletions.
8 changes: 2 additions & 6 deletions api/v1beta2/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ func SetDefaults_Labels(obj *metav1.ObjectMeta) { //nolint:golint,stylecheck
// SetDefaults_AWSMachineSpec is used by defaulter-gen.
func SetDefaults_AWSMachineSpec(obj *AWSMachineSpec) { //nolint:golint,stylecheck
if obj.InstanceMetadataOptions == nil {
obj.InstanceMetadataOptions = &InstanceMetadataOptions{
HTTPEndpoint: InstanceMetadataEndpointStateEnabled,
HTTPPutResponseHopLimit: 1,
HTTPTokens: HTTPTokensStateRequired, // Defaults to IMDSv2
InstanceMetadataTags: InstanceMetadataEndpointStateDisabled,
}
obj.InstanceMetadataOptions = &InstanceMetadataOptions{}
}
obj.InstanceMetadataOptions.SetDefaults()
}
15 changes: 15 additions & 0 deletions api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ type InstanceMetadataOptions struct {
InstanceMetadataTags InstanceMetadataState `json:"instanceMetadataTags,omitempty"`
}

func (obj *InstanceMetadataOptions) SetDefaults() {
if obj.HTTPEndpoint == "" {
obj.HTTPEndpoint = InstanceMetadataEndpointStateEnabled
}
if obj.HTTPPutResponseHopLimit == 0 {
obj.HTTPPutResponseHopLimit = 1
}
if obj.HTTPTokens == "" {
obj.HTTPTokens = HTTPTokensStateRequired // Defaults to IMDSv2
}
if obj.InstanceMetadataTags == "" {
obj.InstanceMetadataTags = InstanceMetadataEndpointStateDisabled
}
}

// Volume encapsulates the configuration options for the storage device.
type Volume struct {
// Device name
Expand Down
2 changes: 2 additions & 0 deletions controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func (r *AWSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

infrav1.SetDefaults_AWSMachineSpec(&awsMachine.Spec)

// Create the machine scope
machineScope, err := scope.NewMachineScope(scope.MachineScopeParams{
Client: r.Client,
Expand Down
6 changes: 6 additions & 0 deletions controllers/awsmachine_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,12 @@ func TestAWSMachineReconcilerReconcileDefaultsToLoadBalancerTypeClassic(t *testi
Placement: &ec2.Placement{
AvailabilityZone: aws.String("thezone"),
},
MetadataOptions: &ec2.InstanceMetadataOptionsResponse{
HttpEndpoint: aws.String(string(infrav1.InstanceMetadataEndpointStateEnabled)),
HttpPutResponseHopLimit: aws.Int64(1),
HttpTokens: aws.String(string(infrav1.HTTPTokensStateRequired)),
InstanceMetadataTags: aws.String(string(infrav1.InstanceMetadataEndpointStateDisabled)),
},
},
},
},
Expand Down
Loading

0 comments on commit 7887778

Please sign in to comment.