diff --git a/CHANGELOG.md b/CHANGELOG.md index 183a6346..2545020b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [MAJOR.MINOR.PATCH] - YYYY-MM-DD +- Unified User-Agent format with the Terraform Provider + ## v0.18.1 - 2024-04-02 - Add `KafkaSchemaRegistryACL` kind diff --git a/controllers/common.go b/controllers/common.go index b9e2128e..4d785375 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -132,14 +132,26 @@ func isAivenServerError(err error) bool { return status >= http.StatusInternalServerError } +// userAgent is a helper function to create a User-Agent string used for the Go client. +func userAgent(kubeVersion, operatorVersion string) string { + // Remove the leading "v" from the version strings, if present. + // This is to unify the version format across the Terraform Provider and the Kubernetes Operator. + // + // Cf. terraform-provider-aiven/A.B.C/X.Y.Z vs. k8s-operator/vA.B.C/vX.Y.Z. + kubeVersion = strings.TrimPrefix(kubeVersion, "v") + operatorVersion = strings.TrimPrefix(operatorVersion, "v") + + return fmt.Sprintf("k8s-operator/%s/%s", kubeVersion, operatorVersion) +} + // NewAivenClient returns Aiven client (aiven/aiven-go-client/v2) func NewAivenClient(token, kubeVersion, operatorVersion string) (*aiven.Client, error) { - return aiven.NewTokenClient(token, fmt.Sprintf("k8s-operator/%s/%s", kubeVersion, operatorVersion)) + return aiven.NewTokenClient(token, userAgent(kubeVersion, operatorVersion)) } // NewAivenGeneratedClient returns Aiven generated client client (aiven/go-client-codegen) func NewAivenGeneratedClient(token, kubeVersion, operatorVersion string) (avngen.Client, error) { - return avngen.NewClient(avngen.TokenOpt(token), avngen.UserAgentOpt(fmt.Sprintf("k8s-operator/%s/%s", kubeVersion, operatorVersion))) + return avngen.NewClient(avngen.TokenOpt(token), avngen.UserAgentOpt(userAgent(kubeVersion, operatorVersion))) } func fromAnyPointer[T any](v *T) T {