generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsubnamespace_controller_test.go
164 lines (132 loc) · 4.88 KB
/
subnamespace_controller_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
package controllers
import (
"context"
"time"
accuratev2 "github.com/cybozu-go/accurate/api/accurate/v2"
"github.com/cybozu-go/accurate/pkg/constants"
"github.com/cybozu-go/accurate/pkg/indexing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/envtest/komega"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
)
var _ = Describe("SubNamespace controller", func() {
ctx := context.Background()
var stopFunc func()
BeforeEach(func() {
mgr, err := ctrl.NewManager(k8sCfg, ctrl.Options{
Scheme: scheme,
LeaderElection: false,
Metrics: server.Options{BindAddress: "0"},
Controller: config.Controller{
SkipNameValidation: ptr.To(true),
},
})
Expect(err).ToNot(HaveOccurred())
snr := &SubNamespaceReconciler{
Client: mgr.GetClient(),
}
err = snr.SetupWithManager(mgr)
Expect(err).ToNot(HaveOccurred())
err = indexing.SetupIndexForSubNamespace(ctx, mgr)
Expect(err).NotTo(HaveOccurred())
ctx, cancel := context.WithCancel(ctx)
stopFunc = cancel
go func() {
err := mgr.Start(ctx)
if err != nil {
panic(err)
}
}()
time.Sleep(100 * time.Millisecond)
})
AfterEach(func() {
stopFunc()
time.Sleep(100 * time.Millisecond)
})
It("should create and delete sub-namespaces", func() {
ns := &corev1.Namespace{}
ns.Name = "test1"
Expect(k8sClient.Create(ctx, ns)).To(Succeed())
sn := &accuratev2.SubNamespace{}
sn.Namespace = "test1"
sn.Name = "test1-sub1"
sn.Finalizers = []string{constants.Finalizer}
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
sub1 := &corev1.Namespace{}
sub1.Name = "test1-sub1"
Eventually(komega.Get(sub1)).Should(Succeed())
Expect(sub1.Labels).To(HaveKeyWithValue(constants.LabelCreatedBy, "accurate"))
Expect(sub1.Labels).To(HaveKeyWithValue(constants.LabelParent, "test1"))
Eventually(komega.Object(sn)).Should(HaveField("Status.ObservedGeneration", BeNumerically(">", 0)))
Expect(sn.Status.Conditions).To(BeEmpty())
Expect(k8sClient.Delete(ctx, sn)).To(Succeed())
Eventually(komega.Object(sub1)).Should(HaveField("DeletionTimestamp", Not(BeNil())))
})
It("should detect conflicts", func() {
ns := &corev1.Namespace{}
ns.Name = "test2"
Expect(k8sClient.Create(ctx, ns)).To(Succeed())
ns2 := &corev1.Namespace{}
ns2.Name = "test2-sub1"
Expect(k8sClient.Create(ctx, ns2)).To(Succeed())
sn := &accuratev2.SubNamespace{}
sn.Namespace = "test2"
sn.Name = "test2-sub1"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
Eventually(komega.Object(sn)).Should(HaveField("Status.ObservedGeneration", BeNumerically(">", 0)))
Expect(sn.Status.Conditions).To(HaveLen(1))
Expect(sn.Status.Conditions[0].Reason).To(Equal(accuratev2.SubNamespaceConflict))
// It's tempting to test if a conflict can be resolved by deleting the conflicting namespace,
// but this is currently not possible because EnvTest does not support namespace deletion.
// See https://github.com/kubernetes-sigs/controller-runtime/issues/880 for details.
// This feature should be tested in e2e-tests.
})
It("should not delete a conflicting sub-namespace", func() {
ns := &corev1.Namespace{}
ns.Name = "test3"
Expect(k8sClient.Create(ctx, ns)).To(Succeed())
sn := &accuratev2.SubNamespace{}
sn.Namespace = "test3"
sn.Name = "test3-sub1"
sn.Finalizers = []string{constants.Finalizer}
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
sub1 := &corev1.Namespace{}
sub1.Name = "test3-sub1"
Eventually(komega.Get(sub1)).Should(Succeed())
Expect(komega.Update(sub1, func() {
sub1.Labels[constants.LabelParent] = "foo"
})()).To(Succeed())
Eventually(komega.Object(sn)).Should(HaveField("Status.Conditions", HaveLen(1)))
Expect(sn.Status.Conditions[0].Reason).To(Equal(accuratev2.SubNamespaceConflict))
Expect(k8sClient.Delete(ctx, sn)).To(Succeed())
Consistently(komega.Object(sub1)).Should(HaveField("DeletionTimestamp", BeNil()))
})
It("should re-create a sub-namespace if it is deleted", func() {
ns := &corev1.Namespace{}
ns.Name = "test4"
Expect(k8sClient.Create(ctx, ns)).To(Succeed())
sn := &accuratev2.SubNamespace{}
sn.Namespace = "test4"
sn.Name = "test4-sub1"
Expect(k8sClient.Create(ctx, sn)).To(Succeed())
sub1 := &corev1.Namespace{}
sub1.Name = "test4-sub1"
Eventually(komega.Get(sub1)).Should(Succeed())
Expect(k8sClient.Delete(ctx, sub1)).To(Succeed())
uid := sub1.UID
sub1 = &corev1.Namespace{}
sub1.Name = "test4-sub1"
cs, err := kubernetes.NewForConfig(k8sCfg)
Expect(err).NotTo(HaveOccurred())
_, err = cs.CoreV1().Namespaces().Finalize(ctx, sub1, metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred())
Eventually(komega.Object(sub1)).Should(HaveField("UID", Not(Equal(uid))))
})
})