@@ -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"
@@ -29,13 +30,17 @@ import (
29
30
30
31
"cloud.google.com/go/compute/metadata"
31
32
"golang.org/x/oauth2"
33
+ computealpha "google.golang.org/api/compute/v0.alpha"
32
34
computebeta "google.golang.org/api/compute/v0.beta"
33
35
"google.golang.org/api/compute/v1"
34
36
"google.golang.org/api/googleapi"
35
37
"k8s.io/apimachinery/pkg/util/wait"
36
38
"k8s.io/klog/v2"
37
39
)
38
40
41
+ type Environment string
42
+ type Version string
43
+
39
44
const (
40
45
TokenURL = "https://accounts.google.com/o/oauth2/token"
41
46
diskSourceURITemplateSingleZone = "projects/%s/zones/%s/disks/%s" // {gce.projectID}/zones/{disk.Zone}/disks/{disk.Name}"
@@ -45,7 +50,12 @@ const (
45
50
46
51
regionURITemplate = "projects/%s/regions/%s"
47
52
48
- replicaZoneURITemplateSingleZone = "projects/%s/zones/%s" // {gce.projectID}/zones/{disk.Zone}
53
+ replicaZoneURITemplateSingleZone = "projects/%s/zones/%s" // {gce.projectID}/zones/{disk.Zone}
54
+ versionV1 Version = "v1"
55
+ versionBeta Version = "beta"
56
+ versionAlpha Version = "alpha"
57
+ EnvironmentStaging Environment = "staging"
58
+ EnvironmentProduction Environment = "production"
49
59
)
50
60
51
61
type CloudProvider struct {
@@ -70,7 +80,7 @@ type ConfigGlobal struct {
70
80
Zone string `gcfg:"zone"`
71
81
}
72
82
73
- func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint string ) (* CloudProvider , error ) {
83
+ func CreateCloudProvider (ctx context.Context , vendorVersion string , configPath string , computeEndpoint * url. URL , computeEnvironment Environment ) (* CloudProvider , error ) {
74
84
configFile , err := readConfig (configPath )
75
85
if err != nil {
76
86
return nil , err
@@ -85,15 +95,23 @@ func CreateCloudProvider(ctx context.Context, vendorVersion string, configPath s
85
95
return nil , err
86
96
}
87
97
88
- svc , err := createCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
98
+ svc , err := createCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
99
+ if err != nil {
100
+ return nil , err
101
+ }
102
+ klog .Infof ("Compute endpoint for V1 version: %s" , svc .BasePath )
103
+
104
+ betasvc , err := createBetaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
89
105
if err != nil {
90
106
return nil , err
91
107
}
108
+ klog .Infof ("Compute endpoint for Beta version: %s" , betasvc .BasePath )
92
109
93
- betasvc , err := createBetaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint )
110
+ alphasvc , err := createAlphaCloudService (ctx , vendorVersion , tokenSource , computeEndpoint , computeEnvironment )
94
111
if err != nil {
95
112
return nil , err
96
113
}
114
+ klog .Infof ("Compute endpoint for Alpha version: %s" , alphasvc .BasePath )
97
115
98
116
project , zone , err := getProjectAndZone (configFile )
99
117
if err != nil {
@@ -156,16 +174,23 @@ func readConfig(configPath string) (*ConfigFile, error) {
156
174
return cfg , nil
157
175
}
158
176
159
- func createBetaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* computebeta.Service , error ) {
160
- client , err := newOauthClient (ctx , tokenSource )
177
+ func createAlphaCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint * url.URL , computeEnvironment Environment ) (* computealpha.Service , error ) {
178
+ computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionAlpha )
179
+ if err != nil {
180
+ klog .Errorf ("Failed to get compute endpoint: %s" , err )
181
+ }
182
+ service , err := computealpha .NewService (ctx , computeOpts ... )
161
183
if err != nil {
162
184
return nil , err
163
185
}
186
+ service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
187
+ return service , nil
188
+ }
164
189
165
- computeOpts := []option. ClientOption { option . WithHTTPClient ( client )}
166
- if computeEndpoint != "" {
167
- betaEndpoint := fmt . Sprintf ( "%s/compute/beta/" , computeEndpoint )
168
- computeOpts = append ( computeOpts , option . WithEndpoint ( betaEndpoint ) )
190
+ func createBetaCloudService ( ctx context. Context , vendorVersion string , tokenSource oauth2. TokenSource , computeEndpoint * url. URL , computeEnvironment Environment ) ( * computebeta. Service , error ) {
191
+ computeOpts , err := getComputeVersion ( ctx , tokenSource , computeEndpoint , computeEnvironment , versionBeta )
192
+ if err != nil {
193
+ klog . Errorf ( "Failed to get compute endpoint: %s" , err )
169
194
}
170
195
service , err := computebeta .NewService (ctx , computeOpts ... )
171
196
if err != nil {
@@ -175,28 +200,41 @@ func createBetaCloudService(ctx context.Context, vendorVersion string, tokenSour
175
200
return service , nil
176
201
}
177
202
178
- func createCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* compute.Service , error ) {
179
- svc , err := createCloudServiceWithDefaultServiceAccount (ctx , vendorVersion , tokenSource , computeEndpoint )
180
- return svc , err
203
+ func createCloudService (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint * url.URL , computeEnvironment Environment ) (* compute.Service , error ) {
204
+ computeOpts , err := getComputeVersion (ctx , tokenSource , computeEndpoint , computeEnvironment , versionV1 )
205
+ if err != nil {
206
+ klog .Errorf ("Failed to get compute endpoint: %s" , err )
207
+ }
208
+ service , err := compute .NewService (ctx , computeOpts ... )
209
+ if err != nil {
210
+ return nil , err
211
+ }
212
+ service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
213
+ return service , nil
181
214
}
182
215
183
- func createCloudServiceWithDefaultServiceAccount (ctx context.Context , vendorVersion string , tokenSource oauth2.TokenSource , computeEndpoint string ) (* compute. Service , error ) {
216
+ func getComputeVersion (ctx context.Context , tokenSource oauth2.TokenSource , computeEndpoint * url. URL , computeEnvironment Environment , computeVersion Version ) ([]option. ClientOption , error ) {
184
217
client , err := newOauthClient (ctx , tokenSource )
185
218
if err != nil {
186
219
return nil , err
187
220
}
188
-
189
221
computeOpts := []option.ClientOption {option .WithHTTPClient (client )}
190
- if computeEndpoint != "" {
191
- v1Endpoint := fmt .Sprintf ("%s/compute/v1/" , computeEndpoint )
192
- computeOpts = append (computeOpts , option .WithEndpoint (v1Endpoint ))
222
+
223
+ if computeEndpoint != nil {
224
+ computeEnvironmentSuffix := constructComputeEndpointPath (computeEnvironment , computeVersion )
225
+ computeEndpoint .Path = computeEnvironmentSuffix
226
+ endpoint := computeEndpoint .String ()
227
+ computeOpts = append (computeOpts , option .WithEndpoint (endpoint ))
193
228
}
194
- service , err := compute .NewService (ctx , computeOpts ... )
195
- if err != nil {
196
- return nil , err
229
+ return computeOpts , nil
230
+ }
231
+
232
+ func constructComputeEndpointPath (env Environment , version Version ) string {
233
+ prefix := ""
234
+ if env == EnvironmentStaging {
235
+ prefix = fmt .Sprintf ("%s_" , env )
197
236
}
198
- service .UserAgent = fmt .Sprintf ("GCE CSI Driver/%s (%s %s)" , vendorVersion , runtime .GOOS , runtime .GOARCH )
199
- return service , nil
237
+ return fmt .Sprintf ("compute/%s%s/" , prefix , version )
200
238
}
201
239
202
240
func newOauthClient (ctx context.Context , tokenSource oauth2.TokenSource ) (* http.Client , error ) {
0 commit comments