forked from clastix/kamaji
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment_test.go
More file actions
287 lines (249 loc) · 10.3 KB
/
Copy pathdeployment_test.go
File metadata and controls
287 lines (249 loc) · 10.3 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
// Copyright 2022 Clastix Labs
// SPDX-License-Identifier: Apache-2.0
package controlplane
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
pointer "k8s.io/utils/ptr"
kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1"
)
func TestControlplaneDeployment(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Controlplane Deployment Suite")
}
var _ = Describe("Controlplane Deployment", func() {
var d Deployment
BeforeEach(func() {
d = Deployment{
DataStoreOverrides: []DataStoreOverrides{{
Resource: "/events",
DataStore: kamajiv1alpha1.DataStore{
Spec: kamajiv1alpha1.DataStoreSpec{
Endpoints: kamajiv1alpha1.Endpoints{"etcd-0", "etcd-1", "etcd-2"},
},
},
}},
}
})
Describe("DataStoreOverrides flag generation", func() {
It("should generate valid --etcd-servers-overrides value", func() {
etcdSerVersOverrides := d.etcdServersOverrides()
Expect(etcdSerVersOverrides).To(Equal("/events#https://etcd-0;https://etcd-1;https://etcd-2"))
})
It("should generate valid --etcd-servers-overrides value with 2 DataStoreOverrides", func() {
d.DataStoreOverrides = append(d.DataStoreOverrides, DataStoreOverrides{
Resource: "/pods",
DataStore: kamajiv1alpha1.DataStore{
Spec: kamajiv1alpha1.DataStoreSpec{
Endpoints: kamajiv1alpha1.Endpoints{"etcd-3", "etcd-4", "etcd-5"},
},
},
})
etcdSerVersOverrides := d.etcdServersOverrides()
Expect(etcdSerVersOverrides).To(Equal("/events#https://etcd-0;https://etcd-1;https://etcd-2,/pods#https://etcd-3;https://etcd-4;https://etcd-5"))
})
})
Describe("applyProbeOverrides", func() {
var probe *corev1.Probe
BeforeEach(func() {
probe = &corev1.Probe{
InitialDelaySeconds: 0,
TimeoutSeconds: 1,
PeriodSeconds: 10,
SuccessThreshold: 1,
FailureThreshold: 3,
}
})
It("should not modify probe when spec is nil", func() {
applyProbeOverrides(probe, nil)
Expect(probe.InitialDelaySeconds).To(Equal(int32(0)))
Expect(probe.TimeoutSeconds).To(Equal(int32(1)))
Expect(probe.PeriodSeconds).To(Equal(int32(10)))
Expect(probe.SuccessThreshold).To(Equal(int32(1)))
Expect(probe.FailureThreshold).To(Equal(int32(3)))
})
It("should override only FailureThreshold when only it is set", func() {
spec := &kamajiv1alpha1.ProbeSpec{
FailureThreshold: pointer.To(int32(30)),
}
applyProbeOverrides(probe, spec)
Expect(probe.FailureThreshold).To(Equal(int32(30)))
Expect(probe.InitialDelaySeconds).To(Equal(int32(0)))
Expect(probe.TimeoutSeconds).To(Equal(int32(1)))
Expect(probe.PeriodSeconds).To(Equal(int32(10)))
Expect(probe.SuccessThreshold).To(Equal(int32(1)))
})
It("should override all fields when all are set", func() {
spec := &kamajiv1alpha1.ProbeSpec{
InitialDelaySeconds: pointer.To(int32(15)),
TimeoutSeconds: pointer.To(int32(5)),
PeriodSeconds: pointer.To(int32(30)),
SuccessThreshold: pointer.To(int32(2)),
FailureThreshold: pointer.To(int32(10)),
}
applyProbeOverrides(probe, spec)
Expect(probe.InitialDelaySeconds).To(Equal(int32(15)))
Expect(probe.TimeoutSeconds).To(Equal(int32(5)))
Expect(probe.PeriodSeconds).To(Equal(int32(30)))
Expect(probe.SuccessThreshold).To(Equal(int32(2)))
Expect(probe.FailureThreshold).To(Equal(int32(10)))
})
It("should cascade global then component overrides", func() {
global := &kamajiv1alpha1.ProbeSpec{
FailureThreshold: pointer.To(int32(10)),
PeriodSeconds: pointer.To(int32(20)),
}
applyProbeOverrides(probe, global)
component := &kamajiv1alpha1.ProbeSpec{
FailureThreshold: pointer.To(int32(60)),
}
applyProbeOverrides(probe, component)
Expect(probe.FailureThreshold).To(Equal(int32(60)))
Expect(probe.PeriodSeconds).To(Equal(int32(20)))
Expect(probe.TimeoutSeconds).To(Equal(int32(1)))
Expect(probe.InitialDelaySeconds).To(Equal(int32(0)))
Expect(probe.SuccessThreshold).To(Equal(int32(1)))
})
It("should not panic when probe is nil", func() {
spec := &kamajiv1alpha1.ProbeSpec{PeriodSeconds: pointer.To(int32(20))}
Expect(func() { applyProbeOverrides(nil, spec) }).ToNot(Panic())
})
})
Describe("mergeAPIServerArgs", func() {
var (
safeDefaults map[string]string
managed map[string]string
)
BeforeEach(func() {
safeDefaults = map[string]string{
"--authorization-mode": "Node,RBAC",
"--service-account-issuer": "https://kubernetes.default.svc.cluster.local",
}
managed = map[string]string{
"--secure-port": "6443",
"--service-cluster-ip-range": "10.96.0.0/12",
}
})
It("applies safe defaults when the user did not provide them", func() {
got := mergeAPIServerArgs(nil, nil, safeDefaults, managed)
Expect(got).To(ContainElement("--authorization-mode=Node,RBAC"))
Expect(got).To(ContainElement("--service-account-issuer=https://kubernetes.default.svc.cluster.local"))
})
It("lets the user override a safe default", func() {
user := []string{"--authorization-mode=AlwaysAllow"}
got := mergeAPIServerArgs(nil, user, safeDefaults, managed)
Expect(got).To(ContainElement("--authorization-mode=AlwaysAllow"))
Expect(got).NotTo(ContainElement("--authorization-mode=Node,RBAC"))
})
It("ignores user attempts to override a managed flag", func() {
user := []string{"--secure-port=9443"}
got := mergeAPIServerArgs(nil, user, safeDefaults, managed)
Expect(got).To(ContainElement("--secure-port=6443"))
Expect(got).NotTo(ContainElement("--secure-port=9443"))
})
It("preserves multiple user values for a repeatable flag", func() {
user := []string{
"--service-account-issuer=https://issuer-one.example.com",
"--service-account-issuer=https://issuer-two.example.com",
}
got := mergeAPIServerArgs(nil, user, safeDefaults, managed)
Expect(got).To(ContainElement("--service-account-issuer=https://issuer-one.example.com"))
Expect(got).To(ContainElement("--service-account-issuer=https://issuer-two.example.com"))
Expect(got).NotTo(ContainElement("--service-account-issuer=https://kubernetes.default.svc.cluster.local"))
})
It("sorts Kamaji-owned flags and appends user extras verbatim at the end", func() {
user := []string{
"--service-account-issuer=https://issuer-one.example.com",
"--service-account-issuer=https://issuer-two.example.com",
"--audit-log-path=/var/log/audit.log",
}
got := mergeAPIServerArgs(nil, user, safeDefaults, managed)
Expect(got).To(Equal([]string{
"--authorization-mode=Node,RBAC",
"--secure-port=6443",
"--service-cluster-ip-range=10.96.0.0/12",
"--service-account-issuer=https://issuer-one.example.com",
"--service-account-issuer=https://issuer-two.example.com",
"--audit-log-path=/var/log/audit.log",
}))
})
It("preserves foreign flags from current, sorted within the Kamaji-owned segment", func() {
current := []string{"--egress-selector-config-file=/etc/kubernetes/konnectivity/egress.yaml"}
got := mergeAPIServerArgs(current, nil, safeDefaults, managed)
Expect(got).To(Equal([]string{
"--authorization-mode=Node,RBAC",
"--egress-selector-config-file=/etc/kubernetes/konnectivity/egress.yaml",
"--secure-port=6443",
"--service-account-issuer=https://kubernetes.default.svc.cluster.local",
"--service-cluster-ip-range=10.96.0.0/12",
}))
})
})
Describe("control plane probes", func() {
// helper: find a container by name in a built PodSpec
containerByName := func(spec *corev1.PodSpec, name string) corev1.Container {
for _, c := range spec.Containers {
if c.Name == name {
return c
}
}
Fail("container not found: " + name)
return corev1.Container{}
}
It("renders a readiness probe for kube-scheduler on /healthz:10259", func() {
podSpec := &corev1.PodSpec{}
d.buildScheduler(podSpec, kamajiv1alpha1.TenantControlPlane{})
c := containerByName(podSpec, "kube-scheduler")
Expect(c.ReadinessProbe).ToNot(BeNil())
Expect(c.ReadinessProbe.HTTPGet.Path).To(Equal("/healthz"))
Expect(c.ReadinessProbe.HTTPGet.Port.IntValue()).To(Equal(10259))
Expect(c.ReadinessProbe.HTTPGet.Scheme).To(Equal(corev1.URISchemeHTTPS))
Expect(c.ReadinessProbe.PeriodSeconds).To(Equal(int32(10)))
})
It("renders a readiness probe for kube-controller-manager on /healthz:10257", func() {
podSpec := &corev1.PodSpec{}
d.buildControllerManager(podSpec, kamajiv1alpha1.TenantControlPlane{})
c := containerByName(podSpec, "kube-controller-manager")
Expect(c.ReadinessProbe).ToNot(BeNil())
Expect(c.ReadinessProbe.HTTPGet.Path).To(Equal("/healthz"))
Expect(c.ReadinessProbe.HTTPGet.Port.IntValue()).To(Equal(10257))
Expect(c.ReadinessProbe.HTTPGet.Scheme).To(Equal(corev1.URISchemeHTTPS))
})
It("cascades global then component readiness overrides onto the scheduler", func() {
tcp := kamajiv1alpha1.TenantControlPlane{}
tcp.Spec.ControlPlane.Deployment.Probes = &kamajiv1alpha1.ControlPlaneProbes{
Readiness: &kamajiv1alpha1.ProbeSpec{PeriodSeconds: pointer.To(int32(20))},
Scheduler: &kamajiv1alpha1.ProbeSet{
Readiness: &kamajiv1alpha1.ProbeSpec{PeriodSeconds: pointer.To(int32(30))},
},
}
podSpec := &corev1.PodSpec{}
d.buildScheduler(podSpec, tcp)
c := containerByName(podSpec, "kube-scheduler")
Expect(c.ReadinessProbe.PeriodSeconds).To(Equal(int32(30))) // component wins over global
})
It("applies a global-only readiness override to the scheduler", func() {
tcp := kamajiv1alpha1.TenantControlPlane{}
tcp.Spec.ControlPlane.Deployment.Probes = &kamajiv1alpha1.ControlPlaneProbes{
Readiness: &kamajiv1alpha1.ProbeSpec{PeriodSeconds: pointer.To(int32(20))},
}
podSpec := &corev1.PodSpec{}
d.buildScheduler(podSpec, tcp)
c := containerByName(podSpec, "kube-scheduler")
Expect(c.ReadinessProbe.PeriodSeconds).To(Equal(int32(20)))
})
It("leaves the kube-apiserver probes unchanged (regression guard)", func() {
podSpec := &corev1.PodSpec{}
tcp := kamajiv1alpha1.TenantControlPlane{}
tcp.Spec.NetworkProfile.Port = 6443
d.buildKubeAPIServer(podSpec, tcp, "")
c := containerByName(podSpec, "kube-apiserver")
Expect(c.LivenessProbe.HTTPGet.Path).To(Equal("/livez"))
Expect(c.ReadinessProbe.HTTPGet.Path).To(Equal("/readyz"))
Expect(c.StartupProbe.HTTPGet.Path).To(Equal("/livez"))
Expect(c.ReadinessProbe.HTTPGet.Port.IntValue()).To(Equal(6443))
})
})
})