From 4c9c7f12cc5abe29a25f024d8c8e2d9aebddb6a9 Mon Sep 17 00:00:00 2001 From: galal-hussein Date: Fri, 6 Dec 2024 00:11:54 +0200 Subject: [PATCH] Fix deletion logic and controller reference Signed-off-by: galal-hussein --- k3k-kubelet/controller/service.go | 39 ++++++++++++++++++++++++++++++- k3k-kubelet/provider/provider.go | 2 +- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/k3k-kubelet/controller/service.go b/k3k-kubelet/controller/service.go index fce5ea1..944f8d5 100644 --- a/k3k-kubelet/controller/service.go +++ b/k3k-kubelet/controller/service.go @@ -4,6 +4,7 @@ import ( "context" "github.com/rancher/k3k/k3k-kubelet/translate" + "github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1" "github.com/rancher/k3k/pkg/log" v1 "k8s.io/api/core/v1" @@ -13,6 +14,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" ) @@ -20,6 +22,7 @@ import ( const ( serviceSyncerController = "service-syncer-controller" maxConcurrentReconciles = 1 + serviceFinalizerName = "service.k3k.io/finalizer" ) // TODO: change into a generic syncer @@ -29,6 +32,7 @@ type ServiceReconciler struct { clusterName string clusterNamespace string Scheme *runtime.Scheme + HostScheme *runtime.Scheme logger *log.Logger Translater translate.ToHostTranslater } @@ -44,6 +48,7 @@ func AddServiceSyncer(ctx context.Context, virtMgr, hostMgr manager.Manager, clu virtualClient: virtMgr.GetClient(), hostClient: hostMgr.GetClient(), Scheme: virtMgr.GetScheme(), + HostScheme: hostMgr.GetScheme(), logger: logger.Named(serviceSyncerController), Translater: translater, clusterName: clusterName, @@ -66,11 +71,41 @@ func (s *ServiceReconciler) Reconcile(ctx context.Context, req reconcile.Request var ( virtService v1.Service hostService v1.Service + cluster v1alpha1.Cluster ) - if err := s.virtualClient.Get(ctx, req.NamespacedName, &virtService); err != nil { + // getting the cluster for setting the controller reference + if err := s.hostClient.Get(ctx, types.NamespacedName{Name: s.clusterName, Namespace: s.clusterNamespace}, &cluster); err != nil { return reconcile.Result{}, err } + if err := s.virtualClient.Get(ctx, req.NamespacedName, &virtService); err != nil { + return reconcile.Result{}, ctrlruntimeclient.IgnoreNotFound(err) + } syncedService := s.service(&virtService) + if err := controllerutil.SetControllerReference(&cluster, syncedService, s.HostScheme); err != nil { + return reconcile.Result{}, err + } + if !virtService.DeletionTimestamp.IsZero() { + // deleting the synced service if exists + if err := s.hostClient.Delete(ctx, syncedService); err != nil { + return reconcile.Result{}, ctrlruntimeclient.IgnoreNotFound(err) + } + // remove the finalizer after cleaning up the synced service + if controllerutil.ContainsFinalizer(&virtService, serviceFinalizerName) { + controllerutil.RemoveFinalizer(&virtService, serviceFinalizerName) + if err := s.virtualClient.Update(ctx, &virtService); err != nil { + return reconcile.Result{}, err + } + } + return reconcile.Result{}, nil + } + // Add finalizer if it does not exist + if !controllerutil.ContainsFinalizer(&virtService, serviceFinalizerName) { + controllerutil.AddFinalizer(&virtService, serviceFinalizerName) + if err := s.virtualClient.Update(ctx, &virtService); err != nil { + return reconcile.Result{}, err + } + } + if err := s.hostClient.Get(ctx, types.NamespacedName{Name: syncedService.Name, Namespace: s.clusterNamespace}, &hostService); err != nil { if apierrors.IsNotFound(err) { log.Info("creating the service for the first time on the host cluster") @@ -85,5 +120,7 @@ func (s *ServiceReconciler) Reconcile(ctx context.Context, req reconcile.Request func (s *ServiceReconciler) service(obj *v1.Service) *v1.Service { hostService := obj.DeepCopy() s.Translater.TranslateTo(hostService) + // don't sync finalizers to the host + hostService.Finalizers = nil return hostService } diff --git a/k3k-kubelet/provider/provider.go b/k3k-kubelet/provider/provider.go index 4bfb298..73ec7e2 100644 --- a/k3k-kubelet/provider/provider.go +++ b/k3k-kubelet/provider/provider.go @@ -537,7 +537,7 @@ func (p *Provider) configureNetworking(podName, podNamespace string, pod *corev1 p.dnsIP, }, Searches: []string{ - podNamespace + ".svc.cluster.local", "svc.cluster.local", "cluster.local", "hgalal.az", + podNamespace + ".svc.cluster.local", "svc.cluster.local", "cluster.local", }, } }