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

SDK2: Remove old SDK from Dynamic/ValidateQuota #3889

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 16 additions & 16 deletions pkg/frontend/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"context"
"testing"

sdknetwork "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"
mgmtcompute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
mgmtnetwork "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-08-01/network"
"github.com/Azure/go-autorest/autorest/to"
"go.uber.org/mock/gomock"

"github.com/Azure/ARO-RP/pkg/api"
mock_armnetwork "github.com/Azure/ARO-RP/pkg/util/mocks/azureclient/azuresdk/armnetwork"
mock_compute "github.com/Azure/ARO-RP/pkg/util/mocks/azureclient/mgmt/compute"
mock_network "github.com/Azure/ARO-RP/pkg/util/mocks/azureclient/mgmt/network"
utilerror "github.com/Azure/ARO-RP/test/util/error"
)

Expand All @@ -23,13 +23,13 @@ func TestValidateQuota(t *testing.T) {

type test struct {
name string
mocks func(*test, *mock_compute.MockUsageClient, *mock_network.MockUsageClient)
mocks func(*test, *mock_compute.MockUsageClient, *mock_armnetwork.MockUsagesClient)
wantErr string
}
for _, tt := range []*test{
{
name: "allow when there's enough resources - limits set to exact requirements, offset by 100 of current value",
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_network.MockUsageClient) {
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_armnetwork.MockUsagesClient) {
cuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtcompute.Usage{
Expand Down Expand Up @@ -63,10 +63,10 @@ func TestValidateQuota(t *testing.T) {
},
}, nil)
nuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtnetwork.Usage{
List(ctx, "ocLocation", nil).
Return([]*sdknetwork.Usage{
{
Name: &mgmtnetwork.UsageName{
Name: &sdknetwork.UsageName{
Value: to.StringPtr("PublicIPAddresses"),
},
CurrentValue: to.Int64Ptr(4),
Expand All @@ -78,7 +78,7 @@ func TestValidateQuota(t *testing.T) {
{
name: "not enough cores",
wantErr: "400: ResourceQuotaExceeded: : Resource quota of cores exceeded. Maximum allowed: 212, Current in use: 101, Additional requested: 112.",
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_network.MockUsageClient) {
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_armnetwork.MockUsagesClient) {
cuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtcompute.Usage{
Expand All @@ -95,7 +95,7 @@ func TestValidateQuota(t *testing.T) {
{
name: "not enough virtualMachines",
wantErr: "400: ResourceQuotaExceeded: : Resource quota of virtualMachines exceeded. Maximum allowed: 114, Current in use: 101, Additional requested: 14.",
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_network.MockUsageClient) {
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_armnetwork.MockUsagesClient) {
cuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtcompute.Usage{
Expand All @@ -112,7 +112,7 @@ func TestValidateQuota(t *testing.T) {
{
name: "not enough standardDSv3Family",
wantErr: "400: ResourceQuotaExceeded: : Resource quota of standardDSv3Family exceeded. Maximum allowed: 212, Current in use: 101, Additional requested: 112.",
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_network.MockUsageClient) {
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_armnetwork.MockUsagesClient) {
cuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtcompute.Usage{
Expand All @@ -129,7 +129,7 @@ func TestValidateQuota(t *testing.T) {
{
name: "not enough premium disks",
wantErr: "400: ResourceQuotaExceeded: : Resource quota of PremiumDiskCount exceeded. Maximum allowed: 114, Current in use: 101, Additional requested: 14.",
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_network.MockUsageClient) {
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_armnetwork.MockUsagesClient) {
cuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtcompute.Usage{
Expand All @@ -146,15 +146,15 @@ func TestValidateQuota(t *testing.T) {
{
name: "not enough public ip addresses",
wantErr: "400: ResourceQuotaExceeded: : Resource quota of PublicIPAddresses exceeded. Maximum allowed: 6, Current in use: 4, Additional requested: 3.",
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_network.MockUsageClient) {
mocks: func(tt *test, cuc *mock_compute.MockUsageClient, nuc *mock_armnetwork.MockUsagesClient) {
cuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtcompute.Usage{}, nil)
nuc.EXPECT().
List(ctx, "ocLocation").
Return([]mgmtnetwork.Usage{
List(ctx, "ocLocation", nil).
Return([]*sdknetwork.Usage{
{
Name: &mgmtnetwork.UsageName{
Name: &sdknetwork.UsageName{
Value: to.StringPtr("PublicIPAddresses"),
},
CurrentValue: to.Int64Ptr(4),
Expand All @@ -169,7 +169,7 @@ func TestValidateQuota(t *testing.T) {
defer controller.Finish()

computeUsageClient := mock_compute.NewMockUsageClient(controller)
networkUsageClient := mock_network.NewMockUsageClient(controller)
networkUsageClient := mock_armnetwork.NewMockUsagesClient(controller)
if tt.mocks != nil {
tt.mocks(tt, computeUsageClient, networkUsageClient)
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/frontend/quota_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/Azure/ARO-RP/pkg/api/validate"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/util/azureclient"
"github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/armnetwork"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/compute"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/network"
)

type QuotaValidator interface {
Expand Down Expand Up @@ -44,13 +44,22 @@ func (q quotaValidator) ValidateQuota(ctx context.Context, azEnv *azureclient.AR
return err
}

credential, err := environment.FPNewClientCertificateCredential(tenantID)
if err != nil {
return err
}
options := environment.Environment().ArmClientOptions()

spComputeUsage := compute.NewUsageClient(azEnv, subscriptionID, fpAuthorizer)
spNetworkUsage := network.NewUsageClient(azEnv, subscriptionID, fpAuthorizer)
spNetworkUsage, err := armnetwork.NewUsagesClient(subscriptionID, credential, options)
if err != nil {
return err
}

return validateQuota(ctx, oc, spNetworkUsage, spComputeUsage)
}

func validateQuota(ctx context.Context, oc *api.OpenShiftCluster, spNetworkUsage network.UsageClient, spComputeUsage compute.UsageClient) error {
func validateQuota(ctx context.Context, oc *api.OpenShiftCluster, spNetworkUsage armnetwork.UsagesClient, spComputeUsage compute.UsageClient) error {
// If ValidateQuota runs outside install process, we should skip quota validation
requiredResources := map[string]int{}

Expand Down Expand Up @@ -89,7 +98,7 @@ func validateQuota(ctx context.Context, oc *api.OpenShiftCluster, spNetworkUsage
}
}

netUsages, err := spNetworkUsage.List(ctx, oc.Location)
netUsages, err := spNetworkUsage.List(ctx, oc.Location, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/azureclient/azuresdk/armnetwork/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package armnetwork
// Licensed under the Apache License 2.0.

//go:generate rm -rf ../../../../util/mocks/$GOPACKAGE
//go:generate mockgen -destination=../../../../util/mocks/azureclient/azuresdk/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/$GOPACKAGE InterfacesClient,LoadBalancersClient,LoadBalancerBackendAddressPoolsClient,PrivateEndpointsClient,PrivateLinkServicesClient,PublicIPAddressesClient,SecurityGroupsClient,SubnetsClient
//go:generate mockgen -destination=../../../../util/mocks/azureclient/azuresdk/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/$GOPACKAGE InterfacesClient,LoadBalancersClient,LoadBalancerBackendAddressPoolsClient,PrivateEndpointsClient,PrivateLinkServicesClient,PublicIPAddressesClient,SecurityGroupsClient,SubnetsClient,UsagesClient,VirtualNetworksClient
//go:generate goimports -local=github.com/Azure/ARO-RP -e -w ../../../../util/mocks/azureclient/azuresdk/$GOPACKAGE/$GOPACKAGE.go
27 changes: 27 additions & 0 deletions pkg/util/azureclient/azuresdk/armnetwork/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package armnetwork

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"

"github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/azcore"
)

// UsagesClient is a minimal interface for azure UsageClient
type UsagesClient interface {
UsageClientAddons
}

type usagesClient struct {
*armnetwork.UsagesClient
}

// NewUsagesClient creates a new UsageClient
func NewUsagesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (UsagesClient, error) {
client, err := armnetwork.NewUsagesClient(subscriptionID, credential, options)

return &usagesClient{client}, err
}
28 changes: 28 additions & 0 deletions pkg/util/azureclient/azuresdk/armnetwork/usage_addons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package armnetwork

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2"
)

// UsageClientAddons contains addons to UsageClient
type UsageClientAddons interface {
List(ctx context.Context, location string, options *armnetwork.UsagesClientListOptions) (result []*armnetwork.Usage, err error)
}

func (c *usagesClient) List(ctx context.Context, location string, options *armnetwork.UsagesClientListOptions) (result []*armnetwork.Usage, err error) {
pager := c.UsagesClient.NewListPager(location, options)

for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
result = append(result, page.Value...)
}
return result, nil
}
2 changes: 1 addition & 1 deletion pkg/util/azureclient/mgmt/network/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package network
// Licensed under the Apache License 2.0.

//go:generate rm -rf ../../../../util/mocks/$GOPACKAGE
//go:generate mockgen -destination=../../../../util/mocks/azureclient/mgmt/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/$GOPACKAGE InterfacesClient,LoadBalancersClient,PrivateEndpointsClient,PublicIPAddressesClient,LoadBalancerBackendAddressPoolsClient,RouteTablesClient,SubnetsClient,VirtualNetworksClient,UsageClient,FlowLogsClient
//go:generate mockgen -destination=../../../../util/mocks/azureclient/mgmt/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/$GOPACKAGE InterfacesClient,LoadBalancersClient,PrivateEndpointsClient,PublicIPAddressesClient,LoadBalancerBackendAddressPoolsClient,RouteTablesClient,SubnetsClient,VirtualNetworksClient,FlowLogsClient
//go:generate goimports -local=github.com/Azure/ARO-RP -e -w ../../../../util/mocks/azureclient/mgmt/$GOPACKAGE/$GOPACKAGE.go
33 changes: 0 additions & 33 deletions pkg/util/azureclient/mgmt/network/usage.go

This file was deleted.

32 changes: 0 additions & 32 deletions pkg/util/azureclient/mgmt/network/usage_addons.go

This file was deleted.

80 changes: 78 additions & 2 deletions pkg/util/mocks/azureclient/azuresdk/armnetwork/armnetwork.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading