Skip to content

Commit 4006ce7

Browse files
committed
update driver to support staging compute
1 parent e75b9a3 commit 4006ce7

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

cmd/gce-pd-csi-driver/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var (
6767

6868
maxConcurrentFormatAndMount = flag.Int("max-concurrent-format-and-mount", 1, "If set then format and mount operations are serialized on each node. This is stronger than max-concurrent-format as it includes fsck and other mount operations")
6969
formatAndMountTimeout = flag.Duration("format-and-mount-timeout", 1*time.Minute, "The maximum duration of a format and mount operation before another such operation will be started. Used only if --serialize-format-and-mount")
70+
computeEnvironment = flag.String("compute-environment", "prod", "Sets the compute environment")
7071

7172
fallbackRequisiteZonesFlag = flag.String("fallback-requisite-zones", "", "Comma separated list of requisite zones that will be used if there are not sufficient zones present in requisite topologies when provisioning a disk")
7273

@@ -156,7 +157,7 @@ func handle() {
156157
// Initialize requirements for the controller service
157158
var controllerServer *driver.GCEControllerServer
158159
if *runControllerService {
159-
cloudProvider, err := gce.CreateCloudProvider(ctx, version, *cloudConfigFilePath, *computeEndpoint)
160+
cloudProvider, err := gce.CreateCloudProvider(ctx, version, *cloudConfigFilePath, *computeEndpoint, *computeEnvironment)
160161
if err != nil {
161162
klog.Fatalf("Failed to get cloud provider: %v", err.Error())
162163
}

pkg/gce-cloud-provider/compute/gce.go

+63-35
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"net/http"
22+
"net/url"
2223
"os"
2324
"runtime"
2425
"time"
@@ -47,8 +48,33 @@ const (
4748
regionURITemplate = "projects/%s/regions/%s"
4849

4950
replicaZoneURITemplateSingleZone = "projects/%s/zones/%s" // {gce.projectID}/zones/{disk.Zone}
51+
versionV1 = "v1"
52+
versionBeta = "beta"
53+
versionAlpha = "alpha"
54+
googleEnv = "googleapis"
5055
)
5156

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+
5278
type CloudProvider struct {
5379
service *compute.Service
5480
betaService *computebeta.Service
@@ -72,7 +98,7 @@ type ConfigGlobal struct {
7298
Zone string `gcfg:"zone"`
7399
}
74100

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) {
76102
configFile, err := readConfig(configPath)
77103
if err != nil {
78104
return nil, err
@@ -87,20 +113,23 @@ func CreateCloudProvider(ctx context.Context, vendorVersion string, configPath s
87113
return nil, err
88114
}
89115

90-
svc, err := createCloudService(ctx, vendorVersion, tokenSource, computeEndpoint)
116+
svc, err := createCloudService(ctx, vendorVersion, tokenSource, computeEndpoint, computeEnvironment)
91117
if err != nil {
92118
return nil, err
93119
}
120+
klog.Infof("Compute endpoint for V1 version: %s", svc.BasePath)
94121

95-
betasvc, err := createBetaCloudService(ctx, vendorVersion, tokenSource, computeEndpoint)
122+
betasvc, err := createBetaCloudService(ctx, vendorVersion, tokenSource, computeEndpoint, computeEnvironment)
96123
if err != nil {
97124
return nil, err
98125
}
126+
klog.Infof("Compute endpoint for Beta version: %s", betasvc.BasePath)
99127

100-
alphasvc, err := createAlphaCloudService(ctx, vendorVersion, tokenSource, computeEndpoint)
128+
alphasvc, err := createAlphaCloudService(ctx, vendorVersion, tokenSource, computeEndpoint, computeEnvironment)
101129
if err != nil {
102130
return nil, err
103131
}
132+
klog.Infof("Compute endpoint for Alpha version: %s", alphasvc.BasePath)
104133

105134
project, zone, err := getProjectAndZone(configFile)
106135
if err != nil {
@@ -164,16 +193,23 @@ func readConfig(configPath string) (*ConfigFile, error) {
164193
return cfg, nil
165194
}
166195

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...)
169202
if err != nil {
170203
return nil, err
171204
}
205+
service.UserAgent = fmt.Sprintf("GCE CSI Driver/%s (%s %s)", vendorVersion, runtime.GOOS, runtime.GOARCH)
206+
return service, nil
207+
}
172208

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)
177213
}
178214
service, err := computebeta.NewService(ctx, computeOpts...)
179215
if err != nil {
@@ -183,47 +219,39 @@ func createBetaCloudService(ctx context.Context, vendorVersion string, tokenSour
183219
return service, nil
184220
}
185221

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)
188224
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)
196226
}
197-
service, err := computealpha.NewService(ctx, computeOpts...)
227+
service, err := compute.NewService(ctx, computeOpts...)
198228
if err != nil {
199229
return nil, err
200230
}
201231
service.UserAgent = fmt.Sprintf("GCE CSI Driver/%s (%s %s)", vendorVersion, runtime.GOOS, runtime.GOARCH)
202232
return service, nil
203233
}
204234

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) {
211236
client, err := newOauthClient(ctx, tokenSource)
212237
if err != nil {
213238
return nil, err
214239
}
215-
240+
computeEnvironmentSuffix, ok := computeVersionMap[googleEnv][computeEnvironment][computeVersion]
241+
if !ok {
242+
return nil, errors.New("Unable to fetch compute endpoint")
243+
}
216244
computeOpts := []option.ClientOption{option.WithHTTPClient(client)}
217245
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 compuet endpoint %s", endpoint)
251+
}
252+
computeOpts = append(computeOpts, option.WithEndpoint(endpoint))
224253
}
225-
service.UserAgent = fmt.Sprintf("GCE CSI Driver/%s (%s %s)", vendorVersion, runtime.GOOS, runtime.GOARCH)
226-
return service, nil
254+
return computeOpts, nil
227255
}
228256

229257
func newOauthClient(ctx context.Context, tokenSource oauth2.TokenSource) (*http.Client, error) {

pkg/gce-pd-csi-driver/controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const (
159159
)
160160

161161
var (
162-
validResourceApiVersions = map[string]bool{"v1": true, "alpha": true, "beta": true}
162+
validResourceApiVersions = map[string]bool{"v1": true, "alpha": true, "beta": true, "staging_v1": true, "staging_beta": true, "staging_alpha": true}
163163
)
164164

165165
func isDiskReady(disk *gce.CloudDisk) (bool, error) {

0 commit comments

Comments
 (0)