k8s.io/kubernetes@v1.29.3/pkg/kubeapiserver/options/cloudprovider.go (about) 1 /* 2 Copyright 2017 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 options 18 19 import ( 20 "fmt" 21 22 "github.com/spf13/pflag" 23 utilfeature "k8s.io/apiserver/pkg/util/feature" 24 cloudprovider "k8s.io/cloud-provider" 25 "k8s.io/kubernetes/pkg/features" 26 ) 27 28 // CloudProviderOptions contains cloud provider config 29 type CloudProviderOptions struct { 30 CloudConfigFile string 31 CloudProvider string 32 } 33 34 // NewCloudProviderOptions creates a default CloudProviderOptions 35 func NewCloudProviderOptions() *CloudProviderOptions { 36 return &CloudProviderOptions{} 37 } 38 39 // Validate checks invalid config 40 func (opts *CloudProviderOptions) Validate() []error { 41 var errs []error 42 43 switch { 44 case opts.CloudProvider == "": 45 case opts.CloudProvider == "external": 46 if !utilfeature.DefaultFeatureGate.Enabled(features.DisableCloudProviders) { 47 errs = append(errs, fmt.Errorf("when using --cloud-provider set to '%s', "+ 48 "please set DisableCloudProviders feature to true", opts.CloudProvider)) 49 } 50 if !utilfeature.DefaultFeatureGate.Enabled(features.DisableKubeletCloudCredentialProviders) { 51 errs = append(errs, fmt.Errorf("when using --cloud-provider set to '%s', "+ 52 "please set DisableKubeletCloudCredentialProviders feature to true", opts.CloudProvider)) 53 } 54 case cloudprovider.IsDeprecatedInternal(opts.CloudProvider): 55 if utilfeature.DefaultFeatureGate.Enabled(features.DisableCloudProviders) { 56 errs = append(errs, fmt.Errorf("when using --cloud-provider set to '%s', "+ 57 "please set DisableCloudProviders feature to false", opts.CloudProvider)) 58 } 59 if utilfeature.DefaultFeatureGate.Enabled(features.DisableKubeletCloudCredentialProviders) { 60 errs = append(errs, fmt.Errorf("when using --cloud-provider set to '%s', "+ 61 "please set DisableKubeletCloudCredentialProviders feature to false", opts.CloudProvider)) 62 } 63 default: 64 errs = append(errs, fmt.Errorf("unknown --cloud-provider: %s", opts.CloudProvider)) 65 } 66 67 return errs 68 } 69 70 // AddFlags returns flags of cloud provider for a API Server 71 func (s *CloudProviderOptions) AddFlags(fs *pflag.FlagSet) { 72 fs.StringVar(&s.CloudProvider, "cloud-provider", s.CloudProvider, 73 "The provider for cloud services. Empty string for no provider.") 74 fs.MarkDeprecated("cloud-provider", "will be removed in a future version") // nolint: errcheck 75 fs.StringVar(&s.CloudConfigFile, "cloud-config", s.CloudConfigFile, 76 "The path to the cloud provider configuration file. Empty string for no configuration file.") 77 fs.MarkDeprecated("cloud-config", "will be removed in a future version") // nolint: errcheck 78 }