Repository: Azure/ARO-RP
Pull request #2105 (2022-05-03 04:22:49)

Move armDeployTemplate into util
### Which issue this PR addresses:

Part of M5 installer cleanup: https://msazure.visualstudio.com/AzureRedHatOpenShift/_workitems/edit/14317614

### What this PR does / why we need it:

When the installer code is isolated we will need to use the `armDeployTemplate` code in two locations. This moves it out of `pkg/cluster/` along with the tests, and also removes the need for a wrapper function in `pkg/cluster/`. We could move it into an interface and mock it later, if we wanted.

### Test plan for issue:

Unit tests follow the move, plus E2E should catch any problems.

### Is there any documentation that needs to be updated for this PR?

N/A



Patch:
diff --git a/pkg/cluster/clusterserviceprincipal.go b/pkg/cluster/clusterserviceprincipal.go
index d2441046d..b1751435b 100644
--- a/pkg/cluster/clusterserviceprincipal.go
+++ b/pkg/cluster/clusterserviceprincipal.go
@@ -66,7 +66,7 @@ func (m *manager) createOrUpdateClusterServicePrincipalRBAC(ctx context.Context)
 			ContentVersion: "1.0.0.0",
 			Resources:      []*arm.Resource{m.clusterServicePrincipalRBAC()},
 		}
-		err = m.deployARMTemplate(ctx, resourceGroup, "storage", t, nil)
+		err = arm.DeployTemplate(ctx, m.log, m.deployments, resourceGroup, "storage", t, nil)
 		if err != nil {
 			return err
 		}
diff --git a/pkg/cluster/denyassignment.go b/pkg/cluster/denyassignment.go
index 1881cb6f7..9c2368315 100644
--- a/pkg/cluster/denyassignment.go
+++ b/pkg/cluster/denyassignment.go
@@ -32,5 +32,5 @@ func (m *manager) createOrUpdateDenyAssignment(ctx context.Context) error {
 		},
 	}
 
-	return m.deployARMTemplate(ctx, resourceGroup, "storage", t, nil)
+	return arm.DeployTemplate(ctx, m.log, m.deployments, resourceGroup, "storage", t, nil)
 }
diff --git a/pkg/cluster/deployresources.go b/pkg/cluster/deployresources.go
index f2b2556ee..505dc4ecb 100644
--- a/pkg/cluster/deployresources.go
+++ b/pkg/cluster/deployresources.go
@@ -52,7 +52,7 @@ func (m *manager) deployResourceTemplate(ctx context.Context) error {
 			m.computeMasterVMs(installConfig, zones, machineMaster),
 		},
 	}
