Skip to content

Commit

Permalink
fix yurtctl (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peeknut authored May 25, 2021
1 parent ada536f commit d23036b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 71 deletions.
82 changes: 43 additions & 39 deletions pkg/yurtctl/cmd/convert/edgenode.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,22 @@ func (c *ConvertEdgeNodeOptions) Complete(flags *pflag.FlagSet) error {
}
c.KubeadmConfPath = kubeadmConfPath

joinToken, err := flags.GetString("join-token")
c.clientSet, err = enutil.GenClientSet(flags)
if err != nil {
return err
}
c.JoinToken = joinToken

c.clientSet, err = enutil.GenClientSet(flags)
joinToken, err := flags.GetString("join-token")
if err != nil {
return err
}
if joinToken == "" {
joinToken, err = kubeutil.GetOrCreateJoinTokenString(c.clientSet)
if err != nil {
return err
}
}
c.JoinToken = joinToken

openyurtDir := os.Getenv("OPENYURT_DIR")
if openyurtDir == "" {
Expand All @@ -182,11 +188,11 @@ func (c *ConvertEdgeNodeOptions) RunConvertEdgeNode() (err error) {
}
klog.V(4).Info("the server version is valid")

nodeName, err := enutil.GetNodeName()
nodeName, err := enutil.GetNodeName(c.KubeadmConfPath)
if err != nil {
return err
nodeName = ""
}
if len(c.EdgeNodes) > 1 || len(c.EdgeNodes) == 1 && c.EdgeNodes[0] != nodeName {
if len(c.EdgeNodes) > 1 || (len(c.EdgeNodes) == 1 && c.EdgeNodes[0] != nodeName) {
// 2 remote edgenode convert
nodeLst, err := c.clientSet.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
Expand All @@ -203,8 +209,7 @@ func (c *ConvertEdgeNodeOptions) RunConvertEdgeNode() (err error) {
}
for _, edgeNode := range c.EdgeNodes {
if !strutil.IsInStringLst(edgeNodeNames, edgeNode) {
klog.Errorf("Cannot do the convert, the worker node: %s is not a Kubernetes node.", edgeNode)
return err
return fmt.Errorf("Cannot do the convert, the worker node: %s is not a Kubernetes node.", edgeNode)
}
}

Expand All @@ -213,23 +218,17 @@ func (c *ConvertEdgeNodeOptions) RunConvertEdgeNode() (err error) {
if strutil.IsInStringLst(c.EdgeNodes, node.GetName()) {
_, condition := nodeutil.GetNodeCondition(&node.Status, v1.NodeReady)
if condition == nil || condition.Status != v1.ConditionTrue {
klog.Errorf("Cannot do the convert, the status of worker node: %s is not 'Ready'.", node.Name)
return err
return fmt.Errorf("Cannot do the convert, the status of worker node: %s is not 'Ready'.", node.Name)
}
}
}

// 2.3. deploy yurt-hub and reset the kubelet service
joinToken, err := kubeutil.GetOrCreateJoinTokenString(c.clientSet)
if err != nil {
return err
}

ctx := map[string]string{
"action": "convert",
"yurtctl_servant_image": c.YurctlServantImage,
"yurthub_image": c.YurthubImage,
"joinToken": joinToken,
"joinToken": c.JoinToken,
"pod_manifest_path": c.PodMainfestPath,
"kubeadm_conf_path": c.KubeadmConfPath,
}
Expand All @@ -242,41 +241,33 @@ func (c *ConvertEdgeNodeOptions) RunConvertEdgeNode() (err error) {
klog.Errorf("fail to run ServantJobs: %s", err)
return err
}
} else {
} else if (len(c.EdgeNodes) == 0 && nodeName != "") || (len(c.EdgeNodes) == 1 && c.EdgeNodes[0] == nodeName) {
// 3. local edgenode convert
// 3.1. check if critical files exist
if _, err := enutil.FileExists(c.KubeadmConfPath); err != nil {
return err
}
if ok, err := enutil.DirExists(c.PodMainfestPath); !ok {
return err
}

// 3.2. check the state of EdgeNodes
node, err := c.clientSet.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
return err
}

// 3.1. check the state of EdgeNodes
_, condition := nodeutil.GetNodeCondition(&node.Status, v1.NodeReady)
if condition == nil || condition.Status != v1.ConditionTrue {
klog.Errorf("Cannot do the convert, the status of worker node: %s is not 'Ready'.", node.Name)
return err
return fmt.Errorf("Cannot do the convert, the status of worker node: %s is not 'Ready'.", node.Name)
}

// 3.2. check the label of EdgeNodes
// 3.3. check the label of EdgeNodes
_, ok := node.Labels[projectinfo.GetEdgeWorkerLabelKey()]
if ok {
klog.Errorf("Cannot do the convert, the worker node: %s is not a Kubernetes node.", node.Name)
return err
return fmt.Errorf("Cannot do the convert, the worker node: %s is not a Kubernetes node.", node.Name)
}

// 3.3. label node as edge node
klog.Infof("mark %s as the edge-node", nodeName)
_, err = kubeutil.LabelNode(c.clientSet, node, projectinfo.GetEdgeWorkerLabelKey(), "true")
if err != nil {
return err
}

// 3. deploy yurt-hub and reset the kubelet service
if c.JoinToken == "" {
c.JoinToken, err = kubeutil.GetOrCreateJoinTokenString(c.clientSet)
if err != nil {
return err
}
}
// 3.4. deploy yurt-hub and reset the kubelet service
err = c.SetupYurthub()
if err != nil {
return fmt.Errorf("fail to set up the yurthub pod: %v", err)
Expand All @@ -285,8 +276,21 @@ func (c *ConvertEdgeNodeOptions) RunConvertEdgeNode() (err error) {
if err != nil {
return fmt.Errorf("fail to reset the kubelet service: %v", err)
}

// 3.5. label node as edge node
klog.Infof("mark %s as the edge-node", nodeName)
node, err = c.clientSet.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
return err
}
_, err = kubeutil.LabelNode(c.clientSet, node, projectinfo.GetEdgeWorkerLabelKey(), "true")
if err != nil {
return err
}
} else {
return fmt.Errorf("fail to revert edge node, flag --edge-nodes %s err", c.EdgeNodes)
}
return
return nil
}

// SetupYurthub sets up the yurthub pod and wait for the its status to be Running
Expand Down
73 changes: 44 additions & 29 deletions pkg/yurtctl/cmd/revert/edgenode.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ func (r *RevertEdgeNodeOptions) RunRevertEdgeNode() (err error) {
}
klog.V(4).Info("the server version is valid")

nodeName, err := enutil.GetNodeName()
nodeName, err := enutil.GetNodeName(r.KubeadmConfPath)
if err != nil {
return err
nodeName = ""
}
if len(r.EdgeNodes) > 1 || len(r.EdgeNodes) == 1 && r.EdgeNodes[0] != nodeName {
if len(r.EdgeNodes) > 1 || (len(r.EdgeNodes) == 1 && r.EdgeNodes[0] != nodeName) {
// 2. remote edgenode revert
nodeLst, err := r.clientSet.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
Expand Down Expand Up @@ -193,48 +193,68 @@ func (r *RevertEdgeNodeOptions) RunRevertEdgeNode() (err error) {
klog.Errorf("fail to revert edge node: %s", err)
return err
}
} else {
} else if (len(r.EdgeNodes) == 0 && nodeName != "") || (len(r.EdgeNodes) == 1 && r.EdgeNodes[0] == nodeName) {
// 3. local edgenode revert
// 3.1. check if critical files exist
yurtKubeletConf := r.getYurthubKubeletConf()
if ok, err := enutil.FileExists(yurtKubeletConf); !ok {
return err
}
kubeletSvcBk := r.getKubeletSvcBackup()
if _, err := enutil.FileExists(kubeletSvcBk); err != nil {
klog.Errorf("fail to get file %s, should revise the %s directly", kubeletSvcBk, r.KubeadmConfPath)
return err
}
yurthubYaml := r.getYurthubYaml()
if ok, err := enutil.FileExists(yurthubYaml); !ok {
return err
}

// 3.2. check the state of EdgeNodes
node, err := r.clientSet.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
return err
}

// 3.1. check the state of EdgeNodes
_, condition := nodeutil.GetNodeCondition(&node.Status, v1.NodeReady)
if condition == nil || condition.Status != v1.ConditionTrue {
klog.Errorf("Cannot do the revert, the status of worker node: %s is not 'Ready'.", node.Name)
return err
}

// 3.2. check the label of EdgeNodes and remove label
// 3.3. check the label of EdgeNodes
isEdgeNode, ok := node.Labels[projectinfo.GetEdgeWorkerLabelKey()]
if ok && isEdgeNode == "true" {
_, foundAutonomy := node.Annotations[constants.AnnotationAutonomy]
if foundAutonomy {
delete(node.Annotations, constants.AnnotationAutonomy)
}

delete(node.Labels, projectinfo.GetEdgeWorkerLabelKey())
if _, err = r.clientSet.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{}); err != nil {
return err
}
} else {
klog.Errorf("Cannot do the revert, the worker node: %s is not a Yurt edge node.", node.Name)
return err
if !ok || isEdgeNode == "false" {
return fmt.Errorf("Cannot do the revert, the worker node: %s is not a Yurt edge node.", node.Name)
}
klog.Info("label openyurt.io/is-edge-worker is removed")

// 3.3. remove yurt-hub and revert kubelet service
// 3.4. remove yurt-hub and revert kubelet service
if err := r.RevertKubelet(); err != nil {
return fmt.Errorf("fail to revert kubelet: %v", err)
}
if err := r.RemoveYurthub(); err != nil {
return err
}

// 3.5. remove label of EdgeNodes
node, err = r.clientSet.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
if err != nil {
return err
}
_, foundAutonomy := node.Annotations[constants.AnnotationAutonomy]
if foundAutonomy {
delete(node.Annotations, constants.AnnotationAutonomy)
}
delete(node.Labels, projectinfo.GetEdgeWorkerLabelKey())
if _, err = r.clientSet.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{}); err != nil {
return err
}
klog.Info("label openyurt.io/is-edge-worker is removed")

} else {
return fmt.Errorf("fail to revert edge node, flag --edge-nodes %s err", r.EdgeNodes)
}

return
return nil
}

