@@ -18,11 +18,8 @@ package e2e
1818
1919//nolint:revive // Complains about the dot imports
2020import (
21- "encoding/json"
2221 "fmt"
23- "os"
2422 "os/exec"
25- "path/filepath"
2623 "time"
2724
2825 . "github.com/onsi/ginkgo/v2"
@@ -34,15 +31,6 @@ import (
3431// namespace where the project is deployed in
3532const namespace = "mapserver-operator-system"
3633
37- // serviceAccountName created for the project
38- const serviceAccountName = "mapserver-operator-controller-manager"
39-
40- // metricsServiceName is the name of the metrics service of the project
41- const metricsServiceName = "mapserver-operator-controller-manager-metrics-service"
42-
43- // metricsRoleBindingName is the name of the RBAC that will be created to allow get the metrics data
44- const metricsRoleBindingName = "mapserver-operator-metrics-binding"
45-
4634var _ = Describe ("Manager" , Ordered , func () {
4735 var controllerPodName string
4836
@@ -170,220 +158,5 @@ var _ = Describe("Manager", Ordered, func() {
170158 }
171159 Eventually (verifyControllerUp ).Should (Succeed ())
172160 })
173-
174- It ("should ensure the metrics endpoint is serving metrics" , func () {
175- By ("creating a ClusterRoleBinding for the service account to allow access to metrics" )
176-
177- cmd := exec .Command ("kubectl" , "create" , "clusterrolebinding" , metricsRoleBindingName ,
178- "--clusterrole=mapserver-operator-metrics-reader" ,
179- fmt .Sprintf ("--serviceaccount=%s:%s" , namespace , serviceAccountName ),
180- )
181- _ , err := utils .Run (cmd )
182- Expect (err ).NotTo (HaveOccurred (), "Failed to create ClusterRoleBinding" )
183-
184- By ("validating that the metrics service is available" )
185- cmd = exec .Command ("kubectl" , "get" , "service" , metricsServiceName , "-n" , namespace )
186- _ , err = utils .Run (cmd )
187- Expect (err ).NotTo (HaveOccurred (), "Metrics service should exist" )
188-
189- By ("validating that the ServiceMonitor for Prometheus is applied in the namespace" )
190- cmd = exec .Command ("kubectl" , "get" , "ServiceMonitor" , "-n" , namespace )
191- _ , err = utils .Run (cmd )
192- Expect (err ).NotTo (HaveOccurred (), "ServiceMonitor should exist" )
193-
194- By ("getting the service account token" )
195- token , err := serviceAccountToken ()
196- Expect (err ).NotTo (HaveOccurred ())
197- Expect (token ).NotTo (BeEmpty ())
198-
199- By ("waiting for the metrics endpoint to be ready" )
200- verifyMetricsEndpointReady := func (g Gomega ) {
201- cmd := exec .Command ("kubectl" , "get" , "endpoints" , metricsServiceName , "-n" , namespace )
202- output , err := utils .Run (cmd )
203- g .Expect (err ).NotTo (HaveOccurred ())
204- g .Expect (output ).To (ContainSubstring ("8443" ), "Metrics endpoint is not ready" )
205- }
206- Eventually (verifyMetricsEndpointReady ).Should (Succeed ())
207-
208- By ("verifying that the controller manager is serving the metrics server" )
209- verifyMetricsServerStarted := func (g Gomega ) {
210- cmd := exec .Command ("kubectl" , "logs" , controllerPodName , "-n" , namespace )
211- output , err := utils .Run (cmd )
212- g .Expect (err ).NotTo (HaveOccurred ())
213- g .Expect (output ).To (ContainSubstring ("Serving metrics server" ),
214- "Metrics server not yet started" )
215- }
216- Eventually (verifyMetricsServerStarted ).Should (Succeed ())
217-
218- By ("creating the curl-metrics pod to access the metrics endpoint" )
219- cmd = exec .Command ("kubectl" , "run" , "curl-metrics" , "--restart=Never" ,
220- "--namespace" , namespace ,
221- "--image=curlimages/curl:latest" ,
222- "--overrides" ,
223- fmt .Sprintf (`{
224- "spec": {
225- "containers": [{
226- "name": "curl",
227- "image": "curlimages/curl:latest",
228- "command": ["/bin/sh", "-c"],
229- "args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics"],
230- "securityContext": {
231- "allowPrivilegeEscalation": false,
232- "capabilities": {
233- "drop": ["ALL"]
234- },
235- "runAsNonRoot": true,
236- "runAsUser": 1000,
237- "seccompProfile": {
238- "type": "RuntimeDefault"
239- }
240- }
241- }],
242- "serviceAccount": "%s"
243- }
244- }` , token , metricsServiceName , namespace , serviceAccountName ))
245- _ , err = utils .Run (cmd )
246- Expect (err ).NotTo (HaveOccurred (), "Failed to create curl-metrics pod" )
247-
248- By ("waiting for the curl-metrics pod to complete." )
249- verifyCurlUp := func (g Gomega ) {
250- cmd := exec .Command ("kubectl" , "get" , "pods" , "curl-metrics" ,
251- "-o" , "jsonpath={.status.phase}" ,
252- "-n" , namespace )
253- output , err := utils .Run (cmd )
254- g .Expect (err ).NotTo (HaveOccurred ())
255- g .Expect (output ).To (Equal ("Succeeded" ), "curl pod in wrong status" )
256- }
257- Eventually (verifyCurlUp , 5 * time .Minute ).Should (Succeed ())
258-
259- By ("getting the metrics by checking curl-metrics logs" )
260- metricsOutput := getMetricsOutput ()
261- Expect (metricsOutput ).To (ContainSubstring (
262- "controller_runtime_reconcile_total" ,
263- ))
264- })
265-
266- It ("should provisioned cert-manager" , func () {
267- By ("validating that cert-manager has the certificate Secret" )
268- verifyCertManager := func (g Gomega ) {
269- cmd := exec .Command ("kubectl" , "get" , "secrets" , "webhook-server-cert" , "-n" , namespace )
270- _ , err := utils .Run (cmd )
271- g .Expect (err ).NotTo (HaveOccurred ())
272- }
273- Eventually (verifyCertManager ).Should (Succeed ())
274- })
275-
276- It ("should have CA injection for WFS conversion webhook" , func () {
277- By ("checking CA injection for WFS conversion webhook" )
278- verifyCAInjection := func (g Gomega ) {
279- cmd := exec .Command ("kubectl" , "get" ,
280- "customresourcedefinitions.apiextensions.k8s.io" ,
281- "wfs..pdok.nl" ,
282- "-o" , "go-template={{ .spec.conversion.webhook.clientConfig.caBundle }}" )
283- vwhOutput , err := utils .Run (cmd )
284- g .Expect (err ).NotTo (HaveOccurred ())
285- g .Expect (len (vwhOutput )).To (BeNumerically (">" , 10 ))
286- }
287- Eventually (verifyCAInjection ).Should (Succeed ())
288- })
289-
290- It ("should have CA injection for WMS conversion webhook" , func () {
291- By ("checking CA injection for WMS conversion webhook" )
292- verifyCAInjection := func (g Gomega ) {
293- cmd := exec .Command ("kubectl" , "get" ,
294- "customresourcedefinitions.apiextensions.k8s.io" ,
295- "wms..pdok.nl" ,
296- "-o" , "go-template={{ .spec.conversion.webhook.clientConfig.caBundle }}" )
297- vwhOutput , err := utils .Run (cmd )
298- g .Expect (err ).NotTo (HaveOccurred ())
299- g .Expect (len (vwhOutput )).To (BeNumerically (">" , 10 ))
300- }
301- Eventually (verifyCAInjection ).Should (Succeed ())
302- })
303-
304- It ("should have CA injection for validating webhooks" , func () {
305- By ("checking CA injection for validating webhooks" )
306- verifyCAInjection := func (g Gomega ) {
307- cmd := exec .Command ("kubectl" , "get" ,
308- "validatingwebhookconfigurations.admissionregistration.k8s.io" ,
309- "mapserver-operator-validating-webhook-configuration" ,
310- "-o" , "go-template={{ range .webhooks }}{{ .clientConfig.caBundle }}{{ end }}" )
311- vwhOutput , err := utils .Run (cmd )
312- g .Expect (err ).NotTo (HaveOccurred ())
313- g .Expect (len (vwhOutput )).To (BeNumerically (">" , 10 ))
314- }
315- Eventually (verifyCAInjection ).Should (Succeed ())
316- })
317-
318- // +kubebuilder:scaffold:e2e-webhooks-checks
319-
320- // TODO: Customize the e2e test suite with scenarios specific to your project.
321- // Consider applying sample/CR(s) and check their status and/or verifying
322- // the reconciliation by using the metrics, i.e.:
323- // metricsOutput := getMetricsOutput()
324- // Expect(metricsOutput).To(ContainSubstring(
325- // fmt.Sprintf(`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
326- // strings.ToLower(<Kind>),
327- // ))
328161 })
329162})
330-
331- // serviceAccountToken returns a token for the specified service account in the given namespace.
332- // It uses the Kubernetes TokenRequest API to generate a token by directly sending a request
333- // and parsing the resulting token from the API response.
334- func serviceAccountToken () (string , error ) {
335-
336- const tokenRequestRawString = `{
337- "apiVersion": "authentication.k8s.io/v1",
338- "kind": "TokenRequest"
339- }`
340-
341- // Temporary file to store the token request
342- secretName := serviceAccountName + "-token-request"
343- tokenRequestFile := filepath .Join ("/tmp" , secretName )
344- err := os .WriteFile (tokenRequestFile , []byte (tokenRequestRawString ), os .FileMode (0o644 ))
345- if err != nil {
346- return "" , err
347- }
348-
349- var out string
350- verifyTokenCreation := func (g Gomega ) {
351- // Execute kubectl command to create the token
352- cmd := exec .Command ("kubectl" , "create" , "--raw" , fmt .Sprintf (
353- "/api/v1/namespaces/%s/serviceaccounts/%s/token" ,
354- namespace ,
355- serviceAccountName ,
356- ), "-f" , tokenRequestFile )
357-
358- output , err := cmd .CombinedOutput ()
359- g .Expect (err ).NotTo (HaveOccurred ())
360-
361- // Parse the JSON output to extract the token
362- var token tokenRequest
363- err = json .Unmarshal (output , & token )
364- g .Expect (err ).NotTo (HaveOccurred ())
365-
366- out = token .Status .Token
367- }
368- Eventually (verifyTokenCreation ).Should (Succeed ())
369-
370- return out , err
371- }
372-
373- // getMetricsOutput retrieves and returns the logs from the curl pod used to access the metrics endpoint.
374- func getMetricsOutput () string {
375- By ("getting the curl-metrics logs" )
376- cmd := exec .Command ("kubectl" , "logs" , "curl-metrics" , "-n" , namespace )
377- metricsOutput , err := utils .Run (cmd )
378- Expect (err ).NotTo (HaveOccurred (), "Failed to retrieve logs from curl pod" )
379- Expect (metricsOutput ).To (ContainSubstring ("< HTTP/1.1 200 OK" ))
380- return metricsOutput
381- }
382-
383- // tokenRequest is a simplified representation of the Kubernetes TokenRequest API response,
384- // containing only the token field that we need to extract.
385- type tokenRequest struct {
386- Status struct {
387- Token string `json:"token"`
388- } `json:"status"`
389- }
0 commit comments