Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-environment support for AKS #136

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Edit the CRD
Create AKS secret with clientID and clientSecret

export REPLACE_WITH_K8S_SECRETS_NAME=aks-secret
kubectl create secret generic $REPLACE_WITH_K8S_SECRETS_NAME --from-literal=azurecredentialConfig-subscriptionId=<REPLACE_WITH_SUBSCRIPTIONID> --from-literal=azurecredentialConfig-clientId=<REPLACE_WITH_CLIENTID> --from-literal=azurecredentialConfig-clientSecret=<REPLACE_WITH_CLIENTSECRET>
kubectl create secret generic $REPLACE_WITH_K8S_SECRETS_NAME --from-literal=azurecredentialConfig-subscriptionId=<REPLACE_WITH_SUBSCRIPTIONID> --from-literal=azurecredentialConfig-clientId=<REPLACE_WITH_CLIENTID> --from-literal=azurecredentialConfig-clientSecret=<REPLACE_WITH_CLIENTSECRET> --from-literal=azurecredentialConfig-environment=<REPLACE_WITH_AZURE_ENVIRONMENT>

Start the AKS operator

Expand Down
4 changes: 4 additions & 0 deletions controller/aks-cluster-config-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ func BuildUpstreamClusterState(ctx context.Context, secretsCache wranglerv1.Secr
upstreamSpec.Tags = to.StringMap(clusterState.Tags)
}

// set BaseURL && AuthBaseURL
upstreamSpec.AuthBaseURL = credentials.AuthBaseURL
upstreamSpec.BaseURL = credentials.BaseURL

// set AgentPool profile
for _, np := range *clusterState.AgentPoolProfiles {
var upstreamNP aksv1.AKSNodePool
Expand Down
31 changes: 28 additions & 3 deletions pkg/aks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func GetSecrets(secretsCache wranglerv1.SecretCache, secretClient wranglerv1.Sec
subscriptionIDBytes := secret.Data["azurecredentialConfig-subscriptionId"]
clientIDBytes := secret.Data["azurecredentialConfig-clientId"]
clientSecretBytes := secret.Data["azurecredentialConfig-clientSecret"]
clientEnvironment := ""
if secret.Data["azurecredentialConfig-environment"] != nil {
clientEnvironment = string(secret.Data["azurecredentialConfig-environment"])
}
azureEnvironment := GetEnvironment(clientEnvironment)

cannotBeNilError := "field [azurecredentialConfig-%s] must be provided in cloud credential"
if subscriptionIDBytes == nil {
Expand All @@ -89,8 +94,8 @@ func GetSecrets(secretsCache wranglerv1.SecretCache, secretClient wranglerv1.Sec
cred.SubscriptionID = string(subscriptionIDBytes)
cred.ClientID = string(clientIDBytes)
cred.ClientSecret = string(clientSecretBytes)
cred.AuthBaseURL = spec.AuthBaseURL
cred.BaseURL = spec.BaseURL
cred.AuthBaseURL = &azureEnvironment.ActiveDirectoryEndpoint
cred.BaseURL = &azureEnvironment.ResourceManagerEndpoint

if cred.TenantID == "" {
cred.TenantID, err = GetCachedTenantID(secretClient, cred.SubscriptionID, secret)
Expand Down Expand Up @@ -124,7 +129,14 @@ func GetCachedTenantID(secretClient secretClient, subscriptionID string, secret
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
logrus.Debugf("retrieving tenant ID from Azure public cloud")
tenantID, err := azureutil.FindTenantID(ctx, azure.PublicCloud, subscriptionID)

clientEnvironment := ""
if secret.Data["azurecredentialConfig-environment"] != nil {
clientEnvironment = string(secret.Data["azurecredentialConfig-environment"])
}
azureEnvironment := GetEnvironment(clientEnvironment)

tenantID, err := azureutil.FindTenantID(ctx, azureEnvironment, subscriptionID)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -152,3 +164,16 @@ func NewClusterClient(cred *Credentials) (*containerservice.ManagedClustersClien

return &client, nil
}

func GetEnvironment(env string) azure.Environment {
richardcase marked this conversation as resolved.
Show resolved Hide resolved
switch env {
case "AzureGermanCloud":
return azure.GermanCloud
case "AzureChinaCloud":
return azure.ChinaCloud
case "AzureUSGovernmentCloud":
return azure.USGovernmentCloud
default:
return azure.PublicCloud
}
}