@@ -17,11 +17,8 @@ limitations under the License.
1717package e2e
1818
1919import (
20- "encoding/json"
2120 "fmt"
22- "os"
2321 "os/exec"
24- "path/filepath"
2522 "time"
2623
2724 . "github.com/onsi/ginkgo/v2"
@@ -39,8 +36,8 @@ const serviceAccountName = "func-operator-controller-manager"
3936// metricsServiceName is the name of the metrics service of the project
4037const metricsServiceName = "func-operator-controller-manager-metrics-service"
4138
42- // metricsRoleBindingName is the name of the RBAC that will be created to allow get the metrics data
43- const metricsRoleBindingName = "func-operator-metrics-binding "
39+ // metricsPort is the port of the metrics service providing the managers metrics
40+ const metricsPort = "8080 "
4441
4542var _ = Describe ("Manager" , Ordered , func () {
4643 var controllerPodName string
@@ -174,30 +171,17 @@ var _ = Describe("Manager", Ordered, func() {
174171 })
175172
176173 It ("should ensure the metrics endpoint is serving metrics" , func () {
177- By ("creating a ClusterRoleBinding for the service account to allow access to metrics" )
178- cmd := exec .Command ("kubectl" , "create" , "clusterrolebinding" , metricsRoleBindingName ,
179- "--clusterrole=func-operator-metrics-reader" ,
180- fmt .Sprintf ("--serviceaccount=%s:%s" , namespace , serviceAccountName ),
181- )
182- _ , err := utils .Run (cmd )
183- Expect (err ).NotTo (HaveOccurred (), "Failed to create ClusterRoleBinding" )
184-
185174 By ("validating that the metrics service is available" )
186- cmd = exec .Command ("kubectl" , "get" , "service" , metricsServiceName , "-n" , namespace )
187- _ , err = utils .Run (cmd )
175+ cmd : = exec .Command ("kubectl" , "get" , "service" , metricsServiceName , "-n" , namespace )
176+ _ , err : = utils .Run (cmd )
188177 Expect (err ).NotTo (HaveOccurred (), "Metrics service should exist" )
189178
190- By ("getting the service account token" )
191- token , err := serviceAccountToken ()
192- Expect (err ).NotTo (HaveOccurred ())
193- Expect (token ).NotTo (BeEmpty ())
194-
195179 By ("waiting for the metrics endpoint to be ready" )
196180 verifyMetricsEndpointReady := func (g Gomega ) {
197181 cmd := exec .Command ("kubectl" , "get" , "endpoints" , metricsServiceName , "-n" , namespace )
198182 output , err := utils .Run (cmd )
199183 g .Expect (err ).NotTo (HaveOccurred ())
200- g .Expect (output ).To (ContainSubstring ("8443" ), "Metrics endpoint is not ready" )
184+ g .Expect (output ).To (ContainSubstring (metricsPort ), "Metrics endpoint is not ready" )
201185 }
202186 Eventually (verifyMetricsEndpointReady ).Should (Succeed ())
203187
@@ -222,7 +206,7 @@ var _ = Describe("Manager", Ordered, func() {
222206 "name": "curl",
223207 "image": "curlimages/curl:latest",
224208 "command": ["/bin/sh", "-c"],
225- "args": ["curl -v -k -H 'Authorization: Bearer %s' https://%s .%s.svc.cluster.local:8443 /metrics"],
209+ "args": ["curl -v %s .%s.svc.cluster.local:%s /metrics"],
226210 "securityContext": {
227211 "allowPrivilegeEscalation": false,
228212 "capabilities": {
@@ -237,7 +221,7 @@ var _ = Describe("Manager", Ordered, func() {
237221 }],
238222 "serviceAccount": "%s"
239223 }
240- }` , token , metricsServiceName , namespace , serviceAccountName ))
224+ }` , metricsServiceName , namespace , metricsPort , serviceAccountName ))
241225 _ , err = utils .Run (cmd )
242226 Expect (err ).NotTo (HaveOccurred (), "Failed to create curl-metrics pod" )
243227
@@ -272,47 +256,6 @@ var _ = Describe("Manager", Ordered, func() {
272256 })
273257})
274258
275- // serviceAccountToken returns a token for the specified service account in the given namespace.
276- // It uses the Kubernetes TokenRequest API to generate a token by directly sending a request
277- // and parsing the resulting token from the API response.
278- func serviceAccountToken () (string , error ) {
279- const tokenRequestRawString = `{
280- "apiVersion": "authentication.k8s.io/v1",
281- "kind": "TokenRequest"
282- }`
283-
284- // Temporary file to store the token request
285- secretName := fmt .Sprintf ("%s-token-request" , serviceAccountName )
286- tokenRequestFile := filepath .Join ("/tmp" , secretName )
287- err := os .WriteFile (tokenRequestFile , []byte (tokenRequestRawString ), os .FileMode (0o644 ))
288- if err != nil {
289- return "" , err
290- }
291-
292- var out string
293- verifyTokenCreation := func (g Gomega ) {
294- // Execute kubectl command to create the token
295- cmd := exec .Command ("kubectl" , "create" , "--raw" , fmt .Sprintf (
296- "/api/v1/namespaces/%s/serviceaccounts/%s/token" ,
297- namespace ,
298- serviceAccountName ,
299- ), "-f" , tokenRequestFile )
300-
301- output , err := cmd .CombinedOutput ()
302- g .Expect (err ).NotTo (HaveOccurred ())
303-
304- // Parse the JSON output to extract the token
305- var token tokenRequest
306- err = json .Unmarshal (output , & token )
307- g .Expect (err ).NotTo (HaveOccurred ())
308-
309- out = token .Status .Token
310- }
311- Eventually (verifyTokenCreation ).Should (Succeed ())
312-
313- return out , err
314- }
315-
316259// getMetricsOutput retrieves and returns the logs from the curl pod used to access the metrics endpoint.
317260func getMetricsOutput () string {
318261 By ("getting the curl-metrics logs" )
@@ -322,11 +265,3 @@ func getMetricsOutput() string {
322265 Expect (metricsOutput ).To (ContainSubstring ("< HTTP/1.1 200 OK" ))
323266 return metricsOutput
324267}
325-
326- // tokenRequest is a simplified representation of the Kubernetes TokenRequest API response,
327- // containing only the token field that we need to extract.
328- type tokenRequest struct {
329- Status struct {
330- Token string `json:"token"`
331- } `json:"status"`
332- }
0 commit comments