Skip to content

Commit

Permalink
Merge pull request #3108 from sebcworks/sebcworks-customapiserverport
Browse files Browse the repository at this point in the history
Allow to specify api server port through CLI fix #2781
  • Loading branch information
tstromberg authored Jan 16, 2019
2 parents 00caba2 + e551231 commit 7b32547
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
5 changes: 4 additions & 1 deletion cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
createMount = "mount"
featureGates = "feature-gates"
apiServerName = "apiserver-name"
apiServerPort = "apiserver-port"
dnsDomain = "dns-domain"
serviceCIDR = "service-cluster-ip-range"
mountString = "mount-string"
Expand Down Expand Up @@ -226,6 +227,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 @@ -281,7 +283,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 @@ -472,6 +474,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 @@ -427,6 +427,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 @@ -443,7 +449,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 @@ -177,7 +177,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 @@ -58,6 +58,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 @@ -249,12 +249,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 @@ -284,3 +288,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
}

1 comment on commit 7b32547

@mr-c
Copy link

@mr-c mr-c commented on 7b32547 Jan 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please sign in to comment.