github.com/nmstate/kubernetes-nmstate@v0.82.0/test/e2e/handler/metrics.go (about) 1 /* 2 Copyright The Kubernetes NMState Authors. 3 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 */ 17 18 package handler 19 20 import ( 21 "strings" 22 23 "sigs.k8s.io/controller-runtime/pkg/metrics" 24 25 "github.com/nmstate/kubernetes-nmstate/test/cmd" 26 "github.com/nmstate/kubernetes-nmstate/test/runner" 27 ) 28 29 func getMetrics(token string) map[string]string { 30 bearer := "Authorization: Bearer " + token 31 return indexMetrics(runner.RunAtMetricsPod("curl", "-s", "-k", "--header", 32 bearer, metrics.DefaultBindAddress, "https://127.0.0.1:8443/metrics")) 33 } 34 35 func getPrometheusToken() (string, error) { 36 const ( 37 monitoringNamespace = "monitoring" 38 prometheusPod = "prometheus-k8s-0" 39 container = "prometheus" 40 tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" // #nosec G101 41 ) 42 43 return cmd.Kubectl("exec", "-n", monitoringNamespace, prometheusPod, "-c", container, "--", "cat", tokenPath) 44 } 45 46 func indexMetrics(metrics string) map[string]string { 47 metricsMap := map[string]string{} 48 for _, metric := range strings.Split(metrics, "\n") { 49 if strings.Contains(metric, "#") { // Ignore comments 50 continue 51 } 52 metricSplit := strings.Split(metric, " ") 53 if len(metricSplit) != 2 { 54 continue 55 } 56 metricsMap[metricSplit[0]] = metricSplit[1] 57 } 58 return metricsMap 59 }