k8s.io/kubernetes@v1.29.3/test/e2e/instrumentation/metrics.go (about)

     1  /*
     2  Copyright 2023 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 instrumentation
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"time"
    23  
    24  	"github.com/onsi/gomega"
    25  	clientset "k8s.io/client-go/kubernetes"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	e2emetrics "k8s.io/kubernetes/test/e2e/framework/metrics"
    28  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    29  	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
    30  	"k8s.io/kubernetes/test/e2e/instrumentation/common"
    31  	admissionapi "k8s.io/pod-security-admission/api"
    32  
    33  	"github.com/onsi/ginkgo/v2"
    34  )
    35  
    36  var _ = common.SIGDescribe("Metrics", func() {
    37  	f := framework.NewDefaultFramework("metrics")
    38  	f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
    39  	var c, ec clientset.Interface
    40  	var grabber *e2emetrics.Grabber
    41  	ginkgo.BeforeEach(func(ctx context.Context) {
    42  		var err error
    43  		c = f.ClientSet
    44  		ec = f.KubemarkExternalClusterClientSet
    45  		gomega.Eventually(ctx, func() error {
    46  			grabber, err = e2emetrics.NewMetricsGrabber(ctx, c, ec, f.ClientConfig(), true, true, true, true, true, true)
    47  			if err != nil {
    48  				framework.ExpectNoError(err, "failed to create metrics grabber")
    49  			}
    50  			return nil
    51  		}, 5*time.Minute, 10*time.Second).Should(gomega.BeNil())
    52  	})
    53  
    54  	/*
    55  	   Release: v1.29
    56  	   Testname: Kubelet resource metrics
    57  	   Description: Should attempt to grab all resource metrics from kubelet metrics/resource endpoint.
    58  	*/
    59  	ginkgo.It("should grab all metrics from kubelet /metrics/resource endpoint", func(ctx context.Context) {
    60  		ginkgo.By("Connecting to kubelet's /metrics/resource endpoint")
    61  		node, err := e2enode.GetRandomReadySchedulableNode(ctx, f.ClientSet)
    62  		if errors.Is(err, e2emetrics.MetricsGrabbingDisabledError) {
    63  			e2eskipper.Skipf("%v", err)
    64  		}
    65  		framework.ExpectNoError(err)
    66  		response, err := grabber.GrabResourceMetricsFromKubelet(ctx, node.Name)
    67  		framework.ExpectNoError(err)
    68  		gomega.Expect(response).NotTo(gomega.BeEmpty())
    69  	})
    70  })