|
| 1 | +// Copyright 2025 The Kubernetes Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controllers |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + policyv1 "k8s.io/api/policy/v1" |
| 24 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + sandboxv1alpha1 "sigs.k8s.io/agent-sandbox/api/v1alpha1" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 30 | +) |
| 31 | + |
| 32 | +// TestSandboxPolicyReconciler_Reconcile covers creation and no-op scenarios. |
| 33 | +func TestSandboxPolicyReconciler_Reconcile(t *testing.T) { |
| 34 | + sandboxName := "test-sandbox" |
| 35 | + sandboxNs := "default" |
| 36 | + sandboxKey := types.NamespacedName{Name: sandboxName, Namespace: sandboxNs} |
| 37 | + |
| 38 | + // A pod that would be created by the main sandbox-controller |
| 39 | + existingPod := &corev1.Pod{ |
| 40 | + ObjectMeta: metav1.ObjectMeta{ |
| 41 | + Name: sandboxName, |
| 42 | + Namespace: sandboxNs, |
| 43 | + Labels: map[string]string{ |
| 44 | + // Assume this label is present for the selector to match |
| 45 | + sandboxLabel: NameHash(sandboxName), |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + testCases := []struct { |
| 51 | + name string |
| 52 | + initialSandbox *sandboxv1alpha1.Sandbox |
| 53 | + expectPDB bool |
| 54 | + expectAnnotation bool |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "should create PDB and add annotation when annotation is present", |
| 58 | + initialSandbox: &sandboxv1alpha1.Sandbox{ |
| 59 | + ObjectMeta: metav1.ObjectMeta{ |
| 60 | + Name: sandboxName, |
| 61 | + Namespace: sandboxNs, |
| 62 | + Annotations: map[string]string{ |
| 63 | + sandboxv1alpha1.PDBRequiredAnnotation: "true", |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + expectPDB: true, |
| 68 | + expectAnnotation: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "should do nothing when annotation is absent", |
| 72 | + initialSandbox: &sandboxv1alpha1.Sandbox{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{ |
| 74 | + Name: sandboxName, |
| 75 | + Namespace: sandboxNs, |
| 76 | + }, |
| 77 | + }, |
| 78 | + expectPDB: false, |
| 79 | + expectAnnotation: false, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tc := range testCases { |
| 84 | + t.Run(tc.name, func(t *testing.T) { |
| 85 | + fakeClient := fake.NewClientBuilder(). |
| 86 | + WithScheme(Scheme). |
| 87 | + WithRuntimeObjects(tc.initialSandbox.DeepCopy(), existingPod.DeepCopy()). |
| 88 | + Build() |
| 89 | + |
| 90 | + r := &SandboxPolicyReconciler{Client: fakeClient} |
| 91 | + req := ctrl.Request{NamespacedName: sandboxKey} |
| 92 | + |
| 93 | + _, err := r.Reconcile(context.Background(), req) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + // Check PDB state |
| 97 | + pdb := &policyv1.PodDisruptionBudget{} |
| 98 | + err = r.Get(context.Background(), sandboxKey, pdb) |
| 99 | + if tc.expectPDB { |
| 100 | + require.NoError(t, err, "Expected PDB to be created") |
| 101 | + } else { |
| 102 | + require.True(t, k8serrors.IsNotFound(err), "Expected PDB to not exist") |
| 103 | + } |
| 104 | + |
| 105 | + // Check Pod annotation state |
| 106 | + pod := &corev1.Pod{} |
| 107 | + err = r.Get(context.Background(), sandboxKey, pod) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + _, hasAnnotation := pod.Annotations[safeToEvictAnnotation] |
| 111 | + require.Equal(t, tc.expectAnnotation, hasAnnotation) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// TestSandboxPolicyReconciler_Cleanup tests the update/cleanup scenario. |
| 117 | +func TestSandboxPolicyReconciler_Cleanup(t *testing.T) { |
| 118 | + sandboxName := "cleanup-sandbox" |
| 119 | + sandboxNs := "default" |
| 120 | + sandboxKey := types.NamespacedName{Name: sandboxName, Namespace: sandboxNs} |
| 121 | + |
| 122 | + initialSandbox := &sandboxv1alpha1.Sandbox{ |
| 123 | + ObjectMeta: metav1.ObjectMeta{ |
| 124 | + Name: sandboxName, |
| 125 | + Namespace: sandboxNs, |
| 126 | + Annotations: map[string]string{ |
| 127 | + sandboxv1alpha1.PDBRequiredAnnotation: "true", |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + initialPod := &corev1.Pod{ |
| 132 | + ObjectMeta: metav1.ObjectMeta{ |
| 133 | + Name: sandboxName, |
| 134 | + Namespace: sandboxNs, |
| 135 | + Labels: map[string]string{sandboxLabel: NameHash(sandboxName)}, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + fakeClient := fake.NewClientBuilder(). |
| 140 | + WithScheme(Scheme). |
| 141 | + WithRuntimeObjects(initialSandbox, initialPod). |
| 142 | + Build() |
| 143 | + |
| 144 | + r := &SandboxPolicyReconciler{Client: fakeClient} |
| 145 | + req := ctrl.Request{NamespacedName: sandboxKey} |
| 146 | + |
| 147 | + // First reconcile: Create the PDB and add annotation |
| 148 | + _, err := r.Reconcile(context.Background(), req) |
| 149 | + require.NoError(t, err) |
| 150 | + |
| 151 | + // Verify PDB was created |
| 152 | + pdb := &policyv1.PodDisruptionBudget{} |
| 153 | + require.NoError(t, r.Get(context.Background(), sandboxKey, pdb), "PDB should exist after first reconcile") |
| 154 | + |
| 155 | + // Verify Pod has annotation |
| 156 | + pod := &corev1.Pod{} |
| 157 | + require.NoError(t, r.Get(context.Background(), sandboxKey, pod)) |
| 158 | + require.Equal(t, "false", pod.Annotations[safeToEvictAnnotation]) |
| 159 | + |
| 160 | + // Update the Sandbox to remove the annotation |
| 161 | + updatedSandbox := &sandboxv1alpha1.Sandbox{} |
| 162 | + require.NoError(t, r.Get(context.Background(), sandboxKey, updatedSandbox)) |
| 163 | + delete(updatedSandbox.Annotations, sandboxv1alpha1.PDBRequiredAnnotation) |
| 164 | + require.NoError(t, r.Update(context.Background(), updatedSandbox)) |
| 165 | + |
| 166 | + // Second reconcile: Should clean up the PDB and annotation |
| 167 | + _, err = r.Reconcile(context.Background(), req) |
| 168 | + require.NoError(t, err) |
| 169 | + |
| 170 | + // Verify PDB was deleted |
| 171 | + err = r.Get(context.Background(), sandboxKey, pdb) |
| 172 | + require.True(t, k8serrors.IsNotFound(err), "PDB should be deleted after annotation is removed") |
| 173 | + |
| 174 | + // Verify Pod annotation was removed |
| 175 | + require.NoError(t, r.Get(context.Background(), sandboxKey, pod)) |
| 176 | + _, hasAnnotation := pod.Annotations[safeToEvictAnnotation] |
| 177 | + require.False(t, hasAnnotation, "Pod annotation should be removed") |
| 178 | +} |
0 commit comments