@@ -19,12 +19,9 @@ package e2e
1919import (
2020 "fmt"
2121 "os/exec"
22- "time"
23-
24- . "github.com/onsi/ginkgo/v2"
25- . "github.com/onsi/gomega"
2622
2723 "github.com/functions-dev/func-operator/test/utils"
24+ . "github.com/onsi/ginkgo/v2"
2825)
2926
3027// namespace where the project is deployed in
@@ -33,191 +30,6 @@ const namespace = "func-operator-system"
3330// serviceAccountName created for the project
3431const serviceAccountName = "func-operator-controller-manager"
3532
36- // metricsServiceName is the name of the metrics service of the project
37- const metricsServiceName = "func-operator-controller-manager-metrics-service"
38-
39- // metricsPort is the port of the metrics service providing the managers metrics
40- const metricsPort = "8080"
41-
42- var _ = Describe ("Manager" , func () {
43- var controllerPodName string
44-
45- // After each test, check for failures and collect logs, events,
46- // and pod descriptions for debugging.
47- AfterEach (func () {
48- specReport := CurrentSpecReport ()
49- if specReport .Failed () {
50- By ("Fetching controller manager pod logs" )
51- cmd := exec .Command ("kubectl" , "logs" , controllerPodName , "-n" , namespace )
52- controllerLogs , err := utils .Run (cmd )
53- if err == nil {
54- _ , _ = fmt .Fprintf (GinkgoWriter , "Controller logs:\n %s" , controllerLogs )
55- } else {
56- _ , _ = fmt .Fprintf (GinkgoWriter , "Failed to get Controller logs: %s" , err )
57- }
58-
59- By ("Fetching Kubernetes events" )
60- cmd = exec .Command ("kubectl" , "get" , "events" , "-n" , namespace , "--sort-by=.lastTimestamp" )
61- eventsOutput , err := utils .Run (cmd )
62- if err == nil {
63- _ , _ = fmt .Fprintf (GinkgoWriter , "Kubernetes events:\n %s" , eventsOutput )
64- } else {
65- _ , _ = fmt .Fprintf (GinkgoWriter , "Failed to get Kubernetes events: %s" , err )
66- }
67-
68- By ("Fetching curl-metrics logs" )
69- cmd = exec .Command ("kubectl" , "logs" , "curl-metrics" , "-n" , namespace )
70- metricsOutput , err := utils .Run (cmd )
71- if err == nil {
72- _ , _ = fmt .Fprintf (GinkgoWriter , "Metrics logs:\n %s" , metricsOutput )
73- } else {
74- _ , _ = fmt .Fprintf (GinkgoWriter , "Failed to get curl-metrics logs: %s" , err )
75- }
76-
77- By ("Fetching controller manager pod description" )
78- cmd = exec .Command ("kubectl" , "describe" , "pod" , controllerPodName , "-n" , namespace )
79- podDescription , err := utils .Run (cmd )
80- if err == nil {
81- fmt .Println ("Pod description:\n " , podDescription )
82- } else {
83- fmt .Println ("Failed to describe controller pod" )
84- }
85- }
86- })
87-
88- SetDefaultEventuallyTimeout (2 * time .Minute )
89- SetDefaultEventuallyPollingInterval (time .Second )
90-
91- Context ("Manager" , func () {
92- It ("should run successfully" , func () {
93- By ("validating that the controller-manager pod is running as expected" )
94- verifyControllerUp := func (g Gomega ) {
95- // Get the name of the controller-manager pod
96- cmd := exec .Command ("kubectl" , "get" ,
97- "pods" , "-l" , "control-plane=controller-manager" ,
98- "-o" , "go-template={{ range .items }}" +
99- "{{ if not .metadata.deletionTimestamp }}" +
100- "{{ .metadata.name }}" +
101- "{{ \" \\ n\" }}{{ end }}{{ end }}" ,
102- "-n" , namespace ,
103- )
104-
105- podOutput , err := utils .Run (cmd )
106- g .Expect (err ).NotTo (HaveOccurred (), "Failed to retrieve controller-manager pod information" )
107- podNames := utils .GetNonEmptyLines (podOutput )
108- g .Expect (podNames ).To (HaveLen (1 ), "expected 1 controller pod running" )
109- controllerPodName = podNames [0 ]
110- g .Expect (controllerPodName ).To (ContainSubstring ("controller-manager" ))
111-
112- // Validate the pod's status
113- cmd = exec .Command ("kubectl" , "get" ,
114- "pods" , controllerPodName , "-o" , "jsonpath={.status.phase}" ,
115- "-n" , namespace ,
116- )
117- output , err := utils .Run (cmd )
118- g .Expect (err ).NotTo (HaveOccurred ())
119- g .Expect (output ).To (Equal ("Running" ), "Incorrect controller-manager pod status" )
120- }
121- Eventually (verifyControllerUp ).Should (Succeed ())
122- })
123-
124- Context ("with curl-metrics-pod" , func () {
125- curlMetricPodName := "curl-metrics"
126-
127- AfterEach (func () {
128- cmd := exec .Command ("kubectl" , "delete" , "pod" , curlMetricPodName , "-n" , namespace , "--ignore-not-found" )
129- _ , err := utils .Run (cmd )
130- Expect (err ).NotTo (HaveOccurred ())
131- })
132-
133- It ("should ensure the metrics endpoint is serving metrics" , func () {
134- By ("validating that the metrics service is available" )
135- cmd := exec .Command ("kubectl" , "get" , "service" , metricsServiceName , "-n" , namespace )
136- _ , err := utils .Run (cmd )
137- Expect (err ).NotTo (HaveOccurred (), "Metrics service should exist" )
138-
139- By ("waiting for the metrics endpoint to be ready" )
140- verifyMetricsEndpointReady := func (g Gomega ) {
141- cmd := exec .Command ("kubectl" , "get" , "endpoints" , metricsServiceName , "-n" , namespace )
142- output , err := utils .Run (cmd )
143- g .Expect (err ).NotTo (HaveOccurred ())
144- g .Expect (output ).To (ContainSubstring (metricsPort ), "Metrics endpoint is not ready" )
145- }
146- Eventually (verifyMetricsEndpointReady ).Should (Succeed ())
147-
148- By ("verifying that the controller manager is serving the metrics server" )
149- verifyMetricsServerStarted := func (g Gomega ) {
150- cmd := exec .Command ("kubectl" , "logs" , controllerPodName , "-n" , namespace )
151- output , err := utils .Run (cmd )
152- g .Expect (err ).NotTo (HaveOccurred ())
153- g .Expect (output ).To (ContainSubstring ("controller-runtime.metrics\t Serving metrics server" ),
154- "Metrics server not yet started" )
155- }
156- Eventually (verifyMetricsServerStarted ).Should (Succeed ())
157-
158- By ("creating the curl-metrics pod to access the metrics endpoint" )
159- cmd = exec .Command ("kubectl" , "run" , curlMetricPodName , "--restart=Never" ,
160- "--namespace" , namespace ,
161- "--image=curlimages/curl:latest" ,
162- "--overrides" ,
163- fmt .Sprintf (`{
164- "spec": {
165- "containers": [{
166- "name": "curl",
167- "image": "curlimages/curl:latest",
168- "command": ["/bin/sh", "-c"],
169- "args": ["curl -v %s.%s.svc.cluster.local:%s/metrics"],
170- "securityContext": {
171- "allowPrivilegeEscalation": false,
172- "capabilities": {
173- "drop": ["ALL"]
174- },
175- "runAsNonRoot": true,
176- "runAsUser": 1000,
177- "seccompProfile": {
178- "type": "RuntimeDefault"
179- }
180- }
181- }],
182- "serviceAccount": "%s"
183- }
184- }` , metricsServiceName , namespace , metricsPort , serviceAccountName ))
185- _ , err = utils .Run (cmd )
186- Expect (err ).NotTo (HaveOccurred (), "Failed to create curl-metrics pod" )
187-
188- By ("waiting for the curl-metrics pod to complete." )
189- verifyCurlUp := func (g Gomega ) {
190- cmd := exec .Command ("kubectl" , "get" , "pods" , curlMetricPodName ,
191- "-o" , "jsonpath={.status.phase}" ,
192- "-n" , namespace )
193- output , err := utils .Run (cmd )
194- g .Expect (err ).NotTo (HaveOccurred ())
195- g .Expect (output ).To (Equal ("Succeeded" ), "curl pod in wrong status" )
196- }
197- Eventually (verifyCurlUp , 5 * time .Minute ).Should (Succeed ())
198-
199- By ("getting the metrics by checking curl-metrics logs" )
200- metricsOutput := getMetricsOutput ()
201- Expect (metricsOutput ).To (ContainSubstring (
202- "controller_runtime_reconcile_total" ,
203- ))
204- })
205- })
206-
207- // +kubebuilder:scaffold:e2e-webhooks-checks
208- })
209- })
210-
211- // getMetricsOutput retrieves and returns the logs from the curl pod used to access the metrics endpoint.
212- func getMetricsOutput () string {
213- By ("getting the curl-metrics logs" )
214- cmd := exec .Command ("kubectl" , "logs" , "curl-metrics" , "-n" , namespace )
215- metricsOutput , err := utils .Run (cmd )
216- Expect (err ).NotTo (HaveOccurred (), "Failed to retrieve logs from curl pod" )
217- Expect (metricsOutput ).To (ContainSubstring ("< HTTP/1.1 200 OK" ))
218- return metricsOutput
219- }
220-
22133// logFailedTestDetails logs function resource and controller logs on test failure
22234func logFailedTestDetails (functionName , functionNamespace string ) {
22335 specReport := CurrentSpecReport ()
0 commit comments