Skip to content

Commit

Permalink
chore: remove usage of module autorest/azure (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
christinalau0 authored Jun 5, 2024
1 parent 3e29e0a commit 6bc4f90
Show file tree
Hide file tree
Showing 29 changed files with 172 additions and 100 deletions.
9 changes: 8 additions & 1 deletion cmd/addpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ func (apc *addPoolCmd) load() error {
return err
}

if apc.client, err = apc.authArgs.getClient(); err != nil {
// Set env var if custom cloud profile is not nil
var env *api.Environment
if apc.containerService != nil &&
apc.containerService.Properties != nil &&
apc.containerService.Properties.CustomCloudProfile != nil {
env = apc.containerService.Properties.CustomCloudProfile.Environment
}
if apc.client, err = apc.authArgs.getClient(env); err != nil {
return errors.Wrap(err, "failed to get client")
}

Expand Down
9 changes: 8 additions & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,14 @@ func (dc *deployCmd) loadAPIModel() error {
return err
}

dc.client, err = dc.authProvider.getClient()
// Set env var if custom cloud profile is not nil
var env *api.Environment
if dc.containerService != nil &&
dc.containerService.Properties != nil &&
dc.containerService.Properties.CustomCloudProfile != nil {
env = dc.containerService.Properties.CustomCloudProfile.Environment
}
dc.client, err = dc.authProvider.getClient(env)
if err != nil {
return errors.Wrap(err, "failed to get client")
}
Expand Down
22 changes: 15 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/google/uuid"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -103,7 +102,7 @@ func writeDefaultModel(out io.Writer) error {

type authProvider interface {
getAuthArgs() *authArgs
getClient() (armhelpers.AKSEngineClient, error)
getClient(env *api.Environment) (armhelpers.AKSEngineClient, error)
}

type authArgs struct {
Expand Down Expand Up @@ -175,7 +174,17 @@ func (authArgs *authArgs) validateAuthArgs() error {
authArgs.SubscriptionID = subID
}

if _, err = azure.EnvironmentFromName(authArgs.RawAzureEnvironment); err != nil {
switch strings.ToUpper(authArgs.RawAzureEnvironment) {
case "AZURESTACKCLOUD":
// Azure stack cloud environment, verify file path can be read
fileName := os.Getenv("AZURE_ENVIRONMENT_FILEPATH")
if fileContents, err := os.ReadFile(fileName); err != nil ||
json.Unmarshal(fileContents, &api.Environment{}) != nil {
return errors.New(fmt.Sprintf("failed to read file or unmarshal JSON from file %s: %v", fileName, err))
}
case "AZURECHINACLOUD", "AZUREGERMANCLOUD", "AZUREPUBLICCLOUD", "AZUREUSGOVERNMENTCLOUD":
// Known environment, no action needed
default:
return errors.New("failed to parse --azure-env as a valid target Azure cloud environment")
}
return nil
Expand Down Expand Up @@ -220,7 +229,7 @@ func getCloudSubFromAzConfig(cloud string, f *ini.File) (uuid.UUID, error) {
return uuid.Parse(sub.String())
}

func (authArgs *authArgs) getClient() (armhelpers.AKSEngineClient, error) {
func (authArgs *authArgs) getClient(env *api.Environment) (armhelpers.AKSEngineClient, error) {
var cc cloud.Configuration
switch authArgs.RawAzureEnvironment {
case api.AzureUSGovernmentCloud:
Expand All @@ -231,9 +240,8 @@ func (authArgs *authArgs) getClient() (armhelpers.AKSEngineClient, error) {
cc = cloud.AzurePublic
}
if authArgs.isAzureStackCloud() {
env, err := azure.EnvironmentFromName(authArgs.RawAzureEnvironment)
if err != nil {
return nil, err
if env == nil {
return nil, errors.New("failed to get azure stack cloud client, API model Properties.CustomCloudProfile.Environment cannot be nil")
}
cc = cloud.Configuration{
ActiveDirectoryAuthorityHost: env.ActiveDirectoryEndpoint,
Expand Down
5 changes: 2 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/Azure/aks-engine-azurestack/pkg/armhelpers"
"github.com/Azure/aks-engine-azurestack/pkg/helpers"
"github.com/Azure/aks-engine-azurestack/pkg/i18n"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/google/uuid"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
Expand All @@ -25,7 +24,7 @@ type mockAuthProvider struct {
*authArgs
}

func (provider *mockAuthProvider) getClient() (armhelpers.AKSEngineClient, error) {
func (provider *mockAuthProvider) getClient(env *api.Environment) (armhelpers.AKSEngineClient, error) {
if provider.getClientMock == nil {
return &armhelpers.MockAKSEngineClient{}, nil
}
Expand Down Expand Up @@ -370,7 +369,7 @@ func prepareCustomCloudProfile() (*api.ContainerService, error) {
CustomCloudProfile: &api.CustomCloudProfile{
IdentitySystem: api.AzureADIdentitySystem,
AuthenticationMethod: api.ClientSecretAuthMethod,
Environment: &azure.Environment{
Environment: &api.Environment{
Name: name,
ManagementPortalURL: managementPortalURL,
PublishSettingsURL: publishSettingsURL,
Expand Down
9 changes: 8 additions & 1 deletion cmd/rotate_certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,14 @@ func (rcc *rotateCertsCmd) loadAPIModel() (err error) {
if err = rcc.getAuthArgs().validateAuthArgs(); err != nil {
return errors.Wrap(err, "failed to get validate auth args")
}
armClient, err := rcc.authProvider.getClient()
// Set env var if custom cloud profile is not nil
var env *api.Environment
if rcc.cs != nil &&
rcc.cs.Properties != nil &&
rcc.cs.Properties.CustomCloudProfile != nil {
env = rcc.cs.Properties.CustomCloudProfile.Environment
}
armClient, err := rcc.authProvider.getClient(env)
if err != nil {
return errors.Wrap(err, "failed to get ARM client")
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ func (sc *scaleCmd) load() error {
return err
}

if sc.client, err = sc.authArgs.getClient(); err != nil {
// Set env var if custom cloud profile is not nil
var env *api.Environment
if sc.containerService != nil &&
sc.containerService.Properties != nil &&
sc.containerService.Properties.CustomCloudProfile != nil {
env = sc.containerService.Properties.CustomCloudProfile.Environment
}
if sc.client, err = sc.authArgs.getClient(env); err != nil {
return errors.Wrap(err, "failed to get client")
}

Expand Down
9 changes: 8 additions & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,14 @@ func (uc *upgradeCmd) loadCluster() error {
return err
}

if uc.client, err = uc.getAuthArgs().getClient(); err != nil {
// Set env var if custom cloud profile is not nil
var env *api.Environment
if uc.containerService != nil &&
uc.containerService.Properties != nil &&
uc.containerService.Properties.CustomCloudProfile != nil {
env = uc.containerService.Properties.CustomCloudProfile.Environment
}
if uc.client, err = uc.getAuthArgs().getClient(env); err != nil {
return errors.Wrap(err, "failed to get client")
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.2
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.3.2
github.com/Azure/azure-storage-blob-go v0.7.0
github.com/Azure/go-autorest/autorest v0.11.12
github.com/BurntSushi/toml v0.3.1
github.com/Jeffail/gabs v1.1.1
github.com/blang/semver v3.5.1+incompatible
Expand Down Expand Up @@ -43,6 +42,7 @@ require (
github.com/Azure/azure-pipeline-go v0.2.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.12 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.10 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/autorest/to v0.3.0 // indirect
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/Azure/aks-engine-azurestack/pkg/api/common"
"github.com/Azure/aks-engine-azurestack/pkg/helpers/to"
"github.com/Azure/go-autorest/autorest/azure"
)

func TestAppendAddonIfNotPresent(t *testing.T) {
Expand Down Expand Up @@ -1913,7 +1912,7 @@ func TestSetAddonsConfig(t *testing.T) {
},
},
CustomCloudProfile: &CustomCloudProfile{
Environment: &azure.Environment{
Environment: &Environment{
Name: "AzureStackCloud",
},
},
Expand Down
9 changes: 4 additions & 5 deletions pkg/api/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/Azure/aks-engine-azurestack/pkg/api/common"
"github.com/Azure/aks-engine-azurestack/pkg/helpers/to"
"github.com/Azure/go-autorest/autorest/azure"
)

const (
Expand Down Expand Up @@ -853,7 +852,7 @@ func TestGetContainerImages(t *testing.T) {
csOneDotFifteenCustomImagesComponents := GetK8sComponentsByVersionMap(&KubernetesConfig{KubernetesImageBaseType: common.KubernetesImageBaseTypeMCR})
csOneDotFifteenAzureStack := getContainerServicesMap()["1.15"]
csOneDotFifteenAzureStack.Properties.CustomCloudProfile = &CustomCloudProfile{
Environment: &azure.Environment{
Environment: &Environment{
Name: "AzureStackCloud",
},
}
Expand All @@ -864,7 +863,7 @@ func TestGetContainerImages(t *testing.T) {
csOneDotSixteenCustomImagesComponents := GetK8sComponentsByVersionMap(&KubernetesConfig{KubernetesImageBaseType: common.KubernetesImageBaseTypeMCR})
csOneDotSixteenAzureStack := getContainerServicesMap()["1.16"]
csOneDotSixteenAzureStack.Properties.CustomCloudProfile = &CustomCloudProfile{
Environment: &azure.Environment{
Environment: &Environment{
Name: "AzureStackCloud",
},
}
Expand All @@ -875,7 +874,7 @@ func TestGetContainerImages(t *testing.T) {
csOneDotSeventeenCustomImagesComponents := GetK8sComponentsByVersionMap(&KubernetesConfig{KubernetesImageBaseType: common.KubernetesImageBaseTypeMCR})
csOneDotSeventeenAzureStack := getContainerServicesMap()["1.17"]
csOneDotSeventeenAzureStack.Properties.CustomCloudProfile = &CustomCloudProfile{
Environment: &azure.Environment{
Environment: &Environment{
Name: "AzureStackCloud",
},
}
Expand All @@ -886,7 +885,7 @@ func TestGetContainerImages(t *testing.T) {
csOneDotEighteenCustomImagesComponents := GetK8sComponentsByVersionMap(&KubernetesConfig{KubernetesImageBaseType: common.KubernetesImageBaseTypeMCR})
csOneDotEighteenAzureStack := getContainerServicesMap()["1.18"]
csOneDotEighteenAzureStack.Properties.CustomCloudProfile = &CustomCloudProfile{
Environment: &azure.Environment{
Environment: &Environment{
Name: "AzureStackCloud",
},
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/converterfromapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/Azure/aks-engine-azurestack/pkg/api/vlabs"
"github.com/Azure/aks-engine-azurestack/pkg/helpers/to"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/blang/semver"
)

Expand Down Expand Up @@ -671,7 +670,7 @@ func convertFeatureFlagsToVLabs(api *FeatureFlags, vlabs *vlabs.FeatureFlags) {

func convertCloudProfileToVLabs(api *CustomCloudProfile, vlabsccp *vlabs.CustomCloudProfile) {
if api.Environment != nil {
vlabsccp.Environment = &azure.Environment{}
vlabsccp.Environment = &vlabs.Environment{}
vlabsccp.Environment.Name = api.Environment.Name
vlabsccp.Environment.ManagementPortalURL = api.Environment.ManagementPortalURL
vlabsccp.Environment.PublishSettingsURL = api.Environment.PublishSettingsURL
Expand All @@ -692,7 +691,7 @@ func convertCloudProfileToVLabs(api *CustomCloudProfile, vlabsccp *vlabs.CustomC
vlabsccp.Environment.ResourceManagerVMDNSSuffix = api.Environment.ResourceManagerVMDNSSuffix
vlabsccp.Environment.ContainerRegistryDNSSuffix = api.Environment.ContainerRegistryDNSSuffix
vlabsccp.Environment.TokenAudience = api.Environment.TokenAudience
vlabsccp.Environment.ResourceIdentifiers = azure.ResourceIdentifier{
vlabsccp.Environment.ResourceIdentifiers = vlabs.ResourceIdentifier{
Graph: api.Environment.ResourceIdentifiers.Graph,
KeyVault: api.Environment.ResourceIdentifiers.KeyVault,
Datalake: api.Environment.ResourceIdentifiers.Datalake,
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/converterfromapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Azure/aks-engine-azurestack/pkg/api/common"
"github.com/Azure/aks-engine-azurestack/pkg/api/vlabs"
"github.com/Azure/aks-engine-azurestack/pkg/helpers/to"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -50,7 +49,7 @@ func TestConvertCloudProfileToVLabs(t *testing.T) {
CustomCloudProfile: &CustomCloudProfile{
IdentitySystem: AzureADIdentitySystem,
AuthenticationMethod: ClientSecretAuthMethod,
Environment: &azure.Environment{
Environment: &Environment{
Name: name,
ManagementPortalURL: managementPortalURL,
PublishSettingsURL: publishSettingsURL,
Expand All @@ -71,7 +70,7 @@ func TestConvertCloudProfileToVLabs(t *testing.T) {
ResourceManagerVMDNSSuffix: resourceManagerVMDNSSuffix,
ContainerRegistryDNSSuffix: containerRegistryDNSSuffix,
TokenAudience: tokenAudience,
ResourceIdentifiers: azure.ResourceIdentifier{
ResourceIdentifiers: ResourceIdentifier{
Graph: graphResourceIdentifier,
KeyVault: keyVaultResourceIdentifier,
Datalake: datalakeResourceIdentifier,
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/convertertoapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/Azure/aks-engine-azurestack/pkg/api/vlabs"
"github.com/Azure/aks-engine-azurestack/pkg/helpers"
"github.com/Azure/aks-engine-azurestack/pkg/helpers/to"
"github.com/Azure/go-autorest/autorest/azure"
)

///////////////////////////////////////////////////////////
Expand Down Expand Up @@ -708,7 +707,7 @@ func convertVLabsAADProfile(vlabs *vlabs.AADProfile, api *AADProfile) {

func convertVLabsCustomCloudProfile(vlabs *vlabs.CustomCloudProfile, api *CustomCloudProfile) {
if vlabs.Environment != nil {
api.Environment = &azure.Environment{}
api.Environment = &Environment{}
api.Environment.Name = vlabs.Environment.Name
api.Environment.ManagementPortalURL = vlabs.Environment.ManagementPortalURL
api.Environment.PublishSettingsURL = vlabs.Environment.PublishSettingsURL
Expand All @@ -729,7 +728,7 @@ func convertVLabsCustomCloudProfile(vlabs *vlabs.CustomCloudProfile, api *Custom
api.Environment.ResourceManagerVMDNSSuffix = vlabs.Environment.ResourceManagerVMDNSSuffix
api.Environment.ContainerRegistryDNSSuffix = vlabs.Environment.ContainerRegistryDNSSuffix
api.Environment.TokenAudience = vlabs.Environment.TokenAudience
api.Environment.ResourceIdentifiers = azure.ResourceIdentifier{
api.Environment.ResourceIdentifiers = ResourceIdentifier{
Graph: vlabs.Environment.ResourceIdentifiers.Graph,
KeyVault: vlabs.Environment.ResourceIdentifiers.KeyVault,
Datalake: vlabs.Environment.ResourceIdentifiers.Datalake,
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/convertertoapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/Azure/aks-engine-azurestack/pkg/api/vlabs"
"github.com/Azure/aks-engine-azurestack/pkg/helpers/to"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -153,7 +152,7 @@ func TestCustomCloudProfile(t *testing.T) {
CustomCloudProfile: &vlabs.CustomCloudProfile{
IdentitySystem: ADFSIdentitySystem,
AuthenticationMethod: ClientCertificateAuthMethod,
Environment: &azure.Environment{
Environment: &vlabs.Environment{
Name: name,
ManagementPortalURL: managementPortalURL,
PublishSettingsURL: publishSettingsURL,
Expand All @@ -174,7 +173,7 @@ func TestCustomCloudProfile(t *testing.T) {
ResourceManagerVMDNSSuffix: resourceManagerVMDNSSuffix,
ContainerRegistryDNSSuffix: containerRegistryDNSSuffix,
TokenAudience: tokenAudience,
ResourceIdentifiers: azure.ResourceIdentifier{
ResourceIdentifiers: vlabs.ResourceIdentifier{
Graph: graphResourceIdentifier,
KeyVault: keyVaultResourceIdentifier,
Datalake: datalakeResourceIdentifier,
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/defaults-custom-cloud-profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/Azure/aks-engine-azurestack/pkg/helpers"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -47,12 +46,12 @@ func (cs *ContainerService) setCustomCloudProfileDefaults(params CustomCloudProf
return nil
}

// SetCustomCloudProfileEnvironment retrieves the endpoints from metadata endpoint (when required) and sets the values for azure.Environment
// SetCustomCloudProfileEnvironment retrieves the endpoints from metadata endpoint (when required) and sets the values for Environment
func (cs *ContainerService) SetCustomCloudProfileEnvironment() error {
p := cs.Properties
if p.IsCustomCloudProfile() {
if p.CustomCloudProfile.Environment == nil {
p.CustomCloudProfile.Environment = &azure.Environment{}
p.CustomCloudProfile.Environment = &Environment{}
}

env := p.CustomCloudProfile.Environment
Expand Down
Loading

0 comments on commit 6bc4f90

Please sign in to comment.