Skip to content

Commit 3de9624

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 0e37217 commit 3de9624

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

pkg/cache/cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ func New(config *rest.Config, opts Options) (Cache, error) {
236236
}
237237

238238
func defaultOpts(config *rest.Config, opts Options) (Options, error) {
239+
config = rest.CopyConfig(config)
240+
if config.UserAgent == "" {
241+
config.UserAgent = rest.DefaultKubernetesUserAgent()
242+
}
243+
239244
logger := log.WithName("setup")
240245

241246
// Use the rest HTTP client for the provided config if unset

pkg/client/client.go

Lines changed: 6 additions & 2 deletions
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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
179179
return nil, errors.New("must specify Config")
180180
}
181181

182+
originalConfig := config
183+
184+
config = rest.CopyConfig(config)
185+
if config.UserAgent == "" {
186+
config.UserAgent = rest.DefaultKubernetesUserAgent()
187+
}
188+
182189
options := Options{}
183190
for _, opt := range opts {
184191
opt(&options)
@@ -275,7 +282,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
275282
}
276283

277284
return &cluster{
278-
config: config,
285+
config: originalConfig,
279286
httpClient: options.HTTPClient,
280287
scheme: options.Scheme,
281288
cache: cache,

pkg/manager/manager.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package manager
1919
import (
2020
"context"
2121
"crypto/tls"
22+
"errors"
2223
"fmt"
2324
"net"
2425
"net/http"
@@ -391,6 +392,9 @@ type LeaderElectionRunnable interface {
391392

392393
// New returns a new Manager for creating Controllers.
393394
func New(config *rest.Config, options Options) (Manager, error) {
395+
if config == nil {
396+
return nil, errors.New("must specify Config")
397+
}
394398
// Set default values for options fields
395399
options = setOptionsDefaults(options)
396400

@@ -412,6 +416,11 @@ func New(config *rest.Config, options Options) (Manager, error) {
412416
return nil, err
413417
}
414418

419+
config = rest.CopyConfig(config)
420+
if config.UserAgent == "" {
421+
config.UserAgent = rest.DefaultKubernetesUserAgent()
422+
}
423+
415424
// Create the recorder provider to inject event recorders for the components.
416425
// TODO(directxman12): the log for the event provider should have a context (name, tags, etc) specific
417426
// to the particular controller that it's being injected into, rather than a generic one like is here.

0 commit comments

Comments
 (0)