Skip to content

Commit

Permalink
feat: provide config option for yurtadm (#1547)
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Deng <[email protected]>
  • Loading branch information
YTGhost authored Jun 19, 2023
1 parent cc68ac5 commit d871f12
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
11 changes: 11 additions & 0 deletions pkg/yurtadm/cmd/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
)

type joinOptions struct {
cfgPath string
token string
nodeType string
nodeName string
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -207,6 +211,7 @@ func (nodeJoiner *nodeJoiner) Run() error {
}

type joinData struct {
cfgPath string
joinNodeData *joindata.NodeRegistration
apiServerEndpoint string
token string
Expand Down Expand Up @@ -271,6 +276,7 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
}

data := &joinData{
cfgPath: opt.cfgPath,
apiServerEndpoint: apiServerEndpoint,
token: opt.token,
tlsBootstrapCfg: nil,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/yurtadm/cmd/join/joindata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type NodeRegistration struct {
}

type YurtJoinData interface {
CfgPath() string
ServerAddr() string
JoinToken() string
PauseImage() string
Expand Down
7 changes: 6 additions & 1 deletion pkg/yurtadm/cmd/join/phases/joinnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pkg/yurtadm/cmd/join/phases/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion pkg/yurtadm/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions pkg/yurtadm/util/yurthub/yurthub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ type testData struct {
joinNodeData *joindata.NodeRegistration
}

func (j *testData) CfgPath() string {
return ""
}

func (j *testData) ServerAddr() string {
return ""
}
Expand Down

0 comments on commit d871f12

Please sign in to comment.