@@ -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,33 @@ 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
+ type versionMap struct {
58
+ versionV1 * compute.Service
59
+ versionBeta * computebeta.Service
60
+ versionAlpha * computealpha.Service
61
+ }
62
+
63
+ var computeVersionMap = map [string ]map [string ]map [string ]string {
64
+ googleEnv : {
65
+ "prod" : {
66
+ versionV1 : "compute/v1/" ,
67
+ versionBeta : "compute/beta/" ,
68
+ versionAlpha : "compute/alpha/" ,
69
+ },
70
+ "staging" : {
71
+ versionV1 : "compute/staging_v1/" ,
72
+ versionBeta : "compute/staging_beta/" ,
73
+ versionAlpha : "compute/staging_alpha/" ,
74
+ },
75
+ },
76
+ }
77
+
52
78
type CloudProvider struct {
53
79
service * compute.Service
54
80
betaService * computebeta.Service
@@ -72,7 +98,7 @@ type ConfigGlobal struct {
72
98
Zone string `gcfg:"zone"`
73
99
}
74
100
75
- func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint string ) (* CloudProvider , error ) {
101
+ func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint string , computeEnvironment string ) (* CloudProvider , error ) {
76
102
configFile , err := readConfig (configPath )
77
103
if err != nil {
78
104
return nil , err
@@ -87,20 +113,23 @@ func CreateCloudProvider(ctx context.Context, vendorVersion string, configPath s
87
113
return nil , err
88
114
}
89
115
90
- svc , err := createCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
116
+ svc , err := createCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
91
117
if err != nil {
92
118
return nil , err
93
119
}
120
+ klog .Infof ("Compute endpoint for V1 version: %s" , svc .BasePath )
94
121
95
- betasvc , err := createBetaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
122
+ betasvc , err := createBetaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
96
123
if err != nil {
97
124
return nil , err
98
125
}
126
+ klog .Infof ("Compute endpoint for Beta version: %s" , betasvc .BasePath )
99
127
100
- alphasvc , err := createAlphaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
128
+ alphasvc , err := createAlphaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
101
129
if err != nil {
102
130
return nil , err
103
131
}
132
+ klog .Infof ("Compute endpoint for Alpha version: %s" , alphasvc .BasePath )
104
133
105
134
project , zone , err := getProjectAndZone (configFile )
106
135
if err != nil {
@@ -164,16 +193,23 @@ func readConfig(configPath string) (*ConfigFile, error) {
164
193
return cfg , nil
165
194
}
166
195
167
- func createBetaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* computebeta.Service , error ) {
168
- client , err := newOauthClient (ctx , tokenSource )
196
+ func createAlphaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment string ) (* computealpha.Service , error ) {
197
+ computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionAlpha )
198
+ if err != nil {
199
+ klog .Errorf ("Failed to get compute endpoint: %s" , err )
200
+ }
201
+ service , err := computealpha .NewService (ctx , computeOpts ... )
169
202
if err != nil {
170
203
return nil , err
171
204
}
205
+ service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
206
+ return service , nil
207
+ }
172
208
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 ) )
209
+ func createBetaCloudService ( ctx context. Context , vendorVersion string , tokenSource oauth2. TokenSource , computeEndpoint string , computeEnvironment string ) ( * computebeta. Service , error ) {
210
+ computeOpts , err := getComputeVersion ( ctx , tokenSource , computeEndpoint , computeEnvironment , versionBeta )
211
+ if err != nil {
212
+ klog . Errorf ( "Failed to get compute endpoint: %s" , err )
177
213
}
178
214
service , err := computebeta .NewService (ctx , computeOpts ... )
179
215
if err != nil {
@@ -183,47 +219,39 @@ func createBetaCloudService(ctx context.Context, vendorVersion string, tokenSour
183
219
return service , nil
184
220
}
185
221
186
- func createAlphaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* computealpha .Service , error ) {
187
- client , err := newOauthClient (ctx , tokenSource )
222
+ func createCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment string ) (* compute .Service , error ) {
223
+ computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionV1 )
188
224
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 ))
225
+ klog .Errorf ("Failed to get compute endpoint: %s" , err )
196
226
}
197
- service , err := computealpha .NewService (ctx , computeOpts ... )
227
+ service , err := compute .NewService (ctx , computeOpts ... )
198
228
if err != nil {
199
229
return nil , err
200
230
}
201
231
service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
202
232
return service , nil
203
233
}
204
234
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 ) {
235
+ func getComputeVersion (ctx context.Context , tokenSource oauth2.TokenSource , computeEndpoint string , computeEnvironment string , computeVersion string ) ([]option.ClientOption , error ) {
211
236
client , err := newOauthClient (ctx , tokenSource )
212
237
if err != nil {
213
238
return nil , err
214
239
}
215
-
240
+ computeEnvironmentSuffix , ok := computeVersionMap [googleEnv ][computeEnvironment ][computeVersion ]
241
+ if ! ok {
242
+ return nil , errors .New ("Unable to fetch compute endpoint" )
243
+ }
216
244
computeOpts := []option.ClientOption {option .WithHTTPClient (client )}
217
245
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
246
+ endpoint := fmt .Sprintf ("%s%s" , computeEndpoint , computeEnvironmentSuffix )
247
+ klog .Infof ("Got compute endpoint %s" , endpoint )
248
+ _ , err := url .ParseRequestURI (endpoint )
249
+ if err != nil {
250
+ klog .Fatalf ("Error parsing compute endpoint %s" , endpoint )
251
+ }
252
+ computeOpts = append (computeOpts , option .WithEndpoint (endpoint ))
224
253
}
225
- service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
226
- return service , nil
254
+ return computeOpts , nil
227
255
}
228
256
229
257
func newOauthClient (ctx context.Context , tokenSource oauth2.TokenSource ) (* http.Client , error ) {
0 commit comments