Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion controllers/sandbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,29 @@ func (r *SandboxReconciler) reconcileService(ctx context.Context, sandbox *sandb
}

case resourceOwnedBySandbox:
// Already owned by this sandbox — no action needed.
desiredSelector := map[string]string{
sandboxLabel: nameHash,
}
needsUpdate := false

if service.Labels == nil {
service.Labels = make(map[string]string)
}
if service.Labels[sandboxLabel] != nameHash {
service.Labels[sandboxLabel] = nameHash
needsUpdate = true
}
if !reflect.DeepEqual(service.Spec.Selector, desiredSelector) {
service.Spec.Selector = desiredSelector
needsUpdate = true
}

if needsUpdate {
log.Info("Reconciling owned service drift", "Service.Name", service.Name, "Sandbox.Name", sandbox.Name)
Comment thread
rayowang marked this conversation as resolved.
Outdated
if err := r.Update(ctx, service); err != nil {
Comment thread
rayowang marked this conversation as resolved.
Outdated
return nil, fmt.Errorf("failed to update owned service: %w", err)
}
}
}

setServiceStatus(sandbox, service)
Expand Down
42 changes: 42 additions & 0 deletions controllers/sandbox_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,48 @@ func TestReconcileService(t *testing.T) {
wantStatusService: sandboxName,
wantStatusServiceFQDN: sandboxName + "." + sandboxNs + ".svc.cluster.local",
},
{
name: "repairs selector and label drift on service owned by this sandbox",
initialObjs: []runtime.Object{
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: sandboxName,
Namespace: sandboxNs,
ResourceVersion: "1",
Labels: map[string]string{
"keep": "me",
},
OwnerReferences: []metav1.OwnerReference{sandboxControllerRef(sandboxName)},
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": "something-else",
},
},
},
},
sandbox: sandboxObj,
wantService: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: sandboxName,
Namespace: sandboxNs,
ResourceVersion: "2",
Labels: map[string]string{
"keep": "me",
sandboxLabel: nameHash,
},
OwnerReferences: []metav1.OwnerReference{sandboxControllerRef(sandboxName)},
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
sandboxLabel: nameHash,
},
},
},
wantStatusService: sandboxName,
wantStatusServiceFQDN: sandboxName + "." + sandboxNs + ".svc.cluster.local",
},

{
name: "refuses to use service owned by a different controller",
initialObjs: []runtime.Object{
Expand Down