Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track2 sdk: override default UserAgent header in sdk #7301

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/azclient/arm_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"

"sigs.k8s.io/cloud-provider-azure/pkg/azclient/policy/useragent"
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/utils"
)

Expand Down Expand Up @@ -52,7 +53,10 @@ func GetAzCoreClientOption(armConfig *ARMClientConfig) (*policy.ClientOptions, e
azCoreClientConfig := utils.GetDefaultAzCoreClientOption()
if armConfig != nil {
//update user agent header
azCoreClientConfig.Telemetry.ApplicationID = strings.TrimSpace(armConfig.UserAgent)
if userAgent := strings.TrimSpace(armConfig.UserAgent); userAgent != "" {
azCoreClientConfig.Telemetry.Disabled = true
azCoreClientConfig.PerCallPolicies = append(azCoreClientConfig.PerCallPolicies, useragent.NewCustomUserAgentPolicy(userAgent))
}
//set cloud
cloudConfig, err := GetAzureCloudConfig(armConfig)
if err != nil {
Expand Down
57 changes: 0 additions & 57 deletions pkg/azclient/arm_conf_test.go

This file was deleted.

71 changes: 0 additions & 71 deletions pkg/azclient/factory_conf_test.go

This file was deleted.

46 changes: 46 additions & 0 deletions pkg/azclient/policy/useragent/user_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package useragent

import (
"net/http"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
)

type CustomUserAgentPolicy struct {
CustomUserAgent string
}

const HeaderUserAgent = "User-Agent"

func NewCustomUserAgentPolicy(customUserAgent string) policy.Policy {
return &CustomUserAgentPolicy{
CustomUserAgent: customUserAgent,
}
}

func (p CustomUserAgentPolicy) Do(req *policy.Request) (*http.Response, error) {
if p.CustomUserAgent == "" {
return req.Next()
}
// preserve the existing User-Agent string
if ua := req.Raw().Header.Get(HeaderUserAgent); ua == "" {
req.Raw().Header.Set(HeaderUserAgent, p.CustomUserAgent)
}
return req.Next()
}
93 changes: 93 additions & 0 deletions pkg/azclient/policy/useragent/user_agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package useragent_test

import (
"context"
"net/http"
"strings"
"sync"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

"sigs.k8s.io/cloud-provider-azure/pkg/azclient/policy/useragent"
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/utils"
)

var _ = ginkgo.Describe("useragent", func() {
ginkgo.Describe("useragent", func() {
ginkgo.It("should respect useragent", func() {
once := sync.Once{}
userAgentPolicy := &useragent.CustomUserAgentPolicy{}
pipeline := runtime.NewPipeline("testmodule", "v0.1.0", runtime.PipelineOptions{}, &policy.ClientOptions{
Telemetry: policy.TelemetryOptions{
Disabled: true,
},
PerCallPolicies: []policy.Policy{
userAgentPolicy,
utils.FuncPolicyWrapper(
func(*policy.Request) (*http.Response, error) {
resp := &http.Response{
StatusCode: http.StatusOK,
Body: http.NoBody,
}
once.Do(func() {
resp = &http.Response{
StatusCode: http.StatusTooManyRequests,
Body: http.NoBody,
Header: http.Header{
"Retry-After": []string{"10"},
},
}
})
return resp, nil
},
),
},
})
userAgentPolicy.CustomUserAgent = "test"
req, err := runtime.NewRequest(context.Background(), http.MethodPut, "http://localhost:8080")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = req.SetBody(streaming.NopCloser(strings.NewReader(`{"etag":"etag"}`)), "application/json")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
_, err = pipeline.Do(req)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(req.Raw().Header.Get(useragent.HeaderUserAgent)).To(gomega.Equal("test"))
userAgentPolicy.CustomUserAgent = ""
req, err = runtime.NewRequest(context.Background(), http.MethodPut, "http://localhost:8080")
req.Raw().Header.Set(useragent.HeaderUserAgent, "test-override")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = req.SetBody(streaming.NopCloser(strings.NewReader(`{"etag":"etag"}`)), "application/json")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
_, err = pipeline.Do(req)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(req.Raw().Header.Get(useragent.HeaderUserAgent)).To(gomega.Equal("test-override"))
userAgentPolicy.CustomUserAgent = ""
req, err = runtime.NewRequest(context.Background(), http.MethodPut, "http://localhost:8080")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = req.SetBody(streaming.NopCloser(strings.NewReader(`{"etag":"etag"}`)), "application/json")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
_, err = pipeline.Do(req)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(req.Raw().Header.Get(useragent.HeaderUserAgent)).To(gomega.BeEmpty())
})
})
})
29 changes: 29 additions & 0 deletions pkg/azclient/policy/useragent/useragent_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package useragent_test

import (
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

func TestUserAgent(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "UserAgent Suite")
}
Loading