// RevertKubelet resets the kubelet service
Expand All @@ -245,13 +265,8 @@ func (r *RevertEdgeNodeOptions) RevertKubelet() error {
return err
}
kubeletSvcBk := r.getKubeletSvcBackup()
_, err := enutil.FileExists(kubeletSvcBk)
if err != nil {
klog.Errorf("fail to get file %s, will revise the %s directly", kubeletSvcBk, r.KubeadmConfPath)
return err
}
klog.Infof("found backup file %s, will use it to revert the node", kubeletSvcBk)
err = os.Rename(kubeletSvcBk, r.KubeadmConfPath)
err := os.Rename(kubeletSvcBk, r.KubeadmConfPath)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/yurtctl/util/edgenode/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ func ReplaceRegularExpression(content string, replace map[string]string) string

// GetNodeName gets the node name based on environment variable, parameters --hostname-override
// in the configuration file or hostname
func GetNodeName() (string, error) {
func GetNodeName(kubeadmConfPath string) (string, error) {
//1. from env NODE_NAME
nodename := os.Getenv("NODE_NAME")
if nodename != "" {
return nodename, nil
}

//2. find --hostname-override in 10-kubeadm.conf
nodeName, err := GetSingleContentFromFile(KubeletSvcPath, KubeletHostname)
nodeName, err := GetSingleContentFromFile(kubeadmConfPath, KubeletHostname)
if nodeName != "" {
nodeName = strings.Split(nodeName, "=")[1]
return nodeName, nil
Expand All @@ -123,7 +123,7 @@ func GetNodeName() (string, error) {
}

//3. find --hostname-override in EnvironmentFile
environmentFiles, err := GetContentFormFile(KubeletSvcPath, KubeletEnvironmentFile)
environmentFiles, err := GetContentFormFile(kubeadmConfPath, KubeletEnvironmentFile)
if err != nil {
return "", err
}
Expand Down

0 comments on commit d23036b

Please sign in to comment.