diff --git a/pkg/util/BUILD.bazel b/pkg/util/BUILD.bazel index 87335011e..c4a9f5b07 100644 --- a/pkg/util/BUILD.bazel +++ b/pkg/util/BUILD.bazel @@ -12,6 +12,7 @@ go_library( deps = [ "//pkg/apis/certmanager/v1:go_default_library", "@io_k8s_apimachinery//pkg/apis/meta/v1/validation:go_default_library", + "@io_k8s_client_go//rest:go_default_library", ], ) @@ -23,7 +24,10 @@ go_test( "version_test.go", ], embed = [":go_default_library"], - deps = ["@com_github_stretchr_testify//assert:go_default_library"], + deps = [ + "@com_github_stretchr_testify//assert:go_default_library", + "@io_k8s_client_go//rest:go_default_library", + ], ) filegroup( diff --git a/pkg/util/useragent.go b/pkg/util/useragent.go index da9cac8f2..34a5a757b 100644 --- a/pkg/util/useragent.go +++ b/pkg/util/useragent.go @@ -18,15 +18,24 @@ package util import ( "bytes" + "fmt" "strings" "unicode" "unicode/utf8" "k8s.io/apimachinery/pkg/apis/meta/v1/validation" + "k8s.io/client-go/rest" ) -// CertManagerUserAgent is the user agent that http clients in this codebase should use -var CertManagerUserAgent = "cert-manager/" + version() +// RestConfigWithUserAgent returns a copy of the Kubernetes REST config with +// the User Agent set which includes the optional component strings given. +func RestConfigWithUserAgent(restConfig *rest.Config, component ...string) *rest.Config { + restConfig = rest.CopyConfig(restConfig) + restConfig.UserAgent = fmt.Sprintf("%s/%s (%s) cert-manager/%s", + strings.Join(append([]string{"cert-manager"}, component...), "-"), + version(), VersionInfo().Platform, VersionInfo().GitCommit) + return restConfig +} // PrefixFromUserAgent takes the characters preceding the first /, quote // unprintable character and then trim what's beyond the FieldManagerMaxLength diff --git a/pkg/util/useragent_test.go b/pkg/util/useragent_test.go index 69db03b5f..1d9ed8bca 100644 --- a/pkg/util/useragent_test.go +++ b/pkg/util/useragent_test.go @@ -17,11 +17,48 @@ limitations under the License. package util import ( + "runtime" "testing" "github.com/stretchr/testify/assert" + "k8s.io/client-go/rest" ) +func Test_RestConfigWithUserAgent(t *testing.T) { + AppGitCommit = "test-commit" + + tests := map[string]struct { + component []string + expRestConfig rest.Config + }{ + "if no component name given, expect just cert-manager field manager": { + component: nil, + expRestConfig: rest.Config{ + UserAgent: "cert-manager/canary-test-commit (" + runtime.GOOS + "/" + runtime.GOARCH + ") cert-manager/test-commit", + }, + }, + "if single component name given, expect cert-manager with single component field manager": { + component: []string{"controller"}, + expRestConfig: rest.Config{ + UserAgent: "cert-manager-controller/canary-test-commit (" + runtime.GOOS + "/" + runtime.GOARCH + ") cert-manager/test-commit", + }, + }, + "if multiple component names given, expect cert-manager with multiple component field manager": { + component: []string{"controller", "issuing-foo", "bar"}, + expRestConfig: rest.Config{ + UserAgent: "cert-manager-controller-issuing-foo-bar/canary-test-commit (" + runtime.GOOS + "/" + runtime.GOARCH + ") cert-manager/test-commit", + }, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + gotRestConfig := RestConfigWithUserAgent(new(rest.Config), test.component...) + assert.Equal(t, &test.expRestConfig, gotRestConfig) + }) + } +} + // Adapted from // https://github.com/kubernetes/apiserver/blob/cecf3a2e57ffdfa8f3b36db4ee0c44e59ad656e9/pkg/endpoints/handlers/create_test.go#L24 func Test_PrefixFromUserAgent(t *testing.T) {