Adds rest config builder to include new user agent
Signed-off-by: joshvanl <vleeuwenjoshua@gmail.com>
This commit is contained in:
parent
d89c3e71dc
commit
8f0c79396f
@ -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(
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user