osbuild-worker: use aws sdk v2 for asg scale-in protection

This commit is contained in:
Sanne Raymaekers 2024-08-06 16:41:46 +02:00
parent 990ed6a9ad
commit 2624516f1a
4 changed files with 65 additions and 47 deletions

View file

@ -0,0 +1,45 @@
package awscloud
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
)
func (a *AWS) ASGSetProtectHost(protect bool) error {
identity, err := a.ec2imds.GetInstanceIdentityDocument(context.Background(), &imds.GetInstanceIdentityDocumentInput{})
if err != nil {
return err
}
descrASG, err := a.asg.DescribeAutoScalingInstances(
context.Background(),
&autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: []string{
identity.InstanceID,
},
},
)
if err != nil {
return err
}
if len(descrASG.AutoScalingInstances) == 0 {
return nil
}
_, err = a.asg.SetInstanceProtection(
context.Background(),
&autoscaling.SetInstanceProtectionInput{
AutoScalingGroupName: descrASG.AutoScalingInstances[0].AutoScalingGroupName,
InstanceIds: []string{
identity.InstanceID,
},
ProtectedFromScaleIn: aws.Bool(protect),
},
)
return err
}

View file

@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
@ -26,6 +27,7 @@ type AWS struct {
s3 S3
s3uploader S3Manager
s3presign S3Presign
asg ASG
}
func newForTest(ec2cli EC2, ec2imds EC2Imds, s3cli S3, upldr S3Manager, sign S3Presign) *AWS {
@ -35,6 +37,7 @@ func newForTest(ec2cli EC2, ec2imds EC2Imds, s3cli S3, upldr S3Manager, sign S3P
s3: s3cli,
s3uploader: upldr,
s3presign: sign,
asg: nil,
}
}
@ -48,6 +51,7 @@ func newAwsFromConfig(cfg aws.Config) *AWS {
s3: s3cli,
s3uploader: manager.NewUploader(s3cli),
s3presign: s3.NewPresignClient(s3cli),
asg: autoscaling.NewFromConfig(cfg),
}
}
@ -159,6 +163,7 @@ func newAwsFromCredsWithEndpoint(creds config.LoadOptionsFunc, region, endpoint,
s3: s3cli,
s3uploader: manager.NewUploader(s3cli),
s3presign: s3.NewPresignClient(s3cli),
asg: autoscaling.NewFromConfig(cfg),
}, nil
}

View file

@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
@ -60,6 +61,11 @@ type EC2Imds interface {
GetInstanceIdentityDocument(context.Context, *imds.GetInstanceIdentityDocumentInput, ...func(*imds.Options)) (*imds.GetInstanceIdentityDocumentOutput, error)
}
type ASG interface {
DescribeAutoScalingInstances(context.Context, *autoscaling.DescribeAutoScalingInstancesInput, ...func(*autoscaling.Options)) (*autoscaling.DescribeAutoScalingInstancesOutput, error)
SetInstanceProtection(context.Context, *autoscaling.SetInstanceProtectionInput, ...func(*autoscaling.Options)) (*autoscaling.SetInstanceProtectionOutput, error)
}
type S3 interface {
DeleteObject(context.Context, *s3.DeleteObjectInput, ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
PutObjectAcl(context.Context, *s3.PutObjectAclInput, ...func(*s3.Options)) (*s3.PutObjectAclOutput, error)