Skip to content

Commit dba36b8

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 dba36b8

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

pkg/cache/cache.go

Lines changed: 5 additions & 0 deletions
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

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
158158
return nil, errors.New("must specify Config")
159159
}
160160

161+
config = rest.CopyConfig(config)
162+
if config.UserAgent == "" {
163+
config.UserAgent = rest.DefaultKubernetesUserAgent()
164+
}
165+
161166
options := Options{}
162167
for _, opt := range opts {
163168
opt(&options)

pkg/manager/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ func New(config *rest.Config, options Options) (Manager, error) {
322322
// Set default values for options fields
323323
options = setOptionsDefaults(options)
324324

325+
config = rest.CopyConfig(config)
326+
if config.UserAgent == "" {
327+
config.UserAgent = rest.DefaultKubernetesUserAgent()
328+
}
329+
325330
cluster, err := cluster.New(config, func(clusterOptions *cluster.Options) {
326331
clusterOptions.Scheme = options.Scheme
327332
clusterOptions.MapperProvider = options.MapperProvider

0 commit comments

Comments
 (0)