|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | appsv1 "k8s.io/api/apps/v1" |
| 8 | + corev1 "k8s.io/api/core/v1" |
8 | 9 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
9 | 10 | "sigs.k8s.io/controller-runtime/pkg/client" |
10 | 11 | "sigs.k8s.io/controller-runtime/pkg/client/fake" |
@@ -99,3 +100,116 @@ func TestReconcileDeploymentKeepsRuntimePolicyLabelsOutOfSelector(t *testing.T) |
99 | 100 | t.Fatalf("expected runtime policy label on pod template, got %#v", deployment.Spec.Template.Labels) |
100 | 101 | } |
101 | 102 | } |
| 103 | + |
| 104 | +func TestReconcileDeploymentPreservesExistingSelectorOnUpgrade(t *testing.T) { |
| 105 | + scheme := newControllerTestScheme(t) |
| 106 | + oldSelector := map[string]string{ |
| 107 | + "spritz.sh/name": "tidy-otter", |
| 108 | + ownerLabelKey: ownerLabelValue("user-1"), |
| 109 | + } |
| 110 | + spritz := &spritzv1.Spritz{ |
| 111 | + ObjectMeta: metav1.ObjectMeta{Name: "tidy-otter", Namespace: "spritz-test"}, |
| 112 | + Spec: spritzv1.SpritzSpec{ |
| 113 | + Image: "example.com/openclaw:latest", |
| 114 | + Owner: spritzv1.SpritzOwner{ID: "user-1"}, |
| 115 | + RuntimePolicy: &spritzv1.SpritzRuntimePolicy{ |
| 116 | + NetworkProfile: "dev-cluster-only", |
| 117 | + MountProfile: "dev-default", |
| 118 | + ExposureProfile: "internal-acp", |
| 119 | + Revision: "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + existingDeployment := &appsv1.Deployment{ |
| 124 | + ObjectMeta: metav1.ObjectMeta{Name: spritz.Name, Namespace: spritz.Namespace}, |
| 125 | + Spec: appsv1.DeploymentSpec{ |
| 126 | + Selector: &metav1.LabelSelector{MatchLabels: oldSelector}, |
| 127 | + Template: corev1.PodTemplateSpec{ |
| 128 | + ObjectMeta: metav1.ObjectMeta{Labels: oldSelector}, |
| 129 | + Spec: corev1.PodSpec{ |
| 130 | + Containers: []corev1.Container{ |
| 131 | + {Name: spritzContainerName, Image: spritz.Spec.Image}, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + k8sClient := fake.NewClientBuilder(). |
| 138 | + WithScheme(scheme). |
| 139 | + WithObjects(spritz, existingDeployment). |
| 140 | + Build() |
| 141 | + reconciler := &SpritzReconciler{ |
| 142 | + Client: k8sClient, |
| 143 | + Scheme: scheme, |
| 144 | + } |
| 145 | + |
| 146 | + if err := reconciler.reconcileDeployment(context.Background(), spritz); err != nil { |
| 147 | + t.Fatalf("reconcileDeployment returned error: %v", err) |
| 148 | + } |
| 149 | + |
| 150 | + deployment := &appsv1.Deployment{} |
| 151 | + if err := k8sClient.Get( |
| 152 | + context.Background(), |
| 153 | + client.ObjectKey{Name: spritz.Name, Namespace: spritz.Namespace}, |
| 154 | + deployment, |
| 155 | + ); err != nil { |
| 156 | + t.Fatalf("failed to load deployment: %v", err) |
| 157 | + } |
| 158 | + if deployment.Spec.Selector == nil { |
| 159 | + t.Fatal("expected deployment selector") |
| 160 | + } |
| 161 | + if deployment.Spec.Selector.MatchLabels["spritz.sh/name"] != spritz.Name { |
| 162 | + t.Fatalf("expected existing deployment selector to keep spritz name, got %#v", deployment.Spec.Selector.MatchLabels) |
| 163 | + } |
| 164 | + if deployment.Spec.Selector.MatchLabels[ownerLabelKey] != ownerLabelValue("user-1") { |
| 165 | + t.Fatalf("expected existing deployment selector to keep owner label, got %#v", deployment.Spec.Selector.MatchLabels) |
| 166 | + } |
| 167 | + if deployment.Spec.Template.Labels[runtimeNetworkProfileLabelKey] != "dev-cluster-only" { |
| 168 | + t.Fatalf("expected runtime policy label on pod template, got %#v", deployment.Spec.Template.Labels) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestReconcileServiceKeepsRuntimePolicyLabelsOutOfSelector(t *testing.T) { |
| 173 | + scheme := newControllerTestScheme(t) |
| 174 | + spritz := &spritzv1.Spritz{ |
| 175 | + ObjectMeta: metav1.ObjectMeta{Name: "tidy-otter", Namespace: "spritz-test"}, |
| 176 | + Spec: spritzv1.SpritzSpec{ |
| 177 | + Image: "example.com/openclaw:latest", |
| 178 | + Owner: spritzv1.SpritzOwner{ID: "user-1"}, |
| 179 | + RuntimePolicy: &spritzv1.SpritzRuntimePolicy{ |
| 180 | + NetworkProfile: "dev-cluster-only", |
| 181 | + MountProfile: "dev-default", |
| 182 | + ExposureProfile: "internal-acp", |
| 183 | + Revision: "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 184 | + }, |
| 185 | + }, |
| 186 | + } |
| 187 | + k8sClient := fake.NewClientBuilder(). |
| 188 | + WithScheme(scheme). |
| 189 | + WithObjects(spritz). |
| 190 | + Build() |
| 191 | + reconciler := &SpritzReconciler{ |
| 192 | + Client: k8sClient, |
| 193 | + Scheme: scheme, |
| 194 | + } |
| 195 | + |
| 196 | + if err := reconciler.reconcileService(context.Background(), spritz); err != nil { |
| 197 | + t.Fatalf("reconcileService returned error: %v", err) |
| 198 | + } |
| 199 | + |
| 200 | + service := &corev1.Service{} |
| 201 | + if err := k8sClient.Get( |
| 202 | + context.Background(), |
| 203 | + client.ObjectKey{Name: spritz.Name, Namespace: spritz.Namespace}, |
| 204 | + service, |
| 205 | + ); err != nil { |
| 206 | + t.Fatalf("failed to load service: %v", err) |
| 207 | + } |
| 208 | + if len(service.Spec.Selector) != 1 || |
| 209 | + service.Spec.Selector["spritz.sh/name"] != spritz.Name { |
| 210 | + t.Fatalf("expected stable service selector, got %#v", service.Spec.Selector) |
| 211 | + } |
| 212 | + if _, ok := service.Spec.Selector[runtimeNetworkProfileLabelKey]; ok { |
| 213 | + t.Fatalf("service selector must not depend on runtime policy labels: %#v", service.Spec.Selector) |
| 214 | + } |
| 215 | +} |
0 commit comments