k8s.io/kubernetes@v1.29.3/test/e2e/framework/debug/init/init.go (about)

     1  /*
     2  Copyright 2022 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 init sets debug.DumpAllNamespaceInfo as implementation in the framework
    18  // and enables log size verification and resource gathering.
    19  package init
    20  
    21  import (
    22  	"context"
    23  	"sync"
    24  	"time"
    25  
    26  	"github.com/onsi/ginkgo/v2"
    27  	"k8s.io/kubernetes/test/e2e/framework"
    28  	e2edebug "k8s.io/kubernetes/test/e2e/framework/debug"
    29  )
    30  
    31  func init() {
    32  	framework.NewFrameworkExtensions = append(framework.NewFrameworkExtensions,
    33  		func(f *framework.Framework) {
    34  			f.DumpAllNamespaceInfo = func(ctx context.Context, f *framework.Framework, ns string) {
    35  				e2edebug.DumpAllNamespaceInfo(ctx, f.ClientSet, ns)
    36  			}
    37  
    38  			if framework.TestContext.GatherLogsSizes {
    39  				ginkgo.BeforeEach(func() {
    40  					var wg sync.WaitGroup
    41  					wg.Add(1)
    42  					ctx, cancel := context.WithCancel(context.Background())
    43  					verifier := e2edebug.NewLogsVerifier(ctx, f.ClientSet)
    44  					go func() {
    45  						defer wg.Done()
    46  						verifier.Run(ctx)
    47  					}()
    48  					ginkgo.DeferCleanup(func() {
    49  						ginkgo.By("Gathering log sizes data", func() {
    50  							cancel()
    51  							wg.Wait()
    52  							f.TestSummaries = append(f.TestSummaries, verifier.GetSummary())
    53  						})
    54  					})
    55  				})
    56  			}
    57  
    58  			if framework.TestContext.GatherKubeSystemResourceUsageData != "false" &&
    59  				framework.TestContext.GatherKubeSystemResourceUsageData != "none" {
    60  				ginkgo.BeforeEach(func(ctx context.Context) {
    61  					var nodeMode e2edebug.NodesSet
    62  					switch framework.TestContext.GatherKubeSystemResourceUsageData {
    63  					case "master":
    64  						nodeMode = e2edebug.MasterNodes
    65  					case "masteranddns":
    66  						nodeMode = e2edebug.MasterAndDNSNodes
    67  					default:
    68  						nodeMode = e2edebug.AllNodes
    69  					}
    70  
    71  					gatherer, err := e2edebug.NewResourceUsageGatherer(ctx, f.ClientSet, e2edebug.ResourceGathererOptions{
    72  						InKubemark:                  framework.ProviderIs("kubemark"),
    73  						Nodes:                       nodeMode,
    74  						ResourceDataGatheringPeriod: 60 * time.Second,
    75  						ProbeDuration:               15 * time.Second,
    76  						PrintVerboseLogs:            false,
    77  					}, nil)
    78  					if err != nil {
    79  						framework.Logf("Error while creating NewResourceUsageGatherer: %v", err)
    80  						return
    81  					}
    82  
    83  					go gatherer.StartGatheringData(ctx)
    84  					ginkgo.DeferCleanup(func() {
    85  						ginkgo.By("Collecting resource usage data", func() {
    86  							summary, resourceViolationError := gatherer.StopAndSummarize([]int{90, 99, 100}, nil /* no constraints */)
    87  							// Always record the summary, even if there was an error.
    88  							f.TestSummaries = append(f.TestSummaries, summary)
    89  							// Now fail if there was an error.
    90  							framework.ExpectNoError(resourceViolationError)
    91  						})
    92  					})
    93  				})
    94  			}
    95  		},
    96  	)
    97  }