Skip to content

Commit

Permalink
Optimization: Reuse HTTPClient / DynamicClient in reconcile
Browse files Browse the repository at this point in the history
Should be slightly more efficient via better HTTP connection reuse.
  • Loading branch information
justinsb committed Jan 16, 2024
1 parent 8e6cf52 commit 49d6914
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
9 changes: 9 additions & 0 deletions commonclient/factory_cr11.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ package commonclient

import (
"context"
"net/http"

"k8s.io/client-go/rest"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
Expand All @@ -23,6 +26,12 @@ func WrapEventHandler(h EventHandler) handler.EventHandler {
return &eventHandlerWithoutContext{h: h}
}

// GetHTTPClient returns the http.Client associated with the Cluster
func GetHTTPClient(c cluster.Cluster) (*http.Client, error) {
// This is a "polyfill", later versions of controller-runtime do it better.
return rest.HTTPClientFor(c.GetConfig())
}

type eventHandlerWithoutContext struct {
h EventHandler
}
Expand Down
8 changes: 8 additions & 0 deletions commonclient/factory_cr15.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
package commonclient

import (
"net/http"

"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
)
Expand All @@ -19,4 +22,9 @@ func WrapEventHandler(h handler.EventHandler) handler.EventHandler {
return h
}

// GetHTTPClient returns the http.Client associated with the Cluster
func GetHTTPClient(c cluster.Cluster) (*http.Client, error) {
return c.GetHTTPClient(), nil
}

type EventHandler = handler.EventHandler
29 changes: 20 additions & 9 deletions pkg/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"path/filepath"
"reflect"
"strings"
Expand All @@ -41,6 +42,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/kustomize/kyaml/filesys"

"sigs.k8s.io/kubebuilder-declarative-pattern/commonclient"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/utils"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/kustomize"
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/applier"
Expand All @@ -51,18 +53,20 @@ var _ reconcile.Reconciler = &Reconciler{}

type Reconciler struct {
prototype DeclarativeObject
client client.Client
config *rest.Config

client client.Client
restConfig *rest.Config
httpClient *http.Client
dynamicClient dynamic.Interface
restMapper meta.RESTMapper

metrics reconcileMetrics
mgr manager.Manager

// recorder is the EventRecorder for creating k8s events
recorder recorder.EventRecorder
dynamicClient dynamic.Interface
recorder recorder.EventRecorder

restMapper meta.RESTMapper
options reconcilerParams
options reconcilerParams
}

type DeclarativeObject interface {
Expand Down Expand Up @@ -105,11 +109,17 @@ func (r *Reconciler) Init(mgr manager.Manager, prototype DeclarativeObject, opts
r.recorder = mgr.GetEventRecorderFor(controllerName)

r.client = mgr.GetClient()
r.config = mgr.GetConfig()
r.restConfig = mgr.GetConfig()
httpClient, err := commonclient.GetHTTPClient(mgr)
if err != nil {
return fmt.Errorf("getting HTTP client: %w", err)
}
r.httpClient = httpClient

r.mgr = mgr
globalObjectTracker.mgr = mgr

d, err := dynamic.NewForConfig(r.config)
d, err := dynamic.NewForConfigAndClient(r.restConfig, r.httpClient)
if err != nil {
return err
}
Expand Down Expand Up @@ -379,7 +389,7 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
return statusInfo, fmt.Errorf("building applyset parent: %w", err)
}
applierOpt := applier.ApplierOptions{
RESTConfig: r.config,
RESTConfig: r.restConfig,
RESTMapper: r.restMapper,
Namespace: ns,
ParentRef: parentRef,
Expand All @@ -389,6 +399,7 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
Force: true,
CascadingStrategy: r.options.cascadingStrategy,
Client: r.client,
DynamicClient: r.dynamicClient,
}

applyOperation := &ApplyOperation{
Expand Down

0 comments on commit 49d6914

Please sign in to comment.