Skip to content

Commit 8a932a2

Browse files
authored
Merge pull request #371 from justinsb/reuse_http_client
Optimization: Reuse HTTPClient / DynamicClient in reconcile
2 parents fd25a83 + 88b702d commit 8a932a2

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

commonclient/factory_cr11.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ package commonclient
44

55
import (
66
"context"
7+
"net/http"
78

9+
"k8s.io/client-go/rest"
810
"k8s.io/client-go/util/workqueue"
911
"sigs.k8s.io/controller-runtime/pkg/cache"
1012
"sigs.k8s.io/controller-runtime/pkg/client"
13+
"sigs.k8s.io/controller-runtime/pkg/cluster"
1114
"sigs.k8s.io/controller-runtime/pkg/event"
1215
"sigs.k8s.io/controller-runtime/pkg/handler"
1316
"sigs.k8s.io/controller-runtime/pkg/source"
@@ -23,6 +26,12 @@ func WrapEventHandler(h EventHandler) handler.EventHandler {
2326
return &eventHandlerWithoutContext{h: h}
2427
}
2528

29+
// GetHTTPClient returns the http.Client associated with the Cluster
30+
func GetHTTPClient(c cluster.Cluster) (*http.Client, error) {
31+
// This is a "polyfill", later versions of controller-runtime do it better.
32+
return rest.HTTPClientFor(c.GetConfig())
33+
}
34+
2635
type eventHandlerWithoutContext struct {
2736
h EventHandler
2837
}

commonclient/factory_cr15.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
package commonclient
44

55
import (
6+
"net/http"
7+
68
"sigs.k8s.io/controller-runtime/pkg/cache"
79
"sigs.k8s.io/controller-runtime/pkg/client"
10+
"sigs.k8s.io/controller-runtime/pkg/cluster"
811
"sigs.k8s.io/controller-runtime/pkg/handler"
912
"sigs.k8s.io/controller-runtime/pkg/source"
1013
)
@@ -19,4 +22,9 @@ func WrapEventHandler(h handler.EventHandler) handler.EventHandler {
1922
return h
2023
}
2124

25+
// GetHTTPClient returns the http.Client associated with the Cluster
26+
func GetHTTPClient(c cluster.Cluster) (*http.Client, error) {
27+
return c.GetHTTPClient(), nil
28+
}
29+
2230
type EventHandler = handler.EventHandler

pkg/patterns/declarative/pkg/applier/direct.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@ func (d *DirectApplier) Apply(ctx context.Context, opt ApplierOptions) error {
8888
b := d.inner.NewBuilder(opt)
8989
f := d.inner.NewFactory(opt)
9090

91-
// TODO can we just reuse this
92-
dynamicClient, err := f.DynamicClient()
93-
if err != nil {
94-
return err
91+
dynamicClient := opt.DynamicClient
92+
if dynamicClient == nil {
93+
dc, err := f.DynamicClient()
94+
if err != nil {
95+
return err
96+
}
97+
dynamicClient = dc
9598
}
9699

97100
if opt.Validate {

pkg/patterns/declarative/pkg/applier/type.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ type ApplierOptions struct {
4343

4444
// DynamicClient, if set, will be used for applying additional objects.
4545
// If not set, a dynamic client will be built from RESTConfig.
46+
// If the caller can provide a cached DynamicClient, that is more efficient.
4647
DynamicClient dynamic.Interface
4748
}

pkg/patterns/declarative/reconciler.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"net/http"
2324
"path/filepath"
2425
"reflect"
2526
"strings"
@@ -41,6 +42,7 @@ import (
4142
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4243
"sigs.k8s.io/kustomize/kyaml/filesys"
4344

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

5254
type Reconciler struct {
5355
prototype DeclarativeObject
54-
client client.Client
55-
config *rest.Config
56+
57+
client client.Client
58+
restConfig *rest.Config
59+
httpClient *http.Client
60+
dynamicClient dynamic.Interface
61+
restMapper meta.RESTMapper
5662

5763
metrics reconcileMetrics
5864
mgr manager.Manager
5965

6066
// recorder is the EventRecorder for creating k8s events
61-
recorder recorder.EventRecorder
62-
dynamicClient dynamic.Interface
67+
recorder recorder.EventRecorder
6368

64-
restMapper meta.RESTMapper
65-
options reconcilerParams
69+
options reconcilerParams
6670
}
6771

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

107111
r.client = mgr.GetClient()
108-
r.config = mgr.GetConfig()
112+
r.restConfig = mgr.GetConfig()
113+
httpClient, err := commonclient.GetHTTPClient(mgr)
114+
if err != nil {
115+
return fmt.Errorf("getting HTTP client: %w", err)
116+
}
117+
r.httpClient = httpClient
118+
109119
r.mgr = mgr
110120
globalObjectTracker.mgr = mgr
111121

112-
d, err := dynamic.NewForConfig(r.config)
122+
d, err := dynamic.NewForConfigAndClient(r.restConfig, r.httpClient)
113123
if err != nil {
114124
return err
115125
}
@@ -379,7 +389,7 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
379389
return statusInfo, fmt.Errorf("building applyset parent: %w", err)
380390
}
381391
applierOpt := applier.ApplierOptions{
382-
RESTConfig: r.config,
392+
RESTConfig: r.restConfig,
383393
RESTMapper: r.restMapper,
384394
Namespace: ns,
385395
ParentRef: parentRef,
@@ -389,6 +399,7 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
389399
Force: true,
390400
CascadingStrategy: r.options.cascadingStrategy,
391401
Client: r.client,
402+
DynamicClient: r.dynamicClient,
392403
}
393404

394405
applyOperation := &ApplyOperation{

0 commit comments

Comments
 (0)