Skip to content

Commit

Permalink
Cherry-pick 4689 FixBug-invalidMetadataUrl-add-getSubscriptionIdFromI…
Browse files Browse the repository at this point in the history
…nstanceMetadata

FixBug-invalidMetadataUrl-add-getSubscriptionIdFromInstanceMetadata
  • Loading branch information
k8s-ci-robot authored and gandhipr committed Apr 7, 2022
1 parent 009d47f commit 6c411cf
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 6c411cf

Please sign in to comment.