Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add ability for 'yurtctl convert/revert' to deploy/remove yurthub on cloud side. #513

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions pkg/yurtctl/cmd/convert/cloudnode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2020 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 convert

import (
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog"

"github.com/openyurtio/openyurt/pkg/yurthub/util"
)

// ConvertCloudNodeOptions has the information required by sub command convert cloudnode
type ConvertCloudNodeOptions struct {
ConvertNodeOptions
}

// NewConvertCloudNodeOptions creates a new ConvertCloudNodeOptions
func NewConvertCloudNodeOptions() *ConvertCloudNodeOptions {
return &ConvertCloudNodeOptions{}
}

// NewConvertCloudNodeCmd generates a new sub command convert cloudnode
func NewConvertCloudNodeCmd() *cobra.Command {
c := NewConvertCloudNodeOptions()
cmd := &cobra.Command{
Use: "cloudnode",
Short: "Converts the kubernetes node to a yurt cloud node",
Run: func(cmd *cobra.Command, _ []string) {
if err := c.Complete(cmd.Flags()); err != nil {
klog.Fatalf("fail to complete the convert cloudnode option: %s", err)
}
if err := c.RunConvertNode(util.WorkingModeCloud); err != nil {
klog.Fatalf("fail to convert the kubernetes node to a yurt node: %s", err)
}
},
}
cmd.Flags().StringP("cloud-nodes", "c", "",
"The list of cloud nodes wanted to be convert.(e.g. -e cloudnode1,cloudnode2)")
commonFlags(cmd)
return cmd
}

// Complete completes all the required options.
func (c *ConvertCloudNodeOptions) Complete(flags *pflag.FlagSet) error {
enStr, err := flags.GetString("cloud-nodes")
if err != nil {
return err
}
if enStr != "" {
c.Nodes = strings.Split(enStr, ",")
}
return c.ConvertNodeOptions.Complete(flags)
}
23 changes: 15 additions & 8 deletions pkg/yurtctl/cmd/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
"k8s.io/klog"
clusterinfophase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo"
nodeutil "k8s.io/kubernetes/pkg/controller/util/node"

"github.com/openyurtio/openyurt/pkg/projectinfo"
"github.com/openyurtio/openyurt/pkg/yurtctl/lock"
Expand Down Expand Up @@ -108,7 +106,7 @@ func NewConvertCmd() *cobra.Command {
}

cmd.AddCommand(NewConvertEdgeNodeCmd())

cmd.AddCommand(NewConvertCloudNodeCmd())
cmd.Flags().StringP("cloud-nodes", "c", "",
"The list of cloud nodes.(e.g. -c cloudnode1,cloudnode2)")
cmd.Flags().StringP("provider", "p", "minikube",
Expand Down Expand Up @@ -304,8 +302,7 @@ func (co *ConvertOptions) RunConvert() (err error) {
}
for _, node := range nodeLst.Items {
if !strutil.IsInStringLst(co.CloudNodes, node.GetName()) || strutil.IsInStringLst(kcmNodeNames, node.GetName()) {
_, condition := nodeutil.GetNodeCondition(&node.Status, v1.NodeReady)
if condition == nil || condition.Status != v1.ConditionTrue {
if !isNodeReady(&node.Status) {
klog.Errorf("Cannot do the convert, the status of worker node or kube-controller-manager node: %s is not 'Ready'.", node.Name)
return
}
Expand Down Expand Up @@ -389,8 +386,6 @@ func (co *ConvertOptions) RunConvert() (err error) {
return err
}

// 9. deploy yurt-hub and reset the kubelet service
klog.Infof("deploying the yurt-hub and resetting the kubelet service...")
joinToken, err := kubeutil.GetOrCreateJoinTokenString(co.clientSet)
if err != nil {
return err
Expand All @@ -409,11 +404,23 @@ func (co *ConvertOptions) RunConvert() (err error) {
ctx["yurthub_healthcheck_timeout"] = co.YurthubHealthCheckTimeout.String()
}

// 9. deploy yurt-hub and reset the kubelet service on edge nodes.
klog.Infof("deploying the yurt-hub and resetting the kubelet service on edge nodes...")
ctx["sub_command"] = "edgenode"
if err = kubeutil.RunServantJobs(co.clientSet, ctx, edgeNodeNames); err != nil {
klog.Errorf("fail to run ServantJobs: %s", err)
return
}
klog.Info("complete deploying yurt-hub")
klog.Info("complete deploying yurt-hub on edge nodes")

// 10. deploy yurt-hub and reset the kubelet service on cloud nodes
klog.Infof("deploying the yurt-hub and resetting the kubelet service on cloud nodes")
ctx["sub_command"] = "cloudnode"
if err = kubeutil.RunServantJobs(co.clientSet, ctx, co.CloudNodes); err != nil {
klog.Errorf("fail to run ServantJobs: %s", err)
return
}
klog.Info("complete deploying yurt-hub on cloud nodes")

return
}
Expand Down
Loading