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

kubeadm: print the join command #70795

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 21 additions & 10 deletions cmd/kubeadm/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func NewCmdInit(out io.Writer) *cobra.Command {
// via the subcommands automatically created by initRunner.BindToCommand
err = runInit(&data, out)
kubeadmutil.CheckErr(err)

err = showJoinCommand(&data, out)
kubeadmutil.CheckErr(err)
},
}

Expand Down Expand Up @@ -397,6 +400,9 @@ func (d initData) KubeConfigDir() string {

// KubeConfigPath returns the path to the kubeconfig file to use for connecting to Kubernetes
func (d initData) KubeConfigPath() string {
if d.dryRun {
d.kubeconfigPath = filepath.Join(d.dryRunDir, kubeadmconstants.AdminKubeConfigFileName)
Copy link
Member

Choose a reason for hiding this comment

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

that was a bug indeed, thanks for fixing. @yuexiao-wang

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for your help

}
return d.kubeconfigPath
}

Expand Down Expand Up @@ -458,19 +464,16 @@ func (d initData) Tokens() []string {

// runInit executes master node provisioning
func runInit(i *initData, out io.Writer) error {
Copy link
Member

Choose a reason for hiding this comment

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

I'm expecting this PR to remove this func after rebasing when all the phases merge


// Get directories to write files to; can be faked if we're dry-running
klog.V(1).Infof("[init] Getting certificates directory from configuration")
certsDirToWriteTo, kubeConfigDir, _, _, err := getDirectoriesToUse(i.dryRun, i.dryRunDir, i.cfg.CertificatesDir)
certsDirToWriteTo, _, _, _, err := getDirectoriesToUse(i.dryRun, i.dryRunDir, i.cfg.CertificatesDir)
if err != nil {
return errors.Wrap(err, "error getting directories to use")
}

// certsDirToWriteTo is gonna equal cfg.CertificatesDir in the normal case, but gonna be a temp directory if dryrunning
i.cfg.CertificatesDir = certsDirToWriteTo

adminKubeConfigPath := filepath.Join(kubeConfigDir, kubeadmconstants.AdminKubeConfigFileName)

// TODO: client and waiter are temporary until the rest of the phases that use them
// are removed from this function.
client, err := i.Client()
Expand Down Expand Up @@ -526,12 +529,6 @@ func runInit(i *initData, out io.Writer) error {
return nil
}

// Prints the join command, multiple times in case the user has multiple tokens
for _, token := range i.Tokens() {
if err := printJoinCommand(out, adminKubeConfigPath, token, i.skipTokenPrint); err != nil {
return errors.Wrap(err, "failed to print join command")
}
}
return nil
}

Expand Down Expand Up @@ -559,3 +556,17 @@ func getDirectoriesToUse(dryRun bool, dryRunDir string, defaultPkiDir string) (s

return defaultPkiDir, kubeadmconstants.KubernetesDir, kubeadmconstants.GetStaticPodDirectory(), kubeadmconstants.KubeletRunDirectory, nil
}

// showJoinCommand prints the join command after all the phases in init have finished
func showJoinCommand(i *initData, out io.Writer) error {
adminKubeConfigPath := i.KubeConfigPath()

// Prints the join command, multiple times in case the user has multiple tokens
for _, token := range i.Tokens() {
if err := printJoinCommand(out, adminKubeConfigPath, token, i.skipTokenPrint); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

initData already holds a method to get the kubeconfig path:
adminKubeConfigPath -> i.KubeConfigPath()
it can replace the code above :)

	_, kubeConfigDir, _, _, err := getDirectoriesToUse(i.dryRun, i.dryRunDir, i.cfg.CertificatesDir)
	if err != nil {
		return errors.Wrap(err, "failed to get directories to use")
	}
 	adminKubeConfigPath := filepath.Join(kubeConfigDir, kubeadmconstants.AdminKubeConfigFileName)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

return errors.Wrap(err, "failed to print join command")
}
}

return nil
}