|
1 | 1 | package metrics
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/base64" |
4 | 5 | "fmt"
|
| 6 | + "strings" |
5 | 7 | "time"
|
6 | 8 |
|
7 | 9 | "github.com/onsi/ginkgo/v2"
|
8 | 10 | "github.com/onsi/gomega"
|
9 | 11 |
|
10 | 12 | "github.com/operator-framework/ansible-operator-plugins/pkg/testutils/kubernetes"
|
11 | 13 | "github.com/operator-framework/ansible-operator-plugins/pkg/testutils/sample"
|
| 14 | + "github.com/operator-framework/ansible-operator-plugins/test/common" |
12 | 15 | )
|
13 | 16 |
|
14 | 17 | // GetMetrics creates a pod with the permissions to `curl` metrics. It will then return the output of the `curl` pod
|
15 | 18 | func GetMetrics(sample sample.Sample, kubectl kubernetes.Kubectl, metricsClusterRoleBindingName string) string {
|
| 19 | + ginkgo.By("granting permissions to access the metrics and read the token") |
| 20 | + out, err := kubectl.Command("create", "clusterrolebinding", metricsClusterRoleBindingName, |
| 21 | + fmt.Sprintf("--clusterrole=%s-metrics-reader", sample.Name()), |
| 22 | + fmt.Sprintf("--serviceaccount=%s:%s", kubectl.Namespace(), kubectl.ServiceAccount())) |
| 23 | + fmt.Println("OUT --", out) |
| 24 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 25 | + |
| 26 | + // As of Kubernetes 1.24 a ServiceAccount no longer has a ServiceAccount token secret autogenerated. We have to create it manually here |
| 27 | + ginkgo.By("Creating the ServiceAccount token") |
| 28 | + secretFile, err := common.GetSASecret(kubectl.ServiceAccount(), sample.Dir()) |
| 29 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 30 | + gomega.Eventually(func() error { |
| 31 | + out, err = kubectl.Apply(true, "-f", secretFile) |
| 32 | + fmt.Println("OUT -- ", out) |
| 33 | + return err |
| 34 | + }, time.Minute, time.Second).Should(gomega.Succeed()) |
| 35 | + |
| 36 | + ginkgo.By("reading the metrics token") |
| 37 | + // Filter token query by service account in case more than one exists in a namespace. |
| 38 | + query := fmt.Sprintf(`{.items[?(@.metadata.annotations.kubernetes\.io/service-account\.name=="%s")].data.token}`, |
| 39 | + kubectl.ServiceAccount(), |
| 40 | + ) |
| 41 | + out, err = kubectl.Get(true, "secrets") |
| 42 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 43 | + fmt.Println("OUT --", out) |
| 44 | + b64Token, err := kubectl.Get(true, "secrets", "-o=jsonpath="+query) |
| 45 | + fmt.Println("OUT--", b64Token) |
| 46 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 47 | + token, err := base64.StdEncoding.DecodeString(strings.TrimSpace(b64Token)) |
| 48 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 49 | + gomega.Expect(len(token)).To(gomega.BeNumerically(">", 0)) |
| 50 | + |
16 | 51 | ginkgo.By("creating a curl pod")
|
17 | 52 | cmdOpts := []string{
|
18 | 53 | "run", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", "--",
|
19 |
| - "curl", "-v", |
20 |
| - fmt.Sprintf("http://%s-controller-manager-metrics-service.%s.svc:8443/metrics", sample.Name(), kubectl.Namespace()), |
| 54 | + "curl", "-v", "-k", "-H", fmt.Sprintf(`Authorization: Bearer %s`, token), |
| 55 | + fmt.Sprintf("https://%s-controller-manager-metrics-service.%s.svc:8443/metrics", sample.Name(), kubectl.Namespace()), |
21 | 56 | }
|
22 |
| - out, err := kubectl.CommandInNamespace(cmdOpts...) |
| 57 | + out, err = kubectl.CommandInNamespace(cmdOpts...) |
23 | 58 | fmt.Println("OUT --", out)
|
24 | 59 | gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
25 | 60 |
|
@@ -58,5 +93,10 @@ func CleanUpMetrics(kubectl kubernetes.Kubectl, metricsClusterRoleBindingName st
|
58 | 93 | return fmt.Errorf("encountered an error when deleting the metrics pod: %w", err)
|
59 | 94 | }
|
60 | 95 |
|
| 96 | + _, err = kubectl.Delete(false, "clusterrolebinding", metricsClusterRoleBindingName) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("encountered an error when deleting the metrics clusterrolebinding: %w", err) |
| 99 | + } |
| 100 | + |
61 | 101 | return nil
|
62 | 102 | }
|
0 commit comments