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

     1  /*
     2  Copyright 2019 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 e2e
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"path"
    24  	"time"
    25  
    26  	clientset "k8s.io/client-go/kubernetes"
    27  	"k8s.io/kubernetes/test/e2e/framework"
    28  	e2emetrics "k8s.io/kubernetes/test/e2e/framework/metrics"
    29  )
    30  
    31  // AfterSuiteActions are actions that are run on ginkgo's SynchronizedAfterSuite
    32  func AfterSuiteActions(ctx context.Context) {
    33  	// Run only Ginkgo on node 1
    34  	framework.Logf("Running AfterSuite actions on node 1")
    35  	if framework.TestContext.ReportDir != "" {
    36  		framework.CoreDump(framework.TestContext.ReportDir)
    37  	}
    38  	if framework.TestContext.GatherSuiteMetricsAfterTest {
    39  		if err := gatherTestSuiteMetrics(ctx); err != nil {
    40  			framework.Logf("Error gathering metrics: %v", err)
    41  		}
    42  	}
    43  	if framework.TestContext.NodeKiller.NodeKillerStop != nil {
    44  		framework.TestContext.NodeKiller.NodeKillerStop()
    45  	}
    46  }
    47  
    48  func gatherTestSuiteMetrics(ctx context.Context) error {
    49  	framework.Logf("Gathering metrics")
    50  	config, err := framework.LoadConfig()
    51  	if err != nil {
    52  		return fmt.Errorf("error loading client config: %w", err)
    53  	}
    54  	c, err := clientset.NewForConfig(config)
    55  	if err != nil {
    56  		return fmt.Errorf("error creating client: %w", err)
    57  	}
    58  
    59  	// Grab metrics for apiserver, scheduler, controller-manager, kubelet (for non-kubemark case) and cluster autoscaler (optionally).
    60  	grabber, err := e2emetrics.NewMetricsGrabber(ctx, c, nil, config, !framework.ProviderIs("kubemark"), true, true, true, framework.TestContext.IncludeClusterAutoscalerMetrics, false)
    61  	if err != nil {
    62  		return fmt.Errorf("failed to create MetricsGrabber: %w", err)
    63  	}
    64  
    65  	received, err := grabber.Grab(ctx)
    66  	if err != nil {
    67  		return fmt.Errorf("failed to grab metrics: %w", err)
    68  	}
    69  
    70  	metricsForE2E := (*e2emetrics.ComponentCollection)(&received)
    71  	metricsJSON := metricsForE2E.PrintJSON()
    72  	if framework.TestContext.ReportDir != "" {
    73  		filePath := path.Join(framework.TestContext.ReportDir, "MetricsForE2ESuite_"+time.Now().Format(time.RFC3339)+".json")
    74  		if err := os.WriteFile(filePath, []byte(metricsJSON), 0644); err != nil {
    75  			return fmt.Errorf("error writing to %q: %w", filePath, err)
    76  		}
    77  	} else {
    78  		framework.Logf("\n\nTest Suite Metrics:\n%s\n", metricsJSON)
    79  	}
    80  
    81  	return nil
    82  }