Skip to content

Commit e566808

Browse files
authored
Improve handling of missing namespace in SecretKeyReference (#146)
Fixes aws-controllers-k8s/community#1699 When reconciling a resource that references a Kubernetes Secret via a `SecretKeyReference`, if the namespace field is not specified, the code previously defaulted to the "default" namespace. This commit changes that behavior to instead use the namespace of the resource being reconciled, if available in the reconcile context. Signed-off-by: Amine Hilaly <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent f67ec48 commit e566808

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

pkg/runtime/reconciler.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,18 @@ func (r *reconciler) SecretValueFromReference(
126126
}
127127

128128
namespace := ref.Namespace
129-
if namespace == "" {
130-
namespace = "default"
129+
// During the reconcile process, the resourceNamespace is stored in the context
130+
// and can be used to fetch the secret if the namespace is not provided in the
131+
// SecretKeyReference.
132+
//
133+
// NOTE(a-hilaly): When refactoring the runtime, we might want to consider passing
134+
// the ObjectMeta in the context.
135+
ctxResourceNamespace := ctx.Value("resourceNamespace")
136+
if namespace == "" && ctxResourceNamespace != nil {
137+
ctxNamespace, ok := ctxResourceNamespace.(string)
138+
if ok {
139+
namespace = ctxNamespace
140+
}
131141
}
132142

133143
nsn := client.ObjectKey{
@@ -175,6 +185,7 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
175185
// We're storing a logger pointer in the context, so that any changes to the logger
176186
// will be reflected in the context.
177187
ctx = context.WithValue(ctx, ackrtlog.ContextKey, rlog)
188+
ctx = context.WithValue(ctx, "resourceNamespace", req.Namespace)
178189

179190
// If a user has specified a namespace that is annotated with the
180191
// an owner account ID, we need an appropriate role ARN to assume

0 commit comments

Comments
 (0)