-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathe2e_v2.go
300 lines (265 loc) · 9.55 KB
/
e2e_v2.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
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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("kubebuilder", func() {
Context("with v2 scaffolding", func() {
var kbc *KBTestContext
BeforeEach(func() {
var err error
kbc, err = TestContext("GO111MODULE=on")
Expect(err).NotTo(HaveOccurred())
Expect(kbc.Prepare()).To(Succeed())
By("installing cert manager bundle")
Expect(kbc.InstallCertManager()).To(Succeed())
})
AfterEach(func() {
By("clean up created API objects during test process")
kbc.CleanupManifests(filepath.Join("config", "default"))
By("uninstalling cert manager bundle")
kbc.UninstallCertManager()
By("remove container image and work dir")
kbc.Destroy()
})
It("should generate a runnable project", func() {
var controllerPodName string
By("init v2 project")
err := kbc.Init(
"--project-version", "2",
"--domain", kbc.Domain,
"--dep=false")
Expect(err).Should(Succeed())
By("creating api definition")
err = kbc.CreateAPI(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--namespaced",
"--resource",
"--controller",
"--make=false")
Expect(err).Should(Succeed())
By("implementing the API")
Expect(insertCode(
filepath.Join(kbc.Dir, "api", kbc.Version, fmt.Sprintf("%s_types.go", strings.ToLower(kbc.Kind))),
fmt.Sprintf(`type %sSpec struct {
`, kbc.Kind),
` // +optional
Count int `+"`"+`json:"count,omitempty"`+"`"+`
`)).Should(Succeed())
By("scaffolding mutating and validating webhook")
err = kbc.CreateWebhook(
"--group", kbc.Group,
"--version", kbc.Version,
"--kind", kbc.Kind,
"--defaulting",
"--programmatic-validation")
Expect(err).Should(Succeed())
By("implementing the mutating and validating webhooks")
err = implementWebhooks(filepath.Join(
kbc.Dir, "api", kbc.Version,
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
Expect(err).Should(Succeed())
By("uncomment kustomization.yaml to enable webhook and ca injection")
Expect(uncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../webhook", "#")).To(Succeed())
Expect(uncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../certmanager", "#")).To(Succeed())
Expect(uncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- manager_webhook_patch.yaml", "#")).To(Succeed())
Expect(uncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- webhookcainjection_patch.yaml", "#")).To(Succeed())
Expect(uncommentCode(filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
`#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
# objref:
# kind: Certificate
# group: certmanager.k8s.io
# version: v1alpha1
# name: serving-cert # this name should match the one in certificate.yaml
# fieldref:
# fieldpath: metadata.namespace
#- name: CERTIFICATE_NAME
# objref:
# kind: Certificate
# group: certmanager.k8s.io
# version: v1alpha1
# name: serving-cert # this name should match the one in certificate.yaml
#- name: SERVICE_NAMESPACE # namespace of the service
# objref:
# kind: Service
# version: v1
# name: webhook-service
# fieldref:
# fieldpath: metadata.namespace
#- name: SERVICE_NAME
# objref:
# kind: Service
# version: v1
# name: webhook-service`, "#")).To(Succeed())
By("building image")
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
Expect(err).Should(Succeed())
By("loading docker image into kind cluster")
err = kbc.LoadImageToKindCluster()
Expect(err).Should(Succeed())
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
// $ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin --user=myname@mycompany.com
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
By("deploying controller manager")
err = kbc.Make("deploy", "IMG="+kbc.ImageName)
Expect(err).Should(Succeed())
By("validate the controller-manager pod running as expected")
verifyControllerUp := func() error {
// Get pod name
podOutput, err := kbc.Kubectl.Get(
true,
"pods", "-l", "control-plane=controller-manager",
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}{{ \"\\n\" }}{{ end }}{{ end }}")
Expect(err).NotTo(HaveOccurred())
podNames := getNonEmptyLines(podOutput)
if len(podNames) != 1 {
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
}
controllerPodName = podNames[0]
Expect(controllerPodName).Should(ContainSubstring("controller-manager"))
// Validate pod status
status, err := kbc.Kubectl.Get(
true,
"pods", controllerPodName, "-o", "jsonpath={.status.phase}")
Expect(err).NotTo(HaveOccurred())
if status != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
}
Eventually(verifyControllerUp, time.Minute, time.Second).Should(Succeed())
By("validate cert manager has provisioned the certificate secret")
Eventually(func() error {
_, err := kbc.Kubectl.Get(
true,
"secrets", "webhook-server-cert")
return err
}, time.Minute, time.Second).Should(Succeed())
By("validate the mutating|validating webhooks have the CA injected")
verifyCAInjection := func() error {
mwhOutput, err := kbc.Kubectl.Get(
false,
"mutatingwebhookconfigurations.admissionregistration.k8s.io",
fmt.Sprintf("e2e-%s-mutating-webhook-configuration", kbc.TestSuffix),
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
Expect(err).NotTo(HaveOccurred())
// sanity check that ca should be long enough, because there may be a place holder "\n"
Expect(len(mwhOutput)).To(BeNumerically(">", 10))
vwhOutput, err := kbc.Kubectl.Get(
false,
"validatingwebhookconfigurations.admissionregistration.k8s.io",
fmt.Sprintf("e2e-%s-validating-webhook-configuration", kbc.TestSuffix),
"-o", "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}")
Expect(err).NotTo(HaveOccurred())
// sanity check that ca should be long enough, because there may be a place holder "\n"
Expect(len(vwhOutput)).To(BeNumerically(">", 10))
return nil
}
Eventually(verifyCAInjection, time.Minute, time.Second).Should(Succeed())
By("creating an instance of CR")
// currently controller-runtime doesn't provide a readiness probe, we retry a few times
// we can change it to probe the readiness endpoint after CR supports it.
sampleFile := filepath.Join("config", "samples", fmt.Sprintf("%s_%s_%s.yaml", kbc.Group, kbc.Version, strings.ToLower(kbc.Kind)))
Eventually(func() error {
_, err = kbc.Kubectl.Apply(true, "-f", sampleFile)
return err
}, time.Minute, time.Second).Should(Succeed())
By("validate the created resource object gets reconciled in controller")
managerContainerLogs := func() string {
logOutput, err := kbc.Kubectl.Logs(controllerPodName, "-c", "manager")
Expect(err).NotTo(HaveOccurred())
return logOutput
}
Eventually(managerContainerLogs, time.Minute, time.Second).Should(ContainSubstring("Successfully Reconciled"))
By("validate mutating and validating webhooks are working fine")
cnt, err := kbc.Kubectl.Get(
true,
"-f", sampleFile,
"-o", "go-template={{ .spec.count }}")
Expect(err).NotTo(HaveOccurred())
count, err := strconv.Atoi(cnt)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(BeNumerically("==", 5))
})
})
})
func implementWebhooks(filename string) error {
bs, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
str := string(bs)
str, err = ensureExistAndReplace(
str,
"import (",
`import (
"errors"`)
if err != nil {
return err
}
// implement defaulting webhook logic
str, err = ensureExistAndReplace(
str,
"// TODO(user): fill in your defaulting logic.",
`if r.Spec.Count == 0 {
r.Spec.Count = 5
}`)
if err != nil {
return err
}
// implement validation webhook logic
str, err = ensureExistAndReplace(
str,
"// TODO(user): fill in your validation logic upon object creation.",
`if r.Spec.Count < 0 {
return errors.New(".spec.count must >= 0")
}`)
if err != nil {
return err
}
str, err = ensureExistAndReplace(
str,
"// TODO(user): fill in your validation logic upon object update.",
`if r.Spec.Count < 0 {
return errors.New(".spec.count must >= 0")
}`)
if err != nil {
return err
}
return ioutil.WriteFile(filename, []byte(str), 0644)
}
func ensureExistAndReplace(input, match, replace string) (string, error) {
if strings.Index(input, match) == -1 {
return "", fmt.Errorf("can't find %q", match)
}
return strings.Replace(input, match, replace, -1), nil
}