Skip to content

Commit

Permalink
feat: add support for instance metadata options
Browse files Browse the repository at this point in the history
Signed-off-by: Jonah Back <[email protected]>
  • Loading branch information
backjo committed May 15, 2021
1 parent 345ef0c commit 98cfb41
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
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
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
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 {
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
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 {
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

0 comments on commit 98cfb41

Please sign in to comment.