@@ -35,7 +35,7 @@ import (
35
35
"k8s.io/klog/v2"
36
36
37
37
"sigs.k8s.io/cloud-provider-azure/pkg/auth"
38
- azureprovider "sigs.k8s.io/cloud-provider-azure/pkg/provider"
38
+ azure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
39
39
)
40
40
41
41
var (
@@ -45,50 +45,74 @@ var (
45
45
)
46
46
47
47
// IsAzureStackCloud decides whether the driver is running on Azure Stack Cloud.
48
- func IsAzureStackCloud (cloud * azureprovider .Cloud ) bool {
48
+ func IsAzureStackCloud (cloud * azure .Cloud ) bool {
49
49
return strings .EqualFold (cloud .Config .Cloud , "AZURESTACKCLOUD" )
50
50
}
51
51
52
52
// getCloudProvider get Azure Cloud Provider
53
- func getCloudProvider (kubeconfig , nodeID , secretName , secretNamespace , userAgent string ) (* azureprovider .Cloud , error ) {
54
- az := & azureprovider .Cloud {
55
- InitSecretConfig : azureprovider .InitSecretConfig {
53
+ func getCloudProvider (kubeconfig , nodeID , secretName , secretNamespace , userAgent string ) (* azure .Cloud , error ) {
54
+ az := & azure .Cloud {
55
+ InitSecretConfig : azure .InitSecretConfig {
56
56
SecretName : secretName ,
57
57
SecretNamespace : secretNamespace ,
58
58
CloudConfigKey : "cloud-config" ,
59
59
},
60
60
}
61
- az .UserAgent = userAgent
61
+ az .Environment .StorageEndpointSuffix = storage .DefaultBaseURL
62
+
62
63
kubeClient , err := getKubeClient (kubeconfig )
63
- if err != nil && ! os .IsNotExist (err ) && err != rest .ErrNotInCluster {
64
- return az , fmt .Errorf ("failed to get KubeClient: %v" , err )
64
+ if err != nil {
65
+ klog .Warningf ("get kubeconfig(%s) failed with error: %v" , kubeconfig , err )
66
+ if ! os .IsNotExist (err ) && err != rest .ErrNotInCluster {
67
+ return az , fmt .Errorf ("failed to get KubeClient: %v" , err )
68
+ }
65
69
}
66
70
71
+ var (
72
+ config * azure.Config
73
+ fromSecret bool
74
+ )
75
+
67
76
if kubeClient != nil {
68
- klog .V (2 ).Infof ("reading cloud config from secret" )
77
+ klog .V (2 ).Infof ("reading cloud config from secret %s/%s" , az . SecretNamespace , az . SecretName )
69
78
az .KubeClient = kubeClient
70
- if err := az .InitializeCloudFromSecret (); err != nil {
71
- klog .V (2 ).Infof ("InitializeCloudFromSecret failed with error: %v" , err )
79
+ config , err = az .GetConfigFromSecret ()
80
+ if err == nil && config != nil {
81
+ fromSecret = true
82
+ }
83
+ if err != nil {
84
+ klog .Warningf ("InitializeCloudFromSecret: failed to get cloud config from secret %s/%s: %v" , az .SecretNamespace , az .SecretName , err )
72
85
}
73
86
}
74
87
75
- if az . TenantID == "" || az . SubscriptionID == "" || az . ResourceGroup == "" {
76
- klog .V (2 ).Infof ("could not read cloud config from secret" )
88
+ if config == nil {
89
+ klog .V (2 ).Infof ("could not read cloud config from secret %s/%s" , az . SecretNamespace , az . SecretName )
77
90
credFile , ok := os .LookupEnv (DefaultAzureCredentialFileEnv )
78
- if ok {
91
+ if ok && strings . TrimSpace ( credFile ) != "" {
79
92
klog .V (2 ).Infof ("%s env var set as %v" , DefaultAzureCredentialFileEnv , credFile )
80
93
} else {
81
94
credFile = DefaultCredFilePath
82
95
klog .V (2 ).Infof ("use default %s env var: %v" , DefaultAzureCredentialFileEnv , credFile )
83
96
}
84
97
85
- config , errOpenFile := os .Open (credFile )
86
- if errOpenFile != nil {
87
- err = fmt . Errorf ("load azure config from file(%s) failed with %v" , credFile , errOpenFile )
98
+ credFileConfig , err := os .Open (credFile )
99
+ if err != nil {
100
+ klog . Warningf ("load azure config from file(%s) failed with %v" , credFile , err )
88
101
} else {
89
- defer config .Close ()
102
+ defer credFileConfig .Close ()
90
103
klog .V (2 ).Infof ("read cloud config from file: %s successfully" , credFile )
91
- az , err = azureprovider .NewCloudWithoutFeatureGates (config , false )
104
+ if config , err = azure .ParseConfig (credFileConfig ); err != nil {
105
+ klog .Warningf ("parse config file(%s) failed with error: %v" , credFile , err )
106
+ }
107
+ }
108
+ }
109
+
110
+ if config == nil {
111
+ klog .V (2 ).Infof ("no cloud config provided, error: %v, driver will run without cloud config" , err )
112
+ } else {
113
+ config .UserAgent = userAgent
114
+ if err = az .InitializeCloudFromConfig (config , fromSecret , false ); err != nil {
115
+ klog .Warningf ("InitializeCloudFromConfig failed with error: %v" , err )
92
116
}
93
117
}
94
118
@@ -97,13 +121,6 @@ func getCloudProvider(kubeconfig, nodeID, secretName, secretNamespace, userAgent
97
121
az .KubeClient = kubeClient
98
122
}
99
123
100
- if err != nil {
101
- klog .V (2 ).Infof ("no cloud config provided, error: %v, driver will run without cloud config" , err )
102
- if az .Environment .StorageEndpointSuffix == "" {
103
- az .Environment .StorageEndpointSuffix = storage .DefaultBaseURL
104
- }
105
- }
106
-
107
124
isController := (nodeID == "" )
108
125
if isController {
109
126
if err == nil {
@@ -117,6 +134,9 @@ func getCloudProvider(kubeconfig, nodeID, secretName, secretNamespace, userAgent
117
134
klog .V (2 ).Infof ("starting node server on node(%s)" , nodeID )
118
135
}
119
136
137
+ if az .Environment .StorageEndpointSuffix == "" {
138
+ az .Environment .StorageEndpointSuffix = storage .DefaultBaseURL
139
+ }
120
140
return az , nil
121
141
}
122
142
0 commit comments