@@ -19,6 +19,7 @@ import (
19
19
"errors"
20
20
"fmt"
21
21
"net/http"
22
+ "net/url"
22
23
"os"
23
24
"runtime"
24
25
"time"
@@ -47,8 +48,27 @@ const (
47
48
regionURITemplate = "projects/%s/regions/%s"
48
49
49
50
replicaZoneURITemplateSingleZone = "projects/%s/zones/%s" // {gce.projectID}/zones/{disk.Zone}
51
+ versionV1 = "v1"
52
+ versionBeta = "beta"
53
+ versionAlpha = "alpha"
54
+ googleEnv = "googleapis"
50
55
)
51
56
57
+ var computeVersionMap = map [string ]map [string ]map [string ]string {
58
+ googleEnv : {
59
+ "prod" : {
60
+ versionV1 : "compute/v1/" ,
61
+ versionBeta : "compute/beta/" ,
62
+ versionAlpha : "compute/alpha/" ,
63
+ },
64
+ "staging" : {
65
+ versionV1 : "compute/staging_v1/" ,
66
+ versionBeta : "compute/staging_beta/" ,
67
+ versionAlpha : "compute/staging_alpha/" ,
68
+ },
69
+ },
70
+ }
71
+
52
72
type CloudProvider struct {
53
73
service * compute.Service
54
74
betaService * computebeta.Service
@@ -72,7 +92,7 @@ type ConfigGlobal struct {
72
92
Zone string `gcfg:"zone"`
73
93
}
74
94
75
- func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint string ) (* CloudProvider , error ) {
95
+ func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint string , computeEnvironment string ) (* CloudProvider , error ) {
76
96
configFile , err := readConfig (configPath )
77
97
if err != nil {
78
98
return nil , err
@@ -87,20 +107,23 @@ func CreateCloudProvider(ctx context.Context, vendorVersion string, configPath s
87
107
return nil , err
88
108
}
89
109
90
- svc , err := createCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
110
+ svc , err := createCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
91
111
if err != nil {
92
112
return nil , err
93
113
}
114
+ klog .Infof ("Compute endpoint for V1 version: %s" , svc .BasePath )
94
115
95
- betasvc , err := createBetaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
116
+ betasvc , err := createBetaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
96
117
if err != nil {
97
118
return nil , err
98
119
}
120
+ klog .Infof ("Compute endpoint for Beta version: %s" , betasvc .BasePath )
99
121
100
- alphasvc , err := createAlphaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
122
+ alphasvc , err := createAlphaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
101
123
if err != nil {
102
124
return nil , err
103
125
}
126
+ klog .Infof ("Compute endpoint for Alpha version: %s" , alphasvc .BasePath )
104
127
105
128
project , zone , err := getProjectAndZone (configFile )
106
129
if err != nil {
@@ -164,16 +187,23 @@ func readConfig(configPath string) (*ConfigFile, error) {
164
187
return cfg , nil
165
188
}
166
189
167
- func createBetaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* computebeta.Service , error ) {
168
- client , err := newOauthClient (ctx , tokenSource )
190
+ func createAlphaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment string ) (* computealpha.Service , error ) {
191
+ computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionAlpha )
192
+ if err != nil {
193
+ klog .Errorf ("Failed to get compute endpoint: %s" , err )
194
+ }
195
+ service , err := computealpha .NewService (ctx , computeOpts ... )
169
196
if err != nil {
170
197
return nil , err
171
198
}
199
+ service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
200
+ return service , nil
201
+ }
172
202
173
- computeOpts := []option. ClientOption { option . WithHTTPClient ( client )}
174
- if computeEndpoint != "" {
175
- betaEndpoint := fmt . Sprintf ( "%s/compute/beta/" , computeEndpoint )
176
- computeOpts = append ( computeOpts , option . WithEndpoint ( betaEndpoint ) )
203
+ func createBetaCloudService ( ctx context. Context , vendorVersion string , tokenSource oauth2. TokenSource , computeEndpoint string , computeEnvironment string ) ( * computebeta. Service , error ) {
204
+ computeOpts , err := getComputeVersion ( ctx , tokenSource , computeEndpoint , computeEnvironment , versionBeta )
205
+ if err != nil {
206
+ klog . Errorf ( "Failed to get compute endpoint: %s" , err )
177
207
}
178
208
service , err := computebeta .NewService (ctx , computeOpts ... )
179
209
if err != nil {
@@ -183,47 +213,39 @@ func createBetaCloudService(ctx context.Context, vendorVersion string, tokenSour
183
213
return service , nil
184
214
}
185
215
186
- func createAlphaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* computealpha .Service , error ) {
187
- client , err := newOauthClient (ctx , tokenSource )
216
+ func createCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment string ) (* compute .Service , error ) {
217
+ computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionV1 )
188
218
if err != nil {
189
- return nil , err
190
- }
191
-
192
- computeOpts := []option.ClientOption {option .WithHTTPClient (client )}
193
- if computeEndpoint != "" {
194
- alphaEndpoint := fmt .Sprintf ("%s/compute/alpha/" , computeEndpoint )
195
- computeOpts = append (computeOpts , option .WithEndpoint (alphaEndpoint ))
219
+ klog .Errorf ("Failed to get compute endpoint: %s" , err )
196
220
}
197
- service , err := computealpha .NewService (ctx , computeOpts ... )
221
+ service , err := compute .NewService (ctx , computeOpts ... )
198
222
if err != nil {
199
223
return nil , err
200
224
}
201
225
service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
202
226
return service , nil
203
227
}
204
228
205
- func createCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* compute.Service , error ) {
206
- svc , err := createCloudServiceWithDefaultServiceAccount (ctx , vendorVersion , tokenSource , computeEndpoint )
207
- return svc , err
208
- }
209
-
210
- func createCloudServiceWithDefaultServiceAccount (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* compute.Service , error ) {
229
+ func getComputeVersion (ctx context.Context , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment string , computeVersion string ) ([]option.ClientOption , error ) {
211
230
client , err := newOauthClient (ctx , tokenSource )
212
231
if err != nil {
213
232
return nil , err
214
233
}
215
-
234
+ computeEnvironmentSuffix , ok := computeVersionMap [googleEnv ][computeEnvironment ][computeVersion ]
235
+ if ! ok {
236
+ return nil , errors .New ("Unable to fetch compute endpoint" )
237
+ }
216
238
computeOpts := []option.ClientOption {option .WithHTTPClient (client )}
217
239
if computeEndpoint != "" {
218
- v1Endpoint := fmt .Sprintf ("%s/compute/v1/" , computeEndpoint )
219
- computeOpts = append (computeOpts , option .WithEndpoint (v1Endpoint ))
220
- }
221
- service , err := compute .NewService (ctx , computeOpts ... )
222
- if err != nil {
223
- return nil , err
240
+ endpoint := fmt .Sprintf ("%s%s" , computeEndpoint , computeEnvironmentSuffix )
241
+ klog .Infof ("Got compute endpoint %s" , endpoint )
242
+ _ , err := url .ParseRequestURI (endpoint )
243
+ if err != nil {
244
+ klog .Fatalf ("Error parsing compute endpoint %s" , endpoint )
245
+ }
246
+ computeOpts = append (computeOpts , option .WithEndpoint (endpoint ))
224
247
}
225
- service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
226
- return service , nil
248
+ return computeOpts , nil
227
249
}
228
250
229
251
func newOauthClient (ctx context.Context , tokenSource oauth2.TokenSource ) (* http.Client , error ) {
0 commit comments