| 1 | /* |
| 2 | Copyright 2024 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | package e2e |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "testing" |
| 23 | |
| 24 | "github.com/prometheus/common/expfmt" |
| 25 | corev1 "k8s.io/api/core/v1" |
| 26 | "k8s.io/client-go/rest" |
| 27 | "sigs.k8s.io/e2e-framework/klient/k8s/resources" |
| 28 | "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 29 | ) |
| 30 | |
| 31 | func getMetricsGaugeValue(restCfg *rest.Config, namespace string, podName string, adminPort int, metricName string) (int, error) { |
| 32 | client, err := rest.HTTPClientFor(restCfg) |
| 33 | if err != nil { |
| 34 | return 0, fmt.Errorf("could not create http client from RESTConfig: %w", err) |
| 35 | } |
| 36 | url := fmt.Sprintf( |
| 37 | "%v/api/v1/namespaces/%v/pods/%v:%v/proxy/metrics", |
| 38 | restCfg.Host, |
| 39 | namespace, |
| 40 | podName, |
| 41 | adminPort, |
| 42 | ) |
| 43 | resp, err := client.Get(url) |
| 44 | if err != nil { |
| 45 | return 0, fmt.Errorf("could not get metrics from URL %q: %w", url, err) |
| 46 | } else if resp.StatusCode != 200 { |
| 47 | body, _ := io.ReadAll(resp.Body) |
| 48 | return 0, fmt.Errorf("got invalid response from metrics endpoint %q, status code %v: %v", url, resp.StatusCode, string(body)) |
| 49 | } |
| 50 | |
| 51 | metricsParser := &expfmt.TextParser{} |
| 52 | metricsFamilies, err := metricsParser.TextToMetricFamilies(resp.Body) |
| 53 | if err != nil { |
| 54 | return 0, fmt.Errorf("could not parse metrics: %w", err) |
| 55 | } |
| 56 | defer resp.Body.Close() |
| 57 | |
| 58 | metricFamily, exists := metricsFamilies[metricName] |
| 59 | if !exists { |
| 60 | return 0, fmt.Errorf("metric %v does not exist", metricName) |
| 61 | } |
| 62 | value := metricFamily.GetMetric()[0].GetGauge().GetValue() |
| 63 | fmt.Printf("int: %v, float64: %v\n", int(value), value) |
| 64 | return int(value), nil |
| 65 | } |
| 66 | |
| 67 | func assertAgentsAreConnected(expectedConnections int) func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 68 | return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 69 | client := cfg.Client() |
| 70 | |
| 71 | agentPods := &corev1.PodList{} |
| 72 | err := client.Resources().List(ctx, agentPods, resources.WithLabelSelector("k8s-app=konnectivity-agent")) |
| 73 | if err != nil { |
| 74 | t.Fatalf("couldn't get agent pods (label selector 'k8s-app=konnectivity-agent'): %v", err) |
| 75 | } |
| 76 | |
| 77 | for _, agentPod := range agentPods.Items { |
| 78 | numConnections, err := getMetricsGaugeValue(cfg.Client().RESTConfig(), agentPod.Namespace, agentPod.Name, 8093, "konnectivity_network_proxy_agent_open_server_connections") |
| 79 | if err != nil { |
| 80 | t.Fatalf("couldn't get agent metric 'konnectivity_network_proxy_agent_open_server_connections' for pod %v: %v", agentPod.Name, err) |
| 81 | } |
| 82 | |
| 83 | if numConnections != expectedConnections { |
| 84 | t.Errorf("incorrect number of connected servers (want: %d, got: %d)", expectedConnections, numConnections) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return ctx |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func assertServersAreConnected(expectedConnections int) func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 93 | return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 94 | client := cfg.Client() |
| 95 | |
| 96 | serverPods := &corev1.PodList{} |
| 97 | err := client.Resources().List(ctx, serverPods, resources.WithLabelSelector("k8s-app=konnectivity-server")) |
| 98 | if err != nil { |
| 99 | t.Fatalf("couldn't get server pods (label selector 'k8s-app=konnectivity-server'): %v", err) |
| 100 | } |
| 101 | |
| 102 | for _, serverPod := range serverPods.Items { |
| 103 | numConnections, err := getMetricsGaugeValue(cfg.Client().RESTConfig(), serverPod.Namespace, serverPod.Name, 8095, "konnectivity_network_proxy_server_ready_backend_connections") |
| 104 | if err != nil { |
| 105 | t.Fatalf("couldn't get server metric 'konnectivity_network_proxy_server_ready_backend_connections' for pod %v: %v", serverPod.Name, err) |
| 106 | } |
| 107 | |
| 108 | if numConnections != expectedConnections { |
| 109 | t.Errorf("incorrect number of connected agents (want: %d, got: %d)", expectedConnections, numConnections) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return ctx |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func assertAgentKnownServerCount(expectedServerCount int) func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 118 | return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 119 | client := cfg.Client() |
| 120 | |
| 121 | agentPods := &corev1.PodList{} |
| 122 | err := client.Resources().List(ctx, agentPods, resources.WithLabelSelector("k8s-app=konnectivity-agent")) |
| 123 | if err != nil { |
| 124 | t.Fatalf("couldn't get server pods: %v", err) |
| 125 | } |
| 126 | |
| 127 | for _, agentPod := range agentPods.Items { |
| 128 | knownServerCount, err := getMetricsGaugeValue(cfg.Client().RESTConfig(), agentPod.Namespace, agentPod.Name, 8093, "konnectivity_network_proxy_agent_known_server_count") |
| 129 | if err != nil { |
| 130 | t.Fatalf("couldn't get agent metric 'konnectivity_network_proxy_agent_known_server_count' for pod %v", agentPod.Name) |
| 131 | } |
| 132 | |
| 133 | if knownServerCount != expectedServerCount { |
| 134 | t.Errorf("incorrect known server count (want: %v, got: %v)", expectedServerCount, knownServerCount) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return ctx |
| 139 | } |
| 140 | } |