Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify api server port through CLI fix #2781 #3108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/bootstrapper/kubeadm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func restartKubeProxy(k8s config.KubernetesConfig) error {
APIServerPort int
}{
AdvertiseAddress: k8s.NodeIP,
APIServerPort: util.APIServerPort,
APIServerPort: k8s.NodePort,
}

kubeconfig := bytes.Buffer{}
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type MachineConfig struct {
type KubernetesConfig struct {
KubernetesVersion string
NodeIP string
NodePort int
NodeName string
APIServerName string
APIServerNames []string
Expand Down
28 changes: 27 additions & 1 deletion pkg/util/kubeconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}