Skip to content

Commit

Permalink
🐛 Fix Defaulting of the User Agent
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alvaroaleman committed Aug 3, 2023
1 parent 7f0c6dc commit dba36b8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/cache/cache.go
Expand Up @@ -334,6 +334,11 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
}

func defaultOpts(config *rest.Config, opts Options) (Options, error) {
config = rest.CopyConfig(config)
if config.UserAgent == "" {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}

// Use the rest HTTP client for the provided config if unset
if opts.HTTPClient == nil {
var err error
Expand Down
8 changes: 6 additions & 2 deletions pkg/client/client.go
Expand Up @@ -110,14 +110,18 @@ 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")
// Set a WarningHandler, the default WarningHandler
// is log.KubeAPIWarningLogger with deduplication enabled.
// See log.KubeAPIWarningLoggerOptions for considerations
// regarding deduplication.
config = rest.CopyConfig(config)
config.WarningHandler = log.NewKubeAPIWarningLogger(
logger,
log.KubeAPIWarningLoggerOptions{
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/cluster.go
Expand Up @@ -158,6 +158,11 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
return nil, errors.New("must specify Config")
}

config = rest.CopyConfig(config)
if config.UserAgent == "" {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}

options := Options{}
for _, opt := range opts {
opt(&options)
Expand Down
5 changes: 5 additions & 0 deletions pkg/manager/manager.go
Expand Up @@ -322,6 +322,11 @@ func New(config *rest.Config, options Options) (Manager, error) {
// Set default values for options fields
options = setOptionsDefaults(options)

config = rest.CopyConfig(config)
if config.UserAgent == "" {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}

cluster, err := cluster.New(config, func(clusterOptions *cluster.Options) {
clusterOptions.Scheme = options.Scheme
clusterOptions.MapperProvider = options.MapperProvider
Expand Down

0 comments on commit dba36b8

Please sign in to comment.