diff --git a/pkg/kosmosctl/install/install.go b/pkg/kosmosctl/install/install.go index c25fdd4eb..12901b3c7 100644 --- a/pkg/kosmosctl/install/install.go +++ b/pkg/kosmosctl/install/install.go @@ -404,30 +404,6 @@ func (o *CommandInstallOptions) runClustertree() error { } klog.Info("Create CRD " + clustertreeCluster.Name + " successful.") - serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil) - if err != nil { - return err - } - _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{}) - if err != nil { - if !apierrors.IsAlreadyExists(err) { - return fmt.Errorf("kosmosctl install clustertree run error, crd options failed: %v", err) - } - } - klog.Info("Create CRD " + serviceExport.Name + " successful.") - - serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil) - if err != nil { - return err - } - _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{}) - if err != nil { - if !apierrors.IsAlreadyExists(err) { - return fmt.Errorf("kosmosctl install clustertree run error, crd options failed: %v", err) - } - } - klog.Info("Create CRD " + serviceImport.Name + " successful.") - klog.Info("Start creating kosmos-clustertree ConfigMap...") clustertreeConfigMap := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/kosmosctl/join/join.go b/pkg/kosmosctl/join/join.go index a307adc6f..d09838dee 100644 --- a/pkg/kosmosctl/join/join.go +++ b/pkg/kosmosctl/join/join.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" + extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -60,9 +61,11 @@ type CommandJoinOptions struct { UseProxy string EnableTree bool + LeafModel string - KosmosClient versioned.Interface - K8sClient kubernetes.Interface + KosmosClient versioned.Interface + K8sClient kubernetes.Interface + K8sExtensionsClient extensionsclient.Interface } // NewCmdJoin join resource to Kosmos control plane. @@ -99,6 +102,7 @@ func NewCmdJoin(f ctlutil.Factory) *cobra.Command { flags.StringVar(&o.IpFamily, "ip-family", utils.DefaultIPv4, "Specify the IP protocol version used by network devices, common IP families include IPv4 and IPv6.") flags.StringVar(&o.UseProxy, "use-proxy", "false", "Set whether to enable proxy.") flags.BoolVar(&o.EnableTree, "enable-tree", false, "Turn on clustertree.") + flags.StringVar(&o.LeafModel, "leaf-model", "", "Set leaf cluster model, which supports one-to-one model.") flags.IntVarP(&o.WaitTime, "wait-time", "", utils.DefaultWaitTime, "Wait the specified time for the Kosmos install ready.") return cmd @@ -147,7 +151,12 @@ func (o *CommandJoinOptions) Complete(f ctlutil.Factory) error { o.K8sClient, err = kubernetes.NewForConfig(clusterConfig) if err != nil { - return fmt.Errorf("kosmosctl join complete error, generate basic client failed: %v", err) + return fmt.Errorf("kosmosctl join complete error, generate K8s basic client failed: %v", err) + } + + o.K8sExtensionsClient, err = extensionsclient.NewForConfig(clusterConfig) + if err != nil { + return fmt.Errorf("kosmosctl join complete error, generate K8s extensions client failed: %v", err) } } else { return fmt.Errorf("kosmosctl join complete error, arg ClusterKubeConfig is required") @@ -246,10 +255,62 @@ func (o *CommandJoinOptions) runCluster() error { cluster.Spec.ClusterLinkOptions.CNI = o.CNI } - // ToDo ClusterTree currently has no init parameters, can be expanded later. - //if o.EnableTree { - // - //} + if o.EnableTree { + serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil) + if err != nil { + return err + } + _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) + } + } + klog.Info("Create CRD " + serviceExport.Name + " successful.") + + serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil) + if err != nil { + return err + } + _, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err) + } + } + klog.Info("Create CRD " + serviceImport.Name + " successful.") + + if len(o.LeafModel) > 0 { + switch o.LeafModel { + case "one-to-one": + // ToDo Perform follow-up query based on the leaf cluster label + nodes, err := o.K8sClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{ + LabelSelector: utils.KosmosNodeJoinLabel + "=" + utils.KosmosNodeJoinValue, + }) + if err != nil { + return fmt.Errorf("kosmosctl join run error, list cluster node failed: %v", err) + } + var leafModels []v1alpha1.LeafModel + for _, n := range nodes.Items { + leafModel := v1alpha1.LeafModel{ + LeafNodeName: n.Name, + Taints: []corev1.Taint{ + { + Effect: utils.KosmosNodeTaintEffect, + Key: utils.KosmosNodeTaintKey, + Value: utils.KosmosNodeValue, + }, + }, + NodeSelector: v1alpha1.NodeSelector{ + NodeName: n.Name, + }, + } + leafModels = append(leafModels, leafModel) + } + cluster.Spec.ClusterTreeOptions.LeafModels = leafModels + } + } + } if o.RootFlag { cluster.Annotations = map[string]string{ diff --git a/pkg/kosmosctl/manifest/manifest_crds.go b/pkg/kosmosctl/manifest/manifest_crds.go index 20314e586..1174c1550 100644 --- a/pkg/kosmosctl/manifest/manifest_crds.go +++ b/pkg/kosmosctl/manifest/manifest_crds.go @@ -491,60 +491,11 @@ spec: enable: default: true type: boolean - leafModel: - description: LeafModel provide an api to arrange the member cluster + leafModels: + description: LeafModels provide an api to arrange the member cluster with some rules to pretend one or more leaf node items: properties: - labelSelector: - description: LabelSelector is a filter to select member - cluster nodes to pretend a leaf node in clusterTree by - labels. If nil or empty, the hole member cluster nodes - will pretend one leaf node. - properties: - matchExpressions: - description: matchExpressions is a list of label selector - requirements. The requirements are ANDed. - items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that - relates the key and values. - properties: - key: - description: key is the label key that the selector - applies to. - type: string - operator: - description: operator represents a key's relationship - to a set of values. Valid operators are In, - NotIn, Exists and DoesNotExist. - type: string - values: - description: values is an array of string values. - If the operator is In or NotIn, the values array - must be non-empty. If the operator is Exists - or DoesNotExist, the values array must be empty. - This array is replaced during a strategic merge - patch. - items: - type: string - type: array - required: - - key - - operator - type: object - type: array - matchLabels: - additionalProperties: - type: string - description: matchLabels is a map of {key,value} pairs. - A single {key,value} in the matchLabels map is equivalent - to an element of matchExpressions, whose key field - is "key", the operator is "In", and the values array - contains only "value". The requirements are ANDed. - type: object - type: object - x-kubernetes-map-type: atomic labels: additionalProperties: type: string @@ -556,6 +507,65 @@ spec: the leaf node name will generate by controller and fill in cluster link status type: string + nodeSelector: + description: NodeSelector is a selector to select member + cluster nodes to pretend a leaf node in clusterTree. + properties: + labelSelector: + description: LabelSelector is a filter to select member + cluster nodes to pretend a leaf node in clusterTree + by labels. It will work on second level schedule on + pod create in member clusters. + properties: + matchExpressions: + description: matchExpressions is a list of label + selector requirements. The requirements are ANDed. + items: + description: A label selector requirement is a + selector that contains values, a key, and an + operator that relates the key and values. + properties: + key: + description: key is the label key that the + selector applies to. + type: string + operator: + description: operator represents a key's relationship + to a set of values. Valid operators are + In, NotIn, Exists and DoesNotExist. + type: string + values: + description: values is an array of string + values. If the operator is In or NotIn, + the values array must be non-empty. If the + operator is Exists or DoesNotExist, the + values array must be empty. This array is + replaced during a strategic merge patch. + items: + type: string + type: array + required: + - key + - operator + type: object + type: array + matchLabels: + additionalProperties: + type: string + description: matchLabels is a map of {key,value} + pairs. A single {key,value} in the matchLabels + map is equivalent to an element of matchExpressions, + whose key field is "key", the operator is "In", + and the values array contains only "value". The + requirements are ANDed. + type: object + type: object + x-kubernetes-map-type: atomic + nodeName: + description: NodeName is Member cluster origin node + Name + type: string + type: object taints: description: Taints attached to the leaf pretended Node. If nil or empty, controller will set the default no-schedule diff --git a/pkg/kosmosctl/uninstall/uninstall.go b/pkg/kosmosctl/uninstall/uninstall.go index 920147052..97930452b 100644 --- a/pkg/kosmosctl/uninstall/uninstall.go +++ b/pkg/kosmosctl/uninstall/uninstall.go @@ -319,7 +319,7 @@ func (o *CommandUninstallOptions) runClustertree() error { } } else { klog.Info("Deployment " + clustertreeDeploy.Name + " is deleted.") - clustertreeSecret, err := util.GenerateService(manifest.ClusterTreeClusterManagerSecret, manifest.SecretReplace{ + clustertreeSecret, err := util.GenerateSecret(manifest.ClusterTreeClusterManagerSecret, manifest.SecretReplace{ Namespace: o.Namespace, Cert: "", Key: "", diff --git a/pkg/kosmosctl/unjoin/unjoin.go b/pkg/kosmosctl/unjoin/unjoin.go index 82973db6c..24ed7fcfc 100644 --- a/pkg/kosmosctl/unjoin/unjoin.go +++ b/pkg/kosmosctl/unjoin/unjoin.go @@ -6,9 +6,9 @@ import ( "time" "github.com/spf13/cobra" + extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" 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" restclient "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -17,24 +17,29 @@ import ( "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" + "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1" + "github.com/kosmos.io/kosmos/pkg/generated/clientset/versioned" "github.com/kosmos.io/kosmos/pkg/kosmosctl/manifest" "github.com/kosmos.io/kosmos/pkg/kosmosctl/util" "github.com/kosmos.io/kosmos/pkg/utils" ) var unjoinExample = templates.Examples(i18n.T(` - # Unjoin cluster from Kosmos control plane, e.g: - kosmosctl unjoin cluster --name cluster-name --kubeconfig ~/kubeconfig/cluster-kubeconfig -`)) + # Unjoin cluster from Kosmos control plane, e.g: + kosmosctl unjoin cluster --name cluster-name + + # Unjoin cluster from Kosmos control plane, if you need to specify a special cluster kubeconfig, e.g: + kosmosctl unjoin cluster --name cluster-name --kubeconfig ~/kubeconfig/cluster-kubeconfig`)) type CommandUnJoinOptions struct { + Name string + Namespace string KubeConfig string HostKubeConfig string - Name string - - Client kubernetes.Interface - DynamicClient *dynamic.DynamicClient + KosmosClient versioned.Interface + K8sClient kubernetes.Interface + K8sExtensionsClient extensionsclient.Interface } // NewCmdUnJoin Delete resource in Kosmos control plane. @@ -43,7 +48,7 @@ func NewCmdUnJoin(f ctlutil.Factory) *cobra.Command { cmd := &cobra.Command{ Use: "unjoin", - Short: i18n.T("Unjoin resource in kosmos control plane"), + Short: i18n.T("Unjoin resource from Kosmos control plane"), Long: "", Example: unjoinExample, SilenceUsage: true, @@ -56,14 +61,16 @@ func NewCmdUnJoin(f ctlutil.Factory) *cobra.Command { }, } + cmd.Flags().StringVar(&o.Name, "name", "", "Specify the name of the resource to unjoin.") + cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", utils.DefaultNamespace, "Kosmos namespace.") cmd.Flags().StringVar(&o.KubeConfig, "kubeconfig", "", "Absolute path to the cluster kubeconfig file.") cmd.Flags().StringVar(&o.HostKubeConfig, "host-kubeconfig", "", "Absolute path to the special host kubeconfig file.") - cmd.Flags().StringVar(&o.Name, "name", "", "Specify the name of the resource to unjoin.") return cmd } func (o *CommandUnJoinOptions) Complete(f ctlutil.Factory) error { var hostConfig *restclient.Config + var clusterConfig *restclient.Config var err error if o.HostKubeConfig != "" { @@ -78,19 +85,36 @@ func (o *CommandUnJoinOptions) Complete(f ctlutil.Factory) error { } } - clusterConfig, err := clientcmd.BuildConfigFromFlags("", o.KubeConfig) + o.KosmosClient, err = versioned.NewForConfig(hostConfig) if err != nil { - return fmt.Errorf("kosmosctl unjoin complete error, generate clusterConfig failed: %s", err) + return fmt.Errorf("kosmosctl install complete error, generate Kosmos client failed: %v", err) } - o.Client, err = kubernetes.NewForConfig(clusterConfig) + if o.KubeConfig != "" { + clusterConfig, err = clientcmd.BuildConfigFromFlags("", o.KubeConfig) + if err != nil { + return fmt.Errorf("kosmosctl unjoin complete error, generate clusterConfig failed: %s", err) + } + } else { + var cluster *v1alpha1.Cluster + cluster, err = o.KosmosClient.KosmosV1alpha1().Clusters().Get(context.TODO(), o.Name, metav1.GetOptions{}) + if err != nil { + return fmt.Errorf("kosmosctl unjoin complete error, get cluster failed: %s", err) + } + clusterConfig, err = clientcmd.RESTConfigFromKubeConfig(cluster.Spec.Kubeconfig) + if err != nil { + return fmt.Errorf("kosmosctl unjoin complete error, generate clusterConfig failed: %s", err) + } + } + + o.K8sClient, err = kubernetes.NewForConfig(clusterConfig) if err != nil { - return fmt.Errorf("kosmosctl join complete error, generate basic client failed: %v", err) + return fmt.Errorf("kosmosctl unjoin complete error, generate K8s basic client failed: %v", err) } - o.DynamicClient, err = dynamic.NewForConfig(hostConfig) + o.K8sExtensionsClient, err = extensionsclient.NewForConfig(clusterConfig) if err != nil { - return fmt.Errorf("kosmosctl unjoin complete error, generate dynamic client failed: %s", err) + return fmt.Errorf("kosmosctl unjoin complete error, generate K8s extensions client failed: %v", err) } return nil @@ -101,25 +125,6 @@ func (o *CommandUnJoinOptions) Validate(args []string) error { return fmt.Errorf("kosmosctl unjoin validate error, name is not valid") } - switch args[0] { - case "cluster": - _, err := o.DynamicClient.Resource(util.ClusterGVR).Get(context.TODO(), o.Name, metav1.GetOptions{}) - if err != nil { - if apierrors.IsNotFound(err) { - return fmt.Errorf("kosmosctl unjoin validate warning, clsuter is not found: %s", err) - } - return fmt.Errorf("kosmosctl unjoin validate error, get cluster failed: %s", err) - } - case "knode": - _, err := o.DynamicClient.Resource(util.KnodeGVR).Get(context.TODO(), o.Name, metav1.GetOptions{}) - if err != nil { - if apierrors.IsNotFound(err) { - return fmt.Errorf("kosmosctl unjoin validate warning, knode is not found: %s", err) - } - return fmt.Errorf("kosmosctl unjoin validate error, get knode failed: %s", err) - } - } - return nil } @@ -130,11 +135,6 @@ func (o *CommandUnJoinOptions) Run(args []string) error { if err != nil { return err } - case "knode": - err := o.runKnode() - if err != nil { - return err - } } return nil @@ -142,9 +142,9 @@ func (o *CommandUnJoinOptions) Run(args []string) error { func (o *CommandUnJoinOptions) runCluster() error { klog.Info("Start removing cluster from kosmos control plane...") - // 1. delete cluster + // delete cluster for { - err := o.DynamicClient.Resource(util.ClusterGVR).Namespace("").Delete(context.TODO(), o.Name, metav1.DeleteOptions{}) + err := o.KosmosClient.KosmosV1alpha1().Clusters().Delete(context.TODO(), o.Name, metav1.DeleteOptions{}) if err != nil { if apierrors.IsNotFound(err) { break @@ -155,55 +155,97 @@ func (o *CommandUnJoinOptions) runCluster() error { } klog.Info("Cluster: " + o.Name + " has been deleted.") - // 2. delete operator - clusterlinkOperatorDeployment, err := util.GenerateDeployment(manifest.KosmosOperatorDeployment, nil) + // delete crd + serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil) if err != nil { return err } - err = o.Client.AppsV1().Deployments(utils.DefaultNamespace).Delete(context.TODO(), clusterlinkOperatorDeployment.Name, metav1.DeleteOptions{}) - if err != nil && !apierrors.IsNotFound(err) { - return fmt.Errorf("kosmosctl unjoin run error, delete deployment failed: %s", err) + err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Delete(context.Background(), serviceExport.Name, metav1.DeleteOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl unjoin run error, crd options failed: %v", err) + } } - klog.Info("Deployment: " + clusterlinkOperatorDeployment.Name + " has been deleted.") + klog.Info("CRD: " + serviceExport.Name + " has been deleted.") - // 3. delete secret - err = o.Client.CoreV1().Secrets(utils.DefaultNamespace).Delete(context.TODO(), utils.ControlPanelSecretName, metav1.DeleteOptions{}) + serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil) + if err != nil { + return err + } + err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Delete(context.Background(), serviceImport.Name, metav1.DeleteOptions{}) + if err != nil { + if !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("kosmosctl unjoin run error, crd options failed: %v", err) + } + } + klog.Info("CRD: " + serviceImport.Name + " has been deleted.") + + // delete rbac + err = o.K8sClient.CoreV1().Secrets(o.Namespace).Delete(context.TODO(), utils.ControlPanelSecretName, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("kosmosctl unjoin run error, delete secret failed: %s", err) } klog.Info("Secret: " + utils.ControlPanelSecretName + " has been deleted.") - // 4. delete rbac - err = o.Client.RbacV1().ClusterRoleBindings().Delete(context.TODO(), utils.ExternalIPPoolNamePrefix, metav1.DeleteOptions{}) + err = o.K8sClient.RbacV1().ClusterRoleBindings().Delete(context.TODO(), utils.ExternalIPPoolNamePrefix, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("kosmosctl unjoin run error, delete clusterrolebinding failed: %s", err) } klog.Info("ClusterRoleBinding: " + utils.ExternalIPPoolNamePrefix + " has been deleted.") - err = o.Client.RbacV1().ClusterRoles().Delete(context.TODO(), utils.ExternalIPPoolNamePrefix, metav1.DeleteOptions{}) + err = o.K8sClient.RbacV1().ClusterRoles().Delete(context.TODO(), utils.ExternalIPPoolNamePrefix, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("kosmosctl unjoin run error, delete clusterrole failed: %s", err) } klog.Info("ClusterRole: " + utils.ExternalIPPoolNamePrefix + " has been deleted.") - clusterlinkOperatorServiceAccount, err := util.GenerateServiceAccount(manifest.KosmosOperatorServiceAccount, nil) + kosmosCR, err := util.GenerateClusterRole(manifest.KosmosClusterRole, nil) + if err != nil { + return fmt.Errorf("kosmosctl unjoin run error, generate clusterrole failed: %s", err) + } + err = o.K8sClient.RbacV1().ClusterRoles().Delete(context.TODO(), kosmosCR.Name, metav1.DeleteOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("kosmosctl unjoin run error, delete clusterrole failed: %s", err) + } + klog.Info("ClusterRole " + kosmosCR.Name + " has been deleted.") + + kosmosCRB, err := util.GenerateClusterRoleBinding(manifest.KosmosClusterRoleBinding, manifest.ClusterRoleBindingReplace{ + Namespace: o.Namespace, + }) + if err != nil { + return fmt.Errorf("kosmosctl join run error, generate clusterrolebinding failed: %s", err) + } + err = o.K8sClient.RbacV1().ClusterRoleBindings().Delete(context.TODO(), kosmosCRB.Name, metav1.DeleteOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("kosmosctl unjoin run error, delete clusterrolebinding failed: %s", err) + } + klog.Info("ClusterRoleBinding " + kosmosCRB.Name + " has been deleted.") + + kosmosOperatorSA, err := util.GenerateServiceAccount(manifest.KosmosOperatorServiceAccount, nil) if err != nil { return err } - err = o.Client.CoreV1().ServiceAccounts(utils.DefaultNamespace).Delete(context.TODO(), clusterlinkOperatorServiceAccount.Name, metav1.DeleteOptions{}) + err = o.K8sClient.CoreV1().ServiceAccounts(o.Namespace).Delete(context.TODO(), kosmosOperatorSA.Name, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("kosmosctl unjoin run error, delete serviceaccout failed: %s", err) } - klog.Info("ServiceAccount: " + clusterlinkOperatorServiceAccount.Name + " has been deleted.") + klog.Info("ServiceAccount: " + kosmosOperatorSA.Name + " has been deleted.") - // 5. If cluster is not the master, delete namespace - clusterlinkNetworkManagerDeployment, err := util.GenerateDeployment(manifest.ClusterlinkNetworkManagerDeployment, nil) + kosmosControlSA, err := util.GenerateServiceAccount(manifest.KosmosControlServiceAccount, manifest.ServiceAccountReplace{ + Namespace: o.Namespace, + }) if err != nil { - return err + return fmt.Errorf("kosmosctl unjoin run error, generate serviceaccount failed: %s", err) + } + err = o.K8sClient.CoreV1().ServiceAccounts(kosmosControlSA.Namespace).Delete(context.TODO(), kosmosControlSA.Name, metav1.DeleteOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("kosmosctl unjoin run error, delete serviceaccount failed: %s", err) } - _, err = o.Client.AppsV1().Deployments(utils.DefaultNamespace).Get(context.TODO(), clusterlinkNetworkManagerDeployment.Name, metav1.GetOptions{}) - if err != nil && apierrors.IsNotFound(err) { - err = o.Client.CoreV1().Namespaces().Delete(context.TODO(), utils.DefaultNamespace, metav1.DeleteOptions{}) + klog.Info("ServiceAccount " + kosmosControlSA.Name + " has been deleted.") + + // if cluster is not the master, delete namespace + if o.Name != utils.DefaultClusterName { + err = o.K8sClient.CoreV1().Namespaces().Delete(context.TODO(), o.Namespace, metav1.DeleteOptions{}) if err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("kosmosctl unjoin run error, delete namespace failed: %s", err) } @@ -212,20 +254,3 @@ func (o *CommandUnJoinOptions) runCluster() error { klog.Info("Cluster [" + o.Name + "] is removed.") return nil } - -func (o *CommandUnJoinOptions) runKnode() error { - klog.Info("Start removing knode from kosmos control plane...") - for { - err := o.DynamicClient.Resource(util.KnodeGVR).Namespace("").Delete(context.TODO(), o.Name, metav1.DeleteOptions{}) - if err != nil { - if apierrors.IsNotFound(err) { - break - } - return fmt.Errorf("kosmosctl unjoin run error, delete knode failed: %s", err) - } - time.Sleep(3 * time.Second) - } - - klog.Info("Knode [" + o.Name + "] is removed.") - return nil -} diff --git a/pkg/kosmosctl/util/builder.go b/pkg/kosmosctl/util/builder.go index b29aa4d73..db8c20fcf 100644 --- a/pkg/kosmosctl/util/builder.go +++ b/pkg/kosmosctl/util/builder.go @@ -18,7 +18,6 @@ var ( ClusterGVR = schema.GroupVersionResource{Group: "kosmos.io", Version: "v1alpha1", Resource: "clusters"} ClusterNodeGVR = schema.GroupVersionResource{Group: "kosmos.io", Version: "v1alpha1", Resource: "clusternodes"} NodeConfigGVR = schema.GroupVersionResource{Group: "kosmos.io", Version: "v1alpha1", Resource: "nodeconfigs"} - KnodeGVR = schema.GroupVersionResource{Group: "kosmos.io", Version: "v1alpha1", Resource: "knodes"} ) func GenerateDeployment(deployTemplate string, obj interface{}) (*appsv1.Deployment, error) { diff --git a/pkg/utils/constants.go b/pkg/utils/constants.go index 37ff95e98..6b88f8539 100644 --- a/pkg/utils/constants.go +++ b/pkg/utils/constants.go @@ -66,6 +66,8 @@ const ( KosmosNodePrefix = "kosmos-" KosmosNodeLabel = "kosmos.io/node" KosmosNodeValue = "true" + KosmosNodeJoinLabel = "kosmos.io/join" + KosmosNodeJoinValue = "true" KosmosNodeTaintKey = "kosmos.io/node" KosmosNodeTaintValue = "true" KosmosNodeTaintEffect = "NoSchedule"