Skip to content

Commit

Permalink
debug: add a unique agic identifier suffix to ARM user agent (#1082)
Browse files Browse the repository at this point in the history
* debug: add a unique agic identifier

* fix

* change to klog
  • Loading branch information
akshaysngupta committed Jan 22, 2021
1 parent 5de47a0 commit 72a53e3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/appgw-ingress/main.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pkg/azure/client.go
Expand Up @@ -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)),
Expand Down
17 changes: 17 additions & 0 deletions pkg/utils/utils.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
10 changes: 10 additions & 0 deletions pkg/utils/utils_test.go
Expand Up @@ -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))
})
})
})
})

0 comments on commit 72a53e3

Please sign in to comment.