Skip to content

Commit

Permalink
update yurtcl join
Browse files Browse the repository at this point in the history
  • Loading branch information
adamzhoul committed Nov 25, 2021
1 parent 76bd622 commit 44d5da4
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 292 deletions.
14 changes: 13 additions & 1 deletion pkg/yurtctl/cmd/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type joinOptions struct {
kustomizeDir string
nodeType string
yurthubImage string
markAutonomous bool
}

// newJoinOptions returns a struct ready for being used for creating cmd join flags.
Expand Down Expand Up @@ -116,8 +117,8 @@ func NewCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {

joinRunner.AppendPhase(yurtphase.NewPreparePhase())
joinRunner.AppendPhase(kubeadmPhase.NewPreflightPhase())
joinRunner.AppendPhase(yurtphase.NewEdgeNodePhase())
joinRunner.AppendPhase(yurtphase.NewCloudNodePhase())
joinRunner.AppendPhase(yurtphase.NewConvertPhase())
joinRunner.AppendPhase(yurtphase.NewPostcheckPhase())

joinRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
Expand Down Expand Up @@ -157,6 +158,10 @@ func addJoinConfigFlags(flagSet *flag.FlagSet, joinOptions *joinOptions) {
&joinOptions.yurthubImage, "yurthub-image", "",
"Sets the image version of yurthub component",
)
flagSet.BoolVar(
&joinOptions.markAutonomous, "mark-autonomous", true,
"Annotate node autonomous if set true, default true.",
)
cmdutil.AddCRISocketFlag(flagSet, &joinOptions.externalcfg.NodeRegistration.CRISocket)
}

Expand All @@ -170,6 +175,7 @@ type joinData struct {
kustomizeDir string
nodeType string
yurthubImage string
markAutonomous bool
}

// newJoinData returns a new joinData struct to be used for the execution of the kubeadm join workflow.
Expand Down Expand Up @@ -276,6 +282,7 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
kustomizeDir: opt.kustomizeDir,
nodeType: opt.nodeType,
yurthubImage: opt.yurthubImage,
markAutonomous: opt.markAutonomous,
}, nil
}

Expand Down Expand Up @@ -400,3 +407,8 @@ func (j *joinData) NodeType() string {
func (j *joinData) YurtHubImage() string {
return j.yurthubImage
}

//MarkAutonomous returns markAutonomous setting.
func (j *joinData) MarkAutonomous() bool {
return j.markAutonomous
}
40 changes: 0 additions & 40 deletions pkg/yurtctl/cmd/join/phases/constants.go

This file was deleted.

91 changes: 91 additions & 0 deletions pkg/yurtctl/cmd/join/phases/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2021 The OpenYurt Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package phases

import (
"fmt"
"time"

"github.com/spf13/pflag"
"k8s.io/klog"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"

nodeconverter "github.com/openyurtio/openyurt/pkg/node-servant/convert"
"github.com/openyurtio/openyurt/pkg/yurtctl/constants"
"github.com/openyurtio/openyurt/pkg/yurthub/util"
)

const (
defaultYurthubHealthCheckTimeout = 2 * time.Minute
)

// NewConvertPhase creates a yurtctl workflow phase that convert native k8s node to openyurt node.
func NewConvertPhase() workflow.Phase {
return workflow.Phase{
Name: "Convert node to OpenYurt node. ",
Short: "Convert node",
Run: runConvertNode,
}
}

// if comes to this phase, means node is up to running as a k8s node
// then we convert it into a edge-node or cloud-node
func runConvertNode(c workflow.RunData) error {
data, ok := c.(YurtJoinData)
if !ok {
return fmt.Errorf("Join edge-node phase invoked with an invalid data struct. ")
}

// convert node
o := nodeconverter.NewConvertOptions()
f := constructFlagSet(data)
if err := o.Complete(f); err != nil {
return fmt.Errorf("fail to convert the kubernetes node to a yurt node: %s", err)
}

klog.Infof("convert with options:%v ", o)
converter := nodeconverter.NewConverterWithOptions(o)
if err := converter.Do(); err != nil {
return fmt.Errorf("fail to convert the kubernetes node to a yurt node: %s", err)
}

return nil
}

func constructFlagSet(data YurtJoinData) *pflag.FlagSet {
f := pflag.FlagSet{}

if data.NodeType() == constants.CloudNode {
f.String("working-mode", string(util.WorkingModeCloud), "")
} else if data.NodeType() == constants.EdgeNode {
f.String("working-mode", string(util.WorkingModeEdge), "")
}

yurtHubImg := data.YurtHubImage()
if len(yurtHubImg) == 0 {
yurtHubImg = fmt.Sprintf("%s/%s:%s", constants.DefaultOpenYurtImageRegistry, constants.Yurthub, constants.DefaultOpenYurtVersion)
}
f.String("yurthub-image", yurtHubImg, "")

token := data.Cfg().Discovery.TLSBootstrapToken
f.String("join-token", token, "")

f.Duration("yurthub-healthcheck-timeout", defaultYurthubHealthCheckTimeout, "")
f.String("kubeadm-conf-path", "", "")

return &f
}
16 changes: 5 additions & 11 deletions pkg/yurtctl/cmd/join/phases/join-cloud-node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import (
patchnodephase "k8s.io/kubernetes/cmd/kubeadm/app/phases/patchnode"
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"

"github.com/openyurtio/openyurt/pkg/yurtctl/constants"
)

var (
Expand Down Expand Up @@ -81,11 +79,7 @@ func NewCloudNodePhase() workflow.Phase {
}

//getCloudNodeJoinData get node configuration for cloud-node.
func getCloudNodeJoinData(c workflow.RunData) (*kubeadmapi.JoinConfiguration, *kubeadmapi.InitConfiguration, *clientcmdapi.Config, error) {
data, ok := c.(YurtJoinData)
if !ok {
return nil, nil, nil, errors.New("kubelet-start phase invoked with an invalid data struct")
}
func getCloudNodeJoinData(data YurtJoinData) (*kubeadmapi.JoinConfiguration, *kubeadmapi.InitConfiguration, *clientcmdapi.Config, error) {
cfg := data.Cfg()
initCfg, err := data.InitCfg()
if err != nil {
Expand All @@ -106,10 +100,10 @@ func runKubeletStartJoinPhase(c workflow.RunData) (returnErr error) {
if !ok {
return errors.New("kubelet-start phase invoked with an invalid data struct")
}
if data.NodeType() != constants.CloudNode {
return
}
cfg, initCfg, tlsBootstrapCfg, err := getCloudNodeJoinData(c)
//if data.NodeType() != constants.CloudNode {
// return
//}
cfg, initCfg, tlsBootstrapCfg, err := getCloudNodeJoinData(data)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 44d5da4

Please sign in to comment.