Skip to content

Commit b8985c1

Browse files
committed
fix: address review comments on cross-namespace PR
- Use nil-safe targetName variable in field export log line (comment #2) - Use lookup-or-create pattern in setCrossNsOptInRequiredCondition to avoid duplicate conditions (comment #3) - Return ownerNamespace instead of empty string on error in ValidateCrossNamespaceReference for defensive fail-closed behavior (comment #4) - Update tests to match new return value on error path
1 parent 4eb656a commit b8985c1

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

pkg/runtime/field_export_reconciler.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,17 @@ func (r *fieldExportReconciler) Sync(
159159
return desired, r.onError(ctx, &desired, ackerr.NewTerminalError(nsErr))
160160
}
161161
if isCrossNamespace {
162+
targetName := ""
163+
if desired.Spec.To != nil && desired.Spec.To.Name != nil {
164+
targetName = *desired.Spec.To.Name
165+
}
162166
r.log.V(0).Info(
163167
"cross-namespace field export detected; this behavior will be "+
164168
"disabled by default in a future release. Set --enable-cross-namespace "+
165169
"to preserve this behavior.",
166170
"fieldExportNamespace", desired.Namespace,
167-
"targetNamespace", r.getTargetNamespace(&desired),
168-
"targetName", *desired.Spec.To.Name,
171+
"targetNamespace", resolvedNamespace,
172+
"targetName", targetName,
169173
)
170174
r.setCrossNsOptInRequiredCondition(&desired)
171175
}
@@ -235,6 +239,14 @@ func (r *fieldExportReconciler) setCrossNsOptInRequiredCondition(
235239
desired.Namespace + "\" targets namespace \"" + r.getTargetNamespace(desired) +
236240
"\". Cross-namespace behavior will require explicit opt-in in a future release. " +
237241
"Set --enable-cross-namespace=true to preserve this behavior."
242+
// Use lookup-or-create pattern to avoid duplicate conditions
243+
for i, c := range desired.Status.Conditions {
244+
if c.Type == ackv1alpha1.ConditionTypeCrossNamespaceOptInRequired {
245+
desired.Status.Conditions[i].Status = corev1.ConditionTrue
246+
desired.Status.Conditions[i].Message = &message
247+
return
248+
}
249+
}
238250
condition := &ackv1alpha1.Condition{
239251
Type: ackv1alpha1.ConditionTypeCrossNamespaceOptInRequired,
240252
Status: corev1.ConditionTrue,

pkg/runtime/resource_reference.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func ValidateCrossNamespaceReference(
5858
if enableCrossNamespace {
5959
return *refNamespace, true, nil
6060
}
61-
return "", false, ackerr.ResourceReferenceCrossNamespaceNotAllowedFor(
61+
return ownerNamespace, false, ackerr.ResourceReferenceCrossNamespaceNotAllowedFor(
6262
ownerNamespace, *refNamespace, refName,
6363
)
6464
}

pkg/runtime/resource_reference_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestValidateCrossNamespaceReference(t *testing.T) {
104104
name: "different namespace, flag disabled",
105105
flag: false,
106106
refNs: strPtr(otherNs),
107-
expectedNs: "",
107+
expectedNs: ownerNs,
108108
expectedCrossNs: false,
109109
expectErr: true,
110110
},
@@ -211,7 +211,7 @@ func TestValidateCrossNamespaceReferenceString(t *testing.T) {
211211
name: "different namespace string, flag disabled",
212212
flag: false,
213213
refNs: otherNs,
214-
expectedNs: "",
214+
expectedNs: ownerNs,
215215
expectedCrossNs: false,
216216
expectErr: true,
217217
},

0 commit comments

Comments
 (0)