Skip to content

Commit b0200d9

Browse files
author
b-lnimmala
committed
move FP credential creation out of validators and into caller
1 parent 6f1bd2d commit b0200d9

9 files changed

Lines changed: 47 additions & 57 deletions

pkg/frontend/features_validation.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ import (
88
"fmt"
99
"net/http"
1010

11+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
12+
1113
"github.com/Azure/ARO-RP/pkg/api"
1214
"github.com/Azure/ARO-RP/pkg/env"
13-
"github.com/Azure/ARO-RP/pkg/util/azureclient"
1415
"github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/armfeatures"
1516
)
1617

1718
type FeaturesValidator interface {
18-
ValidateSubscriptionFeatures(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID, tenantID string, oc *api.OpenShiftCluster) error
19+
ValidateSubscriptionFeatures(ctx context.Context, environment env.Interface, subscriptionID string, fpCred azcore.TokenCredential, oc *api.OpenShiftCluster) error
1920
}
2021

2122
type featuresValidator struct{}
2223

23-
func (f featuresValidator) ValidateSubscriptionFeatures(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID, tenantID string, oc *api.OpenShiftCluster) error {
24+
func (f featuresValidator) ValidateSubscriptionFeatures(ctx context.Context, environment env.Interface, subscriptionID string, fpCred azcore.TokenCredential, oc *api.OpenShiftCluster) error {
2425
var fieldPath string
2526
if oc.Properties.MasterProfile.EncryptionAtHost == api.EncryptionAtHostEnabled {
2627
fieldPath = "properties.masterProfile.encryptionAtHost"
@@ -29,12 +30,7 @@ func (f featuresValidator) ValidateSubscriptionFeatures(ctx context.Context, azE
2930
}
3031

3132
if fieldPath != "" {
32-
fpCred, err := environment.FPNewClientCertificateCredential(tenantID, nil)
33-
if err != nil {
34-
return err
35-
}
36-
37-
featuresClient, err := armfeatures.NewFeaturesClient(subscriptionID, fpCred, azEnv.ArmClientOptions())
33+
featuresClient, err := armfeatures.NewFeaturesClient(subscriptionID, fpCred, environment.Environment().ArmClientOptions())
3834
if err != nil {
3935
return err
4036
}

pkg/frontend/openshiftcluster_putorpatch.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,22 @@ func (f *frontend) ValidateNewCluster(ctx context.Context, subscription *api.Sub
450450
return err
451451
}
452452

453-
err = f.skuValidator.ValidateVMSku(ctx, f.env.Environment(), f.env, subscription.ID, subscription.Subscription.Properties.TenantID, cluster)
453+
fpCred, err := f.env.FPNewClientCertificateCredential(subscription.Subscription.Properties.TenantID, nil)
454454
if err != nil {
455455
return err
456456
}
457457

458-
err = f.featuresValidator.ValidateSubscriptionFeatures(ctx, f.env.Environment(), f.env, subscription.ID, subscription.Subscription.Properties.TenantID, cluster)
458+
err = f.skuValidator.ValidateVMSku(ctx, f.env, subscription.ID, fpCred, cluster)
459459
if err != nil {
460460
return err
461461
}
462462

463-
err = f.quotaValidator.ValidateQuota(ctx, f.env.Environment(), f.env, subscription.ID, subscription.Subscription.Properties.TenantID, cluster)
463+
err = f.featuresValidator.ValidateSubscriptionFeatures(ctx, f.env, subscription.ID, fpCred, cluster)
464+
if err != nil {
465+
return err
466+
}
467+
468+
err = f.quotaValidator.ValidateQuota(ctx, f.env.Environment(), f.env, subscription.ID, fpCred, cluster)
464469
if err != nil {
465470
return err
466471
}

pkg/frontend/openshiftcluster_putorpatch_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ func TestPutorPatchOpenShiftClusterCreate(t *testing.T) {
741741
mockQuotaValidator.EXPECT().ValidateQuota(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.quotaValidatorError).AnyTimes()
742742

743743
mockSkuValidator := mock_frontend.NewMockSkuValidator(controller)
744-
mockSkuValidator.EXPECT().ValidateVMSku(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.skuValidatorError).AnyTimes()
744+
mockSkuValidator.EXPECT().ValidateVMSku(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.skuValidatorError).AnyTimes()
745745

746746
mockProvidersValidator := mock_frontend.NewMockProvidersValidator(controller)
747747
mockProvidersValidator.EXPECT().ValidateProviders(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.providersValidatorError).AnyTimes()
@@ -1112,7 +1112,7 @@ func TestPutorPatchOpenShiftClusterUpdatePut(t *testing.T) {
11121112
mockQuotaValidator.EXPECT().ValidateQuota(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.quotaValidatorError).AnyTimes()
11131113

11141114
mockSkuValidator := mock_frontend.NewMockSkuValidator(controller)
1115-
mockSkuValidator.EXPECT().ValidateVMSku(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.skuValidatorError).AnyTimes()
1115+
mockSkuValidator.EXPECT().ValidateVMSku(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.skuValidatorError).AnyTimes()
11161116

11171117
mockProvidersValidator := mock_frontend.NewMockProvidersValidator(controller)
11181118
mockProvidersValidator.EXPECT().ValidateProviders(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.providersValidatorError).AnyTimes()
@@ -1586,7 +1586,7 @@ func TestPutorPatchOpenShiftClusterUpdatePatch(t *testing.T) {
15861586
mockQuotaValidator.EXPECT().ValidateQuota(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.quotaValidatorError).AnyTimes()
15871587

15881588
mockSkuValidator := mock_frontend.NewMockSkuValidator(controller)
1589-
mockSkuValidator.EXPECT().ValidateVMSku(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.skuValidatorError).AnyTimes()
1589+
mockSkuValidator.EXPECT().ValidateVMSku(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.skuValidatorError).AnyTimes()
15901590

15911591
mockProvidersValidator := mock_frontend.NewMockProvidersValidator(controller)
15921592
mockProvidersValidator.EXPECT().ValidateProviders(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(tt.providersValidatorError).AnyTimes()

pkg/frontend/quota_validation.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"fmt"
99
"net/http"
1010

11+
"github.com/jongio/azidext/go/azidext"
12+
13+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
14+
1115
"github.com/Azure/ARO-RP/pkg/api"
1216
"github.com/Azure/ARO-RP/pkg/api/validate"
1317
"github.com/Azure/ARO-RP/pkg/env"
@@ -17,7 +21,7 @@ import (
1721
)
1822

1923
type QuotaValidator interface {
20-
ValidateQuota(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID, tenantID string, oc *api.OpenShiftCluster) error
24+
ValidateQuota(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID string, fpCred azcore.TokenCredential, oc *api.OpenShiftCluster) error
2125
}
2226

2327
type quotaValidator struct{}
@@ -39,20 +43,13 @@ func addRequiredResources(requiredResources map[string]int, vmSize api.VMSize, c
3943
// ValidateQuota checks usage quotas vs. resources required by cluster before cluster
4044
// creation
4145
// It is a method on struct so we can make use of interfaces.
42-
func (q quotaValidator) ValidateQuota(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID, tenantID string, oc *api.OpenShiftCluster) error {
43-
fpAuthorizer, err := environment.FPAuthorizer(tenantID, nil, environment.Environment().ResourceManagerScope)
44-
if err != nil {
45-
return err
46-
}
46+
func (q quotaValidator) ValidateQuota(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID string, fpCred azcore.TokenCredential, oc *api.OpenShiftCluster) error {
47+
fpAuthorizer := azidext.NewTokenCredentialAdapter(fpCred, []string{environment.Environment().ResourceManagerScope})
4748

48-
credential, err := environment.FPNewClientCertificateCredential(tenantID, []string{})
49-
if err != nil {
50-
return err
51-
}
5249
options := environment.Environment().ArmClientOptions()
5350

5451
spComputeUsage := compute.NewUsageClient(azEnv, subscriptionID, fpAuthorizer)
55-
spNetworkUsage, err := armnetwork.NewUsagesClient(subscriptionID, credential, options)
52+
spNetworkUsage, err := armnetwork.NewUsagesClient(subscriptionID, fpCred, options)
5653
if err != nil {
5754
return err
5855
}

pkg/frontend/sku_validation.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,23 @@ import (
88
"fmt"
99
"net/http"
1010

11+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
1112
sdkcompute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v7"
1213

1314
"github.com/Azure/ARO-RP/pkg/api"
1415
"github.com/Azure/ARO-RP/pkg/env"
15-
"github.com/Azure/ARO-RP/pkg/util/azureclient"
1616
"github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/armcompute"
1717
"github.com/Azure/ARO-RP/pkg/util/computeskus"
1818
)
1919

2020
type SkuValidator interface {
21-
ValidateVMSku(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID, tenantID string, oc *api.OpenShiftCluster) error
21+
ValidateVMSku(ctx context.Context, environment env.Interface, subscriptionID string, fpCred azcore.TokenCredential, oc *api.OpenShiftCluster) error
2222
}
2323

2424
type skuValidator struct{}
2525

26-
func (s skuValidator) ValidateVMSku(ctx context.Context, azEnv *azureclient.AROEnvironment, environment env.Interface, subscriptionID, tenantID string, oc *api.OpenShiftCluster) error {
27-
fpCredClusterTenant, err := environment.FPNewClientCertificateCredential(tenantID, nil)
28-
if err != nil {
29-
return err
30-
}
31-
32-
armResourceSKUsClient, err := armcompute.NewResourceSKUsClient(subscriptionID, fpCredClusterTenant, environment.Environment().ArmClientOptions())
26+
func (s skuValidator) ValidateVMSku(ctx context.Context, environment env.Interface, subscriptionID string, fpCred azcore.TokenCredential, oc *api.OpenShiftCluster) error {
27+
armResourceSKUsClient, err := armcompute.NewResourceSKUsClient(subscriptionID, fpCred, environment.Environment().ArmClientOptions())
3328
if err != nil {
3429
return err
3530
}

pkg/util/mocks/frontend/features_validation.go

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/util/mocks/frontend/providers_validation.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/util/mocks/frontend/quota_validation.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/util/mocks/frontend/sku_validation.go

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)