From 3de96247226760a0b7c4c6c36e688d1cb41609b8 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Thu, 3 Aug 2023 16:54:46 -0400 Subject: [PATCH] :bug: Fix Defaulting of the User Agent This broke when we added the HTTP client, because the user-agent gets set by a roundtripper that is constructed within `rest.HTTPClientFor`. As a result, we have to default it before we do that. Currently, it ends up being defaulted to `Go-http-client` which is not very useful. --- pkg/cache/cache.go | 5 +++++ pkg/client/client.go | 8 ++++++-- pkg/cluster/cluster.go | 9 ++++++++- pkg/manager/manager.go | 9 +++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index f01de43810..7600387047 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -236,6 +236,11 @@ func New(config *rest.Config, opts Options) (Cache, error) { } func defaultOpts(config *rest.Config, opts Options) (Options, error) { + config = rest.CopyConfig(config) + if config.UserAgent == "" { + config.UserAgent = rest.DefaultKubernetesUserAgent() + } + logger := log.WithName("setup") // Use the rest HTTP client for the provided config if unset diff --git a/pkg/client/client.go b/pkg/client/client.go index 21067b6f8f..0d8b9fbe18 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -110,6 +110,11 @@ func newClient(config *rest.Config, options Options) (*client, error) { return nil, fmt.Errorf("must provide non-nil rest.Config to client.New") } + config = rest.CopyConfig(config) + if config.UserAgent == "" { + config.UserAgent = rest.DefaultKubernetesUserAgent() + } + if !options.WarningHandler.SuppressWarnings { // surface warnings logger := log.Log.WithName("KubeAPIWarningLogger") @@ -117,7 +122,6 @@ func newClient(config *rest.Config, options Options) (*client, error) { // is log.KubeAPIWarningLogger with deduplication enabled. // See log.KubeAPIWarningLoggerOptions for considerations // regarding deduplication. - config = rest.CopyConfig(config) config.WarningHandler = log.NewKubeAPIWarningLogger( logger, log.KubeAPIWarningLoggerOptions{ @@ -160,7 +164,7 @@ func newClient(config *rest.Config, options Options) (*client, error) { unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta), } - rawMetaClient, err := metadata.NewForConfigAndClient(config, options.HTTPClient) + rawMetaClient, err := metadata.NewForConfigAndClient(metadata.ConfigFor(config), options.HTTPClient) if err != nil { return nil, fmt.Errorf("unable to construct metadata-only client for use as part of client: %w", err) } diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 7d00c3c4b0..7ab76555b3 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -179,6 +179,13 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) { return nil, errors.New("must specify Config") } + originalConfig := config + + config = rest.CopyConfig(config) + if config.UserAgent == "" { + config.UserAgent = rest.DefaultKubernetesUserAgent() + } + options := Options{} for _, opt := range opts { opt(&options) @@ -275,7 +282,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) { } return &cluster{ - config: config, + config: originalConfig, httpClient: options.HTTPClient, scheme: options.Scheme, cache: cache, diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 7e65ef0c3a..72a4a7801a 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -19,6 +19,7 @@ package manager import ( "context" "crypto/tls" + "errors" "fmt" "net" "net/http" @@ -391,6 +392,9 @@ type LeaderElectionRunnable interface { // New returns a new Manager for creating Controllers. func New(config *rest.Config, options Options) (Manager, error) { + if config == nil { + return nil, errors.New("must specify Config") + } // Set default values for options fields options = setOptionsDefaults(options) @@ -412,6 +416,11 @@ func New(config *rest.Config, options Options) (Manager, error) { return nil, err } + config = rest.CopyConfig(config) + if config.UserAgent == "" { + config.UserAgent = rest.DefaultKubernetesUserAgent() + } + // Create the recorder provider to inject event recorders for the components. // TODO(directxman12): the log for the event provider should have a context (name, tags, etc) specific // to the particular controller that it's being injected into, rather than a generic one like is here.