From e551231ec59bed6d3d757e2ff93a508649c8df6b Mon Sep 17 00:00:00 2001 From: Sebastien Collin Date: Sun, 26 Aug 2018 00:16:53 +0200 Subject: [PATCH] Allow to specify api server port through CLI --- cmd/minikube/cmd/start.go | 5 +++- pkg/minikube/bootstrapper/kubeadm/kubeadm.go | 8 +++++- .../bootstrapper/kubeadm/kubeadm_test.go | 26 +++++++++++++++++ pkg/minikube/bootstrapper/kubeadm/util.go | 2 +- pkg/minikube/config/types.go | 1 + pkg/util/kubeconfig/config.go | 28 ++++++++++++++++++- 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index f7a5d24dac1a..5d36440c2647 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -64,6 +64,7 @@ const ( createMount = "mount" featureGates = "feature-gates" apiServerName = "apiserver-name" + apiServerPort = "apiserver-port" dnsDomain = "dns-domain" mountString = "mount-string" disableDriverMounts = "disable-driver-mounts" @@ -212,6 +213,7 @@ func runStart(cmd *cobra.Command, args []string) { kubernetesConfig := cfg.KubernetesConfig{ KubernetesVersion: selectedKubernetesVersion, NodeIP: ip, + NodePort: viper.GetInt(apiServerPort), NodeName: constants.DefaultNodeName, APIServerName: viper.GetString(apiServerName), APIServerNames: apiServerNames, @@ -266,7 +268,7 @@ func runStart(cmd *cobra.Command, args []string) { glog.Errorln("Error connecting to cluster: ", err) } kubeHost = strings.Replace(kubeHost, "tcp://", "https://", -1) - kubeHost = strings.Replace(kubeHost, ":2376", ":"+strconv.Itoa(pkgutil.APIServerPort), -1) + kubeHost = strings.Replace(kubeHost, ":2376", ":"+strconv.Itoa(kubernetesConfig.NodePort), -1) fmt.Println("Setting up kubeconfig...") // setup kubeconfig @@ -389,6 +391,7 @@ func init() { startCmd.Flags().String(NFSSharesRoot, "/nfsshares", "Where to root the NFS Shares (defaults to /nfsshares, only supported with hyperkit now)") startCmd.Flags().StringArrayVar(&dockerEnv, "docker-env", nil, "Environment variables to pass to the Docker daemon. (format: key=value)") startCmd.Flags().StringArrayVar(&dockerOpt, "docker-opt", nil, "Specify arbitrary flags to pass to the Docker daemon. (format: key=value)") + startCmd.Flags().Int(apiServerPort, pkgutil.APIServerPort, "The apiserver listening port") startCmd.Flags().String(apiServerName, constants.APIServerName, "The apiserver name which is used in the generated certificate for kubernetes. This can be used if you want to make the apiserver available from outside the machine") startCmd.Flags().StringArrayVar(&apiServerNames, "apiserver-names", nil, "A set of apiserver names which are used in the generated certificate for kubernetes. This can be used if you want to make the apiserver available from outside the machine") startCmd.Flags().IPSliceVar(&apiServerIPs, "apiserver-ips", nil, "A set of apiserver IP Addresses which are used in the generated certificate for kubernetes. This can be used if you want to make the apiserver available from outside the machine") diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go index 5455f984c7ae..cafd46830aa0 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go @@ -359,6 +359,12 @@ func generateConfig(k8s config.KubernetesConfig) (string, error) { return "", errors.Wrap(err, "generating extra component config for kubeadm") } + // In case of no port assigned, use util.APIServerPort + nodePort := k8s.NodePort + if nodePort <= 0 { + nodePort = util.APIServerPort + } + opts := struct { CertDir string ServiceCIDR string @@ -374,7 +380,7 @@ func generateConfig(k8s config.KubernetesConfig) (string, error) { CertDir: util.DefaultCertPath, ServiceCIDR: util.DefaultServiceCIDR, AdvertiseAddress: k8s.NodeIP, - APIServerPort: util.APIServerPort, + APIServerPort: nodePort, KubernetesVersion: k8s.KubernetesVersion, EtcdDataDir: "/data/minikube", //TODO(r2d4): change to something else persisted NodeName: k8s.NodeName, diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go index a55cc96e6269..0f69b0d35902 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go @@ -228,6 +228,32 @@ schedulerExtraArgs: }, shouldErr: true, }, + { + description: "custom api server port", + cfg: config.KubernetesConfig{ + NodeIP: "192.168.1.100", + NodePort: 18443, + KubernetesVersion: "v1.10.0", + NodeName: "minikube", + }, + expectedCfg: `apiVersion: kubeadm.k8s.io/v1alpha1 +kind: MasterConfiguration +noTaintMaster: true +api: + advertiseAddress: 192.168.1.100 + bindPort: 18443 + controlPlaneEndpoint: localhost +kubernetesVersion: v1.10.0 +certificatesDir: /var/lib/minikube/certs/ +networking: + serviceSubnet: 10.96.0.0/12 +etcd: + dataDir: /data/minikube +nodeName: minikube +apiServerExtraArgs: + admission-control: "Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota" +`, + }, } for _, test := range tests { diff --git a/pkg/minikube/bootstrapper/kubeadm/util.go b/pkg/minikube/bootstrapper/kubeadm/util.go index 2eb59d74e4d7..19d31b985ba4 100644 --- a/pkg/minikube/bootstrapper/kubeadm/util.go +++ b/pkg/minikube/bootstrapper/kubeadm/util.go @@ -171,7 +171,7 @@ func restartKubeProxy(k8s config.KubernetesConfig) error { APIServerPort int }{ AdvertiseAddress: k8s.NodeIP, - APIServerPort: util.APIServerPort, + APIServerPort: k8s.NodePort, } kubeconfig := bytes.Buffer{} diff --git a/pkg/minikube/config/types.go b/pkg/minikube/config/types.go index 332810f7d0d4..bde679493af3 100644 --- a/pkg/minikube/config/types.go +++ b/pkg/minikube/config/types.go @@ -57,6 +57,7 @@ type MachineConfig struct { type KubernetesConfig struct { KubernetesVersion string NodeIP string + NodePort int NodeName string APIServerName string APIServerNames []string diff --git a/pkg/util/kubeconfig/config.go b/pkg/util/kubeconfig/config.go index 15a8455e9ce7..f17f319efb20 100644 --- a/pkg/util/kubeconfig/config.go +++ b/pkg/util/kubeconfig/config.go @@ -222,12 +222,16 @@ func UpdateKubeconfigIP(ip net.IP, filename string, machineName string) (bool, e if kip.Equal(ip) { return false, nil } + kport, err := getPortFromKubeConfig(filename, machineName) + if err != nil { + return false, err + } con, err := ReadConfigOrNew(filename) if err != nil { return false, errors.Wrap(err, "Error getting kubeconfig status") } // Safe to lookup server because if field non-existent getIPFromKubeconfig would have given an error - con.Clusters[machineName].Server = "https://" + ip.String() + ":" + strconv.Itoa(util.APIServerPort) + con.Clusters[machineName].Server = "https://" + ip.String() + ":" + strconv.Itoa(kport) err = WriteConfig(con, filename) if err != nil { return false, err @@ -257,3 +261,25 @@ func getIPFromKubeConfig(filename, machineName string) (net.IP, error) { ip := net.ParseIP(kip) return ip, nil } + +// getPortFromKubeConfig returns the Port number stored for minikube in the kubeconfig specified +func getPortFromKubeConfig(filename, machineName string) (int, error) { + con, err := ReadConfigOrNew(filename) + if err != nil { + return 0, errors.Wrap(err, "Error getting kubeconfig status") + } + cluster, ok := con.Clusters[machineName] + if !ok { + return 0, errors.Errorf("Kubeconfig does not have a record of the machine cluster") + } + kurl, err := url.Parse(cluster.Server) + if err != nil { + return util.APIServerPort, nil + } + _, kport, err := net.SplitHostPort(kurl.Host) + if err != nil { + return util.APIServerPort, nil + } + port, err := strconv.Atoi(kport) + return port, err +}