Skip to content

Commit 9d048f0

Browse files
authored
Merge pull request kubernetes#85432 from jadarsie/az-dyn-env
Support Azure Stack dynamic environments
2 parents c7c0d09 + f96dda6 commit 9d048f0

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

pkg/credentialprovider/azure/azure_credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (a *acrProvider) loadConfig(rdr io.Reader) error {
144144
klog.Errorf("Failed to load azure credential file: %v", err)
145145
}
146146

147-
a.environment, err = auth.ParseAzureEnvironment(a.config.Cloud)
147+
a.environment, err = auth.ParseAzureEnvironment(a.config.Cloud, a.config.ResourceManagerEndpoint, a.config.IdentitySystem)
148148
if err != nil {
149149
return err
150150
}

staging/src/k8s.io/legacy-cloud-providers/azure/auth/azure_auth.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ import (
3030
"k8s.io/klog"
3131
)
3232

33+
const (
34+
// ADFSIdentitySystem is the override value for tenantID on Azure Stack clouds.
35+
ADFSIdentitySystem = "adfs"
36+
)
37+
3338
var (
3439
// ErrorNoAuth indicates that no credentials are provided.
3540
ErrorNoAuth = fmt.Errorf("no credentials provided for Azure cloud provider")
36-
// ADFSIdentitySystem indicates value of tenantId for ADFS on Azure Stack.
37-
ADFSIdentitySystem = "ADFS"
3841
)
3942

4043
// AzureAuthConfig holds auth related part of cloud config
@@ -59,15 +62,19 @@ type AzureAuthConfig struct {
5962
UserAssignedIdentityID string `json:"userAssignedIdentityID,omitempty" yaml:"userAssignedIdentityID,omitempty"`
6063
// The ID of the Azure Subscription that the cluster is deployed in
6164
SubscriptionID string `json:"subscriptionId,omitempty" yaml:"subscriptionId,omitempty"`
62-
// Identity system value for the deployment. This gets populate for Azure Stack case.
65+
// IdentitySystem indicates the identity provider. Relevant only to hybrid clouds (Azure Stack).
66+
// Allowed values are 'azure_ad' (default), 'adfs'.
6367
IdentitySystem string `json:"identitySystem,omitempty" yaml:"identitySystem,omitempty"`
68+
// ResourceManagerEndpoint is the cloud's resource manager endpoint. If set, cloud provider queries this endpoint
69+
// in order to generate an autorest.Environment instance instead of using one of the pre-defined Environments.
70+
ResourceManagerEndpoint string `json:"resourceManagerEndpoint,omitempty" yaml:"resourceManagerEndpoint,omitempty"`
6471
}
6572

6673
// GetServicePrincipalToken creates a new service principal token based on the configuration
6774
func GetServicePrincipalToken(config *AzureAuthConfig, env *azure.Environment) (*adal.ServicePrincipalToken, error) {
6875
var tenantID string
6976
if strings.EqualFold(config.IdentitySystem, ADFSIdentitySystem) {
70-
tenantID = "adfs"
77+
tenantID = ADFSIdentitySystem
7178
} else {
7279
tenantID = config.TenantID
7380
}
@@ -125,13 +132,24 @@ func GetServicePrincipalToken(config *AzureAuthConfig, env *azure.Environment) (
125132
return nil, ErrorNoAuth
126133
}
127134

128-
// ParseAzureEnvironment returns azure environment by name
129-
func ParseAzureEnvironment(cloudName string) (*azure.Environment, error) {
135+
// ParseAzureEnvironment returns the azure environment.
136+
// If 'resourceManagerEndpoint' is set, the environment is computed by quering the cloud's resource manager endpoint.
137+
// Otherwise, a pre-defined Environment is looked up by name.
138+
func ParseAzureEnvironment(cloudName, resourceManagerEndpoint, identitySystem string) (*azure.Environment, error) {
130139
var env azure.Environment
131140
var err error
132-
if cloudName == "" {
141+
if resourceManagerEndpoint != "" {
142+
klog.V(4).Infof("Loading environment from resource manager endpoint: %s", resourceManagerEndpoint)
143+
nameOverride := azure.OverrideProperty{Key: azure.EnvironmentName, Value: cloudName}
144+
env, err = azure.EnvironmentFromURL(resourceManagerEndpoint, nameOverride)
145+
if err == nil {
146+
azureStackOverrides(&env, resourceManagerEndpoint, identitySystem)
147+
}
148+
} else if cloudName == "" {
149+
klog.V(4).Info("Using public cloud environment")
133150
env = azure.PublicCloud
134151
} else {
152+
klog.V(4).Infof("Using %s environment", cloudName)
135153
env, err = azure.EnvironmentFromName(cloudName)
136154
}
137155
return &env, err
@@ -151,3 +169,15 @@ func decodePkcs12(pkcs []byte, password string) (*x509.Certificate, *rsa.Private
151169

152170
return certificate, rsaPrivateKey, nil
153171
}
172+
173+
// azureStackOverrides ensures that the Environment matches what AKSe currently generates for Azure Stack
174+
func azureStackOverrides(env *azure.Environment, resourceManagerEndpoint, identitySystem string) {
175+
env.ManagementPortalURL = strings.Replace(resourceManagerEndpoint, "https://management.", "https://portal.", -1)
176+
env.ServiceManagementEndpoint = env.TokenAudience
177+
env.ResourceManagerVMDNSSuffix = strings.Replace(resourceManagerEndpoint, "https://management.", "cloudapp.", -1)
178+
env.ResourceManagerVMDNSSuffix = strings.TrimSuffix(env.ResourceManagerVMDNSSuffix, "/")
179+
if strings.EqualFold(identitySystem, ADFSIdentitySystem) {
180+
env.ActiveDirectoryEndpoint = strings.TrimSuffix(env.ActiveDirectoryEndpoint, "/")
181+
env.ActiveDirectoryEndpoint = strings.TrimSuffix(env.ActiveDirectoryEndpoint, "adfs")
182+
}
183+
}

staging/src/k8s.io/legacy-cloud-providers/azure/azure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (az *Cloud) InitializeCloudFromConfig(config *Config, fromSecret bool) erro
325325
}
326326
}
327327

328-
env, err := auth.ParseAzureEnvironment(config.Cloud)
328+
env, err := auth.ParseAzureEnvironment(config.Cloud, config.ResourceManagerEndpoint, config.IdentitySystem)
329329
if err != nil {
330330
return err
331331
}

0 commit comments

Comments
 (0)