Skip to content

Commit b61baea

Browse files
authored
Merge pull request kubernetes#75420 from fabriziopandini/fix-kubeadm-init-output
fix kubeadm init output
2 parents 364b18c + fcadf14 commit b61baea

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

cmd/kubeadm/app/cmd/init.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,25 @@ var (
6363
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
6464
https://kubernetes.io/docs/concepts/cluster-administration/addons/
6565
66-
You can now join any number of machines by running the following on each node
67-
as root:
68-
69-
{{.joinCommand}}
70-
66+
{{if .ControlPlaneEndpoint -}}
67+
{{if .UploadCerts -}}
68+
You can now join any number of the control-plane node running the following command on each as root:
69+
70+
{{.joinControlPlaneCommand}}
71+
72+
Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
73+
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
74+
"kubeadm init phase upload-certs --experimental-upload-certs" to reload certs afterward.
75+
76+
{{else -}}
77+
You can now join any number of control-plane nodes by copying certificate authorities
78+
and service account keys on each node and then running the following as root:
79+
80+
{{.joinControlPlaneCommand}}
81+
82+
{{end}}{{end}}Then you can join any number of worker nodes by running the following on each as root:
83+
84+
{{.joinWorkerCommand}}
7185
`)))
7286
)
7387

@@ -509,14 +523,22 @@ func (d *initData) Tokens() []string {
509523
}
510524

511525
func printJoinCommand(out io.Writer, adminKubeConfigPath, token string, i *initData) error {
512-
joinCommand, err := cmdutil.GetJoinCommand(adminKubeConfigPath, token, i.certificateKey, i.skipTokenPrint, i.uploadCerts, i.skipCertificateKeyPrint)
526+
joinControlPlaneCommand, err := cmdutil.GetJoinControlPlaneCommand(adminKubeConfigPath, token, i.certificateKey, i.skipTokenPrint, i.skipCertificateKeyPrint)
527+
if err != nil {
528+
return err
529+
}
530+
531+
joinWorkerCommand, err := cmdutil.GetJoinWorkerCommand(adminKubeConfigPath, token, i.skipTokenPrint)
513532
if err != nil {
514533
return err
515534
}
516535

517-
ctx := map[string]string{
518-
"KubeConfigPath": adminKubeConfigPath,
519-
"joinCommand": joinCommand,
536+
ctx := map[string]interface{}{
537+
"KubeConfigPath": adminKubeConfigPath,
538+
"ControlPlaneEndpoint": i.Cfg().ControlPlaneEndpoint,
539+
"UploadCerts": i.uploadCerts,
540+
"joinControlPlaneCommand": joinControlPlaneCommand,
541+
"joinWorkerCommand": joinWorkerCommand,
520542
}
521543

522544
return initDoneTempl.Execute(out, ctx)

cmd/kubeadm/app/cmd/token.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,8 @@ func RunCreateToken(out io.Writer, client clientset.Interface, cfgPath string, c
228228
// if --print-join-command was specified, print the full `kubeadm join` command
229229
// otherwise, just print the token
230230
if printJoinCommand {
231-
key := ""
232231
skipTokenPrint := false
233-
uploadCerts := false
234-
skipCertificateKeyPrint := false
235-
joinCommand, err := cmdutil.GetJoinCommand(kubeConfigFile, internalcfg.BootstrapTokens[0].Token.String(), key, skipTokenPrint, uploadCerts, skipCertificateKeyPrint)
232+
joinCommand, err := cmdutil.GetJoinWorkerCommand(kubeConfigFile, internalcfg.BootstrapTokens[0].Token.String(), skipTokenPrint)
236233
if err != nil {
237234
return errors.Wrap(err, "failed to get join command")
238235
}

cmd/kubeadm/app/cmd/util/join.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@ import (
3030
)
3131

3232
var joinCommandTemplate = template.Must(template.New("join").Parse(`` +
33-
`{{if .UploadCerts}}You can now join any number of control-plane node running the following command on each as a root:{{else}}You can now join any number of control-plane node by copying the required certificate authorities on each node and then running the following as root:{{end}}
34-
kubeadm join {{.ControlPlaneHostPort}} --token {{.Token}}{{range $h := .CAPubKeyPins}} --discovery-token-ca-cert-hash {{$h}}{{end}} --experimental-control-plane {{if .UploadCerts}}--certificate-key {{.CertificateKey}}
35-
36-
Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
37-
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use kubeadm init phase upload-certs to reload certs afterward.{{end}}
38-
39-
Then you can join any number of worker nodes by running the following on each as root:
40-
kubeadm join {{.ControlPlaneHostPort}} --token {{.Token}}{{range $h := .CAPubKeyPins}} --discovery-token-ca-cert-hash {{$h}}{{end}}`,
33+
`kubeadm join {{.ControlPlaneHostPort}} --token {{.Token}} \
34+
{{range $h := .CAPubKeyPins}}--discovery-token-ca-cert-hash {{$h}} {{end}}{{if .ControlPlane}}\
35+
--experimental-control-plane {{if .CertificateKey}}--certificate-key {{.CertificateKey}}{{end}}{{end}}`,
4136
))
4237

43-
// GetJoinCommand returns the kubeadm join command for a given token and
38+
// GetJoinWorkerCommand returns the kubeadm join command for a given token and
4439
// and Kubernetes cluster (the current cluster in the kubeconfig file)
45-
func GetJoinCommand(kubeConfigFile, token, key string, skipTokenPrint, uploadCerts, skipCertificateKeyPrint bool) (string, error) {
40+
func GetJoinWorkerCommand(kubeConfigFile, token string, skipTokenPrint bool) (string, error) {
41+
return getJoinCommand(kubeConfigFile, token, "", false, skipTokenPrint, false)
42+
}
43+
44+
// GetJoinControlPlaneCommand returns the kubeadm join command for a given token and
45+
// and Kubernetes cluster (the current cluster in the kubeconfig file)
46+
func GetJoinControlPlaneCommand(kubeConfigFile, token, key string, skipTokenPrint, skipCertificateKeyPrint bool) (string, error) {
47+
return getJoinCommand(kubeConfigFile, token, key, true, skipTokenPrint, skipCertificateKeyPrint)
48+
}
49+
50+
func getJoinCommand(kubeConfigFile, token, key string, controlPlane, skipTokenPrint, skipCertificateKeyPrint bool) (string, error) {
4651
// load the kubeconfig file to get the CA certificate and endpoint
4752
config, err := clientcmd.LoadFromFile(kubeConfigFile)
4853
if err != nil {
@@ -81,8 +86,8 @@ func GetJoinCommand(kubeConfigFile, token, key string, skipTokenPrint, uploadCer
8186
"Token": token,
8287
"CAPubKeyPins": publicKeyPins,
8388
"ControlPlaneHostPort": strings.Replace(clusterConfig.Server, "https://", "", -1),
84-
"UploadCerts": uploadCerts,
8589
"CertificateKey": key,
90+
"ControlPlane": controlPlane,
8691
}
8792

8893
if skipTokenPrint {

0 commit comments

Comments
 (0)