Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for instance metadata options #305

Merged
merged 3 commits into from
May 17, 2021
Merged
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
10 changes: 10 additions & 0 deletions api/v1alpha1/instancegroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ type EKSConfiguration struct {
MixedInstancesPolicy *MixedInstancesPolicySpec `json:"mixedInstancesPolicy,omitempty"`
LicenseSpecifications []string `json:"licenseSpecifications,omitempty"`
Placement *PlacementSpec `json:"placement,omitempty"`
MetadataOptions *MetadataOptions `json:"metadataOptions,omitempty"`
}

const (
Expand All @@ -250,6 +251,12 @@ type PlacementSpec struct {
Tenancy string `json:"tenancy,omitempty"`
}

type MetadataOptions struct {
HttpEndpoint string `json:"httpEndpoint,omitempty"`
HttpTokens string `json:"httpTokens,omitempty"`
HttpPutHopLimit int64 `json:"httpPutHopLimit,omitempty"`
}

type InstanceTypeSpec struct {
Type string `json:"type"`
Weight int64 `json:"weight,omitempty"`
Expand Down Expand Up @@ -721,6 +728,9 @@ func (c *EKSConfiguration) GetRoleName() string {
func (c *EKSConfiguration) GetMixedInstancesPolicy() *MixedInstancesPolicySpec {
return c.MixedInstancesPolicy
}
func (c *EKSConfiguration) GetMetadataOptions() *MetadataOptions {
return c.MetadataOptions
}
func (c *EKSConfiguration) GetPlacement() *PlacementSpec {
return c.Placement
}
Expand Down
29 changes: 29 additions & 0 deletions api/v1alpha1/instancegroup_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,35 @@ func TestInstanceGroupSpecValidate(t *testing.T) {
},
want: "",
},
{
name: "eks with metadataoptions validates",
args: args{
instancegroup: MockInstanceGroup("eks", "rollingUpdate", &EKSSpec{
MaxSize: 1,
MinSize: 1,
Type: "LaunchTemplate",
EKSConfiguration: &EKSConfiguration{
EksClusterName: "my-eks-cluster",
NodeSecurityGroups: []string{"sg-123456789"},
Image: "ami-12345",
InstanceType: "m5.large",
KeyPairName: "thisShouldBeOptional",
Subnets: []string{"subnet-1111111", "subnet-222222"},
Placement: &PlacementSpec{
AvailabilityZone: "us-west-2a",
HostResourceGroupArn: "arn:aws:resource-groups:us-west-2:1122334455:group/resourceName",
Tenancy: "host",
},
MetadataOptions: &MetadataOptions{
HttpEndpoint: "enabled",
HttpTokens: "required",
HttpPutHopLimit: 1,
},
},
}, nil, nil),
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

10 changes: 10 additions & 0 deletions config/crd/bases/instancemgr.keikoproj.io_instancegroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ spec:
items:
type: string
type: array
metadataOptions:
properties:
httpEndpoint:
type: string
httpPutHopLimit:
format: int64
type: integer
httpTokens:
type: string
type: object
metricsCollection:
items:
type: string
Expand Down
2 changes: 2 additions & 0 deletions controllers/provisioners/eks/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (ctx *EksInstanceGroupContext) Create() error {
sgs = ctx.ResolveSecurityGroups()
spotPrice = configuration.GetSpotPrice()
placement = configuration.GetPlacement()
metadataOptions = configuration.GetMetadataOptions()
)

ctx.SetState(v1alpha1.ReconcileModifying)
Expand Down Expand Up @@ -73,6 +74,7 @@ func (ctx *EksInstanceGroupContext) Create() error {
SpotPrice: spotPrice,
LicenseSpecifications: configuration.LicenseSpecifications,
Placement: placement,
MetadataOptions: metadataOptions,
}

if err := scalingConfig.Create(config); err != nil {
Expand Down
1 change: 1 addition & 0 deletions controllers/provisioners/eks/scaling/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type CreateConfigurationInput struct {
SpotPrice string
LicenseSpecifications []string
Placement *v1alpha1.PlacementSpec
MetadataOptions *v1alpha1.MetadataOptions
}

func ConvertToLaunchTemplate(resource interface{}) *ec2.LaunchTemplate {
Expand Down
22 changes: 22 additions & 0 deletions controllers/provisioners/eks/scaling/launchconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (lc *LaunchConfiguration) Create(input *CreateConfigurationInput) error {
SecurityGroups: aws.StringSlice(input.SecurityGroups),
UserData: aws.String(input.UserData),
BlockDeviceMappings: devices,
MetadataOptions: lc.metadataOptions(input.MetadataOptions),
}

if !common.StringEmpty(input.SpotPrice) {
Expand Down Expand Up @@ -231,6 +232,16 @@ func (lc *LaunchConfiguration) Drifted(input *CreateConfigurationInput) bool {
drift = true
}

metadataOptions := lc.metadataOptions(input.MetadataOptions)

if !reflect.DeepEqual(metadataOptions, existingConfig.MetadataOptions) {
log.Info("detected drift", "reason", "metadata options have changed", "instancegroup", lc.OwnerName,
"previousValue", existingConfig.MetadataOptions,
"newValue", metadataOptions,
)
drift = true
}

if !drift {
log.Info("drift not detected", "instancegroup", lc.OwnerName)
}
Expand Down Expand Up @@ -282,6 +293,17 @@ func (lc *LaunchConfiguration) blockDeviceList(volumes []v1alpha1.NodeVolume) []
return sortConfigDevices(devices)
}

func (lc *LaunchConfiguration) metadataOptions(input *v1alpha1.MetadataOptions) *autoscaling.InstanceMetadataOptions {
backjo marked this conversation as resolved.
Show resolved Hide resolved
if input == nil {
return nil
}
return &autoscaling.InstanceMetadataOptions{
HttpEndpoint: aws.String(input.HttpEndpoint),
HttpPutResponseHopLimit: aws.Int64(input.HttpPutHopLimit),
HttpTokens: aws.String(input.HttpTokens),
}
}

func getPrefixedConfigurations(configs []*autoscaling.LaunchConfiguration, prefix string) []*autoscaling.LaunchConfiguration {
prefixed := []*autoscaling.LaunchConfiguration{}
for _, lc := range configs {
Expand Down
10 changes: 10 additions & 0 deletions controllers/provisioners/eks/scaling/launchconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,16 @@ func TestLaunchConfigurationDrifted(t *testing.T) {
},
shouldDrift: true,
},
{
launchConfig: &autoscaling.LaunchConfiguration{
LaunchConfigurationName: aws.String("my-launch-config"),
},
input: &CreateConfigurationInput{
SecurityGroups: []string{},
MetadataOptions: &v1alpha1.MetadataOptions{HttpEndpoint: "enabled"},
},
shouldDrift: true,
},
}

for i, tc := range tests {
Expand Down
33 changes: 33 additions & 0 deletions controllers/provisioners/eks/scaling/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (lt *LaunchTemplate) Create(input *CreateConfigurationInput) error {
BlockDeviceMappings: lt.blockDeviceListRequest(input.Volumes),
LicenseSpecifications: lt.LaunchTemplateLicenseConfigurationRequest(input.LicenseSpecifications),
Placement: lt.launchTemplatePlacementRequest(input.Placement),
MetadataOptions: lt.metadataOptionsRequest(input.MetadataOptions),
}

if !lt.Provisioned() {
Expand Down Expand Up @@ -270,6 +271,16 @@ func (lt *LaunchTemplate) Drifted(input *CreateConfigurationInput) bool {
drift = true
}

metadataOptions := lt.metadataOptions(input.MetadataOptions)

if !reflect.DeepEqual(metadataOptions, latestVersion.LaunchTemplateData.MetadataOptions) {
log.Info("detected drift", "reason", "metadata options have changed", "instancegroup", lt.OwnerName,
"previousValue", latestVersion.LaunchTemplateData.MetadataOptions,
"newValue", metadataOptions,
)
drift = true
}

if !drift {
log.Info("drift not detected", "instancegroup", lt.OwnerName)
}
Expand Down Expand Up @@ -345,6 +356,28 @@ func (lt *LaunchTemplate) launchTemplatePlacementRequest(input *v1alpha1.Placeme
return lt.LaunchTemplatePlacementRequest(input.AvailabilityZone, input.HostResourceGroupArn, input.Tenancy)
}

func (lt *LaunchTemplate) metadataOptions(input *v1alpha1.MetadataOptions) *ec2.LaunchTemplateInstanceMetadataOptions {
backjo marked this conversation as resolved.
Show resolved Hide resolved
if input == nil {
return nil
}
return &ec2.LaunchTemplateInstanceMetadataOptions{
HttpEndpoint: aws.String(input.HttpEndpoint),
HttpPutResponseHopLimit: aws.Int64(input.HttpPutHopLimit),
HttpTokens: aws.String(input.HttpTokens),
}
}

func (lt *LaunchTemplate) metadataOptionsRequest(input *v1alpha1.MetadataOptions) *ec2.LaunchTemplateInstanceMetadataOptionsRequest {
if input == nil {
return nil
}
return &ec2.LaunchTemplateInstanceMetadataOptionsRequest{
HttpEndpoint: aws.String(input.HttpEndpoint),
HttpPutResponseHopLimit: aws.Int64(input.HttpPutHopLimit),
HttpTokens: aws.String(input.HttpTokens),
}
}

func (lt *LaunchTemplate) launchTemplatePlacement(input *v1alpha1.PlacementSpec) *ec2.LaunchTemplatePlacement {
if input == nil {
return &ec2.LaunchTemplatePlacement{}
Expand Down
10 changes: 10 additions & 0 deletions controllers/provisioners/eks/scaling/launchtemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,16 @@ func TestLaunchTemplateDrifted(t *testing.T) {
},
shouldDrift: true,
},
{
launchTemplate: MockLaunchTemplate("my-launch-template"),
latestVersion: MockLaunchTemplateVersion(),
input: &CreateConfigurationInput{
MetadataOptions: &v1alpha1.MetadataOptions{
HttpEndpoint: "Enabled",
},
},
shouldDrift: true,
},
}

for i, tc := range tests {
Expand Down
2 changes: 2 additions & 0 deletions controllers/provisioners/eks/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (ctx *EksInstanceGroupContext) Update() error {
sgs = ctx.ResolveSecurityGroups()
spotPrice = configuration.GetSpotPrice()
placement = configuration.GetPlacement()
metadataOptions = configuration.GetMetadataOptions()
)

ctx.SetState(v1alpha1.ReconcileModifying)
Expand All @@ -75,6 +76,7 @@ func (ctx *EksInstanceGroupContext) Update() error {
SpotPrice: spotPrice,
LicenseSpecifications: configuration.LicenseSpecifications,
Placement: placement,
MetadataOptions: metadataOptions,
}

// create new launchconfig if it has drifted
Expand Down