Skip to content

Commit

Permalink
Move dynamic client to main
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewuolle committed Aug 29, 2024
1 parent 0635d50 commit cc422e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand All @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions internal/controller/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
})

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cc422e3

Please sign in to comment.