Merge pull request #15420 from spotinst/feature/new_integrate_instance_metadata

Spotinst: integrate AWS instance metadata config to instance groups #2
This commit is contained in:
Kubernetes Prow Robot 2023-05-16 13:11:37 -07:00 committed by GitHub
commit eccf23c920
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 127 additions and 5 deletions

View File

@ -324,6 +324,9 @@ func (b *SpotInstanceGroupModelBuilder) buildElastigroup(c *fi.CloudupModelBuild
group.AutoScalerOpts.Taints = nil
}
// Instance Metadata Options
group.InstanceMetadataOptions = b.buildInstanceMetadataOptions(ig)
klog.V(4).Infof("Adding task: Elastigroup/%s", fi.ValueOf(group.Name))
c.AddTask(group)
@ -452,6 +455,9 @@ func (b *SpotInstanceGroupModelBuilder) buildOcean(c *fi.CloudupModelBuilderCont
ocean.AutoScalerOpts.Headroom = nil
}
// Instance Metadata Options
ocean.InstanceMetadataOptions = b.buildInstanceMetadataOptions(ig)
if !fi.ValueOf(ocean.UseAsTemplateOnly) {
// Capacity.
ocean.MinSize = fi.PtrTo(int64(0))
@ -545,10 +551,7 @@ func (b *SpotInstanceGroupModelBuilder) buildLaunchSpec(c *fi.CloudupModelBuilde
// Capacity.
minSize, maxSize := b.buildCapacity(ig)
if fi.ValueOf(ocean.UseAsTemplateOnly) {
ocean.MinSize = minSize
ocean.MaxSize = maxSize
} else {
if !fi.ValueOf(ocean.UseAsTemplateOnly) {
ocean.MinSize = fi.PtrTo(fi.ValueOf(ocean.MinSize) + fi.ValueOf(minSize))
ocean.MaxSize = fi.PtrTo(fi.ValueOf(ocean.MaxSize) + fi.ValueOf(maxSize))
}
@ -624,6 +627,9 @@ func (b *SpotInstanceGroupModelBuilder) buildLaunchSpec(c *fi.CloudupModelBuilde
}
}
// Instance Metadata Options
launchSpec.InstanceMetadataOptions = b.buildInstanceMetadataOptions(ig)
klog.V(4).Infof("Adding task: LaunchSpec/%s", fi.ValueOf(launchSpec.Name))
c.AddTask(launchSpec)
@ -1032,6 +1038,16 @@ func (b *SpotInstanceGroupModelBuilder) buildAutoScalerOpts(clusterID string, ig
return opts, nil
}
func (b *SpotInstanceGroupModelBuilder) buildInstanceMetadataOptions(ig *kops.InstanceGroup) *spotinsttasks.InstanceMetadataOptions {
if ig.Spec.InstanceMetadata != nil {
opt := new(spotinsttasks.InstanceMetadataOptions)
opt.HTTPPutResponseHopLimit = fi.PtrTo(fi.ValueOf(ig.Spec.InstanceMetadata.HTTPPutResponseHopLimit))
opt.HTTPTokens = fi.PtrTo(fi.ValueOf(ig.Spec.InstanceMetadata.HTTPTokens))
return opt
}
return nil
}
func parseBool(str string) (*bool, error) {
v, err := strconv.ParseBool(str)
if err != nil {

View File

@ -71,6 +71,7 @@ type Elastigroup struct {
Tenancy *string
RootVolumeOpts *RootVolumeOpts
AutoScalerOpts *AutoScalerOpts
InstanceMetadataOptions *InstanceMetadataOptions
}
type RootVolumeOpts struct {
@ -112,6 +113,11 @@ type AutoScalerResourceLimitsOpts struct {
MaxMemory *int
}
type InstanceMetadataOptions struct {
HTTPPutResponseHopLimit *int64
HTTPTokens *string
}
var (
_ fi.CloudupTask = &Elastigroup{}
_ fi.CompareWithID = &Elastigroup{}
@ -421,6 +427,14 @@ func (e *Elastigroup) Find(c *fi.CloudupContext) (*Elastigroup, error) {
if lc.HealthCheckType != nil {
actual.HealthCheckType = lc.HealthCheckType
}
// Instance Metadata Options
if lc.MetadataOptions != nil {
actual.InstanceMetadataOptions = new(InstanceMetadataOptions)
actual.InstanceMetadataOptions.HTTPTokens = fi.PtrTo(fi.ValueOf(lc.MetadataOptions.HTTPTokens))
actual.InstanceMetadataOptions.HTTPPutResponseHopLimit = fi.PtrTo(int64(fi.ValueOf(lc.MetadataOptions.HTTPPutResponseHopLimit)))
}
}
// Auto Scaler.
@ -748,6 +762,15 @@ func (_ *Elastigroup) create(cloud awsup.AWSCloud, a, e, changes *Elastigroup) e
group.SetIntegration(integration)
}
}
// Instance Metadata Options
{
if e.InstanceMetadataOptions != nil {
opt := new(aws.MetadataOptions)
opt.SetHTTPPutResponseHopLimit(fi.PtrTo(int(fi.ValueOf(e.InstanceMetadataOptions.HTTPPutResponseHopLimit))))
opt.SetHTTPTokens(fi.PtrTo(fi.ValueOf(e.InstanceMetadataOptions.HTTPTokens)))
group.Compute.LaunchSpecification.SetMetadataOptions(opt)
}
}
attempt := 0
maxAttempts := 10
@ -1243,6 +1266,27 @@ func (_ *Elastigroup) update(cloud awsup.AWSCloud, a, e, changes *Elastigroup) e
changed = true
}
}
// Instance Metadata Options
{
if changes.InstanceMetadataOptions != nil {
if group.Compute == nil {
group.Compute = new(aws.Compute)
}
if group.Compute.LaunchSpecification == nil {
group.Compute.LaunchSpecification = new(aws.LaunchSpecification)
}
opt := new(aws.MetadataOptions)
opt.SetHTTPPutResponseHopLimit(fi.PtrTo(int(fi.ValueOf(e.InstanceMetadataOptions.HTTPPutResponseHopLimit))))
opt.SetHTTPTokens(fi.PtrTo(fi.ValueOf(e.InstanceMetadataOptions.HTTPTokens)))
group.Compute.LaunchSpecification.SetMetadataOptions(opt)
changes.InstanceMetadataOptions = nil
changed = true
}
}
}
}

