Skip to content

Commit 9d3a4aa

Browse files
scotwellsclaude
andcommitted
refactor(controller): centralize the companion ResourceBinding name codec
The "-configmap"/"-secret" suffix that maps a companion object to its Karmada ResourceBinding name was open-coded across the GC, orphan-sweep, and resolver paths. Add companionRBName as the inverse of companionFromRBName and share the suffix constants so the build and parse sides cannot drift. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7aa906a commit 9d3a4aa

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

internal/controller/companion_gc_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ func (r *CompanionGCReconciler) Reconcile(ctx context.Context, req ctrl.Request)
131131
if err := writer.DeleteConfigMap(ctx, req.Namespace, req.Name); err != nil {
132132
return ctrl.Result{}, fmt.Errorf("companion-gc: delete ConfigMap %q: %w", req.Name, err)
133133
}
134-
if err := writer.DeleteResourceBinding(ctx, req.Namespace, req.Name+"-configmap"); err != nil {
134+
if err := writer.DeleteResourceBinding(ctx, req.Namespace, companionRBName(req.Name, kindConfigMap)); err != nil {
135135
return ctrl.Result{}, fmt.Errorf("companion-gc: delete ConfigMap RB %q: %w", req.Name, err)
136136
}
137137
case kindSecret:
138138
if err := writer.DeleteSecret(ctx, req.Namespace, req.Name); err != nil {
139139
return ctrl.Result{}, fmt.Errorf("companion-gc: delete Secret %q: %w", req.Name, err)
140140
}
141-
if err := writer.DeleteResourceBinding(ctx, req.Namespace, req.Name+"-secret"); err != nil {
141+
if err := writer.DeleteResourceBinding(ctx, req.Namespace, companionRBName(req.Name, kindSecret)); err != nil {
142142
return ctrl.Result{}, fmt.Errorf("companion-gc: delete Secret RB %q: %w", req.Name, err)
143143
}
144144
}

internal/controller/orphan_rb_controller.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (r *OrphanRBReconciler) isInScope(rb *karmadaworkv1alpha2.ResourceBinding)
127127
return false
128128
}
129129
name := rb.Name
130-
return strings.HasSuffix(name, "-configmap") || strings.HasSuffix(name, "-secret")
130+
return strings.HasSuffix(name, rbSuffixConfigMap) || strings.HasSuffix(name, rbSuffixSecret)
131131
}
132132

133133
// companionFromRBName extracts the companion object name and kind from a
@@ -139,15 +139,24 @@ func (r *OrphanRBReconciler) isInScope(rb *karmadaworkv1alpha2.ResourceBinding)
139139
// "wd-foo-workloaddeployment" → ("", "", false) // not a companion RB
140140
func companionFromRBName(rbName string) (companionName, kind string, ok bool) {
141141
switch {
142-
case strings.HasSuffix(rbName, "-configmap"):
143-
return strings.TrimSuffix(rbName, "-configmap"), kindConfigMap, true
144-
case strings.HasSuffix(rbName, "-secret"):
145-
return strings.TrimSuffix(rbName, "-secret"), kindSecret, true
142+
case strings.HasSuffix(rbName, rbSuffixConfigMap):
143+
return strings.TrimSuffix(rbName, rbSuffixConfigMap), kindConfigMap, true
144+
case strings.HasSuffix(rbName, rbSuffixSecret):
145+
return strings.TrimSuffix(rbName, rbSuffixSecret), kindSecret, true
146146
default:
147147
return "", "", false
148148
}
149149
}
150150

151+
// companionRBName returns the Karmada ResourceBinding name for a companion object
152+
// of the given kind. It is the inverse of companionFromRBName.
153+
func companionRBName(objectName, kind string) string {
154+
if kind == kindSecret {
155+
return objectName + rbSuffixSecret
156+
}
157+
return objectName + rbSuffixConfigMap
158+
}
159+
151160
// isOrphaned returns true when the hub companion is fully absent. A companion
152161
// with a deletionTimestamp is considered NOT orphaned — the deletion is in
153162
// progress and Karmada's cascade (or Component 3's explicit RB delete) is
@@ -196,7 +205,7 @@ func (r *OrphanRBReconciler) SetupWithManager(mgr manager.Manager) error {
196205
return false
197206
}
198207
name := obj.GetName()
199-
return strings.HasSuffix(name, "-configmap") || strings.HasSuffix(name, "-secret")
208+
return strings.HasSuffix(name, rbSuffixConfigMap) || strings.HasSuffix(name, rbSuffixSecret)
200209
})),
201210
).
202211
Named("orphan-rb").

internal/controller/referenceddata_controller.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ const (
6363
// referenceddata.ObjectRef to avoid repeated string literals.
6464
kindConfigMap = "ConfigMap"
6565
kindSecret = "Secret"
66+
67+
// rbSuffixConfigMap and rbSuffixSecret are the companion ResourceBinding name
68+
// suffixes. Karmada names namespace-scoped RBs "{objectName}-{kindLowercase}";
69+
// companionRBName builds these and companionFromRBName parses them, so the wire
70+
// format has one source of truth.
71+
rbSuffixConfigMap = "-configmap"
72+
rbSuffixSecret = "-secret"
6673
)
6774

6875
// companionWriter is the abstraction that the controller uses to materialise
@@ -1162,7 +1169,7 @@ func (r *ReferencedDataController) releaseOneCompanion(
11621169
// "{companionName}-configmap". IgnoreNotFound because Karmada may have
11631170
// already cascaded the deletion.
11641171
if cmDeleted {
1165-
if err := writer.DeleteResourceBinding(ctx, namespace, companionName+"-configmap"); err != nil {
1172+
if err := writer.DeleteResourceBinding(ctx, namespace, companionRBName(companionName, kindConfigMap)); err != nil {
11661173
return fmt.Errorf("referenceddata: delete ResourceBinding for ConfigMap %q: %w", companionName, err)
11671174
}
11681175
}
@@ -1205,7 +1212,7 @@ func (r *ReferencedDataController) releaseOneCompanion(
12051212
// The RB name follows the Karmada binding-controller convention:
12061213
// "{companionName}-secret".
12071214
if secretDeleted {
1208-
if err := writer.DeleteResourceBinding(ctx, namespace, companionName+"-secret"); err != nil {
1215+
if err := writer.DeleteResourceBinding(ctx, namespace, companionRBName(companionName, kindSecret)); err != nil {
12091216
return fmt.Errorf("referenceddata: delete ResourceBinding for Secret %q: %w", companionName, err)
12101217
}
12111218
}

0 commit comments

Comments
 (0)