Skip to content

Commit 5680f13

Browse files
committed
feat: Store resource partition in Status
1 parent 2122428 commit 5680f13

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-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: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,48 @@ func (f *resourceManagerFactory) ResourceDescriptor() acktypes.AWSResourceDescri
3030
return &resourceDescriptor{}
3131
}
3232

33-
// ManagerFor returns a resource manager object that can manage resources for a
34-
// supplied AWS account
35-
func (f *resourceManagerFactory) ManagerFor(
36-
cfg ackcfg.Config,
37-
clientcfg aws.Config,
38-
log logr.Logger,
39-
metrics *ackmetrics.Metrics,
40-
rr acktypes.Reconciler,
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(
4136
id ackv1alpha1.AWSAccountID,
4237
region ackv1alpha1.AWSRegion,
4338
roleARN ackv1alpha1.AWSResourceName,
44-
) (acktypes.AWSResourceManager, error) {
39+
) acktypes.AWSResourceManager {
4540
// We use the account ID, region, and role ARN to uniquely identify a
4641
// resource manager. This helps us to avoid creating multiple resource
4742
// managers for the same account/region/roleARN combination.
4843
rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN)
4944
f.RLock()
5045
rm, found := f.rmCache[rmId]
5146
f.RUnlock()
52-
53-
if found {
54-
return rm, nil
47+
if !found {
48+
return nil
5549
}
5650

51+
return rm
52+
}
53+
54+
// ManagerFor returns a resource manager object that can manage resources for a
55+
// supplied AWS account
56+
func (f *resourceManagerFactory) ManagerFor(
57+
cfg ackcfg.Config,
58+
clientcfg aws.Config,
59+
log logr.Logger,
60+
metrics *ackmetrics.Metrics,
61+
rr acktypes.Reconciler,
62+
id ackv1alpha1.AWSAccountID,
63+
region ackv1alpha1.AWSRegion,
64+
partition ackv1alpha1.AWSPartition,
65+
roleARN ackv1alpha1.AWSResourceName,
66+
) (acktypes.AWSResourceManager, error) {
5767
f.Lock()
5868
defer f.Unlock()
5969
60-
rm, err := newResourceManager(cfg, clientcfg, log, metrics, rr, id, region)
70+
// We use the account ID, region, partition, and role ARN to uniquely identify a
71+
// resource manager. This helps us to avoid creating multiple resource
72+
// managers for the same account/region/roleARN combination.
73+
rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN)
74+
rm, err := newResourceManager(cfg, clientcfg, log, metrics, rr, id, region, partition)
6175
if err != nil {
6276
return nil, err
6377
}

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)