From b68ef41ad66d3c524ad060a65ac33dbe9fc0f0f1 Mon Sep 17 00:00:00 2001 From: wangyizhi1 Date: Tue, 26 Dec 2023 17:10:34 +0800 Subject: [PATCH] fix: serviceimport may not be deleted cleanly Signed-off-by: wangyizhi1 --- .../cluster-manager/cluster_controller.go | 11 +++++------ .../controllers/mcs/serviceimport_controller.go | 16 ++++++++++------ pkg/utils/constants.go | 4 +--- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/clustertree/cluster-manager/cluster_controller.go b/pkg/clustertree/cluster-manager/cluster_controller.go index 9675d8820..52842c391 100644 --- a/pkg/clustertree/cluster-manager/cluster_controller.go +++ b/pkg/clustertree/cluster-manager/cluster_controller.go @@ -276,12 +276,11 @@ func (c *ClusterController) setupControllers( if c.Options.MultiClusterService { serviceImportController := &mcs.ServiceImportController{ - LeafClient: mgr.GetClient(), - LeafKosmosClient: leafKosmosClient, - EventRecorder: mgr.GetEventRecorderFor(mcs.LeafServiceImportControllerName), - Logger: mgr.GetLogger(), - LeafNodeName: cluster.Name, - // todo @wyz + LeafClient: mgr.GetClient(), + LeafKosmosClient: leafKosmosClient, + EventRecorder: mgr.GetEventRecorderFor(mcs.LeafServiceImportControllerName), + Logger: mgr.GetLogger(), + LeafNodeName: cluster.Name, IPFamilyType: cluster.Spec.ClusterLinkOptions.IPFamily, RootResourceManager: c.RootResourceManager, ReservedNamespaces: c.Options.ReservedNamespaces, diff --git a/pkg/clustertree/cluster-manager/controllers/mcs/serviceimport_controller.go b/pkg/clustertree/cluster-manager/controllers/mcs/serviceimport_controller.go index 2c39ab96e..9e3cfa0de 100644 --- a/pkg/clustertree/cluster-manager/controllers/mcs/serviceimport_controller.go +++ b/pkg/clustertree/cluster-manager/controllers/mcs/serviceimport_controller.go @@ -27,6 +27,7 @@ import ( kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1" kosmosversioned "github.com/kosmos.io/kosmos/pkg/generated/clientset/versioned" "github.com/kosmos.io/kosmos/pkg/generated/informers/externalversions" + "github.com/kosmos.io/kosmos/pkg/generated/listers/apis/v1alpha1" "github.com/kosmos.io/kosmos/pkg/utils" "github.com/kosmos.io/kosmos/pkg/utils/flags" "github.com/kosmos.io/kosmos/pkg/utils/helper" @@ -46,9 +47,10 @@ type ServiceImportController struct { processor utils.AsyncWorker RootResourceManager *utils.ResourceManager // ReservedNamespaces are the protected namespaces to prevent Kosmos for deleting system resources - ReservedNamespaces []string - BackoffOptions flags.BackoffOptions - SyncPeriod time.Duration + ReservedNamespaces []string + BackoffOptions flags.BackoffOptions + SyncPeriod time.Duration + serviceImportLister v1alpha1.ServiceImportLister } func (c *ServiceImportController) AddController(mgr manager.Manager) error { @@ -74,6 +76,7 @@ func (c *ServiceImportController) Start(ctx context.Context) error { serviceImportInformerFactory := externalversions.NewSharedInformerFactory(c.LeafKosmosClient, c.SyncPeriod) serviceImportInformer := serviceImportInformerFactory.Multicluster().V1alpha1().ServiceImports() + serviceImportLister := serviceImportInformer.Lister() _, err := serviceImportInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: c.OnAdd, UpdateFunc: c.OnUpdate, @@ -82,6 +85,7 @@ func (c *ServiceImportController) Start(ctx context.Context) error { if err != nil { return err } + c.serviceImportLister = serviceImportLister _, err = c.RootResourceManager.EndpointSliceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: c.OnEpsAdd, @@ -112,8 +116,8 @@ func (c *ServiceImportController) Reconcile(key utils.QueueKey) error { klog.V(4).Infof("============ %s has been reconciled in cluster %s =============", clusterWideKey.NamespaceKey(), c.LeafNodeName) }() - serviceImport := &mcsv1alpha1.ServiceImport{} - if err := c.LeafClient.Get(context.TODO(), types.NamespacedName{Namespace: clusterWideKey.Namespace, Name: clusterWideKey.Name}, serviceImport); err != nil { + serviceImport, err := c.serviceImportLister.ServiceImports(clusterWideKey.Namespace).Get(clusterWideKey.Name) + if err != nil { if !apierrors.IsNotFound(err) { klog.Errorf("Get %s in cluster %s failed, Error: %v", clusterWideKey.NamespaceKey(), c.LeafNodeName, err) return err @@ -130,7 +134,7 @@ func (c *ServiceImportController) Reconcile(key utils.QueueKey) error { return c.removeFinalizer(serviceImport) } - err := c.syncServiceImport(serviceImport) + err = c.syncServiceImport(serviceImport) if err != nil { klog.Errorf("Sync serviceImport %s/%s's finalizer in cluster %s failed, Error: %v", serviceImport.Namespace, serviceImport.Name, c.LeafNodeName, err) return err diff --git a/pkg/utils/constants.go b/pkg/utils/constants.go index 89649fdb7..52c36210f 100644 --- a/pkg/utils/constants.go +++ b/pkg/utils/constants.go @@ -1,8 +1,6 @@ package utils import ( - "time" - corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime/schema" ) @@ -131,7 +129,7 @@ const ( DefaultK8sOS = "linux" DefaultK8sArch = "amd64" - DefaultInformerResyncPeriod = 1 * time.Minute + DefaultInformerResyncPeriod = 0 DefaultListenPort = 10250 DefaultPodSyncWorkers = 10 DefaultWorkers = 5