From d871f12f367263f55e756bd7adc987dfc4becd13 Mon Sep 17 00:00:00 2001 From: Liang Deng <283304489@qq.com> Date: Mon, 19 Jun 2023 14:03:11 +0800 Subject: [PATCH] feat: provide config option for yurtadm (#1547) Signed-off-by: Liang Deng <283304489@qq.com> --- pkg/yurtadm/cmd/join/join.go | 11 +++++++++++ pkg/yurtadm/cmd/join/joindata/data.go | 1 + pkg/yurtadm/cmd/join/phases/joinnode.go | 7 ++++++- pkg/yurtadm/cmd/join/phases/prepare.go | 6 ++++-- pkg/yurtadm/constants/constants.go | 4 +++- pkg/yurtadm/util/yurthub/yurthub_test.go | 4 ++++ 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pkg/yurtadm/cmd/join/join.go b/pkg/yurtadm/cmd/join/join.go index 05e711914ec..df7784e75d0 100644 --- a/pkg/yurtadm/cmd/join/join.go +++ b/pkg/yurtadm/cmd/join/join.go @@ -42,6 +42,7 @@ import ( ) type joinOptions struct { + cfgPath string token string nodeType string nodeName string @@ -112,6 +113,9 @@ func NewCmdJoin(in io.Reader, out io.Writer, outErr io.Writer) *cobra.Command { // addJoinConfigFlags adds join flags bound to the config to the specified flagset func addJoinConfigFlags(flagSet *flag.FlagSet, joinOptions *joinOptions) { + flagSet.StringVar( + &joinOptions.cfgPath, yurtconstants.CfgPath, "", "Path to a joinConfiguration file.", + ) flagSet.StringVar( &joinOptions.token, yurtconstants.TokenStr, "", "Use this token for both discovery-token and tls-bootstrap-token when those values are not provided.", @@ -207,6 +211,7 @@ func (nodeJoiner *nodeJoiner) Run() error { } type joinData struct { + cfgPath string joinNodeData *joindata.NodeRegistration apiServerEndpoint string token string @@ -271,6 +276,7 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) { } data := &joinData{ + cfgPath: opt.cfgPath, apiServerEndpoint: apiServerEndpoint, token: opt.token, tlsBootstrapCfg: nil, @@ -366,6 +372,11 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) { return data, nil } +// CfgPath returns path to a joinConfiguration file. +func (j *joinData) CfgPath() string { + return j.cfgPath +} + // ServerAddr returns the public address of kube-apiserver. func (j *joinData) ServerAddr() string { return j.apiServerEndpoint diff --git a/pkg/yurtadm/cmd/join/joindata/data.go b/pkg/yurtadm/cmd/join/joindata/data.go index 9d50ccdd570..8f5a78322d2 100644 --- a/pkg/yurtadm/cmd/join/joindata/data.go +++ b/pkg/yurtadm/cmd/join/joindata/data.go @@ -31,6 +31,7 @@ type NodeRegistration struct { } type YurtJoinData interface { + CfgPath() string ServerAddr() string JoinToken() string PauseImage() string diff --git a/pkg/yurtadm/cmd/join/phases/joinnode.go b/pkg/yurtadm/cmd/join/phases/joinnode.go index f0568ffd0fb..ad39880a2ef 100644 --- a/pkg/yurtadm/cmd/join/phases/joinnode.go +++ b/pkg/yurtadm/cmd/join/phases/joinnode.go @@ -29,7 +29,12 @@ import ( // RunJoinNode executes the node join process. func RunJoinNode(data joindata.YurtJoinData, out io.Writer, outErr io.Writer) error { - kubeadmJoinConfigFilePath := filepath.Join(constants.KubeletWorkdir, constants.KubeadmJoinConfigFileName) + var kubeadmJoinConfigFilePath string + if data.CfgPath() != "" { + kubeadmJoinConfigFilePath = data.CfgPath() + } else { + kubeadmJoinConfigFilePath = filepath.Join(constants.KubeletWorkdir, constants.KubeadmJoinConfigFileName) + } kubeadmCmd := exec.Command("kubeadm", "join", fmt.Sprintf("--config=%s", kubeadmJoinConfigFilePath)) kubeadmCmd.Stdout = out kubeadmCmd.Stderr = outErr diff --git a/pkg/yurtadm/cmd/join/phases/prepare.go b/pkg/yurtadm/cmd/join/phases/prepare.go index a4839a4a00a..66842b723a6 100644 --- a/pkg/yurtadm/cmd/join/phases/prepare.go +++ b/pkg/yurtadm/cmd/join/phases/prepare.go @@ -76,8 +76,10 @@ func RunPrepare(data joindata.YurtJoinData) error { if err := yurtadmutil.SetDiscoveryConfig(data); err != nil { return err } - if err := yurtadmutil.SetKubeadmJoinConfig(data); err != nil { - return err + if data.CfgPath() == "" { + if err := yurtadmutil.SetKubeadmJoinConfig(data); err != nil { + return err + } } return nil } diff --git a/pkg/yurtadm/constants/constants.go b/pkg/yurtadm/constants/constants.go index 49eded67302..f82b41d38ff 100644 --- a/pkg/yurtadm/constants/constants.go +++ b/pkg/yurtadm/constants/constants.go @@ -92,7 +92,9 @@ const ( Organizations = "organizations" // PauseImage flag sets the pause image for worker node. PauseImage = "pause-image" - // TokenStr flags sets both the discovery-token and the tls-bootstrap-token when those values are not provided + // CfgPath flag sets the path to a JoinConfiguration file. + CfgPath = "config" + // TokenStr flag sets both the discovery-token and the tls-bootstrap-token when those values are not provided TokenStr = "token" // TokenDiscoveryCAHash flag instruct kubeadm to validate that the root CA public key matches this hash (for token-based discovery) TokenDiscoveryCAHash = "discovery-token-ca-cert-hash" diff --git a/pkg/yurtadm/util/yurthub/yurthub_test.go b/pkg/yurtadm/util/yurthub/yurthub_test.go index 0488df46ed3..d1cb1ca476a 100644 --- a/pkg/yurtadm/util/yurthub/yurthub_test.go +++ b/pkg/yurtadm/util/yurthub/yurthub_test.go @@ -235,6 +235,10 @@ type testData struct { joinNodeData *joindata.NodeRegistration } +func (j *testData) CfgPath() string { + return "" +} + func (j *testData) ServerAddr() string { return "" }