Skip to content

Commit

Permalink
Merge pull request #4797 from gandhipr/prachigandhi-cherry-pick-4689
Browse files Browse the repository at this point in the history
cherry pick #4689 onto CA-1.23 - FixBug-invalidMetadataUrl-add-getSubscriptionIdFromInstanceMetadata
  • Loading branch information
k8s-ci-robot authored Apr 7, 2022
2 parents 9efb637 + 6c411cf commit f0ab41e
Show file tree
Hide file tree
Showing 100 changed files with 24,120 additions and 2 deletions.
30 changes: 28 additions & 2 deletions cluster-autoscaler/cloudprovider/azure/azure_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"os"

"strconv"
"strings"
"time"
Expand All @@ -31,14 +32,15 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
"k8s.io/klog/v2"
azclients "sigs.k8s.io/cloud-provider-azure/pkg/azureclients"
providerazure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
)

const (
// The path of deployment parameters for standard vm.
deploymentParametersPath = "/var/lib/azure/azuredeploy.parameters.json"

metadataURL = "http://169.254.169.254/metadata/instance"
imdsServerURL = "http://169.254.169.254"

// backoff
backoffRetriesDefault = 6
Expand Down Expand Up @@ -146,7 +148,6 @@ func BuildAzureConfig(configReader io.Reader) (*Config, error) {
cfg.Cloud = os.Getenv("ARM_CLOUD")
cfg.Location = os.Getenv("LOCATION")
cfg.ResourceGroup = os.Getenv("ARM_RESOURCE_GROUP")
cfg.SubscriptionID = os.Getenv("ARM_SUBSCRIPTION_ID")
cfg.TenantID = os.Getenv("ARM_TENANT_ID")
cfg.AADClientID = os.Getenv("ARM_CLIENT_ID")
cfg.AADClientSecret = os.Getenv("ARM_CLIENT_SECRET")
Expand All @@ -157,6 +158,12 @@ func BuildAzureConfig(configReader io.Reader) (*Config, error) {
cfg.ClusterName = os.Getenv("AZURE_CLUSTER_NAME")
cfg.NodeResourceGroup = os.Getenv("AZURE_NODE_RESOURCE_GROUP")

subscriptionID, err := getSubscriptionIdFromInstanceMetadata()
if err != nil {
return nil, err
}
cfg.SubscriptionID = subscriptionID

useManagedIdentityExtensionFromEnv := os.Getenv("ARM_USE_MANAGED_IDENTITY_EXTENSION")
if len(useManagedIdentityExtensionFromEnv) > 0 {
cfg.UseManagedIdentityExtension, err = strconv.ParseBool(useManagedIdentityExtensionFromEnv)
Expand Down Expand Up @@ -473,3 +480,22 @@ func (cfg *Config) validate() error {

return nil
}

// getSubscriptionId reads the Subscription ID from the instance metadata.
func getSubscriptionIdFromInstanceMetadata() (string, error) {
subscriptionID, present := os.LookupEnv("ARM_SUBSCRIPTION_ID")
if !present {
metadataService, err := providerazure.NewInstanceMetadataService(imdsServerURL)
if err != nil {
return "", err
}

metadata, err := metadataService.GetMetadata(0)
if err != nil {
return "", err
}

return metadata.Compute.SubscriptionID, nil
}
return subscriptionID, nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f0ab41e

Please sign in to comment.