From 31332fa84a0928085200ba5a2e35118516ee2c48 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Mon, 4 Dec 2017 15:06:07 -0500 Subject: [PATCH] Drop using cloud provider to set host address feature As part of the larger plan to drop --cloud-provider and --cloud-config from kube-apiserver, we need to stop calling Cloud Provider API to find the external ip address when one is not specified on the command line. When ExternalHost is not specified, we check if AdvertiseAddress is specified and use that, if that is missing then we use os.Hostname(). When testing this feature, found a problem that when ExternalHost is specified, the port was not added in the generated URL. So fixed that as well. --- cmd/kube-apiserver/app/server.go | 14 +++++- pkg/kubeapiserver/options/BUILD | 2 - pkg/kubeapiserver/options/cloudprovider.go | 49 ------------------- .../src/k8s.io/apiserver/pkg/server/config.go | 16 +++--- 4 files changed, 22 insertions(+), 59 deletions(-) diff --git a/cmd/kube-apiserver/app/server.go b/cmd/kube-apiserver/app/server.go index f578ee677b4..51f4bfd2162 100644 --- a/cmd/kube-apiserver/app/server.go +++ b/cmd/kube-apiserver/app/server.go @@ -630,8 +630,18 @@ func defaultOptions(s *options.ServerRunOptions) error { if err := s.SecureServing.MaybeDefaultWithSelfSignedCerts(s.GenericServerRunOptions.AdvertiseAddress.String(), []string{"kubernetes.default.svc", "kubernetes.default", "kubernetes"}, []net.IP{apiServerServiceIP}); err != nil { return fmt.Errorf("error creating self-signed certificates: %v", err) } - if err := s.CloudProvider.DefaultExternalHost(s.GenericServerRunOptions); err != nil { - return fmt.Errorf("error setting the external host value: %v", err) + + if len(s.GenericServerRunOptions.ExternalHost) == 0 { + if len(s.GenericServerRunOptions.AdvertiseAddress) > 0 { + s.GenericServerRunOptions.ExternalHost = s.GenericServerRunOptions.AdvertiseAddress.String() + } else { + if hostname, err := os.Hostname(); err == nil { + s.GenericServerRunOptions.ExternalHost = hostname + } else { + return fmt.Errorf("error finding host name: %v", err) + } + } + glog.Infof("external host was not specified, using %v", s.GenericServerRunOptions.ExternalHost) } s.Authentication.ApplyAuthorization(s.Authorization) diff --git a/pkg/kubeapiserver/options/BUILD b/pkg/kubeapiserver/options/BUILD index 4c8d3d517cd..6d26b666571 100644 --- a/pkg/kubeapiserver/options/BUILD +++ b/pkg/kubeapiserver/options/BUILD @@ -21,7 +21,6 @@ go_library( deps = [ "//pkg/api/legacyscheme:go_default_library", "//pkg/client/informers/informers_generated/internalversion:go_default_library", - "//pkg/cloudprovider:go_default_library", "//pkg/kubeapiserver/authenticator:go_default_library", "//pkg/kubeapiserver/authorizer:go_default_library", "//pkg/kubeapiserver/authorizer/modes:go_default_library", @@ -29,7 +28,6 @@ go_library( "//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/pborman/uuid:go_default_library", "//vendor/github.com/spf13/pflag:go_default_library", - "//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library", "//vendor/k8s.io/apiserver/pkg/server:go_default_library", diff --git a/pkg/kubeapiserver/options/cloudprovider.go b/pkg/kubeapiserver/options/cloudprovider.go index 310acb592ce..9b8119fb072 100644 --- a/pkg/kubeapiserver/options/cloudprovider.go +++ b/pkg/kubeapiserver/options/cloudprovider.go @@ -17,15 +17,7 @@ limitations under the License. package options import ( - "fmt" - "os" - - "github.com/golang/glog" "github.com/spf13/pflag" - - "k8s.io/api/core/v1" - genericoptions "k8s.io/apiserver/pkg/server/options" - "k8s.io/kubernetes/pkg/cloudprovider" ) type CloudProviderOptions struct { @@ -49,44 +41,3 @@ func (s *CloudProviderOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&s.CloudConfigFile, "cloud-config", s.CloudConfigFile, "The path to the cloud provider configuration file. Empty string for no configuration file.") } - -func (s *CloudProviderOptions) DefaultExternalHost(genericoptions *genericoptions.ServerRunOptions) error { - if len(genericoptions.ExternalHost) != 0 { - return nil - } - - if cloudprovider.IsCloudProvider(s.CloudProvider) { - glog.Info("--external-hostname was not specified. Trying to get it from the cloud provider.") - - cloud, err := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile) - if err != nil { - return fmt.Errorf("%q cloud provider could not be initialized: %v", s.CloudProvider, err) - } - instances, supported := cloud.Instances() - if !supported { - return fmt.Errorf("%q cloud provider has no instances", s.CloudProvider) - } - hostname, err := os.Hostname() - if err != nil { - return fmt.Errorf("failed to get hostname: %v", err) - } - nodeName, err := instances.CurrentNodeName(hostname) - if err != nil { - return fmt.Errorf("failed to get NodeName from %q cloud provider: %v", s.CloudProvider, err) - } - addrs, err := instances.NodeAddresses(nodeName) - if err != nil { - return fmt.Errorf("failed to get external host address from %q cloud provider: %v", s.CloudProvider, err) - } else { - for _, addr := range addrs { - if addr.Type == v1.NodeExternalIP { - genericoptions.ExternalHost = addr.Address - glog.Warning("[Deprecated] Getting host address using cloud provider is " + - "now deprecated. Please use --external-hostname explicitly") - } - } - } - } - - return nil -} diff --git a/staging/src/k8s.io/apiserver/pkg/server/config.go b/staging/src/k8s.io/apiserver/pkg/server/config.go index 877071ad3b2..fe912a94d24 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/config.go +++ b/staging/src/k8s.io/apiserver/pkg/server/config.go @@ -338,13 +338,17 @@ type CompletedConfig struct { // Complete fills in any fields not set that are required to have valid data and can be derived // from other fields. If you're going to `ApplyOptions`, do that first. It's mutating the receiver. func (c *Config) Complete(informers informers.SharedInformerFactory) CompletedConfig { - if len(c.ExternalAddress) == 0 && c.PublicAddress != nil { - hostAndPort := c.PublicAddress.String() - if c.ReadWritePort != 0 { - hostAndPort = net.JoinHostPort(hostAndPort, strconv.Itoa(c.ReadWritePort)) - } - c.ExternalAddress = hostAndPort + host := c.ExternalAddress + if host == "" && c.PublicAddress != nil { + host = c.PublicAddress.String() } + if !strings.Contains(host, ":") { + if c.ReadWritePort != 0 { + host = net.JoinHostPort(host, strconv.Itoa(c.ReadWritePort)) + } + } + c.ExternalAddress = host + if c.OpenAPIConfig != nil && c.OpenAPIConfig.SecurityDefinitions != nil { // Setup OpenAPI security: all APIs will have the same authentication for now. c.OpenAPIConfig.DefaultSecurity = []map[string][]string{}