-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main_test.go
199 lines (191 loc) · 9.77 KB
/
main_test.go
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
package main
import (
"context"
"testing"
"time"
"github.com/TwiN/kevent"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
fakediscovery "k8s.io/client-go/discovery/fake"
fakedynamic "k8s.io/client-go/dynamic/fake"
fakekubernetes "k8s.io/client-go/kubernetes/fake"
)
func TestReconcile(t *testing.T) {
// Before jumping into this test, you must understand that kubernetesClient and dynamicClient are used for two different purposes:
// - kubernetesClient: Takes care of discovery. This means that we need to inject the API resources we want to test through dynamicClient
// - dynamicClient: Takes care of listing and deleting resources.
// Create scheme
scheme := runtime.NewScheme()
_ = appsv1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
_ = metav1.AddMetaToScheme(scheme)
// Create scenarios
scenarios := []struct {
name string
podsToCreate []*unstructured.Unstructured
expectedResourcesLeftAfterReconciliation int
}{
{
name: "expired-pod-is-deleted",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m"}),
},
expectedResourcesLeftAfterReconciliation: 0,
},
{
name: "not-expired-pod-is-not-deleted",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d"}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "unannotated-pod-is-not-deleted",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "unannotated-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "one-out-of-two-pods-is-deleted-because-only-one-expired",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d"}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m"}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "multiple-expired-pods-are-deleted",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-1", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m"}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-2", time.Now().Add(-72*time.Hour), map[string]interface{}{AnnotationTTL: "2d"}),
},
expectedResourcesLeftAfterReconciliation: 0,
},
{
name: "only-expired-pods-are-deleted",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-1", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m"}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d"}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-2", time.Now().Add(-72*time.Hour), map[string]interface{}{AnnotationTTL: "2d"}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "unannotated-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{}),
},
expectedResourcesLeftAfterReconciliation: 2,
},
{
name: "expired-pod-is-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 0,
},
{
name: "not-expired-pod-is-not-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "unannotated-pod-is-not-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "unannotated-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "one-out-of-two-pods-is-deleted-because-only-one-expired-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "multiple-expired-pods-are-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-1", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-2", time.Now().Add(-72*time.Hour), map[string]interface{}{AnnotationTTL: "2d", AnnotationRefreshedAt: time.Now().Add(-49 * time.Hour).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 0,
},
{
name: "only-expired-pods-are-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-1", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-2", time.Now().Add(-72*time.Hour), map[string]interface{}{AnnotationTTL: "2d", AnnotationRefreshedAt: time.Now().Add(-49 * time.Hour).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "unannotated-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 2,
},
}
// Run scenarios
for _, scenario := range scenarios {
// Create clients
kubernetesClient := fakekubernetes.NewSimpleClientset()
dynamicClient := fakedynamic.NewSimpleDynamicClient(scheme)
eventManager := kevent.NewEventManager(kubernetesClient, "k8s-ttl-controller")
fakeDiscovery, _ := kubernetesClient.Discovery().(*fakediscovery.FakeDiscovery)
fakeDiscovery.Fake.Resources = []*metav1.APIResourceList{
{
GroupVersion: "v1",
APIResources: []metav1.APIResource{
{
Name: "pods",
Kind: "Pod",
Namespaced: true,
Verbs: []string{"create", "delete", "get", "list", "patch", "update", "watch"},
},
},
},
}
// Run scenario
t.Run(scenario.name, func(t *testing.T) {
for _, podToCreate := range scenario.podsToCreate {
_, err := dynamicClient.Resource(schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}).Namespace("default").Create(context.TODO(), podToCreate, metav1.CreateOptions{})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
// Make sure that the resources have been created
list, err := dynamicClient.Resource(schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}).Namespace("default").List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(list.Items) != len(scenario.podsToCreate) {
t.Errorf("expected 3 resources, got %d", len(list.Items))
}
// Reconcile once
if err = Reconcile(kubernetesClient, dynamicClient, eventManager); err != nil {
t.Errorf("unexpected error: %v", err)
}
// Make sure that the expired resources have been deleted
list, err = dynamicClient.Resource(schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}).Namespace("default").List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(list.Items) != scenario.expectedResourcesLeftAfterReconciliation {
t.Errorf("expected 3 resources, got %d", len(list.Items))
}
})
}
}
func newUnstructuredWithAnnotations(apiVersion, kind, namespace, name string, creationTimestamp time.Time, annotations map[string]interface{}) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"namespace": namespace,
"name": name,
"creationTimestamp": creationTimestamp.Format(time.RFC3339),
"annotations": annotations,
},
},
}
}