| 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 |
"github.com/prometheus/common/model" |
| 26 |
corev1 "k8s.io/api/core/v1" |
| 27 |
"k8s.io/client-go/rest" |
| 28 |
"sigs.k8s.io/e2e-framework/klient/k8s/resources" |
| 29 |
"sigs.k8s.io/e2e-framework/pkg/envconf" |
| 30 |
) |
| 31 |
|
| 32 |
func getMetricsGaugeValue(restCfg *rest.Config, namespace string, podName string, adminPort int, metricName string) (int, error) { |
| 33 |
client, err := rest.HTTPClientFor(restCfg) |
| 34 |
if err != nil { |
| 35 |
return 0, fmt.Errorf("could not create http client from RESTConfig: %w", err) |
| 36 |
} |
| 37 |
url := fmt.Sprintf( |
| 38 |
"%v/api/v1/namespaces/%v/pods/%v:%v/proxy/metrics", |
| 39 |
restCfg.Host, |
| 40 |
namespace, |
| 41 |
podName, |
| 42 |
adminPort, |
| 43 |
) |
| 44 |
resp, err := client.Get(url) |
| 45 |
if err != nil { |
| 46 |
return 0, fmt.Errorf("could not get metrics from URL %q: %w", url, err) |
| 47 |
} else if resp.StatusCode != 200 { |
| 48 |
body, _ := io.ReadAll(resp.Body) |
| 49 |
return 0, fmt.Errorf("got invalid response from metrics endpoint %q, status code %v: %v", url, resp.StatusCode, string(body)) |
| 50 |
} |
| 51 |
|
| 52 |
metricsParser := expfmt.NewTextParser(model.LegacyValidation) |
| 53 |
metricsFamilies, err := metricsParser.TextToMetricFamilies(resp.Body) |
| 54 |
if err != nil { |
| 55 |
return 0, fmt.Errorf("could not parse metrics: %w", err) |
| 56 |
} |
| 57 |
defer resp.Body.Close() |
| 58 |
|
| 59 |
metricFamily, exists := metricsFamilies[metricName] |
| 60 |
if !exists { |
| 61 |
return 0, fmt.Errorf("metric %v does not exist", metricName) |
| 62 |
} |
| 63 |
value := metricFamily.GetMetric()[0].GetGauge().GetValue() |
| 64 |
fmt.Printf("int: %v, float64: %v\n", int(value), value) |
| 65 |
return int(value), nil |
| 66 |
} |
| 67 |
|
| 68 |
func assertAgentsAreConnected(expectedConnections int) func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 69 |
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 70 |
client := cfg.Client() |
| 71 |
|
| 72 |
agentPods := &corev1.PodList{} |
| 73 |
err := client.Resources().List(ctx, agentPods, resources.WithLabelSelector("k8s-app=konnectivity-agent")) |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("couldn't get agent pods (label selector 'k8s-app=konnectivity-agent'): %v", err) |
| 76 |
} |
| 77 |
|
| 78 |
for _, agentPod := range agentPods.Items { |
| 79 |
numConnections, err := getMetricsGaugeValue(cfg.Client().RESTConfig(), agentPod.Namespace, agentPod.Name, 8093, "konnectivity_network_proxy_agent_open_server_connections") |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("couldn't get agent metric 'konnectivity_network_proxy_agent_open_server_connections' for pod %v: %v", agentPod.Name, err) |
| 82 |
} |
| 83 |
|
| 84 |
if numConnections != expectedConnections { |
| 85 |
t.Errorf("incorrect number of connected servers (want: %d, got: %d)", expectedConnections, numConnections) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return ctx |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func assertServersAreConnected(expectedConnections int) func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 94 |
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 95 |
client := cfg.Client() |
| 96 |
|
| 97 |
serverPods := &corev1.PodList{} |
| 98 |
err := client.Resources().List(ctx, serverPods, resources.WithLabelSelector("k8s-app=konnectivity-server")) |
| 99 |
if err != nil { |
| 100 |
t.Fatalf("couldn't get server pods (label selector 'k8s-app=konnectivity-server'): %v", err) |
| 101 |
} |
| 102 |
|
| 103 |
for _, serverPod := range serverPods.Items { |
| 104 |
numConnections, err := getMetricsGaugeValue(cfg.Client().RESTConfig(), serverPod.Namespace, serverPod.Name, 8095, "konnectivity_network_proxy_server_ready_backend_connections") |
| 105 |
if err != nil { |
| 106 |
t.Fatalf("couldn't get server metric 'konnectivity_network_proxy_server_ready_backend_connections' for pod %v: %v", serverPod.Name, err) |
| 107 |
} |
| 108 |
|
| 109 |
if numConnections != expectedConnections { |
| 110 |
t.Errorf("incorrect number of connected agents (want: %d, got: %d)", expectedConnections, numConnections) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return ctx |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func assertAgentKnownServerCount(expectedServerCount int) func(context.Context, *testing.T, *envconf.Config) context.Context { |
| 119 |
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 120 |
client := cfg.Client() |
| 121 |
|
| 122 |
agentPods := &corev1.PodList{} |
| 123 |
err := client.Resources().List(ctx, agentPods, resources.WithLabelSelector("k8s-app=konnectivity-agent")) |
| 124 |
if err != nil { |
| 125 |
t.Fatalf("couldn't get server pods: %v", err) |
| 126 |
} |
| 127 |
|
| 128 |
for _, agentPod := range agentPods.Items { |
| 129 |
knownServerCount, err := getMetricsGaugeValue(cfg.Client().RESTConfig(), agentPod.Namespace, agentPod.Name, 8093, "konnectivity_network_proxy_agent_known_server_count") |
| 130 |
if err != nil { |
| 131 |
t.Fatalf("couldn't get agent metric 'konnectivity_network_proxy_agent_known_server_count' for pod %v", agentPod.Name) |
| 132 |
} |
| 133 |
|
| 134 |
if knownServerCount != expectedServerCount { |
| 135 |
t.Errorf("incorrect known server count (want: %v, got: %v)", expectedServerCount, knownServerCount) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return ctx |
| 140 |
} |
| 141 |
} |