mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Merge pull request #34885 from apprenda/kubeadm_join_configuration
Automatic merge from submit-queue kubeadm join: Added support for config file. As more behavior (#34719, #34807, fix for #33641) is added to `kubeadm join`, this will be eventually very much needed. Makes sense to go in sooner rather than later. Also references #34501 and #34884. /cc @luxas @mikedanese
This commit is contained in:
commit
ab14c31b84
@ -19,6 +19,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/renstrom/dedent"
|
"github.com/renstrom/dedent"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -27,6 +28,8 @@ import (
|
|||||||
kubenode "k8s.io/kubernetes/cmd/kubeadm/app/node"
|
kubenode "k8s.io/kubernetes/cmd/kubeadm/app/node"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
|
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
|
||||||
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
|
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
|
||||||
|
"k8s.io/kubernetes/pkg/api"
|
||||||
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -44,12 +47,14 @@ var (
|
|||||||
func NewCmdJoin(out io.Writer) *cobra.Command {
|
func NewCmdJoin(out io.Writer) *cobra.Command {
|
||||||
cfg := &kubeadmapi.NodeConfiguration{}
|
cfg := &kubeadmapi.NodeConfiguration{}
|
||||||
var skipPreFlight bool
|
var skipPreFlight bool
|
||||||
|
var cfgPath string
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "join",
|
Use: "join",
|
||||||
Short: "Run this on any machine you wish to join an existing cluster.",
|
Short: "Run this on any machine you wish to join an existing cluster.",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
err := RunJoin(out, cmd, args, cfg, skipPreFlight)
|
j, err := NewJoin(cfgPath, args, cfg, skipPreFlight)
|
||||||
kubeadmutil.CheckErr(err)
|
kubeadmutil.CheckErr(err)
|
||||||
|
kubeadmutil.CheckErr(j.Run(out))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,6 +63,8 @@ func NewCmdJoin(out io.Writer) *cobra.Command {
|
|||||||
"(required) Shared secret used to secure bootstrap. Must match the output of 'kubeadm init'",
|
"(required) Shared secret used to secure bootstrap. Must match the output of 'kubeadm init'",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cmd.PersistentFlags().StringVar(&cfgPath, "config", "", "Path to kubeadm config file")
|
||||||
|
|
||||||
cmd.PersistentFlags().BoolVar(
|
cmd.PersistentFlags().BoolVar(
|
||||||
&skipPreFlight, "skip-preflight-checks", false,
|
&skipPreFlight, "skip-preflight-checks", false,
|
||||||
"skip preflight checks normally run before modifying the system",
|
"skip preflight checks normally run before modifying the system",
|
||||||
@ -66,38 +73,56 @@ func NewCmdJoin(out io.Writer) *cobra.Command {
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunJoin executes worked node provisioning and tries to join an existing cluster.
|
type Join struct {
|
||||||
func RunJoin(out io.Writer, cmd *cobra.Command, args []string, s *kubeadmapi.NodeConfiguration, skipPreFlight bool) error {
|
cfg *kubeadmapi.NodeConfiguration
|
||||||
// TODO(phase1+) this we are missing args from the help text, there should be a way to tell cobra about it
|
}
|
||||||
|
|
||||||
|
func NewJoin(cfgPath string, args []string, cfg *kubeadmapi.NodeConfiguration, skipPreFlight bool) (*Join, error) {
|
||||||
|
if cfgPath != "" {
|
||||||
|
b, err := ioutil.ReadFile(cfgPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to read config from %q [%v]", cfgPath, err)
|
||||||
|
}
|
||||||
|
if err := runtime.DecodeInto(api.Codecs.UniversalDecoder(), b, cfg); err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to decode config from %q [%v]", cfgPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !skipPreFlight {
|
if !skipPreFlight {
|
||||||
fmt.Println("Running pre-flight checks")
|
fmt.Println("Running pre-flight checks")
|
||||||
err := preflight.RunJoinNodeChecks()
|
err := preflight.RunJoinNodeChecks()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &preflight.PreFlightError{Msg: err.Error()}
|
return nil, &preflight.PreFlightError{Msg: err.Error()}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Skipping pre-flight checks")
|
fmt.Println("Skipping pre-flight checks")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(phase1+) this we are missing args from the help text, there should be a way to tell cobra about it
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return fmt.Errorf("<cmd/join> must specify master IP address (see --help)")
|
return nil, fmt.Errorf("must specify master IP address (see --help)")
|
||||||
}
|
}
|
||||||
s.MasterAddresses = append(s.MasterAddresses, args...)
|
cfg.MasterAddresses = append(cfg.MasterAddresses, args...)
|
||||||
|
|
||||||
ok, err := kubeadmutil.UseGivenTokenIfValid(&s.Secrets)
|
ok, err := kubeadmutil.UseGivenTokenIfValid(&cfg.Secrets)
|
||||||
if !ok {
|
if !ok {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("<cmd/join> %v (see --help)\n", err)
|
return nil, fmt.Errorf("%v (see --help)\n", err)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("Must specify --token (see --help)\n")
|
return nil, fmt.Errorf("Must specify --token (see --help)\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
clusterInfo, err := kubenode.RetrieveTrustedClusterInfo(s)
|
return &Join{cfg: cfg}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes worked node provisioning and tries to join an existing cluster.
|
||||||
|
func (j *Join) Run(out io.Writer) error {
|
||||||
|
clusterInfo, err := kubenode.RetrieveTrustedClusterInfo(j.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionDetails, err := kubenode.EstablishMasterConnection(s, clusterInfo)
|
connectionDetails, err := kubenode.EstablishMasterConnection(j.cfg, clusterInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user