Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix Defaulting of the User Agent #2435

Merged
merged 1 commit into from Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
9 changes: 8 additions & 1 deletion pkg/cluster/cluster.go
Expand Up @@ -158,6 +158,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)
Expand Down Expand Up @@ -241,7 +248,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
}

return &cluster{
config: config,
config: originalConfig,
httpClient: options.HTTPClient,
scheme: options.Scheme,
cache: cache,
Expand Down
9 changes: 9 additions & 0 deletions pkg/manager/manager.go
Expand Up @@ -18,6 +18,7 @@ package manager

import (
"context"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -319,6 +320,9 @@ type LeaderElectionRunnable interface {
// will be used for all built-in resources of Kubernetes, and "application/json" is for other types
// including all CRD resources.
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)

Expand All @@ -336,6 +340,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.
Expand Down