View File

@ -54,6 +54,7 @@ type LaunchSpec struct {
AssociatePublicIPAddress *bool
MinSize *int64
MaxSize *int64
InstanceMetadataOptions *InstanceMetadataOptions
Ocean *Ocean
}
@ -327,6 +328,13 @@ func (o *LaunchSpec) Find(c *fi.CloudupContext) (*LaunchSpec, error) {
}
}
// Instance Metadata Options
if spec.InstanceMetadataOptions != nil {
actual.InstanceMetadataOptions = new(InstanceMetadataOptions)
actual.InstanceMetadataOptions.HTTPTokens = fi.PtrTo(fi.ValueOf(spec.InstanceMetadataOptions.HTTPTokens))
actual.InstanceMetadataOptions.HTTPPutResponseHopLimit = fi.PtrTo(int64(fi.ValueOf(spec.InstanceMetadataOptions.HTTPPutResponseHopLimit)))
}
// Avoid spurious changes.
actual.Lifecycle = o.Lifecycle
@ -534,6 +542,15 @@ func (_ *LaunchSpec) create(cloud awsup.AWSCloud, a, e, changes *LaunchSpec) err
spec.SetRestrictScaleDown(e.RestrictScaleDown)
}
}
// Instance Metadata Options
{
if e.InstanceMetadataOptions != nil {
opt := new(aws.LaunchspecInstanceMetadataOptions)
opt.SetHTTPPutResponseHopLimit(fi.PtrTo(int(fi.ValueOf(e.InstanceMetadataOptions.HTTPPutResponseHopLimit))))
opt.SetHTTPTokens(fi.PtrTo(fi.ValueOf(e.InstanceMetadataOptions.HTTPTokens)))
spec.SetLaunchspecInstanceMetadataOptions(opt)
}
}
// Wrap the raw object as a LaunchSpec.
sp, err := spotinst.NewLaunchSpec(cloud.ProviderID(), spec)
@ -787,6 +804,17 @@ func (_ *LaunchSpec) update(cloud awsup.AWSCloud, a, e, changes *LaunchSpec) err
changed = true
}
}
// Instance Metadata Options
{
if changes.InstanceMetadataOptions != nil {
opt := new(aws.LaunchspecInstanceMetadataOptions)
opt.SetHTTPPutResponseHopLimit(fi.PtrTo(int(fi.ValueOf(e.InstanceMetadataOptions.HTTPPutResponseHopLimit))))
opt.SetHTTPTokens(fi.PtrTo(fi.ValueOf(e.InstanceMetadataOptions.HTTPTokens)))
spec.SetLaunchspecInstanceMetadataOptions(opt)
changes.InstanceMetadataOptions = nil
changed = true
}
}
empty := &LaunchSpec{}
if !reflect.DeepEqual(empty, changes) {

View File

@ -63,6 +63,7 @@ type Ocean struct {
UseAsTemplateOnly *bool
RootVolumeOpts *RootVolumeOpts
AutoScalerOpts *AutoScalerOpts
InstanceMetadataOptions *InstanceMetadataOptions
}
var (
@ -290,6 +291,13 @@ func (o *Ocean) Find(c *fi.CloudupContext) (*Ocean, error) {
if lc.UseAsTemplateOnly != nil {
actual.UseAsTemplateOnly = lc.UseAsTemplateOnly
}
// Instance Metadata Options
if lc.InstanceMetadataOptions != nil {
actual.InstanceMetadataOptions = new(InstanceMetadataOptions)
actual.InstanceMetadataOptions.HTTPTokens = fi.PtrTo(fi.ValueOf(lc.InstanceMetadataOptions.HTTPTokens))
actual.InstanceMetadataOptions.HTTPPutResponseHopLimit = fi.PtrTo(int64(fi.ValueOf(lc.InstanceMetadataOptions.HTTPPutResponseHopLimit)))
}
}
// Auto Scaler.
@ -455,7 +463,15 @@ func (_ *Ocean) create(cloud awsup.AWSCloud, a, e, changes *Ocean) error {
ocean.Compute.LaunchSpecification.SetSecurityGroupIDs(securityGroupIDs)
}
}
//
{
if e.InstanceMetadataOptions != nil {
opt := new(aws.InstanceMetadataOptions)
opt.SetHTTPPutResponseHopLimit(fi.PtrTo(int(fi.ValueOf(e.InstanceMetadataOptions.HTTPPutResponseHopLimit))))
opt.SetHTTPTokens(fi.PtrTo(fi.ValueOf(e.InstanceMetadataOptions.HTTPTokens)))
ocean.Compute.LaunchSpecification.SetInstanceMetadataOptions(opt)
}
}
if !fi.ValueOf(e.UseAsTemplateOnly) {
// User data.
{
@ -812,6 +828,24 @@ func (_ *Ocean) update(cloud awsup.AWSCloud, a, e, changes *Ocean) error {
changed = true
}
}
// Instance Metadata Options
{
if changes.InstanceMetadataOptions != nil {
if ocean.Compute == nil {
ocean.Compute = new(aws.Compute)
}
if ocean.Compute.LaunchSpecification == nil {
ocean.Compute.LaunchSpecification = new(aws.LaunchSpecification)
}
opt := new(aws.InstanceMetadataOptions)
opt.SetHTTPPutResponseHopLimit(fi.PtrTo(int(fi.ValueOf(e.InstanceMetadataOptions.HTTPPutResponseHopLimit))))
opt.SetHTTPTokens(fi.PtrTo(fi.ValueOf(e.InstanceMetadataOptions.HTTPTokens)))
ocean.Compute.LaunchSpecification.SetInstanceMetadataOptions(opt)
changes.InstanceMetadataOptions = nil
changed = true
}
}
if !fi.ValueOf(e.UseAsTemplateOnly) {
// User data.