From b663e73cb265504d1289b3bbbe9f3f053073821f Mon Sep 17 00:00:00 2001 From: Jim Minter Date: Thu, 17 Sep 2020 13:42:37 -0500 Subject: [PATCH] pull out deployment.Mode --- cmd/aro/monitor.go | 3 +- cmd/aro/operator.go | 10 ++-- cmd/aro/rp.go | 3 +- pkg/api/admin/register.go | 3 +- pkg/api/register.go | 6 ++- .../openshiftcluster_validatestatic.go | 13 +++--- .../openshiftcluster_validatestatic_test.go | 21 +++++---- pkg/api/v20191231preview/register.go | 11 +++-- .../openshiftcluster_validatestatic.go | 13 +++--- .../openshiftcluster_validatestatic_test.go | 21 +++++---- pkg/api/v20200430/register.go | 11 +++-- .../openshiftcluster_validatestatic.go | 13 +++--- .../openshiftcluster_validatestatic_test.go | 21 +++++---- pkg/api/v20201031preview/register.go | 11 +++-- pkg/api/validate/vm.go | 5 +- pkg/backend/openshiftcluster/create.go | 3 +- pkg/backend/openshiftcluster/delete.go | 5 +- .../openshiftcluster/openshiftcluster.go | 3 +- pkg/cluster/deploystorage.go | 7 +-- pkg/cluster/samples.go | 6 ++- pkg/cluster/tls.go | 9 ++-- pkg/env/dev.go | 5 -- pkg/env/env.go | 18 ++------ pkg/env/env_test.go | 46 ------------------- pkg/env/int.go | 1 - pkg/env/prod.go | 24 +++++----- pkg/env/test.go | 2 + pkg/frontend/frontend.go | 2 +- pkg/frontend/frontend_test.go | 10 ++++ pkg/frontend/middleware/headers.go | 6 +-- pkg/frontend/openshiftcluster_putorpatch.go | 4 +- .../openshiftcluster_putorpatch_test.go | 5 +- pkg/frontend/ready_get.go | 3 +- pkg/frontend/security_test.go | 4 +- pkg/frontend/shared_test.go | 2 + pkg/metrics/statsd/statsd.go | 3 +- .../controllers/checker/checker_controller.go | 5 +- .../controllers/checker/machinechecker.go | 25 +++++----- pkg/operator/deploy/deploy.go | 3 +- .../mgmt/redhatopenshift/openshiftclusters.go | 4 +- .../mgmt/redhatopenshift/operations.go | 4 +- pkg/util/billing/billing.go | 5 +- pkg/util/billing/billing_test.go | 5 +- pkg/util/deployment/deployment.go | 28 +++++++++++ pkg/util/mocks/env/env.go | 43 ++++++----------- test/e2e/adminapi.go | 4 +- test/e2e/setup.go | 3 +- 47 files changed, 230 insertions(+), 232 deletions(-) delete mode 100644 pkg/env/env_test.go create mode 100644 pkg/util/deployment/deployment.go diff --git a/cmd/aro/monitor.go b/cmd/aro/monitor.go index 830fcc81f..a70cccaad 100644 --- a/cmd/aro/monitor.go +++ b/cmd/aro/monitor.go @@ -19,6 +19,7 @@ import ( "github.com/Azure/ARO-RP/pkg/metrics/statsd/azure" "github.com/Azure/ARO-RP/pkg/metrics/statsd/k8s" pkgmonitor "github.com/Azure/ARO-RP/pkg/monitor" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/encryption" ) @@ -31,7 +32,7 @@ func monitor(ctx context.Context, log *logrus.Entry) error { return err } - if !_env.IsDevelopment() { + if _env.DeploymentMode() != deployment.Development { for _, key := range []string{ "CLUSTER_MDM_ACCOUNT", "CLUSTER_MDM_NAMESPACE", diff --git a/cmd/aro/operator.go b/cmd/aro/operator.go index 319f100cf..ad8577769 100644 --- a/cmd/aro/operator.go +++ b/cmd/aro/operator.go @@ -7,8 +7,6 @@ import ( "context" "flag" "fmt" - "os" - "strings" configclient "github.com/openshift/client-go/config/clientset/versioned" securityclient "github.com/openshift/client-go/security/clientset/versioned" @@ -26,6 +24,7 @@ import ( "github.com/Azure/ARO-RP/pkg/operator/controllers/genevalogging" "github.com/Azure/ARO-RP/pkg/operator/controllers/pullsecret" "github.com/Azure/ARO-RP/pkg/operator/controllers/workaround" + "github.com/Azure/ARO-RP/pkg/util/deployment" utillog "github.com/Azure/ARO-RP/pkg/util/log" // +kubebuilder:scaffold:imports ) @@ -37,10 +36,9 @@ func operator(ctx context.Context, log *logrus.Entry) error { default: return fmt.Errorf("invalid role %s", role) } - developmentMode := false - if strings.ToLower(os.Getenv("RP_MODE")) == "development" { + deploymentMode := deployment.NewMode() + if deploymentMode == deployment.Development { log.Warn("running in development mode") - developmentMode = true } ctrl.SetLogger(utillog.LogrWrapper(log)) @@ -109,7 +107,7 @@ func operator(ctx context.Context, log *logrus.Entry) error { if err = (checker.NewReconciler( log.WithField("controller", controllers.CheckerControllerName), - clustercli, arocli, role, developmentMode)).SetupWithManager(mgr); err != nil { + clustercli, arocli, role, deploymentMode)).SetupWithManager(mgr); err != nil { return fmt.Errorf("unable to create controller InternetChecker: %v", err) } // +kubebuilder:scaffold:builder diff --git a/cmd/aro/rp.go b/cmd/aro/rp.go index 4316bfcf8..35aa4d0db 100644 --- a/cmd/aro/rp.go +++ b/cmd/aro/rp.go @@ -28,6 +28,7 @@ import ( "github.com/Azure/ARO-RP/pkg/metrics/statsd" "github.com/Azure/ARO-RP/pkg/metrics/statsd/azure" "github.com/Azure/ARO-RP/pkg/metrics/statsd/k8s" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/encryption" ) @@ -41,7 +42,7 @@ func rp(ctx context.Context, log *logrus.Entry) error { } var keys []string - if _env.IsDevelopment() { + if _env.DeploymentMode() == deployment.Development { keys = []string{ "PULL_SECRET", } diff --git a/pkg/api/admin/register.go b/pkg/api/admin/register.go index 15ed83224..e4f774dc8 100644 --- a/pkg/api/admin/register.go +++ b/pkg/api/admin/register.go @@ -5,6 +5,7 @@ package admin import ( "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // APIVersion contains a version string as it will be used by clients @@ -15,7 +16,7 @@ func init() { OpenShiftClusterConverter: func() api.OpenShiftClusterConverter { return &openShiftClusterConverter{} }, - OpenShiftClusterStaticValidator: func(location, domain string, developmentMode bool, resourceID string) api.OpenShiftClusterStaticValidator { + OpenShiftClusterStaticValidator: func(string, string, deployment.Mode, string) api.OpenShiftClusterStaticValidator { return &openShiftClusterStaticValidator{} }, } diff --git a/pkg/api/register.go b/pkg/api/register.go index e95b9f1c6..9a8e678d4 100644 --- a/pkg/api/register.go +++ b/pkg/api/register.go @@ -3,6 +3,10 @@ package api // Copyright (c) Microsoft Corporation. // Licensed under the Apache License 2.0. +import ( + "github.com/Azure/ARO-RP/pkg/util/deployment" +) + type OpenShiftClusterConverter interface { ToExternal(*OpenShiftCluster) interface{} ToExternalList([]*OpenShiftCluster, string) interface{} @@ -20,7 +24,7 @@ type OpenShiftClusterCredentialsConverter interface { // Version is a set of endpoints implemented by each API version type Version struct { OpenShiftClusterConverter func() OpenShiftClusterConverter - OpenShiftClusterStaticValidator func(string, string, bool, string) OpenShiftClusterStaticValidator + OpenShiftClusterStaticValidator func(string, string, deployment.Mode, string) OpenShiftClusterStaticValidator OpenShiftClusterCredentialsConverter func() OpenShiftClusterCredentialsConverter } diff --git a/pkg/api/v20191231preview/openshiftcluster_validatestatic.go b/pkg/api/v20191231preview/openshiftcluster_validatestatic.go index f8b35fad4..510eed73d 100644 --- a/pkg/api/v20191231preview/openshiftcluster_validatestatic.go +++ b/pkg/api/v20191231preview/openshiftcluster_validatestatic.go @@ -14,6 +14,7 @@ import ( "github.com/Azure/ARO-RP/pkg/api" "github.com/Azure/ARO-RP/pkg/api/validate" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/immutable" "github.com/Azure/ARO-RP/pkg/util/pullsecret" "github.com/Azure/ARO-RP/pkg/util/subnet" @@ -21,10 +22,10 @@ import ( ) type openShiftClusterStaticValidator struct { - location string - domain string - developmentMode bool - resourceID string + location string + domain string + deploymentMode deployment.Mode + resourceID string r azure.Resource } @@ -203,7 +204,7 @@ func (sv *openShiftClusterStaticValidator) validateNetworkProfile(path string, n } func (sv *openShiftClusterStaticValidator) validateMasterProfile(path string, mp *MasterProfile) error { - if !validate.VMSizeIsValid(api.VMSize(mp.VMSize), sv.developmentMode, true) { + if !validate.VMSizeIsValid(api.VMSize(mp.VMSize), sv.deploymentMode, true) { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".vmSize", "The provided master VM size '%s' is invalid.", mp.VMSize) } if !validate.RxSubnetID.MatchString(mp.SubnetID) { @@ -224,7 +225,7 @@ func (sv *openShiftClusterStaticValidator) validateWorkerProfile(path string, wp if wp.Name != "worker" { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".name", "The provided worker name '%s' is invalid.", wp.Name) } - if !validate.VMSizeIsValid(api.VMSize(wp.VMSize), sv.developmentMode, false) { + if !validate.VMSizeIsValid(api.VMSize(wp.VMSize), sv.deploymentMode, false) { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".vmSize", "The provided worker VM size '%s' is invalid.", wp.VMSize) } if !validate.DiskSizeIsValid(wp.DiskSizeGB) { diff --git a/pkg/api/v20191231preview/openshiftcluster_validatestatic_test.go b/pkg/api/v20191231preview/openshiftcluster_validatestatic_test.go index fbc775894..7d7ff7bf1 100644 --- a/pkg/api/v20191231preview/openshiftcluster_validatestatic_test.go +++ b/pkg/api/v20191231preview/openshiftcluster_validatestatic_test.go @@ -13,15 +13,16 @@ import ( uuid "github.com/satori/go.uuid" "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/version" "github.com/Azure/ARO-RP/test/validate" ) type validateTest struct { - name string - modify func(oc *OpenShiftCluster) - developmentMode bool - wantErr string + name string + modify func(oc *OpenShiftCluster) + deploymentMode deployment.Mode + wantErr string } type testMode string @@ -100,10 +101,10 @@ func runTests(t *testing.T, mode testMode, tests []*validateTest) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := &openShiftClusterStaticValidator{ - location: "location", - domain: "location.aroapp.io", - developmentMode: tt.developmentMode, - resourceID: id, + location: "location", + domain: "location.aroapp.io", + deploymentMode: tt.deploymentMode, + resourceID: id, r: azure.Resource{ SubscriptionID: subscriptionID, ResourceGroup: "resourceGroup", @@ -485,8 +486,8 @@ func TestOpenShiftClusterStaticValidateWorkerProfile(t *testing.T) { modify: func(oc *OpenShiftCluster) { oc.Properties.WorkerProfiles[0].VMSize = "Standard_D4s_v3" }, - developmentMode: true, - wantErr: "400: InvalidParameter: properties.workerProfiles['worker'].vmSize: The provided worker VM size 'Standard_D4s_v3' is invalid.", + deploymentMode: deployment.Development, + wantErr: "400: InvalidParameter: properties.workerProfiles['worker'].vmSize: The provided worker VM size 'Standard_D4s_v3' is invalid.", }, { name: "disk too small", diff --git a/pkg/api/v20191231preview/register.go b/pkg/api/v20191231preview/register.go index a6b78a12c..3389e6b22 100644 --- a/pkg/api/v20191231preview/register.go +++ b/pkg/api/v20191231preview/register.go @@ -5,6 +5,7 @@ package v20191231preview import ( "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // APIVersion contains a version string as it will be used by clients @@ -20,12 +21,12 @@ func init() { OpenShiftClusterConverter: func() api.OpenShiftClusterConverter { return &openShiftClusterConverter{} }, - OpenShiftClusterStaticValidator: func(location, domain string, developmentMode bool, resourceID string) api.OpenShiftClusterStaticValidator { + OpenShiftClusterStaticValidator: func(location, domain string, deploymentMode deployment.Mode, resourceID string) api.OpenShiftClusterStaticValidator { return &openShiftClusterStaticValidator{ - location: location, - domain: domain, - developmentMode: developmentMode, - resourceID: resourceID, + location: location, + domain: domain, + deploymentMode: deploymentMode, + resourceID: resourceID, } }, OpenShiftClusterCredentialsConverter: func() api.OpenShiftClusterCredentialsConverter { diff --git a/pkg/api/v20200430/openshiftcluster_validatestatic.go b/pkg/api/v20200430/openshiftcluster_validatestatic.go index 60756996d..aaa3352e2 100644 --- a/pkg/api/v20200430/openshiftcluster_validatestatic.go +++ b/pkg/api/v20200430/openshiftcluster_validatestatic.go @@ -14,6 +14,7 @@ import ( "github.com/Azure/ARO-RP/pkg/api" "github.com/Azure/ARO-RP/pkg/api/validate" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/immutable" "github.com/Azure/ARO-RP/pkg/util/pullsecret" "github.com/Azure/ARO-RP/pkg/util/subnet" @@ -21,10 +22,10 @@ import ( ) type openShiftClusterStaticValidator struct { - location string - domain string - developmentMode bool - resourceID string + location string + domain string + deploymentMode deployment.Mode + resourceID string r azure.Resource } @@ -203,7 +204,7 @@ func (sv *openShiftClusterStaticValidator) validateNetworkProfile(path string, n } func (sv *openShiftClusterStaticValidator) validateMasterProfile(path string, mp *MasterProfile) error { - if !validate.VMSizeIsValid(api.VMSize(mp.VMSize), sv.developmentMode, true) { + if !validate.VMSizeIsValid(api.VMSize(mp.VMSize), sv.deploymentMode, true) { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".vmSize", "The provided master VM size '%s' is invalid.", mp.VMSize) } if !validate.RxSubnetID.MatchString(mp.SubnetID) { @@ -224,7 +225,7 @@ func (sv *openShiftClusterStaticValidator) validateWorkerProfile(path string, wp if wp.Name != "worker" { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".name", "The provided worker name '%s' is invalid.", wp.Name) } - if !validate.VMSizeIsValid(api.VMSize(wp.VMSize), sv.developmentMode, false) { + if !validate.VMSizeIsValid(api.VMSize(wp.VMSize), sv.deploymentMode, false) { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".vmSize", "The provided worker VM size '%s' is invalid.", wp.VMSize) } if !validate.DiskSizeIsValid(wp.DiskSizeGB) { diff --git a/pkg/api/v20200430/openshiftcluster_validatestatic_test.go b/pkg/api/v20200430/openshiftcluster_validatestatic_test.go index c36111131..ed388cbcb 100644 --- a/pkg/api/v20200430/openshiftcluster_validatestatic_test.go +++ b/pkg/api/v20200430/openshiftcluster_validatestatic_test.go @@ -13,15 +13,16 @@ import ( uuid "github.com/satori/go.uuid" "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/version" "github.com/Azure/ARO-RP/test/validate" ) type validateTest struct { - name string - modify func(oc *OpenShiftCluster) - developmentMode bool - wantErr string + name string + modify func(oc *OpenShiftCluster) + deploymentMode deployment.Mode + wantErr string } type testMode string @@ -100,10 +101,10 @@ func runTests(t *testing.T, mode testMode, tests []*validateTest) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := &openShiftClusterStaticValidator{ - location: "location", - domain: "location.aroapp.io", - developmentMode: tt.developmentMode, - resourceID: id, + location: "location", + domain: "location.aroapp.io", + deploymentMode: tt.deploymentMode, + resourceID: id, r: azure.Resource{ SubscriptionID: subscriptionID, ResourceGroup: "resourceGroup", @@ -485,8 +486,8 @@ func TestOpenShiftClusterStaticValidateWorkerProfile(t *testing.T) { modify: func(oc *OpenShiftCluster) { oc.Properties.WorkerProfiles[0].VMSize = "Standard_D4s_v3" }, - developmentMode: true, - wantErr: "400: InvalidParameter: properties.workerProfiles['worker'].vmSize: The provided worker VM size 'Standard_D4s_v3' is invalid.", + deploymentMode: deployment.Development, + wantErr: "400: InvalidParameter: properties.workerProfiles['worker'].vmSize: The provided worker VM size 'Standard_D4s_v3' is invalid.", }, { name: "disk too small", diff --git a/pkg/api/v20200430/register.go b/pkg/api/v20200430/register.go index 9aaca9980..bf4caae5c 100644 --- a/pkg/api/v20200430/register.go +++ b/pkg/api/v20200430/register.go @@ -5,6 +5,7 @@ package v20200430 import ( "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // APIVersion contains a version string as it will be used by clients @@ -20,12 +21,12 @@ func init() { OpenShiftClusterConverter: func() api.OpenShiftClusterConverter { return &openShiftClusterConverter{} }, - OpenShiftClusterStaticValidator: func(location, domain string, developmentMode bool, resourceID string) api.OpenShiftClusterStaticValidator { + OpenShiftClusterStaticValidator: func(location, domain string, deploymentMode deployment.Mode, resourceID string) api.OpenShiftClusterStaticValidator { return &openShiftClusterStaticValidator{ - location: location, - domain: domain, - developmentMode: developmentMode, - resourceID: resourceID, + location: location, + domain: domain, + deploymentMode: deploymentMode, + resourceID: resourceID, } }, OpenShiftClusterCredentialsConverter: func() api.OpenShiftClusterCredentialsConverter { diff --git a/pkg/api/v20201031preview/openshiftcluster_validatestatic.go b/pkg/api/v20201031preview/openshiftcluster_validatestatic.go index f76904999..15823769c 100644 --- a/pkg/api/v20201031preview/openshiftcluster_validatestatic.go +++ b/pkg/api/v20201031preview/openshiftcluster_validatestatic.go @@ -14,6 +14,7 @@ import ( "github.com/Azure/ARO-RP/pkg/api" "github.com/Azure/ARO-RP/pkg/api/validate" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/immutable" "github.com/Azure/ARO-RP/pkg/util/pullsecret" "github.com/Azure/ARO-RP/pkg/util/subnet" @@ -21,10 +22,10 @@ import ( ) type openShiftClusterStaticValidator struct { - location string - domain string - developmentMode bool - resourceID string + location string + domain string + deploymentMode deployment.Mode + resourceID string r azure.Resource } @@ -203,7 +204,7 @@ func (sv *openShiftClusterStaticValidator) validateNetworkProfile(path string, n } func (sv *openShiftClusterStaticValidator) validateMasterProfile(path string, mp *MasterProfile) error { - if !validate.VMSizeIsValid(api.VMSize(mp.VMSize), sv.developmentMode, true) { + if !validate.VMSizeIsValid(api.VMSize(mp.VMSize), sv.deploymentMode, true) { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".vmSize", "The provided master VM size '%s' is invalid.", mp.VMSize) } if !validate.RxSubnetID.MatchString(mp.SubnetID) { @@ -224,7 +225,7 @@ func (sv *openShiftClusterStaticValidator) validateWorkerProfile(path string, wp if wp.Name != "worker" { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".name", "The provided worker name '%s' is invalid.", wp.Name) } - if !validate.VMSizeIsValid(api.VMSize(wp.VMSize), sv.developmentMode, false) { + if !validate.VMSizeIsValid(api.VMSize(wp.VMSize), sv.deploymentMode, false) { return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, path+".vmSize", "The provided worker VM size '%s' is invalid.", wp.VMSize) } if !validate.DiskSizeIsValid(wp.DiskSizeGB) { diff --git a/pkg/api/v20201031preview/openshiftcluster_validatestatic_test.go b/pkg/api/v20201031preview/openshiftcluster_validatestatic_test.go index 7c9720d4d..790dbcc53 100644 --- a/pkg/api/v20201031preview/openshiftcluster_validatestatic_test.go +++ b/pkg/api/v20201031preview/openshiftcluster_validatestatic_test.go @@ -13,15 +13,16 @@ import ( uuid "github.com/satori/go.uuid" "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/version" "github.com/Azure/ARO-RP/test/validate" ) type validateTest struct { - name string - modify func(oc *OpenShiftCluster) - developmentMode bool - wantErr string + name string + modify func(oc *OpenShiftCluster) + deploymentMode deployment.Mode + wantErr string } type testMode string @@ -100,10 +101,10 @@ func runTests(t *testing.T, mode testMode, tests []*validateTest) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { v := &openShiftClusterStaticValidator{ - location: "location", - domain: "location.aroapp.io", - developmentMode: tt.developmentMode, - resourceID: id, + location: "location", + domain: "location.aroapp.io", + deploymentMode: tt.deploymentMode, + resourceID: id, r: azure.Resource{ SubscriptionID: subscriptionID, ResourceGroup: "resourceGroup", @@ -485,8 +486,8 @@ func TestOpenShiftClusterStaticValidateWorkerProfile(t *testing.T) { modify: func(oc *OpenShiftCluster) { oc.Properties.WorkerProfiles[0].VMSize = "Standard_D4s_v3" }, - developmentMode: true, - wantErr: "400: InvalidParameter: properties.workerProfiles['worker'].vmSize: The provided worker VM size 'Standard_D4s_v3' is invalid.", + deploymentMode: deployment.Development, + wantErr: "400: InvalidParameter: properties.workerProfiles['worker'].vmSize: The provided worker VM size 'Standard_D4s_v3' is invalid.", }, { name: "disk too small", diff --git a/pkg/api/v20201031preview/register.go b/pkg/api/v20201031preview/register.go index c8141fa47..f2b6a731f 100644 --- a/pkg/api/v20201031preview/register.go +++ b/pkg/api/v20201031preview/register.go @@ -5,6 +5,7 @@ package v20201031preview import ( "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // APIVersion contains a version string as it will be used by clients @@ -20,12 +21,12 @@ func init() { OpenShiftClusterConverter: func() api.OpenShiftClusterConverter { return &openShiftClusterConverter{} }, - OpenShiftClusterStaticValidator: func(location, domain string, developmentMode bool, resourceID string) api.OpenShiftClusterStaticValidator { + OpenShiftClusterStaticValidator: func(location, domain string, deploymentMode deployment.Mode, resourceID string) api.OpenShiftClusterStaticValidator { return &openShiftClusterStaticValidator{ - location: location, - domain: domain, - developmentMode: developmentMode, - resourceID: resourceID, + location: location, + domain: domain, + deploymentMode: deploymentMode, + resourceID: resourceID, } }, OpenShiftClusterCredentialsConverter: func() api.OpenShiftClusterCredentialsConverter { diff --git a/pkg/api/validate/vm.go b/pkg/api/validate/vm.go index 60dd351f9..f94536113 100644 --- a/pkg/api/validate/vm.go +++ b/pkg/api/validate/vm.go @@ -5,13 +5,14 @@ package validate import ( "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) func DiskSizeIsValid(sizeGB int) bool { return sizeGB >= 128 } -func VMSizeIsValid(vmSize api.VMSize, developmentMode bool, isMaster bool) bool { +func VMSizeIsValid(vmSize api.VMSize, deploymentMode deployment.Mode, isMaster bool) bool { if isMaster { switch vmSize { case api.VMSizeStandardD8sV3, @@ -20,7 +21,7 @@ func VMSizeIsValid(vmSize api.VMSize, developmentMode bool, isMaster bool) bool return true } } else { - if developmentMode { + if deploymentMode == deployment.Development { switch vmSize { case api.VMSizeStandardD2sV3: return true diff --git a/pkg/backend/openshiftcluster/create.go b/pkg/backend/openshiftcluster/create.go index b86de86ea..6a581fd0e 100644 --- a/pkg/backend/openshiftcluster/create.go +++ b/pkg/backend/openshiftcluster/create.go @@ -34,6 +34,7 @@ import ( "github.com/Azure/ARO-RP/pkg/bootstraplogging" "github.com/Azure/ARO-RP/pkg/cluster" "github.com/Azure/ARO-RP/pkg/util/azureerrors" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/pullsecret" "github.com/Azure/ARO-RP/pkg/util/stringutils" "github.com/Azure/ARO-RP/pkg/util/subnet" @@ -65,7 +66,7 @@ func (m *Manager) Create(ctx context.Context) error { resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/') - if !m.env.IsDevelopment() { + if m.env.DeploymentMode() != deployment.Development { rp := m.acrtoken.GetRegistryProfile(m.doc.OpenShiftCluster) if rp == nil { // 1. choose a name and establish the intent to create a token with diff --git a/pkg/backend/openshiftcluster/delete.go b/pkg/backend/openshiftcluster/delete.go index 089482847..1af6906a1 100644 --- a/pkg/backend/openshiftcluster/delete.go +++ b/pkg/backend/openshiftcluster/delete.go @@ -13,6 +13,7 @@ import ( "github.com/Azure/go-autorest/autorest" "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/stringutils" ) @@ -99,7 +100,7 @@ func (m *Manager) Delete(ctx context.Context) error { return err } - if !m.env.IsDevelopment() { + if m.env.DeploymentMode() != deployment.Development { managedDomain, err := m.env.ManagedDomain(m.doc.OpenShiftCluster.Properties.ClusterProfile.Domain) if err != nil { return err @@ -130,7 +131,7 @@ func (m *Manager) Delete(ctx context.Context) error { return err } - if !m.env.IsDevelopment() { + if m.env.DeploymentMode() != deployment.Development { rp := m.acrtoken.GetRegistryProfile(m.doc.OpenShiftCluster) if rp != nil { err = m.acrtoken.Delete(ctx, rp) diff --git a/pkg/backend/openshiftcluster/openshiftcluster.go b/pkg/backend/openshiftcluster/openshiftcluster.go index 846b3b5cb..aade80916 100644 --- a/pkg/backend/openshiftcluster/openshiftcluster.go +++ b/pkg/backend/openshiftcluster/openshiftcluster.go @@ -16,6 +16,7 @@ import ( "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/features" "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/network" "github.com/Azure/ARO-RP/pkg/util/billing" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/dns" "github.com/Azure/ARO-RP/pkg/util/encryption" "github.com/Azure/ARO-RP/pkg/util/keyvault" @@ -64,7 +65,7 @@ func NewManager(log *logrus.Entry, _env env.Interface, db database.OpenShiftClus } var acrtoken pkgacrtoken.Manager - if !_env.IsDevelopment() { + if _env.DeploymentMode() != deployment.Development { acrtoken, err = pkgacrtoken.NewManager(_env, localFPAuthorizer) if err != nil { return nil, err diff --git a/pkg/cluster/deploystorage.go b/pkg/cluster/deploystorage.go index 35351917c..f7a67698b 100644 --- a/pkg/cluster/deploystorage.go +++ b/pkg/cluster/deploystorage.go @@ -33,6 +33,7 @@ import ( "github.com/Azure/ARO-RP/pkg/util/arm" "github.com/Azure/ARO-RP/pkg/util/azureclient" "github.com/Azure/ARO-RP/pkg/util/azureclient/graphrbac" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/feature" "github.com/Azure/ARO-RP/pkg/util/stringutils" "github.com/Azure/ARO-RP/pkg/util/subnet" @@ -111,7 +112,7 @@ func (i *manager) deployStorageTemplate(ctx context.Context, installConfig *inst Location: &installConfig.Config.Azure.Region, ManagedBy: to.StringPtr(i.doc.OpenShiftCluster.ID), } - if i.env.IsDevelopment() { + if i.env.DeploymentMode() == deployment.Development { group.ManagedBy = nil } _, err := i.groups.CreateOrUpdate(ctx, resourceGroup, group) @@ -189,7 +190,7 @@ func (i *manager) deployStorageTemplate(ctx context.Context, installConfig *inst }, } - if i.env.ShouldDeployDenyAssignment() { + if i.env.DeploymentMode() == deployment.Production { t.Resources = append(t.Resources, i.denyAssignments(clusterSPObjectID)) } @@ -282,7 +283,7 @@ func (i *manager) denyAssignments(clusterSPObjectID string) *arm.Resource { } func (i *manager) deploySnapshotUpgradeTemplate(ctx context.Context) error { - if !i.env.ShouldDeployDenyAssignment() { + if i.env.DeploymentMode() != deployment.Production { // only need this upgrade in production, where there are DenyAssignments return nil } diff --git a/pkg/cluster/samples.go b/pkg/cluster/samples.go index 5a3e749d7..472d6edd6 100644 --- a/pkg/cluster/samples.go +++ b/pkg/cluster/samples.go @@ -11,11 +11,13 @@ import ( configscheme "github.com/openshift/client-go/config/clientset/versioned/scheme" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/util/retry" + + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // disableSamples disables the samples if there's no appropriate pull secret func (i *manager) disableSamples(ctx context.Context) error { - if !i.env.IsDevelopment() && + if i.env.DeploymentMode() != deployment.Development && i.doc.OpenShiftCluster.Properties.ClusterProfile.PullSecret != "" { return nil } @@ -36,7 +38,7 @@ func (i *manager) disableSamples(ctx context.Context) error { // disableOperatorHubSources disables operator hub sources if there's no // appropriate pull secret func (i *manager) disableOperatorHubSources(ctx context.Context) error { - if !i.env.IsDevelopment() && + if i.env.DeploymentMode() != deployment.Development && i.doc.OpenShiftCluster.Properties.ClusterProfile.PullSecret != "" { return nil } diff --git a/pkg/cluster/tls.go b/pkg/cluster/tls.go index cb69066e0..6ce3470b4 100644 --- a/pkg/cluster/tls.go +++ b/pkg/cluster/tls.go @@ -15,12 +15,13 @@ import ( coreclient "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/util/retry" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/keyvault" utilpem "github.com/Azure/ARO-RP/pkg/util/pem" ) func (i *manager) createCertificates(ctx context.Context) error { - if i.env.IsDevelopment() { + if i.env.DeploymentMode() == deployment.Development { return nil } @@ -67,7 +68,7 @@ func (i *manager) createCertificates(ctx context.Context) error { } func (i *manager) upgradeCertificates(ctx context.Context) error { - if i.env.IsDevelopment() { + if i.env.DeploymentMode() == deployment.Development { return nil } @@ -143,7 +144,7 @@ func (i *manager) ensureSecret(ctx context.Context, secrets coreclient.SecretInt } func (i *manager) configureAPIServerCertificate(ctx context.Context) error { - if i.env.IsDevelopment() { + if i.env.DeploymentMode() == deployment.Development { return nil } @@ -184,7 +185,7 @@ func (i *manager) configureAPIServerCertificate(ctx context.Context) error { } func (i *manager) configureIngressCertificate(ctx context.Context) error { - if i.env.IsDevelopment() { + if i.env.DeploymentMode() == deployment.Development { return nil } diff --git a/pkg/env/dev.go b/pkg/env/dev.go index d2c842554..3f8f30340 100644 --- a/pkg/env/dev.go +++ b/pkg/env/dev.go @@ -114,7 +114,6 @@ func newDev(ctx context.Context, log *logrus.Entry, instancemetadata instancemet } d.prod.clustersGenevaLoggingEnvironment = "Test" d.prod.clustersGenevaLoggingConfigVersion = "2.3" - d.prod.envType = environmentTypeDevelopment fpGraphAuthorizer, err := d.FPAuthorizer(instancemetadata.TenantID(), azure.PublicCloud.GraphEndpoint) if err != nil { @@ -301,7 +300,3 @@ func (d *dev) E2EStorageAccountRGName() string { func (d *dev) E2EStorageAccountSubID() string { return "0cc1cafa-578f-4fa5-8d6b-ddfd8d82e6ea" } - -func (d *dev) ShouldDeployDenyAssignment() bool { - return false -} diff --git a/pkg/env/env.go b/pkg/env/env.go index d1cd10c2d..9defd5137 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -8,26 +8,17 @@ import ( "crypto/rsa" "crypto/x509" "net" - "os" - "strings" "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure/auth" "github.com/sirupsen/logrus" "github.com/Azure/ARO-RP/pkg/util/clientauthorizer" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/instancemetadata" "github.com/Azure/ARO-RP/pkg/util/refreshable" ) -type environmentType uint8 - -const ( - environmentTypeProduction environmentType = iota - environmentTypeDevelopment - environmentTypeIntegration -) - const ( RPFirstPartySecretName = "rp-firstparty" RPServerSecretName = "rp-server" @@ -39,9 +30,9 @@ const ( ) type Interface interface { + DeploymentMode() deployment.Mode instancemetadata.InstanceMetadata - IsDevelopment() bool InitializeAuthorizers() error ArmClientAuthorizer() clientauthorizer.ClientAuthorizer AdminClientAuthorizer() clientauthorizer.ClientAuthorizer @@ -67,11 +58,10 @@ type Interface interface { E2EStorageAccountName() string E2EStorageAccountRGName() string E2EStorageAccountSubID() string - ShouldDeployDenyAssignment() bool } func NewEnv(ctx context.Context, log *logrus.Entry) (Interface, error) { - if strings.ToLower(os.Getenv("RP_MODE")) == "development" { + if deployment.NewMode() == deployment.Development { log.Warn("running in development mode") return newDev(ctx, log, instancemetadata.NewDev()) } @@ -81,7 +71,7 @@ func NewEnv(ctx context.Context, log *logrus.Entry) (Interface, error) { return nil, err } - if strings.ToLower(os.Getenv("RP_MODE")) == "int" { + if deployment.NewMode() == deployment.Integration { log.Warn("running in int mode") return newInt(ctx, log, im) } diff --git a/pkg/env/env_test.go b/pkg/env/env_test.go deleted file mode 100644 index c7e2193d8..000000000 --- a/pkg/env/env_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package env - -// Copyright (c) Microsoft Corporation. -// Licensed under the Apache License 2.0. - -import "testing" - -func TestEnvironmentType(t *testing.T) { - p := &prod{envType: environmentTypeProduction} - out := p.IsDevelopment() - if out != false { - t.Fatal("didn't return expected value") - } - - p = &prod{envType: environmentTypeIntegration} - out = p.IsDevelopment() - if out != false { - t.Fatal("didn't return expected value") - } - - p = &prod{envType: environmentTypeDevelopment} - out = p.IsDevelopment() - if out != true { - t.Fatal("didn't return expected value") - } -} - -func TestEnvironmentShouldDeployDenyAssignment(t *testing.T) { - p := &prod{envType: environmentTypeProduction} - out := p.ShouldDeployDenyAssignment() - if out != true { - t.Fatal("didn't return expected value") - } - - p = &prod{envType: environmentTypeIntegration} - out = p.ShouldDeployDenyAssignment() - if out != false { - t.Fatal("didn't return expected value") - } - - p = &prod{envType: environmentTypeDevelopment} - out = p.ShouldDeployDenyAssignment() - if out != false { - t.Fatal("didn't return expected value") - } -} diff --git a/pkg/env/int.go b/pkg/env/int.go index 6a4191c7f..ddb489001 100644 --- a/pkg/env/int.go +++ b/pkg/env/int.go @@ -36,7 +36,6 @@ func newInt(ctx context.Context, log *logrus.Entry, instancemetadata instancemet p.e2eStorageAccountName = "arov4e2eint" p.e2eStorageAccountRGName = "global-infra" p.e2eStorageAccountSubID = "0cc1cafa-578f-4fa5-8d6b-ddfd8d82e6ea" - p.envType = environmentTypeIntegration return p, nil } diff --git a/pkg/env/prod.go b/pkg/env/prod.go index 6a6f43034..4d51d6f49 100644 --- a/pkg/env/prod.go +++ b/pkg/env/prod.go @@ -26,6 +26,7 @@ import ( "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/documentdb" "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/keyvault" "github.com/Azure/ARO-RP/pkg/util/clientauthorizer" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/instancemetadata" "github.com/Azure/ARO-RP/pkg/util/pem" "github.com/Azure/ARO-RP/pkg/util/refreshable" @@ -34,6 +35,9 @@ import ( type prod struct { instancemetadata.InstanceMetadata + + deploymentMode deployment.Mode + armClientAuthorizer clientauthorizer.ClientAuthorizer adminClientAuthorizer clientauthorizer.ClientAuthorizer @@ -60,21 +64,21 @@ type prod struct { e2eStorageAccountRGName string e2eStorageAccountSubID string - log *logrus.Entry - envType environmentType + log *logrus.Entry } func newProd(ctx context.Context, log *logrus.Entry, instancemetadata instancemetadata.InstanceMetadata, rpAuthorizer, rpKVAuthorizer autorest.Authorizer) (*prod, error) { p := &prod{ InstanceMetadata: instancemetadata, + deploymentMode: deployment.NewMode(), + keyvault: basekeyvault.New(rpKVAuthorizer), clustersGenevaLoggingEnvironment: "DiagnosticsProd", clustersGenevaLoggingConfigVersion: "2.2", - log: log, - envType: environmentTypeProduction, + log: log, } err := p.populateCosmosDB(ctx, rpAuthorizer) @@ -131,6 +135,10 @@ func newProd(ctx context.Context, log *logrus.Entry, instancemetadata instanceme return p, nil } +func (p *prod) DeploymentMode() deployment.Mode { + return p.deploymentMode +} + func (p *prod) InitializeAuthorizers() error { p.armClientAuthorizer = clientauthorizer.NewARM(p.log) @@ -394,11 +402,3 @@ func (p *prod) E2EStorageAccountRGName() string { func (p *prod) E2EStorageAccountSubID() string { return p.e2eStorageAccountSubID } - -func (p *prod) ShouldDeployDenyAssignment() bool { - return p.envType == environmentTypeProduction -} - -func (p *prod) IsDevelopment() bool { - return p.envType == environmentTypeDevelopment -} diff --git a/pkg/env/test.go b/pkg/env/test.go index bf4c4101f..b26e03a23 100644 --- a/pkg/env/test.go +++ b/pkg/env/test.go @@ -11,11 +11,13 @@ import ( "net" "github.com/Azure/ARO-RP/pkg/util/clientauthorizer" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/refreshable" ) type Test struct { *prod + deployment.Mode L net.Listener diff --git a/pkg/frontend/frontend.go b/pkg/frontend/frontend.go index f58d6c485..32653c761 100644 --- a/pkg/frontend/frontend.go +++ b/pkg/frontend/frontend.go @@ -253,7 +253,7 @@ func (f *frontend) setupRouter() *mux.Router { r.Use(middleware.Log(f.baseLog.WithField("component", "access"))) r.Use(middleware.Metrics(f.m)) r.Use(middleware.Panic) - r.Use(middleware.Headers(f.env)) + r.Use(middleware.Headers(f.env.DeploymentMode())) r.Use(middleware.Validate(f.env, f.apis)) r.Use(middleware.Body) diff --git a/pkg/frontend/frontend_test.go b/pkg/frontend/frontend_test.go index 92a12c8bc..aeda0d532 100644 --- a/pkg/frontend/frontend_test.go +++ b/pkg/frontend/frontend_test.go @@ -14,12 +14,15 @@ import ( "strings" "testing" + "github.com/golang/mock/gomock" "github.com/gorilla/mux" "github.com/sirupsen/logrus" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" + mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env" testlog "github.com/Azure/ARO-RP/test/util/log" ) @@ -166,8 +169,15 @@ func TestAdminReply(t *testing.T) { } func TestRoutesAreNamedWithLowerCasePaths(t *testing.T) { + controller := gomock.NewController(t) + defer controller.Finish() + + _env := mock_env.NewMockInterface(controller) + _env.EXPECT().DeploymentMode().AnyTimes().Return(deployment.Production) + f := &frontend{ baseLog: logrus.NewEntry(logrus.StandardLogger()), + env: _env, } router := f.setupRouter() diff --git a/pkg/frontend/middleware/headers.go b/pkg/frontend/middleware/headers.go index 5c75f540a..899b8d345 100644 --- a/pkg/frontend/middleware/headers.go +++ b/pkg/frontend/middleware/headers.go @@ -7,10 +7,10 @@ import ( "net/http" "strings" - "github.com/Azure/ARO-RP/pkg/env" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) -func Headers(_env env.Interface) func(http.Handler) http.Handler { +func Headers(deploymentMode deployment.Mode) func(http.Handler) http.Handler { return func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -19,7 +19,7 @@ func Headers(_env env.Interface) func(http.Handler) http.Handler { w.Header().Set("X-Ms-Client-Request-Id", r.Header.Get("X-Ms-Client-Request-Id")) } - if _env.IsDevelopment() { + if deploymentMode == deployment.Development { r.Header.Set("Referer", "https://localhost:8443"+r.URL.String()) } diff --git a/pkg/frontend/openshiftcluster_putorpatch.go b/pkg/frontend/openshiftcluster_putorpatch.go index fcb390fe1..f24469edd 100644 --- a/pkg/frontend/openshiftcluster_putorpatch.go +++ b/pkg/frontend/openshiftcluster_putorpatch.go @@ -25,8 +25,6 @@ import ( ) func (f *frontend) putOrPatchOpenShiftCluster(w http.ResponseWriter, r *http.Request) { - developmentMode := f.env.IsDevelopment() - ctx := r.Context() log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry) vars := mux.Vars(r) @@ -35,7 +33,7 @@ func (f *frontend) putOrPatchOpenShiftCluster(w http.ResponseWriter, r *http.Req var b []byte err := cosmosdb.RetryOnPreconditionFailed(func() error { var err error - b, err = f._putOrPatchOpenShiftCluster(ctx, r, &header, f.apis[vars["api-version"]].OpenShiftClusterConverter(), f.apis[vars["api-version"]].OpenShiftClusterStaticValidator(f.env.Location(), f.env.Domain(), developmentMode, r.URL.Path)) + b, err = f._putOrPatchOpenShiftCluster(ctx, r, &header, f.apis[vars["api-version"]].OpenShiftClusterConverter(), f.apis[vars["api-version"]].OpenShiftClusterStaticValidator(f.env.Location(), f.env.Domain(), f.env.DeploymentMode(), r.URL.Path)) return err }) diff --git a/pkg/frontend/openshiftcluster_putorpatch_test.go b/pkg/frontend/openshiftcluster_putorpatch_test.go index a649aecfd..fc9823bd9 100644 --- a/pkg/frontend/openshiftcluster_putorpatch_test.go +++ b/pkg/frontend/openshiftcluster_putorpatch_test.go @@ -20,6 +20,7 @@ import ( "github.com/Azure/ARO-RP/pkg/database/cosmosdb" "github.com/Azure/ARO-RP/pkg/metrics/noop" "github.com/Azure/ARO-RP/pkg/util/bucket" + "github.com/Azure/ARO-RP/pkg/util/deployment" mock_database "github.com/Azure/ARO-RP/pkg/util/mocks/database" "github.com/Azure/ARO-RP/test/util/matcher" ) @@ -49,7 +50,7 @@ func TestPutOrPatchOpenShiftClusterAdminAPI(t *testing.T) { apis := map[string]*api.Version{ "admin": { OpenShiftClusterConverter: api.APIs["admin"].OpenShiftClusterConverter, - OpenShiftClusterStaticValidator: func(string, string, bool, string) api.OpenShiftClusterStaticValidator { + OpenShiftClusterStaticValidator: func(string, string, deployment.Mode, string) api.OpenShiftClusterStaticValidator { return &dummyOpenShiftClusterValidator{} }, OpenShiftClusterCredentialsConverter: api.APIs["admin"].OpenShiftClusterCredentialsConverter, @@ -293,7 +294,7 @@ func TestPutOrPatchOpenShiftCluster(t *testing.T) { apis := map[string]*api.Version{ "2020-04-30": { OpenShiftClusterConverter: api.APIs["2020-04-30"].OpenShiftClusterConverter, - OpenShiftClusterStaticValidator: func(string, string, bool, string) api.OpenShiftClusterStaticValidator { + OpenShiftClusterStaticValidator: func(string, string, deployment.Mode, string) api.OpenShiftClusterStaticValidator { return &dummyOpenShiftClusterValidator{} }, OpenShiftClusterCredentialsConverter: api.APIs["2020-04-30"].OpenShiftClusterCredentialsConverter, diff --git a/pkg/frontend/ready_get.go b/pkg/frontend/ready_get.go index cdc9c984e..d9bba7eb4 100644 --- a/pkg/frontend/ready_get.go +++ b/pkg/frontend/ready_get.go @@ -8,6 +8,7 @@ import ( "time" "github.com/Azure/ARO-RP/pkg/api" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // checkReady checks the ready status of the frontend to make it consistent @@ -15,7 +16,7 @@ import ( // minutes before indicating health. This ensures that there will be a gap in // our health metric if we crash or restart. func (f *frontend) checkReady() bool { - if !f.env.IsDevelopment() && + if f.env.DeploymentMode() != deployment.Development && time.Now().Sub(f.startTime) < 2*time.Minute { return false } diff --git a/pkg/frontend/security_test.go b/pkg/frontend/security_test.go index 1fdab1ebc..38edebad4 100644 --- a/pkg/frontend/security_test.go +++ b/pkg/frontend/security_test.go @@ -18,6 +18,7 @@ import ( "github.com/Azure/ARO-RP/pkg/env" "github.com/Azure/ARO-RP/pkg/metrics/noop" "github.com/Azure/ARO-RP/pkg/util/clientauthorizer" + "github.com/Azure/ARO-RP/pkg/util/deployment" utiltls "github.com/Azure/ARO-RP/pkg/util/tls" "github.com/Azure/ARO-RP/test/util/listener" ) @@ -39,7 +40,8 @@ func TestSecurity(t *testing.T) { defer l.Close() env := &env.Test{ - L: l, + Mode: deployment.Production, + L: l, } env.SetARMClientAuthorizer(clientauthorizer.NewOne(validclientcerts[0].Raw)) env.SetAdminClientAuthorizer(clientauthorizer.NewOne(validadminclientcerts[0].Raw)) diff --git a/pkg/frontend/shared_test.go b/pkg/frontend/shared_test.go index a7249e9f4..4944ea76a 100644 --- a/pkg/frontend/shared_test.go +++ b/pkg/frontend/shared_test.go @@ -21,6 +21,7 @@ import ( "github.com/Azure/ARO-RP/pkg/api" "github.com/Azure/ARO-RP/pkg/env" "github.com/Azure/ARO-RP/pkg/util/clientauthorizer" + "github.com/Azure/ARO-RP/pkg/util/deployment" utiltls "github.com/Azure/ARO-RP/pkg/util/tls" testclusterdata "github.com/Azure/ARO-RP/test/util/clusterdata" "github.com/Azure/ARO-RP/test/util/listener" @@ -61,6 +62,7 @@ func newTestInfra(t *testing.T) (*testInfra, error) { env := &env.Test{ L: l, + Mode: deployment.Production, TestLocation: "eastus", TLSKey: serverkey, TLSCerts: servercerts, diff --git a/pkg/metrics/statsd/statsd.go b/pkg/metrics/statsd/statsd.go index edc346392..d8b8f1291 100644 --- a/pkg/metrics/statsd/statsd.go +++ b/pkg/metrics/statsd/statsd.go @@ -15,6 +15,7 @@ import ( "github.com/Azure/ARO-RP/pkg/env" "github.com/Azure/ARO-RP/pkg/metrics" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/recover" ) @@ -129,7 +130,7 @@ func (s *statsd) write(m *metric) (err error) { if s.conn == nil { err = s.dial() if err != nil { - if s.env.IsDevelopment() { + if s.env.DeploymentMode() == deployment.Development { err = nil } return diff --git a/pkg/operator/controllers/checker/checker_controller.go b/pkg/operator/controllers/checker/checker_controller.go index ca30fca40..554b00655 100644 --- a/pkg/operator/controllers/checker/checker_controller.go +++ b/pkg/operator/controllers/checker/checker_controller.go @@ -18,6 +18,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/typed/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // CheckerController runs a number of checkers @@ -27,11 +28,11 @@ type CheckerController struct { checkers []Checker } -func NewReconciler(log *logrus.Entry, clustercli clusterapi.Interface, arocli aroclient.AroV1alpha1Interface, role string, developmentMode bool) *CheckerController { +func NewReconciler(log *logrus.Entry, clustercli clusterapi.Interface, arocli aroclient.AroV1alpha1Interface, role string, deploymentMode deployment.Mode) *CheckerController { checkers := []Checker{NewInternetChecker(log, arocli, role)} if role == operator.RoleMaster { - checkers = append(checkers, NewMachineChecker(log, clustercli, arocli, role, developmentMode)) + checkers = append(checkers, NewMachineChecker(log, clustercli, arocli, role, deploymentMode)) } return &CheckerController{ diff --git a/pkg/operator/controllers/checker/machinechecker.go b/pkg/operator/controllers/checker/machinechecker.go index b13d3d991..e0bce4955 100644 --- a/pkg/operator/controllers/checker/machinechecker.go +++ b/pkg/operator/controllers/checker/machinechecker.go @@ -22,6 +22,7 @@ import ( aro "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/typed/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers" + "github.com/Azure/ARO-RP/pkg/util/deployment" _ "github.com/Azure/ARO-RP/pkg/util/scheme" ) @@ -31,20 +32,20 @@ const ( // MachineChecker reconciles the alertmanager webhook type MachineChecker struct { - clustercli clusterapi.Interface - arocli aroclient.AroV1alpha1Interface - log *logrus.Entry - developmentMode bool - role string + clustercli clusterapi.Interface + arocli aroclient.AroV1alpha1Interface + log *logrus.Entry + deploymentMode deployment.Mode + role string } -func NewMachineChecker(log *logrus.Entry, clustercli clusterapi.Interface, arocli aroclient.AroV1alpha1Interface, role string, developmentMode bool) *MachineChecker { +func NewMachineChecker(log *logrus.Entry, clustercli clusterapi.Interface, arocli aroclient.AroV1alpha1Interface, role string, deploymentMode deployment.Mode) *MachineChecker { return &MachineChecker{ - clustercli: clustercli, - arocli: arocli, - log: log, - role: role, - developmentMode: developmentMode, + clustercli: clustercli, + arocli: arocli, + log: log, + deploymentMode: deploymentMode, + role: role, } } @@ -79,7 +80,7 @@ func (r *MachineChecker) machineValid(ctx context.Context, machine *machinev1bet return []error{fmt.Errorf("machine %s: failed to read provider spec: %T", machine.Name, o)} } - if !validate.VMSizeIsValid(api.VMSize(machineProviderSpec.VMSize), r.developmentMode, isMaster) { + if !validate.VMSizeIsValid(api.VMSize(machineProviderSpec.VMSize), r.deploymentMode, isMaster) { errs = append(errs, fmt.Errorf("machine %s: invalid VM size '%s'", machine.Name, machineProviderSpec.VMSize)) } diff --git a/pkg/operator/deploy/deploy.go b/pkg/operator/deploy/deploy.go index b691e72ff..252ad340d 100644 --- a/pkg/operator/deploy/deploy.go +++ b/pkg/operator/deploy/deploy.go @@ -28,6 +28,7 @@ import ( arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" aroclient "github.com/Azure/ARO-RP/pkg/operator/clientset/versioned/typed/aro.openshift.io/v1alpha1" "github.com/Azure/ARO-RP/pkg/operator/controllers/genevalogging" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/dynamichelper" "github.com/Azure/ARO-RP/pkg/util/pullsecret" "github.com/Azure/ARO-RP/pkg/util/ready" @@ -92,7 +93,7 @@ func (o *operator) resources() ([]runtime.Object, error) { for i := range d.Spec.Template.Spec.Containers { d.Spec.Template.Spec.Containers[i].Image = o.env.AROOperatorImage() - if o.env.IsDevelopment() { + if o.env.DeploymentMode() == deployment.Development { d.Spec.Template.Spec.Containers[i].Env = append(d.Spec.Template.Spec.Containers[i].Env, corev1.EnvVar{ Name: "RP_MODE", Value: "development", diff --git a/pkg/util/azureclient/mgmt/redhatopenshift/openshiftclusters.go b/pkg/util/azureclient/mgmt/redhatopenshift/openshiftclusters.go index 7536face1..387a88a40 100644 --- a/pkg/util/azureclient/mgmt/redhatopenshift/openshiftclusters.go +++ b/pkg/util/azureclient/mgmt/redhatopenshift/openshiftclusters.go @@ -7,12 +7,12 @@ import ( "context" "crypto/tls" "net/http" - "os" "time" "github.com/Azure/go-autorest/autorest" "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2020-04-30/redhatopenshift" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // OpenShiftClustersClient is a minimal interface for azure OpenshiftClustersClient @@ -31,7 +31,7 @@ var _ OpenShiftClustersClient = &openShiftClustersClient{} // NewOpenShiftClustersClient creates a new OpenShiftClustersClient func NewOpenShiftClustersClient(subscriptionID string, authorizer autorest.Authorizer) OpenShiftClustersClient { var client redhatopenshift.OpenShiftClustersClient - if os.Getenv("RP_MODE") == "development" { + if deployment.NewMode() == deployment.Development { client = redhatopenshift.NewOpenShiftClustersClientWithBaseURI("https://localhost:8443", subscriptionID) client.Sender = &http.Client{ Transport: &http.Transport{ diff --git a/pkg/util/azureclient/mgmt/redhatopenshift/operations.go b/pkg/util/azureclient/mgmt/redhatopenshift/operations.go index d2116723a..8f870caa2 100644 --- a/pkg/util/azureclient/mgmt/redhatopenshift/operations.go +++ b/pkg/util/azureclient/mgmt/redhatopenshift/operations.go @@ -6,11 +6,11 @@ package redhatopenshift import ( "crypto/tls" "net/http" - "os" "github.com/Azure/go-autorest/autorest" "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2020-04-30/redhatopenshift" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) // OperationsClient is a minimal interface for azure OperationsClient @@ -27,7 +27,7 @@ var _ OperationsClient = &operationsClient{} // NewOperationsClient creates a new OperationsClient func NewOperationsClient(subscriptionID string, authorizer autorest.Authorizer) OperationsClient { var client redhatopenshift.OperationsClient - if os.Getenv("RP_MODE") == "development" { + if deployment.NewMode() == deployment.Development { client = redhatopenshift.NewOperationsClientWithBaseURI("https://localhost:8443", subscriptionID) client.Sender = &http.Client{ Transport: &http.Transport{ diff --git a/pkg/util/billing/billing.go b/pkg/util/billing/billing.go index 0a69bb80d..3813eefb1 100644 --- a/pkg/util/billing/billing.go +++ b/pkg/util/billing/billing.go @@ -19,6 +19,7 @@ import ( "github.com/Azure/ARO-RP/pkg/database/cosmosdb" "github.com/Azure/ARO-RP/pkg/env" "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/storage" + "github.com/Azure/ARO-RP/pkg/util/deployment" "github.com/Azure/ARO-RP/pkg/util/feature" ) @@ -48,7 +49,7 @@ type manager struct { func NewManager(_env env.Interface, billing database.Billing, sub database.Subscriptions, log *logrus.Entry) (Manager, error) { var storageClient *azstorage.Client - if !_env.IsDevelopment() { + if _env.DeploymentMode() != deployment.Development { localFPAuthorizer, err := _env.FPAuthorizer(_env.TenantID(), azure.PublicCloud.ResourceManagerEndpoint) if err != nil { return nil, err @@ -137,7 +138,7 @@ func isSubscriptionRegisteredForE2E(sub *api.SubscriptionProperties) bool { // storage account. This is used later on by the billing e2e func (m *manager) createOrUpdateE2EBlob(ctx context.Context, doc *api.BillingDocument) error { //skip updating the storage account if this is a dev scenario - if m.env.IsDevelopment() { + if m.env.DeploymentMode() == deployment.Development { return nil } diff --git a/pkg/util/billing/billing_test.go b/pkg/util/billing/billing_test.go index 553ef66d0..46a5237f9 100644 --- a/pkg/util/billing/billing_test.go +++ b/pkg/util/billing/billing_test.go @@ -15,6 +15,7 @@ import ( "github.com/Azure/ARO-RP/pkg/api" "github.com/Azure/ARO-RP/pkg/database/cosmosdb" + "github.com/Azure/ARO-RP/pkg/util/deployment" mock_database "github.com/Azure/ARO-RP/pkg/util/mocks/database" mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env" ) @@ -237,7 +238,7 @@ func TestDelete(t *testing.T) { defer controller.Finish() _env := mock_env.NewMockInterface(controller) - _env.EXPECT().IsDevelopment().AnyTimes().Return(false) + _env.EXPECT().DeploymentMode().AnyTimes().Return(deployment.Production) log := logrus.NewEntry(logrus.StandardLogger()) @@ -403,7 +404,7 @@ func TestEnsure(t *testing.T) { defer controller.Finish() _env := mock_env.NewMockInterface(controller) - _env.EXPECT().IsDevelopment().AnyTimes().Return(false) + _env.EXPECT().DeploymentMode().AnyTimes().Return(deployment.Production) log := logrus.NewEntry(logrus.StandardLogger()) diff --git a/pkg/util/deployment/deployment.go b/pkg/util/deployment/deployment.go new file mode 100644 index 000000000..cfcbd8904 --- /dev/null +++ b/pkg/util/deployment/deployment.go @@ -0,0 +1,28 @@ +package deployment + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +import ( + "os" + "strings" +) + +type Mode int + +const ( + Production Mode = iota + Integration + Development +) + +func NewMode() Mode { + switch strings.ToLower(os.Getenv("RP_MODE")) { + case "development": + return Development + case "int": + return Integration + default: + return Production + } +} diff --git a/pkg/util/mocks/env/env.go b/pkg/util/mocks/env/env.go index bbf0fdd8a..800b29400 100644 --- a/pkg/util/mocks/env/env.go +++ b/pkg/util/mocks/env/env.go @@ -14,6 +14,7 @@ import ( gomock "github.com/golang/mock/gomock" clientauthorizer "github.com/Azure/ARO-RP/pkg/util/clientauthorizer" + deployment "github.com/Azure/ARO-RP/pkg/util/deployment" refreshable "github.com/Azure/ARO-RP/pkg/util/refreshable" ) @@ -210,6 +211,20 @@ func (mr *MockInterfaceMockRecorder) DatabaseName() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DatabaseName", reflect.TypeOf((*MockInterface)(nil).DatabaseName)) } +// DeploymentMode mocks base method +func (m *MockInterface) DeploymentMode() deployment.Mode { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeploymentMode") + ret0, _ := ret[0].(deployment.Mode) + return ret0 +} + +// DeploymentMode indicates an expected call of DeploymentMode +func (mr *MockInterfaceMockRecorder) DeploymentMode() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeploymentMode", reflect.TypeOf((*MockInterface)(nil).DeploymentMode)) +} + // DialContext mocks base method func (m *MockInterface) DialContext(arg0 context.Context, arg1, arg2 string) (net.Conn, error) { m.ctrl.T.Helper() @@ -341,20 +356,6 @@ func (mr *MockInterfaceMockRecorder) InitializeAuthorizers() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InitializeAuthorizers", reflect.TypeOf((*MockInterface)(nil).InitializeAuthorizers)) } -// IsDevelopment mocks base method -func (m *MockInterface) IsDevelopment() bool { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "IsDevelopment") - ret0, _ := ret[0].(bool) - return ret0 -} - -// IsDevelopment indicates an expected call of IsDevelopment -func (mr *MockInterfaceMockRecorder) IsDevelopment() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsDevelopment", reflect.TypeOf((*MockInterface)(nil).IsDevelopment)) -} - // Listen mocks base method func (m *MockInterface) Listen() (net.Listener, error) { m.ctrl.T.Helper() @@ -427,20 +428,6 @@ func (mr *MockInterfaceMockRecorder) ResourceGroup() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResourceGroup", reflect.TypeOf((*MockInterface)(nil).ResourceGroup)) } -// ShouldDeployDenyAssignment mocks base method -func (m *MockInterface) ShouldDeployDenyAssignment() bool { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ShouldDeployDenyAssignment") - ret0, _ := ret[0].(bool) - return ret0 -} - -// ShouldDeployDenyAssignment indicates an expected call of ShouldDeployDenyAssignment -func (mr *MockInterfaceMockRecorder) ShouldDeployDenyAssignment() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ShouldDeployDenyAssignment", reflect.TypeOf((*MockInterface)(nil).ShouldDeployDenyAssignment)) -} - // SubscriptionID mocks base method func (m *MockInterface) SubscriptionID() string { m.ctrl.T.Helper() diff --git a/test/e2e/adminapi.go b/test/e2e/adminapi.go index 41d605e33..dfa4ad3df 100644 --- a/test/e2e/adminapi.go +++ b/test/e2e/adminapi.go @@ -12,15 +12,15 @@ import ( "io/ioutil" "net/http" "net/url" - "os" . "github.com/onsi/gomega" "github.com/Azure/ARO-RP/pkg/api/admin" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) func adminRequest(ctx context.Context, method, path string, params url.Values, in, out interface{}) (*http.Response, error) { - if os.Getenv("RP_MODE") != "development" { + if deployment.NewMode() != deployment.Development { return nil, errors.New("only development RP mode is supported") } diff --git a/test/e2e/setup.go b/test/e2e/setup.go index 45c35f509..3bdebf9df 100644 --- a/test/e2e/setup.go +++ b/test/e2e/setup.go @@ -23,6 +23,7 @@ import ( "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/features" "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/insights" "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift" + "github.com/Azure/ARO-RP/pkg/util/deployment" ) type clientSet struct { @@ -44,7 +45,7 @@ var ( ) func skipIfNotInDevelopmentEnv() { - if os.Getenv("RP_MODE") != "development" { + if deployment.NewMode() != deployment.Development { Skip("skipping tests in non-development environment") } }