|
4 | 4 | "testing" |
5 | 5 |
|
6 | 6 | . "github.com/onsi/gomega" |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
7 | 8 | corev1 "k8s.io/api/core/v1" |
8 | 9 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
9 | 10 | ) |
@@ -151,3 +152,200 @@ func TestServiceSpecSetter_PreservesExternalAnnotations(t *testing.T) { |
151 | 152 | }) |
152 | 153 | } |
153 | 154 | } |
| 155 | + |
| 156 | +func int32Ptr(i int32) *int32 { return &i } |
| 157 | + |
| 158 | +func TestDeploymentAndDaemonSetSpecSetter(t *testing.T) { |
| 159 | + t.Parallel() |
| 160 | + |
| 161 | + type testCase struct { |
| 162 | + existingAnnotations map[string]string |
| 163 | + desiredAnnotations map[string]string |
| 164 | + expectedAnnotations map[string]string |
| 165 | + name string |
| 166 | + } |
| 167 | + |
| 168 | + tests := []testCase{ |
| 169 | + { |
| 170 | + name: "preserves external annotations while adding NGF annotations", |
| 171 | + existingAnnotations: map[string]string{ |
| 172 | + "deployment.kubernetes.io/revision": "1", |
| 173 | + "field.cattle.io/publicEndpoints": "192.61.0.19", |
| 174 | + "field.cattle.io/ports": "80/tcp", |
| 175 | + }, |
| 176 | + desiredAnnotations: map[string]string{ |
| 177 | + "custom.annotation": "from-ngf", |
| 178 | + }, |
| 179 | + expectedAnnotations: map[string]string{ |
| 180 | + "deployment.kubernetes.io/revision": "1", |
| 181 | + "field.cattle.io/publicEndpoints": "192.61.0.19", |
| 182 | + "field.cattle.io/ports": "80/tcp", |
| 183 | + "custom.annotation": "from-ngf", |
| 184 | + "gateway.nginx.org/internal-managed-annotation-keys": "custom.annotation", |
| 185 | + }, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "preserves existing NGF-managed annotations when still desired", |
| 189 | + existingAnnotations: map[string]string{ |
| 190 | + "custom.annotation": "keep-me", |
| 191 | + "argocd.argoproj.io/sync-options": "Prune=false", |
| 192 | + "gateway.nginx.org/internal-managed-annotation-keys": "custom.annotation", |
| 193 | + }, |
| 194 | + desiredAnnotations: map[string]string{ |
| 195 | + "custom.annotation": "keep-me", |
| 196 | + }, |
| 197 | + expectedAnnotations: map[string]string{ |
| 198 | + "custom.annotation": "keep-me", |
| 199 | + "argocd.argoproj.io/sync-options": "Prune=false", |
| 200 | + "gateway.nginx.org/internal-managed-annotation-keys": "custom.annotation", |
| 201 | + }, |
| 202 | + }, |
| 203 | + { |
| 204 | + name: "removes NGF-managed annotations when no longer desired", |
| 205 | + existingAnnotations: map[string]string{ |
| 206 | + "custom.annotation": "should-be-removed", |
| 207 | + "deployment.kubernetes.io/revision": "2", |
| 208 | + "gateway.nginx.org/internal-managed-annotation-keys": "custom.annotation", |
| 209 | + }, |
| 210 | + desiredAnnotations: map[string]string{}, |
| 211 | + expectedAnnotations: map[string]string{ |
| 212 | + "deployment.kubernetes.io/revision": "2", |
| 213 | + }, |
| 214 | + }, |
| 215 | + { |
| 216 | + name: "NGF annotations take precedence on conflicts", |
| 217 | + existingAnnotations: map[string]string{ |
| 218 | + "custom.annotation": "old-value", |
| 219 | + "daemonSet.kubernetes.io/revision": "7", |
| 220 | + }, |
| 221 | + desiredAnnotations: map[string]string{ |
| 222 | + "custom.annotation": "new-value", |
| 223 | + }, |
| 224 | + expectedAnnotations: map[string]string{ |
| 225 | + "custom.annotation": "new-value", |
| 226 | + "daemonSet.kubernetes.io/revision": "7", |
| 227 | + "gateway.nginx.org/internal-managed-annotation-keys": "custom.annotation", |
| 228 | + }, |
| 229 | + }, |
| 230 | + { |
| 231 | + name: "creates new deployment with annotations", |
| 232 | + existingAnnotations: nil, |
| 233 | + desiredAnnotations: map[string]string{ |
| 234 | + "custom.annotation": "value", |
| 235 | + }, |
| 236 | + expectedAnnotations: map[string]string{ |
| 237 | + "custom.annotation": "value", |
| 238 | + "gateway.nginx.org/internal-managed-annotation-keys": "custom.annotation", |
| 239 | + }, |
| 240 | + }, |
| 241 | + { |
| 242 | + name: "updates tracking annotation when managed keys change", |
| 243 | + existingAnnotations: map[string]string{ |
| 244 | + "annotation-to-keep": "keep-value", |
| 245 | + "annotation-to-remove": "remove-value", |
| 246 | + "argocd.argoproj.io/sync-options": "Validate=true", |
| 247 | + "gateway.nginx.org/internal-managed-annotation-keys": "annotation-to-keep,annotation-to-remove", |
| 248 | + }, |
| 249 | + desiredAnnotations: map[string]string{ |
| 250 | + "annotation-to-keep": "updated-keep-value", |
| 251 | + }, |
| 252 | + expectedAnnotations: map[string]string{ |
| 253 | + "annotation-to-keep": "updated-keep-value", |
| 254 | + "argocd.argoproj.io/sync-options": "Validate=true", |
| 255 | + "gateway.nginx.org/internal-managed-annotation-keys": "annotation-to-keep", |
| 256 | + }, |
| 257 | + }, |
| 258 | + } |
| 259 | + |
| 260 | + labels := map[string]string{ |
| 261 | + "app.kubernetes.io/name": "nginx-gateway-fabric", |
| 262 | + "app.kubernetes.io/instance": "nginx-gateway", |
| 263 | + } |
| 264 | + |
| 265 | + makeDesiredMeta := func(ann map[string]string) metav1.ObjectMeta { |
| 266 | + return metav1.ObjectMeta{ |
| 267 | + Labels: labels, |
| 268 | + Annotations: ann, |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + podTemplate := corev1.PodTemplateSpec{ |
| 273 | + ObjectMeta: metav1.ObjectMeta{Labels: labels}, |
| 274 | + Spec: corev1.PodSpec{ |
| 275 | + Containers: []corev1.Container{{ |
| 276 | + Name: "nginx-gateway", |
| 277 | + Image: "nginx:1.25", |
| 278 | + }}, |
| 279 | + }, |
| 280 | + } |
| 281 | + |
| 282 | + runners := []struct { |
| 283 | + run func(t *testing.T, tc testCase) |
| 284 | + name string |
| 285 | + }{ |
| 286 | + { |
| 287 | + name: "Deployment", |
| 288 | + run: func(t *testing.T, tc testCase) { |
| 289 | + t.Helper() |
| 290 | + g := NewWithT(t) |
| 291 | + |
| 292 | + existing := &appsv1.Deployment{ |
| 293 | + ObjectMeta: metav1.ObjectMeta{ |
| 294 | + Name: "nginx-gateway", |
| 295 | + Namespace: "nginx-gateway", |
| 296 | + Annotations: tc.existingAnnotations, |
| 297 | + }, |
| 298 | + } |
| 299 | + |
| 300 | + spec := appsv1.DeploymentSpec{ |
| 301 | + Replicas: int32Ptr(1), |
| 302 | + Selector: &metav1.LabelSelector{MatchLabels: labels}, |
| 303 | + Template: podTemplate, |
| 304 | + } |
| 305 | + |
| 306 | + err := deploymentSpecSetter(existing, spec, makeDesiredMeta(tc.desiredAnnotations))() |
| 307 | + g.Expect(err).ToNot(HaveOccurred()) |
| 308 | + g.Expect(existing.Annotations).To(Equal(tc.expectedAnnotations)) |
| 309 | + g.Expect(existing.Labels).To(Equal(labels)) |
| 310 | + g.Expect(existing.Spec).To(Equal(spec)) |
| 311 | + }, |
| 312 | + }, |
| 313 | + { |
| 314 | + name: "DaemonSet", |
| 315 | + run: func(t *testing.T, tc testCase) { |
| 316 | + t.Helper() |
| 317 | + g := NewWithT(t) |
| 318 | + |
| 319 | + existing := &appsv1.DaemonSet{ |
| 320 | + ObjectMeta: metav1.ObjectMeta{ |
| 321 | + Name: "nginx-gateway", |
| 322 | + Namespace: "nginx-gateway", |
| 323 | + Annotations: tc.existingAnnotations, |
| 324 | + }, |
| 325 | + } |
| 326 | + |
| 327 | + spec := appsv1.DaemonSetSpec{ |
| 328 | + Selector: &metav1.LabelSelector{MatchLabels: labels}, |
| 329 | + Template: podTemplate, |
| 330 | + } |
| 331 | + |
| 332 | + err := daemonSetSpecSetter(existing, spec, makeDesiredMeta(tc.desiredAnnotations))() |
| 333 | + g.Expect(err).ToNot(HaveOccurred()) |
| 334 | + g.Expect(existing.Annotations).To(Equal(tc.expectedAnnotations)) |
| 335 | + g.Expect(existing.Labels).To(Equal(labels)) |
| 336 | + g.Expect(existing.Spec).To(Equal(spec)) |
| 337 | + }, |
| 338 | + }, |
| 339 | + } |
| 340 | + |
| 341 | + for _, tc := range tests { |
| 342 | + t.Run(tc.name, func(t *testing.T) { |
| 343 | + t.Parallel() |
| 344 | + for _, r := range runners { |
| 345 | + t.Run(r.name, func(t *testing.T) { |
| 346 | + r.run(t, tc) |
| 347 | + }) |
| 348 | + } |
| 349 | + }) |
| 350 | + } |
| 351 | +} |
0 commit comments