k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/provider/provider.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  	"strings"
    22  
    23  	clientset "k8s.io/client-go/kubernetes"
    24  	prom "k8s.io/perf-tests/clusterloader2/pkg/prometheus/clients"
    25  )
    26  
    27  // InitOptions encapsulates the fields needed to init provider.
    28  type InitOptions struct {
    29  	// TODO(#1361) remove this and use providerConfigs.
    30  	KubemarkRootKubeConfigPath string
    31  	ProviderName               string
    32  	ProviderConfigs            []string
    33  }
    34  
    35  // Features represents all features supported by this provider.
    36  type Features struct {
    37  	// Some features do not work for kubemark-like providers or have separate implementation.
    38  	IsKubemarkProvider bool
    39  	// SupportWindowsNodeScraping determines wheter scraping windows node in supported.
    40  	SupportWindowsNodeScraping bool
    41  	// SupportProbe determines whether probe is supported.
    42  	SupportProbe bool
    43  	// SupportImagePreload determines whether image preloading is supported.
    44  	SupportImagePreload bool
    45  	// SupportEnablePrometheusServer determines whether enabling prometheus server is possible.
    46  	SupportEnablePrometheusServer bool
    47  	// SupportSSHToMaster determines whether SSH access to master machines is possible.
    48  	// If false (impossible for many  providers), ClusterLoader will skip operations requiring it.
    49  	SupportSSHToMaster bool
    50  	// SupportAccessAPIServerPprofEndpoint determines whether accessing api server pprof endpoint is possible.
    51  	SupportAccessAPIServerPprofEndpoint bool
    52  	// SupportSnapshotPrometheusDisk determines whether snapshot prometheus disk is supported.
    53  	SupportSnapshotPrometheusDisk bool
    54  	// SupportNodeKiller determines whether node killer is supported.
    55  	SupportNodeKiller bool
    56  	// SupportGrabMetricsFromKubelets determines whether getting metrics from kubelet is supported.
    57  	SupportGrabMetricsFromKubelets bool
    58  	// SupportKubeStateMetrics determines if running kube-state-metrics is supported.
    59  	SupportKubeStateMetrics bool
    60  	// SupportMetricsServerMetrics determines if running metrics server is supported.
    61  	SupportMetricsServerMetrics bool
    62  	// SupportResourceUsageMetering determines if resource usage measurement is supported.
    63  	SupportResourceUsageMetering bool
    64  
    65  	// ShouldPrometheusScrapeApiserverOnly determines if we should set PROMETHEUS_SCRAPE_APISERVER_ONLY by default.
    66  	ShouldPrometheusScrapeApiserverOnly bool
    67  
    68  	// SchedulerInsecurePortDisabled determines if kube-scheduler listens on insecure port.
    69  	SchedulerInsecurePortDisabled bool
    70  
    71  	// ShouldScrapeKubeProxy determines if ScrapeKubeProxy
    72  	ShouldScrapeKubeProxy bool
    73  }
    74  
    75  // Config is the config of the provider.
    76  type Config map[string]string
    77  
    78  // RootFrameworkKubeConfigOverride returns the KubeConfig override for Root Framewor.
    79  func (c Config) RootFrameworkKubeConfigOverride() string {
    80  	return c[RootKubeConfigKey]
    81  }
    82  
    83  // Provider is the interface for
    84  type Provider interface {
    85  	// Name returns name of this provider. It should used only in logs.
    86  	Name() string
    87  	// Features returns the feature supported by this provider.
    88  	Features() *Features
    89  
    90  	GetConfig() Config
    91  
    92  	// GetManagedPrometheusClient returns HTTP client for communicating with the relevant cloud provider's managed Prometheus service.
    93  	GetManagedPrometheusClient() (prom.Client, error)
    94  
    95  	// GetComponentProtocolAndPort returns the protocol and port for the control plane components.
    96  	GetComponentProtocolAndPort(componentName string) (string, int, error)
    97  
    98  	RunSSHCommand(cmd, host string) (string, string, int, error)
    99  
   100  	// Metadata returns provider-specific test run metadata.
   101  	Metadata(client clientset.Interface) (map[string]string, error)
   102  }
   103  
   104  // NewProvider creates a new provider from init options. It will return an error if provider name is not supported.
   105  func NewProvider(initOptions *InitOptions) (Provider, error) {
   106  	configs := parseConfigs(initOptions.ProviderConfigs)
   107  	if initOptions.KubemarkRootKubeConfigPath != "" {
   108  		configs[RootKubeConfigKey] = initOptions.KubemarkRootKubeConfigPath
   109  	}
   110  	switch initOptions.ProviderName {
   111  	case AKSName:
   112  		return NewAKSProvider(configs), nil
   113  	case AWSName:
   114  		return NewAWSProvider(configs), nil
   115  	case AutopilotName:
   116  		return NewAutopilotProvider(configs), nil
   117  	case EKSName:
   118  		return NewEKSProvider(configs), nil
   119  	case GCEName:
   120  		return NewGCEProvider(configs), nil
   121  	case GKEName:
   122  		return NewGKEProvider(configs), nil
   123  	case GKEKubemarkName:
   124  		return NewGKEKubemarkProvider(configs), nil
   125  	case KCPName:
   126  		return NewKCPProvider(configs), nil
   127  	case KindName:
   128  		return NewKindProvider(configs), nil
   129  	case KubemarkName:
   130  		return NewKubemarkProvider(configs), nil
   131  	case LocalName:
   132  		return NewLocalProvider(configs), nil
   133  	case SkeletonName:
   134  		return NewSkeletonProvider(configs), nil
   135  	case VsphereName:
   136  		return NewVsphereProvider(configs), nil
   137  	default:
   138  		return nil, fmt.Errorf("unsupported provider name: %s", initOptions.ProviderName)
   139  	}
   140  }
   141  
   142  func parseConfigs(configs []string) map[string]string {
   143  	cfgMap := map[string]string{}
   144  
   145  	for _, c := range configs {
   146  		pair := strings.Split(c, "=")
   147  		if len(pair) != 2 {
   148  			fmt.Println("error: unused provider config:", c)
   149  		}
   150  		cfgMap[pair[0]] = pair[1]
   151  	}
   152  	return cfgMap
   153  }