diff options
| author | KubeEdge Bot <48982446+kubeedge-bot@users.noreply.github.com> | 2024-04-22 12:52:00 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-22 12:52:00 +0800 |
| commit | 35eb2fe4147ff9b3971c07c008173cdcd6cde4c5 (patch) | |
| tree | 105bf9d45ce68c5f34bfe1f78337a294034a90f4 | |
| parent | Merge pull request #5540 from luomengY/keadminit_image_repository (diff) | |
| parent | Refactor the command reset to third-level commands, like: keamd reset (diff) | |
| download | kubeedge-35eb2fe4147ff9b3971c07c008173cdcd6cde4c5.tar.gz | |
Merge pull request #5463 from luomengY/reset
Refactor the command reset to third-level commands, like: keamd reset
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/cloud/reset.go | 119 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/edge/reset_other.go | 139 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/reset_others.go | 125 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/util/common_others.go | 13 | ||||
| -rw-r--r-- | keadm/cmd/keadm/app/cmd/util/reset.go | 85 |
5 files changed, 392 insertions, 89 deletions
diff --git a/keadm/cmd/keadm/app/cmd/cloud/reset.go b/keadm/cmd/keadm/app/cmd/cloud/reset.go new file mode 100644 index 000000000..3541bc1d8 --- /dev/null +++ b/keadm/cmd/keadm/app/cmd/cloud/reset.go @@ -0,0 +1,119 @@ +/* +Copyright 2024 The KubeEdge 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 cloud + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" + + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common" + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/helm" + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/util" +) + +var ( + resetLongDescription = ` +keadm reset cloud command can be executed edge node +In cloud node it shuts down the cloud processes of KubeEdge +` + resetExample = ` +For cloud node: +keadm reset cloud +` +) + +func NewCloudReset() *cobra.Command { + isEdgeNode := false + reset := util.NewResetOptions() + + var cmd = &cobra.Command{ + Use: "cloud", + Short: "Teardowns CloudCore component", + Long: resetLongDescription, + Example: resetExample, + PreRunE: func(cmd *cobra.Command, args []string) error { + whoRunning := util.CloudCoreRunningModuleV2(reset) + if whoRunning == common.NoneRunning { + fmt.Println("None of CloudCore components are running in this host, exit") + os.Exit(0) + } + + if whoRunning == common.KubeEdgeEdgeRunning { + isEdgeNode = true + } + + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + if !reset.Force { + fmt.Println("[reset] WARNING: Changes made to this host by 'keadm init' or 'keadm join' will be reverted.") + fmt.Print("[reset] Are you sure you want to proceed? [y/N]: ") + s := bufio.NewScanner(os.Stdin) + s.Scan() + if err := s.Err(); err != nil { + return err + } + if strings.ToLower(s.Text()) != "y" { + return fmt.Errorf("aborted reset operation") + } + } + + // 1. kill cloudcore process. + if err := TearDownCloudCore(reset.Kubeconfig); err != nil { + return err + } + + // 3. Clean stateful directories + if err := util.CleanDirectories(isEdgeNode); err != nil { + return err + } + + //4. TODO: clean status information + + return nil + }, + } + + addResetFlags(cmd, reset) + return cmd +} + +// TearDownCloudCore will bring down cloud component, +func TearDownCloudCore(kubeConfig string) error { + ke := &helm.KubeCloudHelmInstTool{ + Common: util.Common{ + KubeConfig: kubeConfig, + }, + } + + err := ke.TearDown() + if err != nil { + return fmt.Errorf("TearDown failed, err:%v", err) + } + return nil +} + +func addResetFlags(cmd *cobra.Command, resetOpts *common.ResetOptions) { + cmd.Flags().StringVar(&resetOpts.Kubeconfig, common.FlagNameKubeConfig, common.DefaultKubeConfig, + "Use this key to set kube-config path, eg: $HOME/.kube/config") + cmd.Flags().BoolVar(&resetOpts.Force, "force", resetOpts.Force, + "Reset the node without prompting for confirmation") +} diff --git a/keadm/cmd/keadm/app/cmd/edge/reset_other.go b/keadm/cmd/keadm/app/cmd/edge/reset_other.go new file mode 100644 index 000000000..efc180d9d --- /dev/null +++ b/keadm/cmd/keadm/app/cmd/edge/reset_other.go @@ -0,0 +1,139 @@ +/* +Copyright 2024 The KubeEdge 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 edge + +import ( + "bufio" + "fmt" + "os" + "strings" + "time" + + "github.com/spf13/cobra" + phases "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/reset" + utilsexec "k8s.io/utils/exec" + + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common" + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/util" +) + +var ( + resetLongDescription = ` +keadm reset edge command can be executed edge node +In edge node it shuts down the edge processes of KubeEdge +` + resetExample = ` +For edge node edge: +keadm reset edge +` +) + +func NewOtherEdgeReset() *cobra.Command { + isEdgeNode := false + reset := util.NewResetOptions() + var cmd = &cobra.Command{ + Use: "edge", + Short: "Teardowns EdgeCore component", + Long: resetLongDescription, + Example: resetExample, + PreRunE: func(cmd *cobra.Command, args []string) error { + whoRunning := util.EdgeCoreRunningModuleV2(reset) + if whoRunning == common.NoneRunning { + fmt.Println("None of EdgeCore components are running in this host, exit") + os.Exit(0) + } + + if whoRunning == common.KubeEdgeEdgeRunning { + isEdgeNode = true + } + + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + if !reset.Force { + fmt.Println("[reset] WARNING: Changes made to this host by 'keadm init' or 'keadm join' will be reverted.") + fmt.Print("[reset] Are you sure you want to proceed? [y/N]: ") + s := bufio.NewScanner(os.Stdin) + s.Scan() + if err := s.Err(); err != nil { + return err + } + if strings.ToLower(s.Text()) != "y" { + return fmt.Errorf("aborted reset operation") + } + } + + staticPodPath := "" + config, err := util.ParseEdgecoreConfig(common.EdgecoreConfigPath) + if err != nil { + fmt.Printf("failed to get edgecore's config with err:%v\n", err) + } else { + if reset.Endpoint == "" { + reset.Endpoint = config.Modules.Edged.TailoredKubeletConfig.ContainerRuntimeEndpoint + } + staticPodPath = config.Modules.Edged.TailoredKubeletConfig.StaticPodPath + } + // first cleanup edge node static pod directory to stop static and mirror pod + if staticPodPath != "" { + if err := phases.CleanDir(staticPodPath); err != nil { + fmt.Printf("Failed to delete static pod directory %s: %v\n", staticPodPath, err) + } else { + time.Sleep(1 * time.Second) + fmt.Printf("Static pod directory has been removed!\n") + } + } + + // 1. kill edgecore process. + // For edgecore, don't delete node from K8S + if err := TearDownEdgeCore(); err != nil { + return err + } + + // 2. Remove containers managed by KubeEdge. Only for edge node. + if err := util.RemoveContainers(reset.Endpoint, utilsexec.New()); err != nil { + fmt.Printf("Failed to remove containers: %v\n", err) + } + + // 3. Clean stateful directories + if err := util.CleanDirectories(isEdgeNode); err != nil { + return err + } + + //4. TODO: clean status information + + return nil + }, + } + + addResetFlags(cmd, reset) + return cmd +} + +// TearDownEdgeCore will bring down edge component, +func TearDownEdgeCore() error { + ke := &util.KubeEdgeInstTool{Common: util.Common{}} + err := ke.TearDown() + if err != nil { + return fmt.Errorf("TearDown failed, err:%v", err) + } + return nil +} + +func addResetFlags(cmd *cobra.Command, resetOpts *common.ResetOptions) { + cmd.Flags().BoolVar(&resetOpts.Force, "force", resetOpts.Force, + "Reset the node without prompting for confirmation") +} diff --git a/keadm/cmd/keadm/app/cmd/reset_others.go b/keadm/cmd/keadm/app/cmd/reset_others.go index 7dc4f5a6f..2f438382f 100644 --- a/keadm/cmd/keadm/app/cmd/reset_others.go +++ b/keadm/cmd/keadm/app/cmd/reset_others.go @@ -27,10 +27,11 @@ import ( "github.com/spf13/cobra" phases "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/reset" - utilruntime "k8s.io/kubernetes/cmd/kubeadm/app/util/runtime" utilsexec "k8s.io/utils/exec" + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/cloud" "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common" + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/edge" "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/helm" "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/util" ) @@ -46,19 +47,13 @@ For cloud node: keadm reset For edge node: -keadm reset +keadm reset edge ` ) -func newResetOptions() *common.ResetOptions { - opts := &common.ResetOptions{} - opts.Kubeconfig = common.DefaultKubeConfig - return opts -} - func NewKubeEdgeReset() *cobra.Command { isEdgeNode := false - reset := newResetOptions() + reset := util.NewResetOptions() var cmd = &cobra.Command{ Use: "reset", @@ -92,16 +87,21 @@ func NewKubeEdgeReset() *cobra.Command { } } - // first cleanup edge node static pod directory to stop static and mirror pod if isEdgeNode { + staticPodPath := "" config, err := util.ParseEdgecoreConfig(common.EdgecoreConfigPath) if err != nil { - return err + fmt.Printf("failed to get edgecore's config with err:%v\n", err) + } else { + if reset.Endpoint == "" { + reset.Endpoint = config.Modules.Edged.TailoredKubeletConfig.ContainerRuntimeEndpoint + } + staticPodPath = config.Modules.Edged.TailoredKubeletConfig.StaticPodPath } - dir := config.Modules.Edged.TailoredKubeletConfig.StaticPodPath - if dir != "" { - if err := phases.CleanDir(dir); err != nil { - fmt.Printf("Failed to delete static pod directory %s: %v\n", dir, err) + // first cleanup edge node static pod directory to stop static and mirror pod + if staticPodPath != "" { + if err := phases.CleanDir(staticPodPath); err != nil { + fmt.Printf("Failed to delete static pod directory %s: %v\n", staticPodPath, err) } else { time.Sleep(1 * time.Second) fmt.Printf("Static pod directory has been removed!\n") @@ -116,108 +116,57 @@ func NewKubeEdgeReset() *cobra.Command { } // 2. Remove containers managed by KubeEdge. Only for edge node. - if err := RemoveContainers(isEdgeNode, utilsexec.New()); err != nil { - fmt.Printf("Failed to remove containers: %v\n", err) + if isEdgeNode { + if err := util.RemoveContainers(reset.Endpoint, utilsexec.New()); err != nil { + fmt.Printf("Failed to remove containers: %v\n", err) + } } // 3. Clean stateful directories - if err := cleanDirectories(isEdgeNode); err != nil { + if err := util.CleanDirectories(isEdgeNode); err != nil { return err } - // cleanup mqtt container - if err := RemoveMqttContainer(reset.Endpoint, ""); err != nil { - fmt.Printf("Failed to remove MQTT container: %v\n", err) - } //4. TODO: clean status information return nil }, } - + edgeCmd := edge.NewOtherEdgeReset() + cloudCmd := cloud.NewCloudReset() + cmd.AddCommand(edgeCmd) + cmd.AddCommand(cloudCmd) addResetFlags(cmd, reset) return cmd } -func RemoveMqttContainer(endpoint, cgroupDriver string) error { - runtime, err := util.NewContainerRuntime(endpoint, cgroupDriver) - if err != nil { - return fmt.Errorf("failed to new container runtime: %v", err) - } - - return runtime.RemoveMQTT() +func addResetFlags(cmd *cobra.Command, resetOpts *common.ResetOptions) { + cmd.Flags().StringVar(&resetOpts.Kubeconfig, common.FlagNameKubeConfig, common.DefaultKubeConfig, + "Use this key to set kube-config path, eg: $HOME/.kube/config") + cmd.Flags().BoolVar(&resetOpts.Force, "force", resetOpts.Force, + "Reset the node without prompting for confirmation") } // TearDownKubeEdge will bring down either cloud or edge components, // depending upon in which type of node it is executed func TearDownKubeEdge(isEdgeNode bool, kubeConfig string) error { var ke common.ToolsInstaller + if isEdgeNode { + ke = &util.KubeEdgeInstTool{Common: util.Common{}} + err := ke.TearDown() + if err != nil { + return fmt.Errorf("TearDown failed, err:%v", err) + } + } + ke = &helm.KubeCloudHelmInstTool{ Common: util.Common{ KubeConfig: kubeConfig, }, } - if isEdgeNode { - ke = &util.KubeEdgeInstTool{Common: util.Common{}} - } - err := ke.TearDown() if err != nil { return fmt.Errorf("TearDown failed, err:%v", err) } return nil } - -// RemoveContainers removes all Kubernetes-managed containers -func RemoveContainers(isEdgeNode bool, execer utilsexec.Interface) error { - if !isEdgeNode { - return nil - } - - criSocketPath, err := utilruntime.DetectCRISocket() - if err != nil { - return err - } - - containerRuntime, err := utilruntime.NewContainerRuntime(execer, criSocketPath) - if err != nil { - return err - } - - containers, err := containerRuntime.ListKubeContainers() - if err != nil { - return err - } - - return containerRuntime.RemoveContainers(containers) -} - -func cleanDirectories(isEdgeNode bool) error { - var dirToClean = []string{ - util.KubeEdgePath, - util.KubeEdgeLogPath, - util.KubeEdgeSocketPath, - util.EdgeRootDir, - } - - if isEdgeNode { - dirToClean = append(dirToClean, "/var/lib/dockershim", "/var/run/kubernetes", "/var/lib/cni") - } - - for _, dir := range dirToClean { - if err := phases.CleanDir(dir); err != nil { - fmt.Printf("Failed to delete directory %s: %v\n", dir, err) - } - } - - return nil -} - -func addResetFlags(cmd *cobra.Command, resetOpts *common.ResetOptions) { - cmd.Flags().StringVar(&resetOpts.Kubeconfig, common.FlagNameCloudCoreIPPort, resetOpts.Kubeconfig, - "Use this key to set kube-config path, eg: $HOME/.kube/config") - cmd.Flags().BoolVar(&resetOpts.Force, "force", resetOpts.Force, - "Reset the node without prompting for confirmation") - cmd.Flags().StringVar(&resetOpts.Endpoint, common.FlagNameRemoteRuntimeEndpoint, resetOpts.Endpoint, - "Use this key to set container runtime endpoint") -} diff --git a/keadm/cmd/keadm/app/cmd/util/common_others.go b/keadm/cmd/keadm/app/cmd/util/common_others.go index a1408b461..47ceb92ef 100644 --- a/keadm/cmd/keadm/app/cmd/util/common_others.go +++ b/keadm/cmd/keadm/app/cmd/util/common_others.go @@ -84,7 +84,14 @@ func HasSystemd() bool { // RunningModuleV2 identifies cloudcore/edgecore running or not. // only used for cloudcore container install and edgecore binary install func RunningModuleV2(opt *types.ResetOptions) types.ModuleRunning { - osType := GetOSInterface() + result := CloudCoreRunningModuleV2(opt) + if result == types.KubeEdgeCloudRunning { + return result + } + return EdgeCoreRunningModuleV2(opt) +} + +func CloudCoreRunningModuleV2(opt *types.ResetOptions) types.ModuleRunning { cloudCoreRunning, err := IsCloudcoreContainerRunning(constants.SystemNamespace, opt.Kubeconfig) if err != nil { // just log the error, maybe we do not care @@ -93,7 +100,11 @@ func RunningModuleV2(opt *types.ResetOptions) types.ModuleRunning { if cloudCoreRunning { return types.KubeEdgeCloudRunning } + return types.NoneRunning +} +func EdgeCoreRunningModuleV2(opt *types.ResetOptions) types.ModuleRunning { + osType := GetOSInterface() edgeCoreRunning, err := osType.IsKubeEdgeProcessRunning(KubeEdgeBinaryName) if err != nil { // just log the error, maybe we do not care diff --git a/keadm/cmd/keadm/app/cmd/util/reset.go b/keadm/cmd/keadm/app/cmd/util/reset.go new file mode 100644 index 000000000..e121d2611 --- /dev/null +++ b/keadm/cmd/keadm/app/cmd/util/reset.go @@ -0,0 +1,85 @@ +/* +Copyright 2024 The KubeEdge 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 util + +import ( + "fmt" + + phases "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/reset" + utilruntime "k8s.io/kubernetes/cmd/kubeadm/app/util/runtime" + utilsexec "k8s.io/utils/exec" + + "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common" +) + +func NewResetOptions() *common.ResetOptions { + opts := &common.ResetOptions{} + return opts +} + +func RemoveMqttContainer(endpoint, cgroupDriver string) error { + runtime, err := NewContainerRuntime(endpoint, cgroupDriver) + if err != nil { + return fmt.Errorf("failed to new container runtime: %v", err) + } + + return runtime.RemoveMQTT() +} + +// RemoveContainers removes all Kubernetes-managed containers +func RemoveContainers(criSocketPath string, execer utilsexec.Interface) error { + if criSocketPath == "" { + var err error + criSocketPath, err = utilruntime.DetectCRISocket() + if err != nil { + return fmt.Errorf("failed to get crisocket with err:%v", err) + } + } + + containerRuntime, err := utilruntime.NewContainerRuntime(execer, criSocketPath) + if err != nil { + return err + } + + containers, err := containerRuntime.ListKubeContainers() + if err != nil { + return err + } + + return containerRuntime.RemoveContainers(containers) +} + +func CleanDirectories(isEdgeNode bool) error { + var dirToClean = []string{ + KubeEdgePath, + KubeEdgeLogPath, + KubeEdgeSocketPath, + EdgeRootDir, + } + + if isEdgeNode { + dirToClean = append(dirToClean, "/var/lib/dockershim", "/var/run/kubernetes", "/var/lib/cni") + } + + for _, dir := range dirToClean { + if err := phases.CleanDir(dir); err != nil { + fmt.Printf("Failed to delete directory %s: %v\n", dir, err) + } + } + + return nil +} |
