Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions controllers/sandbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion controllers/sandbox_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
{
Expand Down