k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/provider/gce.go (about)

     1  /*
     2  Copyright 2020 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 provider
    18  
    19  import (
    20  	"fmt"
    21  	"os/exec"
    22  	"strings"
    23  
    24  	clientset "k8s.io/client-go/kubernetes"
    25  	"k8s.io/klog/v2"
    26  	sshutil "k8s.io/kubernetes/test/e2e/framework/ssh"
    27  	"k8s.io/perf-tests/clusterloader2/pkg/framework/client"
    28  	prom "k8s.io/perf-tests/clusterloader2/pkg/prometheus/clients"
    29  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    30  )
    31  
    32  type GCEProvider struct {
    33  	features Features
    34  }
    35  
    36  func NewGCEProvider(_ map[string]string) Provider {
    37  	return &GCEProvider{
    38  		features: Features{
    39  			SupportWindowsNodeScraping:          true,
    40  			SupportProbe:                        true,
    41  			SupportSSHToMaster:                  true,
    42  			SupportImagePreload:                 true,
    43  			SupportSnapshotPrometheusDisk:       true,
    44  			SupportNodeKiller:                   true,
    45  			SupportEnablePrometheusServer:       true,
    46  			SupportGrabMetricsFromKubelets:      true,
    47  			SupportAccessAPIServerPprofEndpoint: true,
    48  			SupportKubeStateMetrics:             true,
    49  			SupportMetricsServerMetrics:         true,
    50  			SupportResourceUsageMetering:        true,
    51  			ShouldScrapeKubeProxy:               true,
    52  		},
    53  	}
    54  }
    55  
    56  func (p *GCEProvider) Name() string {
    57  	return GCEName
    58  }
    59  
    60  func (p *GCEProvider) Features() *Features {
    61  	return &p.features
    62  }
    63  
    64  func (p *GCEProvider) GetComponentProtocolAndPort(componentName string) (string, int, error) {
    65  	return getComponentProtocolAndPort(componentName)
    66  }
    67  
    68  func (p *GCEProvider) GetConfig() Config {
    69  	return Config{}
    70  }
    71  
    72  func (p *GCEProvider) RunSSHCommand(cmd, host string) (string, string, int, error) {
    73  	// gce provider takes ssh key from GCE_SSH_KEY.
    74  	r, err := sshutil.SSH(cmd, host, "gce")
    75  	return r.Stdout, r.Stderr, r.Code, err
    76  }
    77  
    78  func (p *GCEProvider) Metadata(c clientset.Interface) (map[string]string, error) {
    79  	nodes, err := client.ListNodes(c)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	var masterInstanceIDs []string
    85  	for _, node := range nodes {
    86  		if util.LegacyIsMasterNode(&node) || util.IsControlPlaneNode(&node) {
    87  			zone, ok := node.Labels["topology.kubernetes.io/zone"]
    88  			if !ok {
    89  				// Fallback to old label to make it work for old k8s versions.
    90  				zone, ok = node.Labels["failure-domain.beta.kubernetes.io/zone"]
    91  				if !ok {
    92  					return nil, fmt.Errorf("unknown zone for %q node: no topology-related labels", node.Name)
    93  				}
    94  			}
    95  			cmd := exec.Command("gcloud", "compute", "instances", "describe", "--format", "value(id)", "--zone", zone, node.Name)
    96  			out, err := cmd.Output()
    97  			if err != nil {
    98  				var stderr string
    99  
   100  				if ee, ok := err.(*exec.ExitError); ok {
   101  					stderr = string(ee.Stderr)
   102  				}
   103  				return nil, fmt.Errorf("fetching instanceID for %q failed with: %v (stderr: %q)", node.Name, err, stderr)
   104  			}
   105  			instanceID := strings.TrimSpace(string(out))
   106  			klog.Infof("Detected instanceID for %s/%s: %q", zone, node.Name, instanceID)
   107  			masterInstanceIDs = append(masterInstanceIDs, instanceID)
   108  		}
   109  	}
   110  
   111  	return map[string]string{"masterInstanceIDs": strings.Join(masterInstanceIDs, ",")}, nil
   112  }
   113  
   114  func (p *GCEProvider) GetManagedPrometheusClient() (prom.Client, error) {
   115  	return prom.NewGCPManagedPrometheusClient()
   116  }