Skip to content

Commit

Permalink
fix missing default userAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
jwtty committed Jun 13, 2024
1 parent 46a4d82 commit e76c11c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
3 changes: 3 additions & 0 deletions cmd/kube-egress-gateway-controller/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ func startControllers(cmd *cobra.Command, args []string) {
} else {
cred = authProvider.ClientSecretCredential
}
if cloudConfig.UserAgent == "" {
cloudConfig.UserAgent = consts.DefaultUserAgent
}
var factory azclient.ClientFactory
factory, err = azclient.NewClientFactory(&azclient.ClientFactoryConfig{SubscriptionID: cloudConfig.SubscriptionID}, &azclient.ARMClientConfig{Cloud: cloudConfig.Cloud, UserAgent: cloudConfig.UserAgent}, cred)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions pkg/azmanager/azmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
)

const (
DefaultUserAgent = "kube-egress-gateway-controller"

// LB frontendIPConfiguration ID template
LBFrontendIPConfigTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/frontendIPConfigurations/%s"
// LB backendAddressPool ID template
Expand All @@ -49,10 +47,6 @@ func CreateAzureManager(cloud *config.CloudConfig, factory azclient.ClientFactor
CloudConfig: cloud,
}

if az.UserAgent == "" {
az.UserAgent = DefaultUserAgent
}

if az.LoadBalancerResourceGroup == "" {
az.LoadBalancerResourceGroup = az.ResourceGroup
}
Expand Down
53 changes: 23 additions & 30 deletions pkg/azmanager/azmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,18 @@ import (
func TestCreateAzureManager(t *testing.T) {
tests := []struct {
desc string
userAgent string
expectedUserAgent string
lbResourceGroup string
expectedLBResourceGroup string
vnetResourceGroup string
expectedVnetResourceGroup string
}{
{
desc: "test default userAgent, lbResourceGroup and vnetResourceGroup",
expectedUserAgent: "kube-egress-gateway-controller",
desc: "test default lbResourceGroup and vnetResourceGroup",
expectedLBResourceGroup: "testRG",
expectedVnetResourceGroup: "testRG",
},
{
desc: "test custom userAgent, lbResourceGroup and vnetResourceGroup",
userAgent: "testUserAgent",
expectedUserAgent: "testUserAgent",
desc: "test custom lbResourceGroup and vnetResourceGroup",
lbResourceGroup: "testLBRG",
expectedLBResourceGroup: "testLBRG",
vnetResourceGroup: "testVnetRG",
Expand All @@ -55,11 +50,10 @@ func TestCreateAzureManager(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig(test.userAgent, test.lbResourceGroup, test.vnetResourceGroup)
config := getTestCloudConfig(test.lbResourceGroup, test.vnetResourceGroup)
factory := getMockFactory(ctrl)
az, err := CreateAzureManager(config, factory)
assert.Nil(t, err, "TestCase[%d]: %s", i, test.desc)
assert.Equal(t, az.UserAgent, test.expectedUserAgent, "TestCase[%d]: %s", i, test.desc)
assert.Equal(t, az.LoadBalancerResourceGroup, test.expectedLBResourceGroup, "TestCase[%d]: %s", i, test.desc)
assert.Equal(t, az.VnetResourceGroup, test.expectedVnetResourceGroup, "TestCase[%d]: %s", i, test.desc)
assert.Equal(t, az.SubscriptionID(), config.SubscriptionID, "TestCase[%d]: %s", i, test.desc)
Expand All @@ -70,7 +64,7 @@ func TestCreateAzureManager(t *testing.T) {
func TestGets(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("test", "testLBRG", "")
config := getTestCloudConfig("testLBRG", "")
factory := getMockFactory(ctrl)
az, err := CreateAzureManager(config, factory)
assert.Nil(t, err, "CreateAzureManager() should not return error")
Expand Down Expand Up @@ -107,7 +101,7 @@ func TestGetLB(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
mockLoadBalancerClient := az.LoadBalancerClient.(*mock_loadbalancerclient.MockInterface)
Expand Down Expand Up @@ -137,7 +131,7 @@ func TestCreateOrUpdateLB(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
mockLoadBalancerClient := az.LoadBalancerClient.(*mock_loadbalancerclient.MockInterface)
Expand Down Expand Up @@ -166,7 +160,7 @@ func TestDeleteLB(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
mockLoadBalancerClient := az.LoadBalancerClient.(*mock_loadbalancerclient.MockInterface)
Expand All @@ -184,7 +178,7 @@ func TestListVMSS(t *testing.T) {
}{
{
desc: "ListVMSS() should return expected vmss list",
vmssList: []*compute.VirtualMachineScaleSet{&compute.VirtualMachineScaleSet{Name: to.Ptr("vmss")}},
vmssList: []*compute.VirtualMachineScaleSet{{Name: to.Ptr("vmss")}},
},
{
desc: "ListVMSS() should return expected error",
Expand All @@ -194,7 +188,7 @@ func TestListVMSS(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
mockVMSSClient := az.VmssClient.(*mock_virtualmachinescalesetclient.MockInterface)
Expand Down Expand Up @@ -249,7 +243,7 @@ func TestGetVMSS(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -303,7 +297,7 @@ func TestCreateOrUpdateVMSS(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand All @@ -330,15 +324,15 @@ func TestListVMSSInstances(t *testing.T) {
desc: "ListVMSSInstances() should return expected vmss instances",
expectedRG: "testRG",
vmssName: "vmss",
vms: []*compute.VirtualMachineScaleSetVM{&compute.VirtualMachineScaleSetVM{Name: to.Ptr("vm0")}},
vms: []*compute.VirtualMachineScaleSetVM{{Name: to.Ptr("vm0")}},
expectedCall: true,
},
{
desc: "ListVMSSInstances() should return expected vmss instances with specified resource group",
rg: "customRG",
expectedRG: "customRG",
vmssName: "vmss",
vms: []*compute.VirtualMachineScaleSetVM{&compute.VirtualMachineScaleSetVM{Name: to.Ptr("vm0")}},
vms: []*compute.VirtualMachineScaleSetVM{{Name: to.Ptr("vm0")}},
expectedCall: true,
},
{
Expand All @@ -357,7 +351,7 @@ func TestListVMSSInstances(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -424,7 +418,7 @@ func TestGetVMSSInstance(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -488,7 +482,7 @@ func TestUpdateVMSSInstance(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -542,7 +536,7 @@ func TestGetPublicIPPrefix(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -596,7 +590,7 @@ func TestCreateOrUpdatePublicIPPrefix(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -647,7 +641,7 @@ func TestDeletePublicIPPrefix(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -721,7 +715,7 @@ func TestGetVMSSInterface(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
if test.expectedCall {
Expand Down Expand Up @@ -757,7 +751,7 @@ func TestGetSubnet(t *testing.T) {
for i, test := range tests {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := getTestCloudConfig("", "", "")
config := getTestCloudConfig("", "")
factory := getMockFactory(ctrl)
az, _ := CreateAzureManager(config, factory)
mockSubnetClient := az.SubnetClient.(*mock_subnetclient.MockInterface)
Expand All @@ -779,11 +773,10 @@ func getMockFactory(ctrl *gomock.Controller) azclient.ClientFactory {
return factory
}

func getTestCloudConfig(userAgent, lbRG, vnetRG string) *config.CloudConfig {
func getTestCloudConfig(lbRG, vnetRG string) *config.CloudConfig {
return &config.CloudConfig{
ARMClientConfig: azclient.ARMClientConfig{
Cloud: "AzureTest",
UserAgent: userAgent,
Cloud: "AzureTest",
},
Location: "location",
SubscriptionID: "testSub",
Expand Down
3 changes: 3 additions & 0 deletions pkg/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const (

// Owning StaticGatewayConfiguration name key on secret label
OwningSGCNameLabel = "egressgateway.kubernetes.azure.com/owning-gateway-config-name"

// Default user agent for Azure SDK
DefaultUserAgent = "kube-egress-gateway-controller"
)

const (
Expand Down

0 comments on commit e76c11c

Please sign in to comment.