mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-20 19:25:52 +00:00
fix: return GroupArn on placement groups, built from the group name as AWS does
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/spinifex/spinifex/arn"
|
||||
"github.com/mulgadc/spinifex/spinifex/awserrors"
|
||||
"github.com/mulgadc/spinifex/spinifex/config"
|
||||
"github.com/mulgadc/spinifex/spinifex/filterutil"
|
||||
@@ -49,6 +50,7 @@ type PlacementGroupServiceImpl struct {
|
||||
config *config.Config
|
||||
natsConn *nats.Conn
|
||||
kv jetstream.KeyValue
|
||||
region string
|
||||
}
|
||||
|
||||
// NewPlacementGroupServiceImplWithNATS creates a placement group service with NATS JetStream.
|
||||
@@ -66,12 +68,18 @@ func NewPlacementGroupServiceImplWithNATS(ctx context.Context, cfg *config.Confi
|
||||
return nil, fmt.Errorf("migrate %s: %w", KVBucketPlacementGroups, err)
|
||||
}
|
||||
|
||||
region := config.DefaultAWSRegion
|
||||
if cfg != nil && cfg.Region != "" {
|
||||
region = cfg.Region
|
||||
}
|
||||
|
||||
slog.Info("Placement group service initialized with JetStream KV", "bucket", KVBucketPlacementGroups)
|
||||
|
||||
return &PlacementGroupServiceImpl{
|
||||
config: cfg,
|
||||
natsConn: natsConn,
|
||||
kv: kv,
|
||||
region: region,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -568,6 +576,7 @@ func (s *PlacementGroupServiceImpl) FinalizeClusterInstances(ctx context.Context
|
||||
// recordToEC2 converts an internal record to the AWS SDK PlacementGroup type.
|
||||
func (s *PlacementGroupServiceImpl) recordToEC2(record *PlacementGroupRecord) *ec2.PlacementGroup {
|
||||
pg := &ec2.PlacementGroup{
|
||||
GroupArn: aws.String(arn.FormatEC2(arn.EC2PlacementGroup, s.region, record.AccountID, record.GroupName)),
|
||||
GroupId: aws.String(record.GroupId),
|
||||
GroupName: aws.String(record.GroupName),
|
||||
Strategy: aws.String(record.Strategy),
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/mulgadc/spinifex/spinifex/awserrors"
|
||||
"github.com/mulgadc/spinifex/spinifex/config"
|
||||
"github.com/mulgadc/spinifex/spinifex/kvutil"
|
||||
"github.com/mulgadc/spinifex/spinifex/testutil"
|
||||
"github.com/mulgadc/spinifex/spinifex/utils"
|
||||
@@ -83,6 +84,34 @@ func TestCreatePlacementGroup_Cluster(t *testing.T) {
|
||||
assert.Nil(t, pg.SpreadLevel)
|
||||
}
|
||||
|
||||
// AWS builds GroupArn from the group name, not its ID, on create and describe.
|
||||
func TestPlacementGroup_GroupArnFromName(t *testing.T) {
|
||||
_, nc, _ := testutil.StartTestJetStream(t)
|
||||
svc, err := NewPlacementGroupServiceImplWithNATS(t.Context(), &config.Config{Region: "ap-southeast-2"}, nc)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, strategy := range []string{"cluster", "spread"} {
|
||||
name := "arn-" + strategy
|
||||
want := "arn:aws:ec2:ap-southeast-2:123456789012:placement-group/" + name
|
||||
|
||||
pg := createTestGroup(t, svc, name, strategy)
|
||||
assert.Equal(t, want, aws.StringValue(pg.GroupArn))
|
||||
|
||||
out, err := svc.DescribePlacementGroups(context.Background(), &ec2.DescribePlacementGroupsInput{
|
||||
GroupNames: []*string{aws.String(name)},
|
||||
}, testAccountID)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out.PlacementGroups, 1)
|
||||
assert.Equal(t, want, aws.StringValue(out.PlacementGroups[0].GroupArn))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlacementGroup_GroupArnDefaultRegion(t *testing.T) {
|
||||
svc := setupTestService(t)
|
||||
pg := createTestGroup(t, svc, "no-region", "spread")
|
||||
assert.Equal(t, "arn:aws:ec2:us-east-1:123456789012:placement-group/no-region", aws.StringValue(pg.GroupArn))
|
||||
}
|
||||
|
||||
func TestCreatePlacementGroup_DuplicateName(t *testing.T) {
|
||||
svc := setupTestService(t)
|
||||
createTestGroup(t, svc, "dup-group", "spread")
|
||||
|
||||
Reference in New Issue
Block a user