Skip to content

feat: Store resource partition in Status #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions templates/pkg/resource/identifiers.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ func (ri *resourceIdentifiers) Region() *ackv1alpha1.AWSRegion {
}
return nil
}

// Partition returns the AWS partition in which the reosurce exists, or
// nil if this information is not known.
func (ri *resourceIdentifiers) Partition() *ackv1alpha1.AWSPartition {
if ri.meta != nil {
return ri.meta.Partition
}
return nil
}
8 changes: 6 additions & 2 deletions templates/pkg/resource/manager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type resourceManager struct {
awsAccountID ackv1alpha1.AWSAccountID
// The AWS Region that this resource manager targets
awsRegion ackv1alpha1.AWSRegion
// The AWS Partition that this resource manager targets
awsPartition ackv1alpha1.AWSPartition
// sdk is a pointer to the AWS service API client exposed by the
// aws-sdk-go-v2/services/{alias} package.
sdkapi *svcsdk.Client
Expand Down Expand Up @@ -180,13 +182,13 @@ func (rm *resourceManager) Delete(
// name for the resource
func (rm *resourceManager) ARNFromName(name string) string {
return fmt.Sprintf(
"arn:aws:{{ .ControllerName }}:%s:%s:%s",
"arn:%s:{{ .ControllerName }}:%s:%s:%s",
rm.awsPartition,
rm.awsRegion,
rm.awsAccountID,
name,
)
}

// LateInitialize returns an acktypes.AWSResource after setting the late initialized
// fields from the readOne call. This method will initialize the optional fields
// which were not provided by the k8s user but were defaulted by the AWS service.
Expand Down Expand Up @@ -413,6 +415,7 @@ func newResourceManager(
rr acktypes.Reconciler,
id ackv1alpha1.AWSAccountID,
region ackv1alpha1.AWSRegion,
partition ackv1alpha1.AWSPartition,
) (*resourceManager, error) {
return &resourceManager{
cfg: cfg,
Expand All @@ -422,6 +425,7 @@ func newResourceManager(
rr: rr,
awsAccountID: id,
awsRegion: region,
awsPartition: partition,
sdkapi: svcsdk.NewFromConfig(clientcfg),
}, nil
}
Expand Down
40 changes: 27 additions & 13 deletions templates/pkg/resource/manager_factory.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,48 @@ func (f *resourceManagerFactory) ResourceDescriptor() acktypes.AWSResourceDescri
return &resourceDescriptor{}
}

// ManagerFor returns a resource manager object that can manage resources for a
// supplied AWS account
func (f *resourceManagerFactory) ManagerFor(
cfg ackcfg.Config,
clientcfg aws.Config,
log logr.Logger,
metrics *ackmetrics.Metrics,
rr acktypes.Reconciler,
// GetCachedManager returns a manager object that can manage resources for a
// supplied AWS account if it was already created and cached, or nil if not
func (f *resourceManagerFactory) GetCachedManager(
id ackv1alpha1.AWSAccountID,
region ackv1alpha1.AWSRegion,
roleARN ackv1alpha1.AWSResourceName,
) (acktypes.AWSResourceManager, error) {
) acktypes.AWSResourceManager {
// We use the account ID, region, and role ARN to uniquely identify a
// resource manager. This helps us to avoid creating multiple resource
// managers for the same account/region/roleARN combination.
rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN)
f.RLock()
rm, found := f.rmCache[rmId]
f.RUnlock()

if found {
return rm, nil
if !found {
return nil
}

return rm
}

// ManagerFor returns a resource manager object that can manage resources for a
// supplied AWS account
func (f *resourceManagerFactory) ManagerFor(
cfg ackcfg.Config,
clientcfg aws.Config,
log logr.Logger,
metrics *ackmetrics.Metrics,
rr acktypes.Reconciler,
id ackv1alpha1.AWSAccountID,
region ackv1alpha1.AWSRegion,
partition ackv1alpha1.AWSPartition,
roleARN ackv1alpha1.AWSResourceName,
) (acktypes.AWSResourceManager, error) {
f.Lock()
defer f.Unlock()

rm, err := newResourceManager(cfg, clientcfg, log, metrics, rr, id, region)
// We use the account ID, region, partition, and role ARN to uniquely identify a
// resource manager. This helps us to avoid creating multiple resource
// managers for the same account/region/roleARN combination.
rmId := fmt.Sprintf("%s/%s/%s", id, region, roleARN)
rm, err := newResourceManager(cfg, clientcfg, log, metrics, rr, id, region, partition)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions templates/pkg/resource/sdk.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ func (rm *resourceManager) setStatusDefaults (
if ko.Status.ACKResourceMetadata.Region == nil {
ko.Status.ACKResourceMetadata.Region = &rm.awsRegion
}
if ko.Status.ACKResourceMetadata.Partition == nil {
ko.Status.ACKResourceMetadata.Partition = &rm.awsPartition
}
if ko.Status.ACKResourceMetadata.OwnerAccountID == nil {
ko.Status.ACKResourceMetadata.OwnerAccountID = &rm.awsAccountID
}
Expand Down