Skip to content

Commit a0caf01

Browse files
authored
Add Region() to ResourceMetadata struct and AWSResourceIdentifiers interface (#79)
Currently when a resource is created without setting a region in the annotations, the controller uses either the namespace annotation or the controller region flag. This causes the controller to miss manage the resource if they are restart with a different region, or if run a second controller using a different region flag. This patch adds a new field to `ResourceMetadata` struct and `AWSResourceIdentifiers` interface to store the AWS region in which the resource is created/should be managed. This patch also updates the `reconciler.getRegion` method to lookup for a region in the status.ResourceMetadata before trying to get one from the resource/namespace annotations or the controller configuration. Addresses aws-controllers-k8s/community#1220 This patch will also require some changes to the code-generator. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 4c4d5cb commit a0caf01

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

apis/core/v1alpha1/resource_metadata.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ type ResourceMetadata struct {
3030
// OwnerAccountID is the AWS Account ID of the account that owns the
3131
// backend AWS service API resource.
3232
OwnerAccountID *AWSAccountID `json:"ownerAccountID"`
33+
// Region is the AWS region in which the resource exists or will exist.
34+
Region *AWSRegion `json:"region"`
3335
}

mocks/pkg/types/aws_resource_identifiers.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/reconciler.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -827,14 +827,23 @@ func (r *resourceReconciler) getRoleARN(
827827
return ackv1alpha1.AWSResourceName(roleARN)
828828
}
829829

830-
// getRegion returns the AWS region that the given resource is in or should be
831-
// created in. If the CR have a region associated with it, it is used. Otherwise
832-
// we look for the namespace associated region, if that is set we use it. Finally
833-
// if none of these annotations are set we use the use the region specified in the
834-
// configuration is used
830+
// getRegion returns the region the resource exists in, or if the resource
831+
// has yet to be created, the region the resource *should* be created in.
832+
//
833+
// If the resource has not yet been created, we look for the AWS region
834+
// in the following order of precedence:
835+
// - The resource's `services.k8s.aws/region` annotation, if present
836+
// - The resource's Namespace's `services.k8s.aws/region` annotation, if present
837+
// - The controller's `--aws-region` CLI flag
835838
func (r *resourceReconciler) getRegion(
836839
res acktypes.AWSResource,
837840
) ackv1alpha1.AWSRegion {
841+
// first try to get the region from the status.resourceMetadata
842+
metadataRegion := res.Identifiers().Region()
843+
if metadataRegion != nil {
844+
return *metadataRegion
845+
}
846+
838847
// look for region in CR metadata annotations
839848
resAnnotations := res.MetaObject().GetAnnotations()
840849
region, ok := resAnnotations[ackv1alpha1.AnnotationRegion]

pkg/types/aws_resource_identifiers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ type AWSResourceIdentifiers interface {
2727
// this means the resource has not yet been created in the backend AWS
2828
// service.
2929
ARN() *ackv1alpha1.AWSResourceName
30+
// Region is the AWS region in which the resource exists or will exist.
31+
Region() *ackv1alpha1.AWSRegion
3032
}

0 commit comments

Comments
 (0)