Skip to content

Commit 9b9a3d7

Browse files
committed
test/e2e: add kube-proxy metrics grabber
Signed-off-by: Daman Arora <[email protected]>
1 parent bc8b90b commit 9b9a3d7

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
17+
package metrics
18+
19+
import (
20+
"k8s.io/component-base/metrics/testutil"
21+
)
22+
23+
// KubeProxyMetrics is metrics for kube-proxy
24+
type KubeProxyMetrics testutil.Metrics
25+
26+
// GetCounterMetricValue returns value for metric type counter.
27+
func (m *KubeProxyMetrics) GetCounterMetricValue(metricName string) float64 {
28+
return float64(testutil.Metrics(*m)[metricName][0].Value)
29+
}
30+
31+
func newKubeProxyMetricsMetrics() KubeProxyMetrics {
32+
result := testutil.NewMetrics()
33+
return KubeProxyMetrics(result)
34+
}
35+
36+
func parseKubeProxyMetrics(data string) (KubeProxyMetrics, error) {
37+
result := newKubeProxyMetricsMetrics()
38+
if err := testutil.ParseMetrics(data, (*testutil.Metrics)(&result)); err != nil {
39+
return KubeProxyMetrics{}, err
40+
}
41+
return result, nil
42+
}

test/e2e/framework/metrics/metrics_grabber.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net"
2424
"regexp"
25+
"strconv"
2526
"sync"
2627
"time"
2728

@@ -32,8 +33,9 @@ import (
3233
clientset "k8s.io/client-go/kubernetes"
3334
"k8s.io/client-go/rest"
3435
"k8s.io/klog/v2"
35-
36+
"k8s.io/kubernetes/test/e2e/framework"
3637
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
38+
e2epodoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
3739
)
3840

3941
const (
@@ -43,6 +45,8 @@ const (
4345
kubeControllerManagerPort = 10257
4446
// snapshotControllerPort is the port for the snapshot controller
4547
snapshotControllerPort = 9102
48+
// kubeProxyPort is the default port for the kube-proxy status server.
49+
kubeProxyPort = 10249
4650
)
4751

4852
// MetricsGrabbingDisabledError is an error that is wrapped by the
@@ -233,6 +237,45 @@ func (g *Grabber) getMetricsFromNode(ctx context.Context, nodeName string, kubel
233237
}
234238
}
235239

240+
// GrabFromKubeProxy returns metrics from kube-proxy
241+
func (g *Grabber) GrabFromKubeProxy(ctx context.Context, nodeName string) (KubeProxyMetrics, error) {
242+
nodes, err := g.client.CoreV1().Nodes().List(ctx, metav1.ListOptions{FieldSelector: fields.Set{"metadata.name": nodeName}.AsSelector().String()})
243+
if err != nil {
244+
return KubeProxyMetrics{}, err
245+
}
246+
247+
if len(nodes.Items) != 1 {
248+
return KubeProxyMetrics{}, fmt.Errorf("error listing nodes with name %v, got %v", nodeName, nodes.Items)
249+
}
250+
output, err := g.grabFromKubeProxy(ctx, nodeName)
251+
if err != nil {
252+
return KubeProxyMetrics{}, err
253+
}
254+
return parseKubeProxyMetrics(output)
255+
}
256+
257+
func (g *Grabber) grabFromKubeProxy(ctx context.Context, nodeName string) (string, error) {
258+
hostCmdPodName := fmt.Sprintf("grab-kube-proxy-metrics-%s", framework.RandomSuffix())
259+
hostCmdPod := e2epod.NewExecPodSpec(metav1.NamespaceSystem, hostCmdPodName, true)
260+
nodeSelection := e2epod.NodeSelection{Name: nodeName}
261+
e2epod.SetNodeSelection(&hostCmdPod.Spec, nodeSelection)
262+
if _, err := g.client.CoreV1().Pods(metav1.NamespaceSystem).Create(ctx, hostCmdPod, metav1.CreateOptions{}); err != nil {
263+
return "", fmt.Errorf("failed to create pod to fetch metrics: %w", err)
264+
}
265+
if err := e2epod.WaitTimeoutForPodReadyInNamespace(ctx, g.client, hostCmdPodName, metav1.NamespaceSystem, 5*time.Minute); err != nil {
266+
return "", fmt.Errorf("error waiting for pod to be up: %w", err)
267+
}
268+
269+
host := "127.0.0.1"
270+
if framework.TestContext.ClusterIsIPv6() {
271+
host = "::1"
272+
}
273+
274+
stdout, err := e2epodoutput.RunHostCmd(metav1.NamespaceSystem, hostCmdPodName, fmt.Sprintf("curl --silent %s/metrics", net.JoinHostPort(host, strconv.Itoa(kubeProxyPort))))
275+
_ = g.client.CoreV1().Pods(metav1.NamespaceSystem).Delete(ctx, hostCmdPodName, metav1.DeleteOptions{})
276+
return stdout, err
277+
}
278+
236279
// GrabFromScheduler returns metrics from scheduler
237280
func (g *Grabber) GrabFromScheduler(ctx context.Context) (SchedulerMetrics, error) {
238281
if !g.grabFromScheduler {

0 commit comments

Comments
 (0)