Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow http client reuse in dynamic client #377

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions pkg/patterns/declarative/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package declarative
import (
"context"
"fmt"
"net/http"
"sync"

"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -55,6 +56,9 @@ type WatchChildrenOptions struct {
// RESTConfig is the configuration for connecting to the cluster.
RESTConfig *rest.Config

// HTTPClient is the HTTP client to use for requests.
HTTPClient *http.Client

// LabelMaker is used to build the labels we should watch on.
LabelMaker LabelMaker

Expand Down Expand Up @@ -88,6 +92,21 @@ func WatchChildren(options WatchChildrenOptions) error {
return fmt.Errorf("labelMaker is required to scope watches")
}

var httpClient *http.Client
if options.HTTPClient != nil {
httpClient = options.HTTPClient
} else {
if options.RESTConfig != nil {
hc, err := rest.HTTPClientFor(options.RESTConfig)
if err != nil {
return err
}
httpClient = hc
} else if options.Manager != nil {
httpClient = options.Manager.GetHTTPClient()
}
}

if options.RESTConfig == nil {
if options.Manager != nil {
options.RESTConfig = options.Manager.GetConfig()
Expand All @@ -100,23 +119,19 @@ func WatchChildren(options WatchChildrenOptions) error {
if options.Manager != nil {
restMapper = options.Manager.GetRESTMapper()
} else {
client, err := rest.HTTPClientFor(options.RESTConfig)
if err != nil {
return err
}
rm, err := commonclient.NewDiscoveryRESTMapper(options.RESTConfig, client)
rm, err := commonclient.NewDiscoveryRESTMapper(options.RESTConfig, httpClient)
if err != nil {
return err
}
restMapper = rm
}

client, err := dynamic.NewForConfig(options.RESTConfig)
dynamicClient, err := dynamic.NewForConfigAndClient(options.RESTConfig, httpClient)
if err != nil {
return err
}

dw, events, err := watch.NewDynamicWatch(restMapper, client)
dw, events, err := watch.NewDynamicWatch(restMapper, dynamicClient)
if err != nil {
return fmt.Errorf("creating dynamic watch: %v", err)
}
Expand Down
Loading