diff --git a/cmd/appgw-ingress/main.go b/cmd/appgw-ingress/main.go index a9afc2184..ad5cf6b88 100644 --- a/cmd/appgw-ingress/main.go +++ b/cmd/appgw-ingress/main.go @@ -34,6 +34,7 @@ import ( "github.com/Azure/application-gateway-kubernetes-ingress/pkg/httpserver" "github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext" "github.com/Azure/application-gateway-kubernetes-ingress/pkg/metricstore" + "github.com/Azure/application-gateway-kubernetes-ingress/pkg/utils" "github.com/Azure/application-gateway-kubernetes-ingress/pkg/version" ) @@ -109,7 +110,13 @@ func main() { klog.Fatal(errorLine) } - azClient := azure.NewAzClient(azure.SubscriptionID(env.SubscriptionID), azure.ResourceGroup(env.ResourceGroupName), azure.ResourceName(env.AppGwName), env.ClientID) + uniqueUserAgentSuffix := utils.RandStringRunes(10) + if agicPod != nil { + uniqueUserAgentSuffix = agicPod.Name + } + klog.Infof("Using User Agent Suffix='%s' when communicating with ARM", uniqueUserAgentSuffix) + + azClient := azure.NewAzClient(azure.SubscriptionID(env.SubscriptionID), azure.ResourceGroup(env.ResourceGroupName), azure.ResourceName(env.AppGwName), uniqueUserAgentSuffix, env.ClientID) appGwIdentifier := appgw.Identifier{ SubscriptionID: env.SubscriptionID, ResourceGroup: env.ResourceGroupName, diff --git a/pkg/azure/client.go b/pkg/azure/client.go index 1b5a2465e..e59d131c8 100644 --- a/pkg/azure/client.go +++ b/pkg/azure/client.go @@ -54,13 +54,13 @@ type azClient struct { } // NewAzClient returns an Azure Client -func NewAzClient(subscriptionID SubscriptionID, resourceGroupName ResourceGroup, appGwName ResourceName, clientID string) AzClient { +func NewAzClient(subscriptionID SubscriptionID, resourceGroupName ResourceGroup, appGwName ResourceName, uniqueUserAgentSuffix, clientID string) AzClient { settings, err := auth.GetSettingsFromEnvironment() if err != nil { return nil } - userAgent := fmt.Sprintf("ingress-appgw/%s", version.Version) + userAgent := fmt.Sprintf("ingress-appgw/%s/%s", version.Version, uniqueUserAgentSuffix) az := &azClient{ appGatewaysClient: n.NewApplicationGatewaysClientWithBaseURI(settings.Environment.ResourceManagerEndpoint, string(subscriptionID)), publicIPsClient: n.NewPublicIPAddressesClientWithBaseURI(settings.Environment.ResourceManagerEndpoint, string(subscriptionID)), diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 3122b9567..8828d5c52 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -10,11 +10,19 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math/rand" "strings" + "time" "k8s.io/klog/v2" ) +func init() { + rand.Seed(time.Now().UnixNano()) +} + +var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz0123456789") + // GetResourceKey generates the key in k8s format for a given resource func GetResourceKey(namespace, name string) string { return fmt.Sprintf("%v/%v", namespace, name) @@ -61,3 +69,12 @@ func GetHashCode(i interface{}) string { } return fmt.Sprintf("%x", md5.Sum(jsonBytes)) } + +// RandStringRunes generates n length random string +func RandStringRunes(n int) string { + b := make([]rune, n) + for i := range b { + b[i] = letterRunes[rand.Intn(len(letterRunes))] + } + return string(b) +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 7847ddc92..3ab0b6c5b 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -62,5 +62,15 @@ var _ = Describe("Utils", func() { Expect(hashcode).To(Equal("28a37ff7b783ffb4696dfb7774331163")) }) }) + + Context("Test RandStringRunes", func() { + It("should generate n length string", func() { + Expect(len(RandStringRunes(10))).To(Equal(10)) + }) + + It("should not fail when n = 0", func() { + Expect(len(RandStringRunes(0))).To(Equal(0)) + }) + }) }) })