Skip to content

Commit 300e3ab

Browse files
committed
feat: Store resource partition in Status
1 parent 2122428 commit 300e3ab

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

templates/pkg/resource/identifiers.go.tpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ func (ri *resourceIdentifiers) Region() *ackv1alpha1.AWSRegion {
4040
}
4141
return nil
4242
}
43+
44+
// Partition returns the AWS partition in which the reosurce exists, or
45+
// nil if this information is not known.
46+
func (ri *resourceIdentifiers) Partition() *ackv1alpha1.AWSPartition {
47+
if ri.meta != nil {
48+
return ri.meta.Partition
49+
}
50+
return nil
51+
}

templates/pkg/resource/manager.go.tpl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ type resourceManager struct {
6262
awsAccountID ackv1alpha1.AWSAccountID
6363
// The AWS Region that this resource manager targets
6464
awsRegion ackv1alpha1.AWSRegion
65+
// The AWS Partition that this resource manager targets
66+
awsPartition ackv1alpha1.AWSPartition
6567
// sdk is a pointer to the AWS service API client exposed by the
6668
// aws-sdk-go-v2/services/{alias} package.
6769
sdkapi *svcsdk.Client
@@ -180,13 +182,13 @@ func (rm *resourceManager) Delete(
180182
// name for the resource
181183
func (rm *resourceManager) ARNFromName(name string) string {
182184
return fmt.Sprintf(
183-
"arn:aws:{{ .ControllerName }}:%s:%s:%s",
185+
"arn:%s:{{ .ControllerName }}:%s:%s:%s",
186+
rm.awsPartition,
184187
rm.awsRegion,
185188
rm.awsAccountID,
186189
name,
187190
)
188191
}
189-
190192
// LateInitialize returns an acktypes.AWSResource after setting the late initialized
191193
// fields from the readOne call. This method will initialize the optional fields
192194
// which were not provided by the k8s user but were defaulted by the AWS service.
@@ -413,6 +415,7 @@ func newResourceManager(
413415
rr acktypes.Reconciler,
414416
id ackv1alpha1.AWSAccountID,
415417
region ackv1alpha1.AWSRegion,
418+
partition ackv1alpha1.AWSPartition,
416419
) (*resourceManager, error) {
417420
return &resourceManager{
418421
cfg: cfg,
@@ -422,6 +425,7 @@ func newResourceManager(
422425
rr: rr,
423426
awsAccountID: id,
424427
awsRegion: region,
428+
awsPartition: partition,
425429
sdkapi: svcsdk.NewFromConfig(clientcfg),
426430
}, nil
427431
}

templates/pkg/resource/manager_factory.go.tpl

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ func (f *resourceManagerFactory) ResourceDescriptor() acktypes.AWSResourceDescri
3030
return &resourceDescriptor{}
3131
}
3232

33+
// GetCachedManager returns a manager object that can manage resources for a
34+
// supplied AWS account if it was already created and cached, or nil if not
35+
func (f *resourceManagerFactory) GetCachedManager(
36+
id ackv1alpha1.AWSAccountID,
37+
region ackv1alpha1.AWSRegion,
38+
roleARN ackv1alpha1.AWSResourceName,
39+
) acktypes.AWSResourceManager {
40+
// We use the account ID, region, and role ARN to uniquely identify a
41+
// resource manager. This helps us to avoid creating multiple resource
42+
// managers for the same account/region/roleARN combination.
43+
rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN)
44+
f.RLock()
45+
rm, _ := f.rmCache[rmId]
46+
f.RUnlock()
47+
48+
return rm
49+
}
50+
3351
// ManagerFor returns a resource manager object that can manage resources for a
3452
// supplied AWS account
3553
func (f *resourceManagerFactory) ManagerFor(
@@ -40,24 +58,17 @@ func (f *resourceManagerFactory) ManagerFor(
4058
rr acktypes.Reconciler,
4159
id ackv1alpha1.AWSAccountID,
4260
region ackv1alpha1.AWSRegion,
61+
partition ackv1alpha1.AWSPartition,
4362
roleARN ackv1alpha1.AWSResourceName,
4463
) (acktypes.AWSResourceManager, error) {
45-
// We use the account ID, region, and role ARN to uniquely identify a
46-
// resource manager. This helps us to avoid creating multiple resource
47-
// managers for the same account/region/roleARN combination.
48-
rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN)
49-
f.RLock()
50-
rm, found := f.rmCache[rmId]
51-
f.RUnlock()
52-
53-
if found {
54-
return rm, nil
55-
}
56-
5764
f.Lock()
5865
defer f.Unlock()
5966
60-
rm, err := newResourceManager(cfg, clientcfg, log, metrics, rr, id, region)
67+
// We use the account ID, region, partition, and role ARN to uniquely identify a
68+
// resource manager. This helps us to avoid creating multiple resource
69+
// managers for the same account/region/roleARN combination.
70+
rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN)
71+
rm, err := newResourceManager(cfg, clientcfg, log, metrics, rr, id, region, partition)
6172
if err != nil {
6273
return nil, err
6374
}

templates/pkg/resource/sdk.go.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ func (rm *resourceManager) setStatusDefaults (
201201
if ko.Status.ACKResourceMetadata.Region == nil {
202202
ko.Status.ACKResourceMetadata.Region = &rm.awsRegion
203203
}
204+
if ko.Status.ACKResourceMetadata.Partition == nil {
205+
ko.Status.ACKResourceMetadata.Partition = &rm.awsPartition
206+
}
204207
if ko.Status.ACKResourceMetadata.OwnerAccountID == nil {
205208
ko.Status.ACKResourceMetadata.OwnerAccountID = &rm.awsAccountID
206209
}

0 commit comments

Comments
 (0)