-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontroller_integration_test.go
More file actions
529 lines (475 loc) · 17.8 KB
/
controller_integration_test.go
File metadata and controls
529 lines (475 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//go:build integration_test
package controller
import (
"context"
"fmt"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/github/deployment-tracker/internal/metadata"
"github.com/github/deployment-tracker/pkg/deploymentrecord"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
k8smetadata "k8s.io/client-go/metadata"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)
type mockRecordPoster struct {
mu sync.Mutex
records []*deploymentrecord.DeploymentRecord
err error // to simulate failures
}
func (m *mockRecordPoster) PostOne(_ context.Context, record *deploymentrecord.DeploymentRecord) error {
m.mu.Lock()
defer m.mu.Unlock()
m.records = append(m.records, record)
return m.err
}
// Helper that allows tests to read captured records safely.
func (m *mockRecordPoster) getRecords() []*deploymentrecord.DeploymentRecord {
m.mu.Lock()
defer m.mu.Unlock()
return slices.Clone(m.records)
}
const testControllerNamespace = "test-controller-ns"
func setup(t *testing.T, onlyNamespace string, excludeNamespaces string) (*kubernetes.Clientset, *mockRecordPoster) {
t.Helper()
testEnv := &envtest.Environment{}
cfg, err := testEnv.Start()
if err != nil {
t.Fatalf("failed to start test environment: %v", err)
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
t.Fatalf("failed to create Kubernetes clientset: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
_ = testEnv.Stop()
})
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testControllerNamespace}}
_, err = clientset.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create namespace: %v", err)
}
if onlyNamespace != "" {
ns = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: onlyNamespace}}
_, err = clientset.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create onlyNamespace: %v", err)
}
}
if excludeNamespaces != "" {
for _, nsName := range strings.Split(excludeNamespaces, ",") {
ns = &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: nsName}}
_, err = clientset.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create excludeNamespace %s: %v", nsName, err)
}
}
}
metadataClient, err := k8smetadata.NewForConfig(cfg)
if err != nil {
t.Fatalf("failed to create Kubernetes metadata client: %v", err)
}
metadataAggregator := metadata.NewAggregator(metadataClient)
ctrl, err := New(
clientset,
metadataAggregator,
onlyNamespace,
excludeNamespaces,
&Config{
Template: "{{namespace}}/{{deploymentName}}/{{containerName}}",
LogicalEnvironment: "test-logical-env",
PhysicalEnvironment: "test-physical-env",
Cluster: "test-cluster",
Organization: "test-org",
},
)
if err != nil {
t.Fatalf("failed to create controller: %v", err)
}
mockDeploymentRecordPoster := &mockRecordPoster{}
ctrl.apiClient = mockDeploymentRecordPoster
go func() {
_ = ctrl.Run(ctx, 1)
}()
if !cache.WaitForCacheSync(ctx.Done(), ctrl.podInformer.HasSynced, ctrl.deploymentInformer.HasSynced) {
t.Fatal("timed out waiting for informer cache to sync")
}
return clientset, mockDeploymentRecordPoster
}
func makeDeployment(t *testing.T, clientset *kubernetes.Clientset, owners []metav1.OwnerReference, namespace, name string) *appsv1.Deployment {
t.Helper()
ctx := context.Background()
labels := map[string]string{"app": name}
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
OwnerReferences: owners,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "app", Image: "nginx:latest"}},
},
},
},
}
d, err := clientset.AppsV1().Deployments(namespace).Create(ctx, deployment, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create Deployment: %v", err)
}
return d
}
func makeReplicaSet(t *testing.T, clientset *kubernetes.Clientset, owners []metav1.OwnerReference, namespace, name string) *appsv1.ReplicaSet {
t.Helper()
ctx := context.Background()
labels := map[string]string{"app": name}
replicaSet := &appsv1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
OwnerReferences: owners,
},
Spec: appsv1.ReplicaSetSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: labels},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "app", Image: "nginx:latest"}},
},
},
},
}
rs, err := clientset.AppsV1().ReplicaSets(namespace).Create(ctx, replicaSet, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create ReplicaSet: %v", err)
}
return rs
}
func makePod(t *testing.T, clientset *kubernetes.Clientset, owners []metav1.OwnerReference, namespace, name string) *corev1.Pod {
t.Helper()
ctx := context.Background()
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
OwnerReferences: owners,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "app", Image: "nginx:latest"}},
},
}
created, err := clientset.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create Pod: %v", err)
}
// First set the pod to Pending phase
created.Status.Phase = corev1.PodPending
pending, err := clientset.CoreV1().Pods(namespace).UpdateStatus(ctx, created, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("failed to update Pod status to Pending: %v", err)
}
// Then transition to Running
pending.Status.Phase = corev1.PodRunning
pending.Status.ContainerStatuses = []corev1.ContainerStatus{{
Name: "app",
ImageID: "docker-pullable://nginx@sha256:abc123def456",
}}
updated, err := clientset.CoreV1().Pods(namespace).UpdateStatus(ctx, pending, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("failed to update Pod status to Running: %v", err)
}
return updated
}
func makePodWithInitContainer(t *testing.T, clientset *kubernetes.Clientset, owners []metav1.OwnerReference, namespace, name string) *corev1.Pod {
t.Helper()
ctx := context.Background()
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
OwnerReferences: owners,
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{{Name: "init", Image: "busybox:latest"}},
Containers: []corev1.Container{{Name: "app", Image: "nginx:latest"}},
},
}
created, err := clientset.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create Pod: %v", err)
}
created.Status.Phase = corev1.PodPending
pending, err := clientset.CoreV1().Pods(namespace).UpdateStatus(ctx, created, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("failed to update Pod status to Pending: %v", err)
}
pending.Status.Phase = corev1.PodRunning
pending.Status.InitContainerStatuses = []corev1.ContainerStatus{{
Name: "init",
ImageID: "docker-pullable://busybox@sha256:initdigest789",
}}
pending.Status.ContainerStatuses = []corev1.ContainerStatus{{
Name: "app",
ImageID: "docker-pullable://nginx@sha256:abc123def456",
}}
updated, err := clientset.CoreV1().Pods(namespace).UpdateStatus(ctx, pending, metav1.UpdateOptions{})
if err != nil {
t.Fatalf("failed to update Pod status to Running: %v", err)
}
return updated
}
func deleteDeployment(t *testing.T, clientset *kubernetes.Clientset, namespace, name string) {
t.Helper()
ctx := context.Background()
err := clientset.AppsV1().Deployments(namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
t.Fatalf("failed to delete Deployment: %v", err)
}
}
func deleteReplicaSet(t *testing.T, clientset *kubernetes.Clientset, namespace, name string) {
t.Helper()
ctx := context.Background()
err := clientset.AppsV1().ReplicaSets(namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
t.Fatalf("failed to delete ReplicaSet: %v", err)
}
}
func deletePod(t *testing.T, clientset *kubernetes.Clientset, namespace, name string) {
t.Helper()
ctx := context.Background()
err := clientset.CoreV1().Pods(namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
t.Fatalf("failed to delete Pod: %v", err)
}
}
func TestControllerIntegration_KubernetesDeploymentLifecycle(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
t.Parallel()
namespace := "test-controller-ns"
clientset, mock := setup(t, "", "")
// Create deployment, replicaset, and pod; expect 1 record
deployment := makeDeployment(t, clientset, []metav1.OwnerReference{}, namespace, "test-deployment")
replicaSet := makeReplicaSet(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment.Name,
UID: deployment.UID,
}}, namespace, "test-deployment-123456")
_ = makePod(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet.Name,
UID: replicaSet.UID,
}}, namespace, "test-deployment-123456-1")
require.Eventually(t, func() bool {
return len(mock.getRecords()) >= 1
}, 3*time.Second, 100*time.Millisecond)
records := mock.getRecords()
require.Len(t, records, 1)
assert.Equal(t, deploymentrecord.StatusDeployed, records[0].Status)
// Create another pod in replicaset; the dedup cache should prevent a new record as there is only one worker
// and no risk of multiple workers processing before cache is set.
_ = makePod(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet.Name,
UID: replicaSet.UID,
}}, namespace, "test-deployment-123456-2")
require.Never(t, func() bool {
return len(mock.getRecords()) != 1
}, 3*time.Second, 100*time.Millisecond)
// Delete second pod; still expect 1 record
deletePod(t, clientset, namespace, "test-deployment-123456-2")
require.Never(t, func() bool {
return len(mock.getRecords()) != 1
}, 3*time.Second, 100*time.Millisecond)
// Delete deployment, replicaset, and first pod; expect 2 records
deleteDeployment(t, clientset, namespace, "test-deployment")
deleteReplicaSet(t, clientset, namespace, "test-deployment-123456")
deletePod(t, clientset, namespace, "test-deployment-123456-1")
require.Eventually(t, func() bool {
return len(mock.getRecords()) >= 2
}, 3*time.Second, 100*time.Millisecond)
records = mock.getRecords()
require.Len(t, records, 2)
assert.Equal(t, deploymentrecord.StatusDecommissioned, records[1].Status)
}
func TestControllerIntegration_InitContainers(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
t.Parallel()
namespace := "test-controller-ns"
clientset, mock := setup(t, "", "")
// Create deployment, replicaset, and pod with an init container; expect 2 records (one per container)
deployment := makeDeployment(t, clientset, []metav1.OwnerReference{}, namespace, "init-deployment")
replicaSet := makeReplicaSet(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment.Name,
UID: deployment.UID,
}}, namespace, "init-deployment-abc123")
_ = makePodWithInitContainer(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet.Name,
UID: replicaSet.UID,
}}, namespace, "init-deployment-abc123-1")
require.Eventually(t, func() bool {
return len(mock.getRecords()) >= 2
}, 3*time.Second, 100*time.Millisecond)
records := mock.getRecords()
require.Len(t, records, 2)
// Both records should be deployed; collect deployment names to verify both containers are recorded
deploymentNames := make([]string, len(records))
for i, r := range records {
assert.Equal(t, deploymentrecord.StatusDeployed, r.Status)
deploymentNames[i] = r.DeploymentName
}
assert.Contains(t, deploymentNames, fmt.Sprintf("%s/init-deployment/app", namespace))
assert.Contains(t, deploymentNames, fmt.Sprintf("%s/init-deployment/init", namespace))
// Delete deployment, replicaset, and pod; expect 2 more decommissioned records (one per container)
deleteDeployment(t, clientset, namespace, "init-deployment")
deleteReplicaSet(t, clientset, namespace, "init-deployment-abc123")
deletePod(t, clientset, namespace, "init-deployment-abc123-1")
require.Eventually(t, func() bool {
return len(mock.getRecords()) >= 4
}, 3*time.Second, 100*time.Millisecond)
records = mock.getRecords()
require.Len(t, records, 4)
decommissionedNames := make([]string, 0, 2)
for _, r := range records[2:] {
assert.Equal(t, deploymentrecord.StatusDecommissioned, r.Status)
decommissionedNames = append(decommissionedNames, r.DeploymentName)
}
assert.Contains(t, decommissionedNames, fmt.Sprintf("%s/init-deployment/app", namespace))
assert.Contains(t, decommissionedNames, fmt.Sprintf("%s/init-deployment/init", namespace))
}
func TestControllerIntegration_OnlyWatchOneNamespace(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
t.Parallel()
namespace1 := "namespace1"
namespace2 := "namespace2"
clientset, mock := setup(t, namespace1, "")
// Make invalid namespaces
ns2 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace2}}
_, err := clientset.CoreV1().Namespaces().Create(context.Background(), ns2, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create namespace: %v", err)
}
// Make new deployment in namespace1; expect 1 record
deployment1 := makeDeployment(t, clientset, []metav1.OwnerReference{}, namespace1, "init-deployment")
replicaSet1 := makeReplicaSet(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment1.Name,
UID: deployment1.UID,
}}, namespace1, "init-deployment-abc123")
_ = makePod(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet1.Name,
UID: replicaSet1.UID,
}}, namespace1, "init-deployment-abc123-1")
require.Eventually(t, func() bool {
return len(mock.getRecords()) == 1
}, 3*time.Second, 100*time.Millisecond)
// Make new deployment in namespace2; expect no new records
deployment2 := makeDeployment(t, clientset, []metav1.OwnerReference{}, namespace2, "init-deployment")
replicaSet2 := makeReplicaSet(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment2.Name,
UID: deployment2.UID,
}}, namespace2, "init-deployment-abc123")
_ = makePod(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet2.Name,
UID: replicaSet2.UID,
}}, namespace2, "init-deployment-abc123-1")
require.Never(t, func() bool {
return len(mock.getRecords()) != 1
}, 3*time.Second, 100*time.Millisecond)
}
func TestControllerIntegration_ExcludeNamespaces(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
}
t.Parallel()
namespace1 := "namespace1"
namespace2 := "namespace2"
namespace3 := "namespace3"
clientset, mock := setup(t, "", fmt.Sprintf("%s,%s", namespace2, namespace3))
// Make valid namespace
ns1 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace1}}
_, err := clientset.CoreV1().Namespaces().Create(context.Background(), ns1, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create namespace: %v", err)
}
// Make new deployment in namespace1; expect 1 record
deployment1 := makeDeployment(t, clientset, []metav1.OwnerReference{}, namespace1, "init-deployment")
replicaSet1 := makeReplicaSet(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment1.Name,
UID: deployment1.UID,
}}, namespace1, "init-deployment-abc123")
_ = makePod(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet1.Name,
UID: replicaSet1.UID,
}}, namespace1, "init-deployment-abc123-1")
require.Eventually(t, func() bool {
return len(mock.getRecords()) == 1
}, 3*time.Second, 100*time.Millisecond)
// Make new deployment in namespace2; expect no new records
deployment2 := makeDeployment(t, clientset, []metav1.OwnerReference{}, namespace2, "init-deployment")
replicaSet2 := makeReplicaSet(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment2.Name,
UID: deployment2.UID,
}}, namespace2, "init-deployment-abc123")
_ = makePod(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet2.Name,
UID: replicaSet2.UID,
}}, namespace2, "init-deployment-abc123-1")
// Make new deployment in namespace 3; expect no new records
deployment3 := makeDeployment(t, clientset, []metav1.OwnerReference{}, namespace3, "init-deployment")
replicaSet3 := makeReplicaSet(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: deployment3.Name,
UID: deployment3.UID,
}}, namespace3, "init-deployment-abc123")
_ = makePod(t, clientset, []metav1.OwnerReference{{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: replicaSet3.Name,
UID: replicaSet3.UID,
}}, namespace3, "init-deployment-abc123-1")
require.Never(t, func() bool {
return len(mock.getRecords()) != 1
}, 3*time.Second, 100*time.Millisecond)
}