From cc422e3cd96ef9a446f112f19568342f4639d758 Mon Sep 17 00:00:00 2001 From: Kyle Wuolle Date: Thu, 29 Aug 2024 08:23:24 -0700 Subject: [PATCH] Move dynamic client to main --- cmd/main.go | 8 ++++++++ internal/controller/deployment_controller.go | 12 ++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 7bdab0a6a..aade54c6d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -27,6 +27,7 @@ import ( sourcev1 "github.com/fluxcd/source-controller/api/v1" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/client-go/dynamic" clientgoscheme "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" @@ -152,6 +153,12 @@ func main() { os.Exit(1) } + dc, err := dynamic.NewForConfig(mgr.GetConfig()) + if err != nil { + setupLog.Error(err, "failed to create dynamic client") + os.Exit(1) + } + if err = (&controller.TemplateReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), @@ -163,6 +170,7 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Config: mgr.GetConfig(), + Dc: dc, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Deployment") os.Exit(1) diff --git a/internal/controller/deployment_controller.go b/internal/controller/deployment_controller.go index 487a180f8..b9ddf741d 100644 --- a/internal/controller/deployment_controller.go +++ b/internal/controller/deployment_controller.go @@ -55,6 +55,7 @@ type DeploymentReconciler struct { client.Client Scheme *runtime.Scheme Config *rest.Config + Dc *dynamic.DynamicClient } // Reconcile is part of the main kubernetes reconciliation loop which aims to @@ -91,14 +92,14 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) return r.Update(ctx, l, deployment) } -func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, dc dynamic.Interface, l logr.Logger, deployment *hmc.Deployment) (bool, error) { +func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, l logr.Logger, deployment *hmc.Deployment) (bool, error) { resourceId := schema.GroupVersionResource{ Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "clusters", } - list, err := dc.Resource(resourceId).Namespace(deployment.Namespace).List(ctx, metav1.ListOptions{ + list, err := r.Dc.Resource(resourceId).Namespace(deployment.Namespace).List(ctx, metav1.ListOptions{ LabelSelector: labels.SelectorFromSet(map[string]string{hmc.FluxHelmChartNameKey: deployment.Name}).String(), }) @@ -149,11 +150,6 @@ func (r *DeploymentReconciler) setStatusFromClusterStatus(ctx context.Context, d } func (r *DeploymentReconciler) Update(ctx context.Context, l logr.Logger, deployment *hmc.Deployment) (result ctrl.Result, err error) { - dc, err := dynamic.NewForConfig(r.Config) - if err != nil { - return ctrl.Result{}, fmt.Errorf("failed to create client configuration: %v", err) - } - finalizersUpdated := controllerutil.AddFinalizer(deployment, hmc.DeploymentFinalizer) if finalizersUpdated { if err := r.Client.Update(ctx, deployment); err != nil { @@ -291,7 +287,7 @@ func (r *DeploymentReconciler) Update(ctx context.Context, l logr.Logger, deploy }) } - requeue, err := r.setStatusFromClusterStatus(ctx, dc, l, deployment) + requeue, err := r.setStatusFromClusterStatus(ctx, l, deployment) if err != nil { if requeue { return ctrl.Result{RequeueAfter: 10 * time.Second}, err