diff --git a/controllers/sandbox_controller.go b/controllers/sandbox_controller.go index f6d43d457..7f5626845 100644 --- a/controllers/sandbox_controller.go +++ b/controllers/sandbox_controller.go @@ -451,6 +451,9 @@ func (r *SandboxReconciler) reconcilePod(ctx context.Context, sandbox *sandboxv1 if pod.Labels == nil { pod.Labels = make(map[string]string) } + if pod.Annotations == nil { + pod.Annotations = make(map[string]string) + } changed := false if pod.Labels[sandboxLabel] != nameHash { pod.Labels[sandboxLabel] = nameHash @@ -463,6 +466,12 @@ func (r *SandboxReconciler) reconcilePod(ctx context.Context, sandbox *sandboxv1 changed = true } } + for k, v := range sandbox.Spec.PodTemplate.ObjectMeta.Annotations { + if pod.Annotations[k] != v { + pod.Annotations[k] = v + changed = true + } + } // Set controller reference if the pod is not controlled by anything. if controllerRef := metav1.GetControllerOf(pod); controllerRef == nil { diff --git a/controllers/sandbox_controller_test.go b/controllers/sandbox_controller_test.go index a9209707a..eac8780ae 100644 --- a/controllers/sandbox_controller_test.go +++ b/controllers/sandbox_controller_test.go @@ -620,6 +620,9 @@ func TestReconcilePod(t *testing.T) { "agents.x-k8s.io/sandbox-name-hash": nameHash, "custom-label": "label-val", }, + Annotations: map[string]string{ + "custom-annotation": "anno-val", + }, OwnerReferences: []metav1.OwnerReference{sandboxControllerRef(sandboxName)}, }, Spec: corev1.PodSpec{ @@ -699,7 +702,7 @@ func TestReconcilePod(t *testing.T) { wantPod: nil, }, { - name: "adopts existing pod via annotation - pod gets label and owner reference", + name: "adopts existing pod via annotation - pod gets metadata and owner reference", initialObjs: []runtime.Object{ &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -727,6 +730,11 @@ func TestReconcilePod(t *testing.T) { Spec: sandboxv1alpha1.SandboxSpec{ Replicas: ptr.To(int32(1)), PodTemplate: sandboxv1alpha1.PodTemplate{ + ObjectMeta: sandboxv1alpha1.PodMetadata{ + Annotations: map[string]string{ + "example.com/workspace": "true", + }, + }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ { @@ -745,6 +753,9 @@ func TestReconcilePod(t *testing.T) { Labels: map[string]string{ sandboxLabel: nameHash, }, + Annotations: map[string]string{ + "example.com/workspace": "true", + }, OwnerReferences: []metav1.OwnerReference{sandboxControllerRef(sandboxName)}, }, Spec: corev1.PodSpec{ @@ -797,6 +808,9 @@ func TestReconcilePod(t *testing.T) { "agents.x-k8s.io/sandbox-name-hash": nameHash, "custom-label": "label-val", }, + Annotations: map[string]string{ + "custom-annotation": "anno-val", + }, // Should still have the original controller reference OwnerReferences: []metav1.OwnerReference{ {