Skip to content

Commit a6b9c0b

Browse files
committed
🐛 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.
1 parent 7f0c6dc commit a6b9c0b

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

pkg/cache/cache.go

+5
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,11 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
334334
}
335335

336336
func defaultOpts(config *rest.Config, opts Options) (Options, error) {
337+
config = rest.CopyConfig(config)
338+
if config.UserAgent == "" {
339+
config.UserAgent = rest.DefaultKubernetesUserAgent()
340+
}
341+
337342
// Use the rest HTTP client for the provided config if unset
338343
if opts.HTTPClient == nil {
339344
var err error

pkg/client/client.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,18 @@ func newClient(config *rest.Config, options Options) (*client, error) {
110110
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")
111111
}
112112

113+
config = rest.CopyConfig(config)
114+
if config.UserAgent == "" {
115+
config.UserAgent = rest.DefaultKubernetesUserAgent()
116+
}
117+
113118
if !options.WarningHandler.SuppressWarnings {
114119
// surface warnings
115120
logger := log.Log.WithName("KubeAPIWarningLogger")
116121
// Set a WarningHandler, the default WarningHandler
117122
// is log.KubeAPIWarningLogger with deduplication enabled.
118123
// See log.KubeAPIWarningLoggerOptions for considerations
119124
// regarding deduplication.
120-
config = rest.CopyConfig(config)
121125
config.WarningHandler = log.NewKubeAPIWarningLogger(
122126
logger,
123127
log.KubeAPIWarningLoggerOptions{
@@ -160,7 +164,7 @@ func newClient(config *rest.Config, options Options) (*client, error) {
160164
unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
161165
}
162166

163-
rawMetaClient, err := metadata.NewForConfigAndClient(config, options.HTTPClient)
167+
rawMetaClient, err := metadata.NewForConfigAndClient(metadata.ConfigFor(config), options.HTTPClient)
164168
if err != nil {
165169
return nil, fmt.Errorf("unable to construct metadata-only client for use as part of client: %w", err)
166170
}

pkg/cluster/cluster.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
158158
return nil, errors.New("must specify Config")
159159
}
160160

161+
originalConfig := config
162+
163+
config = rest.CopyConfig(config)
164+
if config.UserAgent == "" {
165+
config.UserAgent = rest.DefaultKubernetesUserAgent()
166+
}
167+
161168
options := Options{}
162169
for _, opt := range opts {
163170
opt(&options)
@@ -241,7 +248,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
241248
}
242249

243250
return &cluster{
244-
config: config,
251+
config: originalConfig,
245252
httpClient: options.HTTPClient,
246253
scheme: options.Scheme,
247254
cache: cache,

pkg/manager/manager.go

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package manager
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"net"
2324
"net/http"
@@ -319,6 +320,9 @@ type LeaderElectionRunnable interface {
319320
// will be used for all built-in resources of Kubernetes, and "application/json" is for other types
320321
// including all CRD resources.
321322
func New(config *rest.Config, options Options) (Manager, error) {
323+
if config == nil {
324+
return nil, errors.New("must specify Config")
325+
}
322326
// Set default values for options fields
323327
options = setOptionsDefaults(options)
324328

@@ -336,6 +340,11 @@ func New(config *rest.Config, options Options) (Manager, error) {
336340
return nil, err
337341
}
338342

343+
config = rest.CopyConfig(config)
344+
if config.UserAgent == "" {
345+
config.UserAgent = rest.DefaultKubernetesUserAgent()
346+
}
347+
339348
// Create the recorder provider to inject event recorders for the components.
340349
// TODO(directxman12): the log for the event provider should have a context (name, tags, etc) specific
341350
// to the particular controller that it's being injected into, rather than a generic one like is here.

0 commit comments

Comments
 (0)