From 4953d62c36b3259b1481cd8c4fe878c3a07eb9ef Mon Sep 17 00:00:00 2001 From: adamzhoul <770822772@qq.com> Date: Fri, 24 Sep 2021 10:20:12 +0800 Subject: [PATCH] add DeleteYurthubSetting to revert (#492) Co-authored-by: zhoulei5 --- pkg/yurtctl/cmd/revert/revert.go | 9 +++++ pkg/yurtctl/util/edgenode/common.go | 27 +++++++++---- pkg/yurtctl/util/kubernetes/apply_addons.go | 42 +++++++++++++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/pkg/yurtctl/cmd/revert/revert.go b/pkg/yurtctl/cmd/revert/revert.go index 8813af51251..3c6e4a81978 100644 --- a/pkg/yurtctl/cmd/revert/revert.go +++ b/pkg/yurtctl/cmd/revert/revert.go @@ -242,6 +242,15 @@ func (ro *RevertOptions) RunRevert() (err error) { return } klog.Info("complete removing yurt-hub and resetting kubelet service") + + // 7.1. remove yut-hub k8s config, roleBinding role + err = kubeutil.DeleteYurthubSetting(ro.clientSet) + if err != nil { + klog.Error("DeleteYurthubSetting err: ", err) + return err + } + klog.Info("delete yurthub clusterrole and clusterrolebinding") + return } diff --git a/pkg/yurtctl/util/edgenode/common.go b/pkg/yurtctl/util/edgenode/common.go index 225d3f4a745..c5d561596da 100644 --- a/pkg/yurtctl/util/edgenode/common.go +++ b/pkg/yurtctl/util/edgenode/common.go @@ -17,14 +17,17 @@ limitations under the License. package edgenode const ( - KubeletSvcPath = "/etc/systemd/system/kubelet.service.d/10-kubeadm.conf" - OpenyurtDir = "/var/lib/openyurt" - StaticPodPath = "/etc/kubernetes/manifests" - KubeCondfigPath = "/etc/kubernetes/kubelet.conf" - KubeCaFile = "/etc/kubernetes/pki/ca.crt" - YurthubYamlName = "yurt-hub.yaml" - KubeletConfName = "kubelet.conf" - KubeletSvcBackup = "%s.bk" + KubeletSvcPath = "/etc/systemd/system/kubelet.service.d/10-kubeadm.conf" + OpenyurtDir = "/var/lib/openyurt" + StaticPodPath = "/etc/kubernetes/manifests" + KubeCondfigPath = "/etc/kubernetes/kubelet.conf" + KubeCaFile = "/etc/kubernetes/pki/ca.crt" + YurthubYamlName = "yurt-hub.yaml" + YurthubComponentName = "yurt-hub" + YurthubNamespace = "kube-system" + YurthubCmName = "yurt-hub-cfg" + KubeletConfName = "kubelet.conf" + KubeletSvcBackup = "%s.bk" Hostname = "/etc/hostname" KubeletHostname = "--hostname-override=[^\"\\s]*" @@ -159,4 +162,12 @@ subjects: kind: Group name: system:nodes ` + YurthubConfigMap = ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: yurt-hub-cfg + namespace: kube-system +data: + cache_agents: ""` ) diff --git a/pkg/yurtctl/util/kubernetes/apply_addons.go b/pkg/yurtctl/util/kubernetes/apply_addons.go index 8155d534b06..69e0c2d3884 100644 --- a/pkg/yurtctl/util/kubernetes/apply_addons.go +++ b/pkg/yurtctl/util/kubernetes/apply_addons.go @@ -17,6 +17,11 @@ limitations under the License. package kubernetes import ( + "context" + "fmt" + + 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" @@ -214,5 +219,42 @@ func DeployYurthubSetting(client *kubernetes.Clientset) error { return err } + // 3. create the Configmap + if err := CreateConfigMapFromYaml(client, + "kube-system", + edgenode.YurthubConfigMap); err != nil { + return err + } + + return nil +} + +// DeleteYurthubSetting rm settings for yurthub pod +func DeleteYurthubSetting(client *kubernetes.Clientset) error { + + // 1. delete the ClusterRoleBinding + if err := client.RbacV1().ClusterRoleBindings(). + Delete(context.Background(), edgenode.YurthubComponentName, + metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("fail to delete the clusterrolebinding/%s: %s", + edgenode.YurthubComponentName, err) + } + + // 2. delete the ClusterRole + if err := client.RbacV1().ClusterRoles(). + Delete(context.Background(), edgenode.YurthubComponentName, + metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("fail to delete the clusterrole/%s: %s", + edgenode.YurthubComponentName, err) + } + + // 3. remove the ConfigMap + if err := client.CoreV1().ConfigMaps(edgenode.YurthubNamespace). + Delete(context.Background(), edgenode.YurthubCmName, + metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("fail to delete the configmap/%s: %s", + edgenode.YurthubCmName, err) + } + return nil }