package util import ( "bytes" "fmt" "strings" "unicode" "unicode/utf8" "k8s.io/apimachinery/pkg/apis/meta/v1/validation" "k8s.io/client-go/rest" ) // 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) anthos-cert-manager/%s", strings.Join(append([]string{"anthos-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 // limit. // Taken from // https://github.com/kubernetes/kubernetes/blob/9a75e7b0fd1b567f774a3373be640e19b33e7ef1/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/create.go#L252 func PrefixFromUserAgent(u string) string { m := strings.Split(u, "/")[0] buf := bytes.NewBuffer(nil) for _, r := range m { // Ignore non-printable characters if !unicode.IsPrint(r) { continue } // Only append if we have room for it if buf.Len()+utf8.RuneLen(r) > validation.FieldManagerMaxLength { break } buf.WriteRune(r) } return buf.String() }