From a0acc28b62784092d5c32d89b5fee62e770a564f Mon Sep 17 00:00:00 2001 From: Lion-Wei Date: Thu, 28 Dec 2017 20:37:15 +0800 Subject: [PATCH] Add validation of apiserver-advertise-address --- cmd/kubeadm/app/cmd/phases/addons.go | 2 +- cmd/kubeadm/app/util/config/masterconfig.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/kubeadm/app/cmd/phases/addons.go b/cmd/kubeadm/app/cmd/phases/addons.go index c837113f9b5..c8ed56e0581 100644 --- a/cmd/kubeadm/app/cmd/phases/addons.go +++ b/cmd/kubeadm/app/cmd/phases/addons.go @@ -144,7 +144,7 @@ func getAddonsSubCommands() []*cobra.Command { cmd.Flags().StringVar(&cfg.ImageRepository, "image-repository", cfg.ImageRepository, `Choose a container registry to pull control plane images from`) if properties.use == "all" || properties.use == "kube-proxy" { - cmd.Flags().StringVar(&cfg.API.AdvertiseAddress, "apiserver-advertise-address", cfg.API.AdvertiseAddress, `The IP address or DNS name the API server is accessible on`) + cmd.Flags().StringVar(&cfg.API.AdvertiseAddress, "apiserver-advertise-address", cfg.API.AdvertiseAddress, `The IP address the API server is accessible on`) cmd.Flags().Int32Var(&cfg.API.BindPort, "apiserver-bind-port", cfg.API.BindPort, `The port the API server is accessible on`) cmd.Flags().StringVar(&cfg.Networking.PodSubnet, "pod-network-cidr", cfg.Networking.PodSubnet, `The range of IP addresses used for the Pod network`) } diff --git a/cmd/kubeadm/app/util/config/masterconfig.go b/cmd/kubeadm/app/util/config/masterconfig.go index d109dab38f7..d8deddc6f2a 100644 --- a/cmd/kubeadm/app/util/config/masterconfig.go +++ b/cmd/kubeadm/app/util/config/masterconfig.go @@ -37,9 +37,14 @@ import ( // SetInitDynamicDefaults checks and sets configuration values for Master node func SetInitDynamicDefaults(cfg *kubeadmapi.MasterConfiguration) error { + // validate cfg.API.AdvertiseAddress. + addressIP := net.ParseIP(cfg.API.AdvertiseAddress) + if addressIP == nil && cfg.API.AdvertiseAddress != "" { + return fmt.Errorf("couldn't use \"%s\" as \"apiserver-advertise-address\", must be ipv4 or ipv6 address", cfg.API.AdvertiseAddress) + } // Choose the right address for the API Server to advertise. If the advertise address is localhost or 0.0.0.0, the default interface's IP address is used // This is the same logic as the API Server uses - ip, err := netutil.ChooseBindAddress(net.ParseIP(cfg.API.AdvertiseAddress)) + ip, err := netutil.ChooseBindAddress(addressIP) if err != nil { return err }