-	return m.deployARMTemplate(ctx, resourceGroup, "resources", t, map[string]interface{}{
+	return arm.DeployTemplate(ctx, m.log, m.deployments, resourceGroup, "resources", t, map[string]interface{}{
 		"sas": map[string]interface{}{
 			"value": map[string]interface{}{
 				"signedStart":         m.doc.OpenShiftCluster.Properties.Install.Now.Format(time.RFC3339),
diff --git a/pkg/cluster/deploystorage.go b/pkg/cluster/deploystorage.go
index cb0e4a923..d2893d8a7 100644
--- a/pkg/cluster/deploystorage.go
+++ b/pkg/cluster/deploystorage.go
@@ -166,7 +166,7 @@ func (m *manager) deployStorageTemplate(ctx context.Context, installConfig *inst
 		t.Resources = append(t.Resources, m.denyAssignment())
 	}
 
-	return m.deployARMTemplate(ctx, resourceGroup, "storage", t, nil)
+	return arm.DeployTemplate(ctx, m.log, m.deployments, resourceGroup, "storage", t, nil)
 }
 
 func (m *manager) ensureGraph(ctx context.Context, installConfig *installconfig.InstallConfig, image *releaseimage.Image) error {
diff --git a/pkg/cluster/gatewayprivateendpoint.go b/pkg/cluster/gatewayprivateendpoint.go
index 093efcd31..bd9c859ef 100644
--- a/pkg/cluster/gatewayprivateendpoint.go
+++ b/pkg/cluster/gatewayprivateendpoint.go
@@ -33,7 +33,7 @@ func (m *manager) ensureGatewayUpgrade(ctx context.Context) error {
 		ContentVersion: "1.0.0.0",
 		Resources:      []*arm.Resource{m.networkPrivateEndpoint()},
 	}
-	err = m.deployARMTemplate(ctx, resourceGroup, "storage", t, nil)
+	err = arm.DeployTemplate(ctx, m.log, m.deployments, resourceGroup, "storage", t, nil)
 	if err != nil {
 		m.log.Print(err)
 		return nil
diff --git a/pkg/cluster/storageaccounts.go b/pkg/cluster/storageaccounts.go
index 89d512c2b..a73983517 100644
--- a/pkg/cluster/storageaccounts.go
+++ b/pkg/cluster/storageaccounts.go
@@ -81,7 +81,7 @@ func (m *manager) migrateStorageAccounts(ctx context.Context) error {
 		},
 	}
 
-	return m.deployARMTemplate(ctx, resourceGroup, "storage", t, nil)
+	return arm.DeployTemplate(ctx, m.log, m.deployments, resourceGroup, "storage", t, nil)
 }
 
 func (m *manager) populateRegistryStorageAccountName(ctx context.Context) error {
diff --git a/pkg/cluster/arm.go b/pkg/util/arm/deploy.go
similarity index 69%
rename from pkg/cluster/arm.go
rename to pkg/util/arm/deploy.go
index 83124cd3c..3bad254fc 100644
--- a/pkg/cluster/arm.go
+++ b/pkg/util/arm/deploy.go
@@ -1,4 +1,4 @@
-package cluster
+package arm
 
 // Copyright (c) Microsoft Corporation.
 // Licensed under the Apache License 2.0.
@@ -11,15 +11,16 @@ import (
 	mgmtfeatures "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/features"
 	"github.com/Azure/go-autorest/autorest"
 	"github.com/Azure/go-autorest/autorest/azure"
+	"github.com/sirupsen/logrus"
 
 	"github.com/Azure/ARO-RP/pkg/api"
-	"github.com/Azure/ARO-RP/pkg/util/arm"
+	"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/features"
 	"github.com/Azure/ARO-RP/pkg/util/azureerrors"
 )
 
-func (m *manager) deployARMTemplate(ctx context.Context, resourceGroupName string, deploymentName string, template *arm.Template, parameters map[string]interface{}) error {
-	m.log.Printf("deploying %s template", deploymentName)
-	err := m.deployments.CreateOrUpdateAndWait(ctx, resourceGroupName, deploymentName, mgmtfeatures.Deployment{
+func DeployTemplate(ctx context.Context, log *logrus.Entry, deployments features.DeploymentsClient, resourceGroupName string, deploymentName string, template *Template, parameters map[string]interface{}) error {
+	log.Printf("deploying %s template", deploymentName)
+	err := deployments.CreateOrUpdateAndWait(ctx, resourceGroupName, deploymentName, mgmtfeatures.Deployment{
 		Properties: &mgmtfeatures.DeploymentProperties{
 			Template:   template,
 			Parameters: parameters,
@@ -28,8 +29,8 @@ func (m *manager) deployARMTemplate(ctx context.Context, resourceGroupName strin
 	})
 
 	if azureerrors.IsDeploymentActiveError(err) {
-		m.log.Printf("waiting for %s template to be deployed", deploymentName)
-		err = m.deployments.Wait(ctx, resourceGroupName, deploymentName)
+		log.Printf("waiting for %s template to be deployed", deploymentName)
+		err = deployments.Wait(ctx, resourceGroupName, deploymentName)
 	}
 
 	if azureerrors.HasAuthorizationFailedError(err) ||


Test patch:
diff --git a/pkg/cluster/arm_test.go b/pkg/util/arm/deploy_test.go
similarity index 92%
rename from pkg/cluster/arm_test.go
rename to pkg/util/arm/deploy_test.go
index f4ab08fc1..4ea0b3ba2 100644
--- a/pkg/cluster/arm_test.go
+++ b/pkg/util/arm/deploy_test.go
@@ -1,4 +1,4 @@
-package cluster
+package arm
 
 // Copyright (c) Microsoft Corporation.
 // Licensed under the Apache License 2.0.
@@ -14,7 +14,6 @@ import (
 	"github.com/sirupsen/logrus"
 	"k8s.io/apimachinery/pkg/util/wait"
 
-	"github.com/Azure/ARO-RP/pkg/util/arm"
 	mock_features "github.com/Azure/ARO-RP/pkg/util/mocks/azureclient/mgmt/features"
 )
 
@@ -25,7 +24,7 @@ func TestDeployARMTemplate(t *testing.T) {
 
 	resourceGroup := "fakeResourceGroup"
 
-	armTemplate := &arm.Template{}
+	armTemplate := &Template{}
 	params := map[string]interface{}{}
 
 	deployment := mgmtfeatures.Deployment{
@@ -108,12 +107,9 @@ func TestDeployARMTemplate(t *testing.T) {
 			deploymentsClient := mock_features.NewMockDeploymentsClient(controller)
 			tt.mocks(deploymentsClient)
 
-			m := &manager{
-				log:         logrus.NewEntry(logrus.StandardLogger()),
-				deployments: deploymentsClient,
-			}
+			log := logrus.NewEntry(logrus.StandardLogger())
 
-			err := m.deployARMTemplate(ctx, resourceGroup, deploymentName, armTemplate, params)
+			err := DeployTemplate(ctx, log, deploymentsClient, resourceGroup, deploymentName, armTemplate, params)
 
 			if err != nil && err.Error() != tt.wantErr ||
 				err == nil && tt.wantErr != "" {