From 39d242a2fe7c531fc13a6c9949cfb31f45047474 Mon Sep 17 00:00:00 2001 From: yehielnetapp Date: Tue, 16 May 2023 13:44:09 +0300 Subject: [PATCH 1/3] add instance metdata config again --- pkg/model/awsmodel/spotinst.go | 19 ++++++++ .../fi/cloudup/spotinsttasks/elastigroup.go | 44 +++++++++++++++++++ .../fi/cloudup/spotinsttasks/launch_spec.go | 28 ++++++++++++ upup/pkg/fi/cloudup/spotinsttasks/ocean.go | 36 ++++++++++++++- 4 files changed, 126 insertions(+), 1 deletion(-) diff --git a/pkg/model/awsmodel/spotinst.go b/pkg/model/awsmodel/spotinst.go index 19c0685e47..c23cb6f4c1 100644 --- a/pkg/model/awsmodel/spotinst.go +++ b/pkg/model/awsmodel/spotinst.go @@ -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)) @@ -624,6 +630,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 +1041,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 { diff --git a/upup/pkg/fi/cloudup/spotinsttasks/elastigroup.go b/upup/pkg/fi/cloudup/spotinsttasks/elastigroup.go index 67d9fc8dda..df74e1441f 100644 --- a/upup/pkg/fi/cloudup/spotinsttasks/elastigroup.go +++ b/upup/pkg/fi/cloudup/spotinsttasks/elastigroup.go @@ -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 + + } + } + } } diff --git a/upup/pkg/fi/cloudup/spotinsttasks/launch_spec.go b/upup/pkg/fi/cloudup/spotinsttasks/launch_spec.go index f5fae79138..632de37384 100644 --- a/upup/pkg/fi/cloudup/spotinsttasks/launch_spec.go +++ b/upup/pkg/fi/cloudup/spotinsttasks/launch_spec.go @@ -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) { diff --git a/upup/pkg/fi/cloudup/spotinsttasks/ocean.go b/upup/pkg/fi/cloudup/spotinsttasks/ocean.go index e317491949..1232a6154d 100644 --- a/upup/pkg/fi/cloudup/spotinsttasks/ocean.go +++ b/upup/pkg/fi/cloudup/spotinsttasks/ocean.go @@ -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. From 30894869e7f979741b68de5dc7eb54db858f515d Mon Sep 17 00:00:00 2001 From: yehielnetapp Date: Tue, 16 May 2023 16:10:35 +0300 Subject: [PATCH 2/3] fix vng size --- pkg/model/awsmodel/spotinst.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/model/awsmodel/spotinst.go b/pkg/model/awsmodel/spotinst.go index c23cb6f4c1..fc990e678e 100644 --- a/pkg/model/awsmodel/spotinst.go +++ b/pkg/model/awsmodel/spotinst.go @@ -552,9 +552,6 @@ 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 { ocean.MinSize = fi.PtrTo(fi.ValueOf(ocean.MinSize) + fi.ValueOf(minSize)) ocean.MaxSize = fi.PtrTo(fi.ValueOf(ocean.MaxSize) + fi.ValueOf(maxSize)) } From 12067887d380db20b414ef4848b71b604f73c243 Mon Sep 17 00:00:00 2001 From: yehielnetapp Date: Tue, 16 May 2023 16:13:02 +0300 Subject: [PATCH 3/3] fix vng size try 2 --- pkg/model/awsmodel/spotinst.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/model/awsmodel/spotinst.go b/pkg/model/awsmodel/spotinst.go index fc990e678e..933a7fab79 100644 --- a/pkg/model/awsmodel/spotinst.go +++ b/pkg/model/awsmodel/spotinst.go @@ -551,7 +551,7 @@ func (b *SpotInstanceGroupModelBuilder) buildLaunchSpec(c *fi.CloudupModelBuilde // Capacity. minSize, maxSize := b.buildCapacity(ig) - if fi.ValueOf(ocean.UseAsTemplateOnly) { + 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)) }