Skip to content

Commit

Permalink
feat: provide config option for yurtadm
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Deng <283304489@qq.com>
  • Loading branch information
YTGhost committed Jun 16, 2023
1 parent 5fa5adb commit da58b33
Show file tree
Hide file tree
Showing 5 changed files with 25 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 @@ -40,6 +40,7 @@ import (
)

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

type joinData struct {
cfgPath string
joinNodeData *joindata.NodeRegistration
apiServerEndpoint string
token string
Expand Down Expand Up @@ -264,6 +269,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 @@ -341,6 +347,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 @@ -30,6 +30,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 @@ -90,7 +90,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

0 comments on commit da58b33

Please sign in to comment.