diff --git a/cmd/clustertree/cluster-manager/app/manager.go b/cmd/clustertree/cluster-manager/app/manager.go index 7c6c1ae37..dbcfd7604 100644 --- a/cmd/clustertree/cluster-manager/app/manager.go +++ b/cmd/clustertree/cluster-manager/app/manager.go @@ -117,23 +117,27 @@ func run(ctx context.Context, opts *options.Options) error { return fmt.Errorf("error starting %s: %v", clusterManager.ControllerName, err) } - // add serviceExport controller - ServiceExportController := mcs.ServiceExportController{ - RootClient: mgr.GetClient(), - EventRecorder: mgr.GetEventRecorderFor(mcs.ServiceExportControllerName), - Logger: mgr.GetLogger(), - } - if err = ServiceExportController.SetupWithManager(mgr); err != nil { - return fmt.Errorf("error starting %s: %v", mcs.ServiceExportControllerName, err) + if opts.MultiClusterService { + // add serviceExport controller + ServiceExportController := mcs.ServiceExportController{ + RootClient: mgr.GetClient(), + EventRecorder: mgr.GetEventRecorderFor(mcs.ServiceExportControllerName), + Logger: mgr.GetLogger(), + } + if err = ServiceExportController.SetupWithManager(mgr); err != nil { + return fmt.Errorf("error starting %s: %v", mcs.ServiceExportControllerName, err) + } } - GlobalDaemonSetService := &GlobalDaemonSetService{ - opts: opts, - ctx: ctx, - defaultWorkNum: 1, - } - if err = GlobalDaemonSetService.SetupWithManager(mgr); err != nil { - return fmt.Errorf("error starting global daemonset : %v", err) + if opts.DaemonSetController { + daemonSetController := &GlobalDaemonSetService{ + opts: opts, + ctx: ctx, + defaultWorkNum: 1, + } + if err = daemonSetController.SetupWithManager(mgr); err != nil { + return fmt.Errorf("error starting global daemonset : %v", err) + } } // init rootPodController @@ -141,6 +145,7 @@ func run(ctx context.Context, opts *options.Options) error { GlobalLeafManager: globalleafManager, RootClient: mgr.GetClient(), DynamicRootClient: dynamicClient, + Options: opts, } if err := RootPodReconciler.SetupWithManager(mgr); err != nil { return fmt.Errorf("error starting RootPodReconciler %s: %v", podcontrollers.RootPodControllerName, err) diff --git a/cmd/clustertree/cluster-manager/app/options/options.go b/cmd/clustertree/cluster-manager/app/options/options.go index c1807ab6f..74c245d72 100644 --- a/cmd/clustertree/cluster-manager/app/options/options.go +++ b/cmd/clustertree/cluster-manager/app/options/options.go @@ -14,9 +14,11 @@ const ( ) type Options struct { - LeaderElection componentbaseconfig.LeaderElectionConfiguration - KubernetesOptions KubernetesOptions - ListenPort int32 + LeaderElection componentbaseconfig.LeaderElectionConfiguration + KubernetesOptions KubernetesOptions + ListenPort int32 + DaemonSetController bool + MultiClusterService bool } type KubernetesOptions struct { @@ -43,4 +45,6 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) { flags.StringVar(&o.KubernetesOptions.KubeConfig, "kubeconfig", "", "Path for kubernetes kubeconfig file, if left blank, will use in cluster way.") flags.StringVar(&o.KubernetesOptions.Master, "master", "", "Used to generate kubeconfig for downloading, if not specified, will use host in kubeconfig.") flags.Int32Var(&o.ListenPort, "listen-port", 10250, "Listen port for requests from the kube-apiserver.") + flags.BoolVar(&o.DaemonSetController, "daemonset-controller", false, "Turn on or off daemonset controller.") + flags.BoolVar(&o.MultiClusterService, "multi-cluster-service", false, "Turn on or off mcs support.") } diff --git a/pkg/clustertree/cluster-manager/cluster_controller.go b/pkg/clustertree/cluster-manager/cluster_controller.go index d0a5864d7..4edc74966 100644 --- a/pkg/clustertree/cluster-manager/cluster_controller.go +++ b/pkg/clustertree/cluster-manager/cluster_controller.go @@ -66,7 +66,6 @@ type ClusterController struct { ControllerManagersLock sync.Mutex RootResourceManager *utils.ResourceManager - mgr manager.Manager GlobalLeafManager leafUtils.LeafResourceManager } @@ -116,9 +115,6 @@ func (c *ClusterController) SetupWithManager(mgr manager.Manager) error { c.ManagerCancelFuncs = make(map[string]*context.CancelFunc) c.ControllerManagers = make(map[string]manager.Manager) c.Logger = mgr.GetLogger() - - // TODO this may not be a good idea - c.mgr = mgr return controllerruntime.NewControllerManagedBy(mgr). Named(ControllerName). WithOptions(controller.Options{}). @@ -246,12 +242,13 @@ func (c *ClusterController) clearClusterControllers(cluster *clusterlinkv1alpha1 } func (c *ClusterController) setupControllers(mgr manager.Manager, cluster *clusterlinkv1alpha1.Cluster, node *corev1.Node, clientDynamic *dynamic.DynamicClient, leafClient kubernetes.Interface, kosmosClient kosmosversioned.Interface) error { - c.GlobalLeafManager.AddLeafResource(cluster.Name, &leafUtils.LeafResource{ + nodeName := fmt.Sprintf("%s%s", utils.KosmosNodePrefix, cluster.Name) + c.GlobalLeafManager.AddLeafResource(nodeName, &leafUtils.LeafResource{ Client: mgr.GetClient(), DynamicClient: clientDynamic, Clientset: leafClient, - NodeName: cluster.Name, - Namespace: cluster.Spec.Namespace, + NodeName: nodeName, + Namespace: "", IgnoreLabels: strings.Split("", ","), EnableServiceAccount: true, }) @@ -271,23 +268,24 @@ func (c *ClusterController) setupControllers(mgr manager.Manager, cluster *clust return fmt.Errorf("error starting %s: %v", controllers.NodeLeaseControllerName, err) } - serviceImportController := &mcs.ServiceImportController{ - LeafClient: mgr.GetClient(), - RootClient: c.Root, - RootKosmosClient: kosmosClient, - EventRecorder: mgr.GetEventRecorderFor(mcs.LeafServiceImportControllerName), - Logger: mgr.GetLogger(), - LeafNodeName: cluster.Name, - RootResourceManager: c.RootResourceManager, - } - - if err := serviceImportController.AddController(mgr); err != nil { - return fmt.Errorf("error starting %s: %v", mcs.LeafServiceImportControllerName, err) + if c.Options.MultiClusterService { + serviceImportController := &mcs.ServiceImportController{ + LeafClient: mgr.GetClient(), + RootClient: c.Root, + RootKosmosClient: kosmosClient, + EventRecorder: mgr.GetEventRecorderFor(mcs.LeafServiceImportControllerName), + Logger: mgr.GetLogger(), + LeafNodeName: nodeName, + RootResourceManager: c.RootResourceManager, + } + if err := serviceImportController.AddController(mgr); err != nil { + return fmt.Errorf("error starting %s: %v", mcs.LeafServiceImportControllerName, err) + } } leafPodController := podcontrollers.LeafPodReconciler{ RootClient: c.Root, - Namespace: cluster.Spec.Namespace, + Namespace: "", } if err := leafPodController.SetupWithManager(mgr); err != nil { @@ -313,34 +311,34 @@ func (c *ClusterController) setupStorageControllers(mgr manager.Manager, node *c return fmt.Errorf("error starting leaf pvc controller %v", err) } - leafPVontroller := pv.LeafPVController{ + leafPVController := pv.LeafPVController{ LeafClient: mgr.GetClient(), RootClient: c.Root, RootClientSet: c.RootClient, NodeName: node.Name, } - if err := leafPVontroller.SetupWithManager(mgr); err != nil { + if err := leafPVController.SetupWithManager(mgr); err != nil { return fmt.Errorf("error starting leaf pv controller %v", err) } - return nil } func (c *ClusterController) createNode(ctx context.Context, cluster *clusterlinkv1alpha1.Cluster, leafClient kubernetes.Interface) (*corev1.Node, error) { + nodeName := fmt.Sprintf("%s%s", utils.KosmosNodePrefix, cluster.Name) serverVersion, err := leafClient.Discovery().ServerVersion() if err != nil { - klog.Errorf("create node failed, can not connect to leaf %s", cluster.Name) + klog.Errorf("create node failed, can not connect to leaf %s", nodeName) return nil, err } - node, err := c.RootClient.CoreV1().Nodes().Get(ctx, cluster.Name, metav1.GetOptions{}) + node, err := c.RootClient.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{}) if err != nil && !errors.IsNotFound(err) { - klog.Errorf("create node failed, can not get node %s", cluster.Name) + klog.Errorf("create node failed, can not get node %s", nodeName) return nil, err } if err != nil && errors.IsNotFound(err) { - node = utils.BuildNodeTemplate(cluster) + node = utils.BuildNodeTemplate(nodeName) node.Status.NodeInfo.KubeletVersion = serverVersion.GitVersion node.Status.DaemonEndpoints = corev1.NodeDaemonEndpoints{ KubeletEndpoint: corev1.DaemonEndpoint{ @@ -349,7 +347,7 @@ func (c *ClusterController) createNode(ctx context.Context, cluster *clusterlink } node, err = c.RootClient.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}) if err != nil && !errors.IsAlreadyExists(err) { - klog.Errorf("create node %s failed, err: %v", cluster.Name, err) + klog.Errorf("create node %s failed, err: %v", nodeName, err) return nil, err } } diff --git a/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go b/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go index 27be6f194..2554e02dc 100644 --- a/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go +++ b/pkg/clustertree/cluster-manager/controllers/pod/leaf_pod_controller.go @@ -8,7 +8,6 @@ import ( "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" @@ -74,11 +73,22 @@ func (dopt *rootDeleteOption) ApplyToDelete(opt *client.DeleteOptions) { } func NewRootDeleteOption(pod *corev1.Pod) client.DeleteOption { - gracePeriodSeconds := pod.DeletionGracePeriodSeconds + // TODO + //gracePeriodSeconds := pod.DeletionGracePeriodSeconds + // + //current := metav1.NewTime(time.Now()) + //if pod.DeletionTimestamp.Before(¤t) { + // gracePeriodSeconds = new(int64) + //} + return &rootDeleteOption{ + GracePeriodSeconds: new(int64), + } +} - current := metav1.NewTime(time.Now()) - if pod.DeletionTimestamp.Before(¤t) { - gracePeriodSeconds = new(int64) +func NewLeafDeleteOption(pod *corev1.Pod) client.DeleteOption { + gracePeriodSeconds := new(int64) + if pod.DeletionGracePeriodSeconds != nil { + gracePeriodSeconds = pod.DeletionGracePeriodSeconds } return &rootDeleteOption{ @@ -124,7 +134,9 @@ func (r *LeafPodReconciler) SetupWithManager(mgr manager.Manager) error { if len(r.Namespace) > 0 && r.Namespace != obj.GetNamespace() { return false } - return true + + p := obj.(*corev1.Pod) + return podutils.IsKosmosPod(p) } return ctrl.NewControllerManagedBy(mgr). @@ -133,14 +145,14 @@ func (r *LeafPodReconciler) SetupWithManager(mgr manager.Manager) error { For(&corev1.Pod{}, builder.WithPredicates(predicate.Funcs{ CreateFunc: func(createEvent event.CreateEvent) bool { // ignore create event - return false + return skipFunc(createEvent.Object) }, UpdateFunc: func(updateEvent event.UpdateEvent) bool { + pod1 := updateEvent.ObjectOld.(*corev1.Pod) + pod2 := updateEvent.ObjectNew.(*corev1.Pod) if !skipFunc(updateEvent.ObjectNew) { return false } - pod1 := updateEvent.ObjectOld.(*corev1.Pod) - pod2 := updateEvent.ObjectNew.(*corev1.Pod) return !cmp.Equal(pod1.Status, pod2.Status) }, DeleteFunc: func(deleteEvent event.DeleteEvent) bool { diff --git a/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go b/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go index 51075b035..505329e34 100644 --- a/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go +++ b/pkg/clustertree/cluster-manager/controllers/pod/root_pod_controller.go @@ -26,6 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "github.com/kosmos.io/kosmos/cmd/clustertree/cluster-manager/app/options" "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/extensions/daemonset" leafUtils "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/utils" "github.com/kosmos.io/kosmos/pkg/utils" @@ -45,6 +46,8 @@ type RootPodReconciler struct { envResourceManager utils.EnvResourceManager GlobalLeafManager leafUtils.LeafResourceManager + + Options *options.Options } type envResourceManager struct { @@ -141,7 +144,7 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req // TODO: GlobalLeafResourceManager may not inited.... // belongs to the current node if !r.GlobalLeafManager.IsInCluded(rootpod.Spec.NodeName) { - return reconcile.Result{}, nil + return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil } lr, err := r.GlobalLeafManager.GetLeafResource(rootpod.Spec.NodeName) @@ -208,7 +211,8 @@ func (r *RootPodReconciler) SetupWithManager(mgr manager.Manager) error { return false } - return true + p := obj.(*corev1.Pod) + return strings.HasPrefix(p.Spec.NodeName, utils.KosmosNodePrefix) } return ctrl.NewControllerManagedBy(mgr). @@ -428,7 +432,6 @@ const CoreDNSServiceName = "kube-dns" // changeToMasterCoreDNS point the dns of the pod to the master cluster, so that the pod can access any service. // The master cluster holds all the services in the multi-cluster. -// TODO func (r *RootPodReconciler) changeToMasterCoreDNS(ctx context.Context, pod *corev1.Pod) { if pod.Spec.DNSPolicy != corev1.DNSClusterFirst && pod.Spec.DNSPolicy != corev1.DNSClusterFirstWithHostNet { return @@ -596,49 +599,49 @@ func (r *RootPodReconciler) CreatePodInLeafCluster(ctx context.Context, lr *leaf // create ns ns := &corev1.Namespace{} nsKey := types.NamespacedName{ - Name: pod.Namespace, + Name: basicPod.Namespace, } if err := lr.Client.Get(ctx, nsKey, ns); err != nil { if !errors.IsNotFound(err) { // cannot get ns in root cluster, retry return err } - klog.Infof("Namespace %s does not exist for pod %s, creating it", pod.Namespace, pod.Name) + klog.Infof("Namespace %s does not exist for pod %s, creating it", basicPod.Namespace, basicPod.Name) ns := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ - Name: pod.Namespace, + Name: basicPod.Namespace, }, } if createErr := lr.Client.Create(ctx, ns); createErr != nil { if !errors.IsAlreadyExists(createErr) { - klog.Infof("Namespace %s create failed error: %v", pod.Namespace, createErr) + klog.Infof("Namespace %s create failed error: %v", basicPod.Namespace, createErr) return err } else { // namespace already existed, skip create - klog.Info("Namespace %s already existed: %v", pod.Namespace, createErr) + klog.Info("Namespace %s already existed: %v", basicPod.Namespace, createErr) } } } // create secret configmap pvc - secretNames := podutils.GetSecrets(pod) - configMaps := podutils.GetConfigmaps(pod) - pvcs := podutils.GetPVCs(pod) + secretNames := podutils.GetSecrets(basicPod) + configMaps := podutils.GetConfigmaps(basicPod) + pvcs := podutils.GetPVCs(basicPod) // nolint:errcheck go wait.PollImmediate(500*time.Millisecond, 10*time.Minute, func() (bool, error) { klog.Info("Trying to creating base dependent") - if err := r.createStorageInLeafCluster(ctx, lr, utils.GVR_CONFIGMAP, configMaps, pod.Namespace); err != nil { + if err := r.createStorageInLeafCluster(ctx, lr, utils.GVR_CONFIGMAP, configMaps, basicPod.Namespace); err != nil { klog.Error(err) return false, nil } - klog.Infof("Create configmaps %v of %v/%v success", configMaps, pod.Namespace, pod.Name) - if err := r.createStorageInLeafCluster(ctx, lr, utils.GVR_PVC, pvcs, pod.Namespace); err != nil { + klog.Infof("Create configmaps %v of %v/%v success", configMaps, basicPod.Namespace, basicPod.Name) + if err := r.createStorageInLeafCluster(ctx, lr, utils.GVR_PVC, pvcs, basicPod.Namespace); err != nil { klog.Error(err) return false, nil } - klog.Infof("Create pvc %v of %v/%v success", pvcs, pod.Namespace, pod.Name) + klog.Infof("Create pvc %v of %v/%v success", pvcs, basicPod.Namespace, basicPod.Name) return true, nil }) var err error @@ -646,7 +649,7 @@ func (r *RootPodReconciler) CreatePodInLeafCluster(ctx context.Context, lr *leaf wait.PollImmediate(100*time.Millisecond, 1*time.Second, func() (bool, error) { klog.Info("Trying to creating secret and service account") - if err = r.createStorageInLeafCluster(ctx, lr, utils.GVR_SECRET, secretNames, pod.Namespace); err != nil { + if err = r.createStorageInLeafCluster(ctx, lr, utils.GVR_SECRET, secretNames, basicPod.Namespace); err != nil { klog.Error(err) return false, nil } @@ -656,16 +659,19 @@ func (r *RootPodReconciler) CreatePodInLeafCluster(ctx context.Context, lr *leaf return fmt.Errorf("create secrets failed: %v", err) } - r.convertAuth(ctx, lr, pod) - r.changeToMasterCoreDNS(ctx, pod) + r.convertAuth(ctx, lr, basicPod) + + if !r.Options.MultiClusterService { + r.changeToMasterCoreDNS(ctx, basicPod) + } - klog.Infof("Creating pod %+v", pod) + klog.Infof("Creating pod %+v", basicPod) err = lr.Client.Create(ctx, basicPod) if err != nil { return fmt.Errorf("could not create pod: %v", err) } - klog.Infof("Create pod %v/%+v success", pod.Namespace, pod.Name) + klog.Infof("Create pod %v/%+v success", basicPod.Namespace, basicPod.Name) return nil } @@ -691,7 +697,10 @@ func (r *RootPodReconciler) UpdatePodInLeafCluster(ctx context.Context, lr *leaf } r.convertAuth(ctx, lr, podCopy) - r.changeToMasterCoreDNS(ctx, podCopy) + + if !r.Options.MultiClusterService { + r.changeToMasterCoreDNS(ctx, podCopy) + } klog.Infof("Updating pod %+v", podCopy) @@ -721,14 +730,8 @@ func (r *RootPodReconciler) DeletePodInLeafCluster(ctx context.Context, lr *leaf return nil } - opts := &metav1.DeleteOptions{ - GracePeriodSeconds: new(int64), // 0 - } - if leafPod.DeletionGracePeriodSeconds != nil { - opts.GracePeriodSeconds = leafPod.DeletionGracePeriodSeconds - } - - err = lr.Client.Delete(ctx, leafPod) + deleteOption := NewLeafDeleteOption(leafPod) + err = lr.Client.Delete(ctx, leafPod, deleteOption) if err != nil { if errors.IsNotFound(err) { klog.Infof("Tried to delete pod %s/%s, but it did not exist in the cluster", leafPod.Namespace, leafPod.Name) diff --git a/pkg/clustertree/cluster-manager/controllers/pv/leaf_pv_controller.go b/pkg/clustertree/cluster-manager/controllers/pv/leaf_pv_controller.go index 0b7c22dfb..260db22b7 100644 --- a/pkg/clustertree/cluster-manager/controllers/pv/leaf_pv_controller.go +++ b/pkg/clustertree/cluster-manager/controllers/pv/leaf_pv_controller.go @@ -66,6 +66,30 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ klog.Errorf("get root pv failed, error: %v", err) return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil } + + if pvCopy.Spec.ClaimRef != nil { + tmpPVC := &v1.PersistentVolumeClaim{} + nn := types.NamespacedName{ + Name: pvCopy.Spec.ClaimRef.Name, + Namespace: pvCopy.Spec.ClaimRef.Namespace, + } + err := l.LeafClient.Get(ctx, nn, tmpPVC) + if err != nil { + if !errors.IsNotFound(err) { + klog.Errorf("get tmp pvc failed, error: %v", err) + return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil + } + klog.Warningf("tmp pvc not exist, error: %v", err) + return reconcile.Result{}, nil + } + if !utils.IsObjectGlobal(&tmpPVC.ObjectMeta) { + return reconcile.Result{}, nil + } + } else { + klog.Warningf("Can't find pvc for pv, error: %v", err) + return reconcile.Result{}, nil + } + rootPV = pv.DeepCopy() filterPV(rootPV, l.NodeName) nn := types.NamespacedName{ @@ -142,7 +166,7 @@ func (l *LeafPVController) SetupWithManager(mgr manager.Manager) error { return ctrl.NewControllerManagedBy(mgr). Named(LeafPVControllerName). WithOptions(controller.Options{}). - For(&v1.PersistentVolumeClaim{}, builder.WithPredicates(predicate.Funcs{ + For(&v1.PersistentVolume{}, builder.WithPredicates(predicate.Funcs{ CreateFunc: func(createEvent event.CreateEvent) bool { return true }, diff --git a/pkg/clustertree/cluster-manager/controllers/pv/root_pv_controller.go b/pkg/clustertree/cluster-manager/controllers/pv/root_pv_controller.go index 8995938dc..f729868e3 100644 --- a/pkg/clustertree/cluster-manager/controllers/pv/root_pv_controller.go +++ b/pkg/clustertree/cluster-manager/controllers/pv/root_pv_controller.go @@ -39,7 +39,7 @@ func (r *RootPVController) SetupWithManager(mgr manager.Manager) error { return ctrl.NewControllerManagedBy(mgr). Named(RootPVControllerName). WithOptions(controller.Options{}). - For(&v1.PersistentVolumeClaim{}, builder.WithPredicates(predicate.Funcs{ + For(&v1.PersistentVolume{}, builder.WithPredicates(predicate.Funcs{ CreateFunc: func(createEvent event.CreateEvent) bool { return false }, diff --git a/pkg/utils/constants.go b/pkg/utils/constants.go index 1a3b2aa61..b0360566b 100644 --- a/pkg/utils/constants.go +++ b/pkg/utils/constants.go @@ -48,6 +48,7 @@ const ( // cluster node const ( + KosmosNodePrefix = "kosmos-" KosmosNodeLabel = "kosmos.io/node" KosmosNodeValue = "true" KosmosNodeTaintKey = "kosmos.io/node" diff --git a/pkg/utils/k8s.go b/pkg/utils/k8s.go index c29b1c68a..bc508d17d 100644 --- a/pkg/utils/k8s.go +++ b/pkg/utils/k8s.go @@ -391,6 +391,9 @@ func IsObjectUnstructuredGlobal(obj map[string]string) bool { } func AddResourceOwnersAnnotations(anno map[string]string, owner string) map[string]string { + if anno == nil { + anno = map[string]string{} + } owners := strings.Split(anno[KosmosResourceOwnersAnnotations], ",") newowners := make([]string, 0) @@ -415,6 +418,9 @@ func AddResourceOwnersAnnotations(anno map[string]string, owner string) map[stri } func RemoveResourceOwnersAnnotations(anno map[string]string, owner string) map[string]string { + if anno == nil { + anno = map[string]string{} + } owners := strings.Split(anno[KosmosResourceOwnersAnnotations], ",") newowners := make([]string, len(owners)-1) @@ -432,6 +438,9 @@ func RemoveResourceOwnersAnnotations(anno map[string]string, owner string) map[s } func HasResourceOwnersAnnotations(anno map[string]string, owner string) bool { + if anno == nil { + anno = map[string]string{} + } owners := strings.Split(anno[KosmosResourceOwnersAnnotations], ",") for _, v := range owners { @@ -444,6 +453,9 @@ func HasResourceOwnersAnnotations(anno map[string]string, owner string) bool { } func ListResourceOwnersAnnotations(anno map[string]string) []string { + if anno == nil { + anno = map[string]string{} + } owners := strings.Split(anno[KosmosResourceOwnersAnnotations], ",") return owners } diff --git a/pkg/utils/node.go b/pkg/utils/node.go index 714bf0f6c..f7cc169c4 100644 --- a/pkg/utils/node.go +++ b/pkg/utils/node.go @@ -3,18 +3,16 @@ package utils import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - clusterlinkv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1" ) -func BuildNodeTemplate(cluster *clusterlinkv1alpha1.Cluster) *corev1.Node { +func BuildNodeTemplate(name string) *corev1.Node { node := &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ - Name: cluster.Name, + Name: name, Labels: map[string]string{ KosmosNodeLabel: KosmosNodeValue, NodeRoleLabel: NodeRoleValue, - NodeHostnameValue: cluster.Name, + NodeHostnameValue: name, NodeArchLabelStable: DefaultK8sArch, NodeOSLabelStable: DefaultK8sOS, NodeOSLabelBeta: DefaultK8sOS,