Repository: helm/helm
Pull request #11226 (2022-08-12 09:09:22)

Fix User-Agent header in requests made by Helm 
> This is the recreation of PR #10207 which has been closed due to problems kicking off CI for it.


Current Helm v3.8.1 installed from package manager or build from main branch when makes requests to K8s API (with use of K8s SDK), e.g. executing the command

```
helm history test
```
does not have the proper value set in `User-Agent` header. It looks like that on macOS Monterey with Apple Silicon.
```yaml
User-Agent: Go-http-client/1.1
```
This PR fixes it and makes it consistent with `User-Agent` set when requests are made to chart repositories (it works as expected). So now it always looks like that
```yaml
User-Agent: Helm/3.8+unreleased
```
or
```yaml
User-Agent: Helm/3.8
```

Hence I removed those comments too from the code
```
// TODO add user-agent
```
because it's been already done and works as expected for interaction with chart repos and etc. Here is the line which provides it https://github.com/helm/helm/blob/0d9ebc7885e427373fbdaa95f461b349fb0b1ac1/pkg/getter/httpgetter.go#L53


mattfarina: @programmer04 could you please rebase this PR from main.

Patch:
diff --git a/pkg/cli/environment.go b/pkg/cli/environment.go
index ef04ed6bd..dac2a4bc1 100644
--- a/pkg/cli/environment.go
+++ b/pkg/cli/environment.go
@@ -14,7 +14,8 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */
 
-/*Package cli describes the operating environment for the Helm CLI.
+/*
+Package cli describes the operating environment for the Helm CLI.
 
 Helm's environment encapsulates all of the service dependencies Helm has.
 These dependencies are expressed as interfaces so that alternate implementations
@@ -33,6 +34,7 @@ import (
 	"k8s.io/cli-runtime/pkg/genericclioptions"
 	"k8s.io/client-go/rest"
 
+	"helm.sh/helm/v3/internal/version"
 	"helm.sh/helm/v3/pkg/helmpath"
 )
 
@@ -120,6 +122,7 @@ func New() *EnvSettings {
 			config.Wrap(func(rt http.RoundTripper) http.RoundTripper {
 				return &retryingRoundTripper{wrapped: rt}
 			})
+			config.UserAgent = version.GetUserAgent()
 			return config
 		},
 	}
diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go
index ac4af575c..3901ad1b0 100644
--- a/pkg/repo/chartrepo.go
+++ b/pkg/repo/chartrepo.go
@@ -120,7 +120,6 @@ func (r *ChartRepository) DownloadIndexFile() (string, error) {
 		return "", err
 	}
 
-	// TODO add user-agent
 	resp, err := r.Client.Get(indexURL,
 		getter.WithURL(r.Config.URL),
 		getter.WithInsecureSkipVerifyTLS(r.Config.InsecureSkipTLSverify),


Test patch:
diff --git a/pkg/cli/environment_test.go b/pkg/cli/environment_test.go
index dbf056e3a..3de6fab4c 100644
--- a/pkg/cli/environment_test.go
+++ b/pkg/cli/environment_test.go
@@ -23,6 +23,8 @@ import (
 	"testing"
 
 	"github.com/spf13/pflag"
+
+	"helm.sh/helm/v3/internal/version"
 )
 
 func TestSetNamespace(t *testing.T) {
@@ -231,6 +233,21 @@ func TestEnvOrBool(t *testing.T) {
 	}
 }
 
+func TestUserAgentHeaderInK8sRESTClientConfig(t *testing.T) {
+	defer resetEnv()()
+
+	settings := New()
+	restConfig, err := settings.RESTClientGetter().ToRESTConfig()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	expectedUserAgent := version.GetUserAgent()
+	if restConfig.UserAgent != expectedUserAgent {
+		t.Errorf("expected User-Agent header %q in K8s REST client config, got %q", expectedUserAgent, restConfig.UserAgent)
+	}
+}
+
 func resetEnv() func() {
 	origEnv := os.Environ()
 
diff --git a/pkg/getter/httpgetter_test.go b/pkg/getter/httpgetter_test.go
index 5a90d8b99..94baef3b8 100644
--- a/pkg/getter/httpgetter_test.go
+++ b/pkg/getter/httpgetter_test.go
@@ -155,9 +155,8 @@ func TestHTTPGetter(t *testing.T) {
 
 func TestDownload(t *testing.T) {
 	expect := "Call me Ishmael"
-	expectedUserAgent := "I am Groot"
 	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		defaultUserAgent := "Helm/" + strings.TrimPrefix(version.GetVersion(), "v")
+		defaultUserAgent := version.GetUserAgent()
 		if r.UserAgent() != defaultUserAgent {
 			t.Errorf("Expected '%s', got '%s'", defaultUserAgent, r.UserAgent())
 		}
@@ -179,6 +178,7 @@ func TestDownload(t *testing.T) {
 	}
 
 	// test with http server
+	const expectedUserAgent = "I am Groot"
 	basicAuthSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		username, password, ok := r.BasicAuth()
 		if !ok || username != "username" || password != "password" {