Skip to content

Commit

Permalink
fix test failures in legacy cloud provider: add 'projects/' after upg…
Browse files Browse the repository at this point in the history
…rade

Signed-off-by: pacoxu <[email protected]>
  • Loading branch information
pacoxu committed Jul 13, 2021
1 parent 2926b45 commit 7a36a5b
Show file tree
Hide file tree
Showing 29 changed files with 15,840 additions and 10,342 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/Azure/go-autorest/autorest v0.11.18
github.com/Azure/go-autorest/autorest/adal v0.9.13
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317
github.com/GoogleCloudPlatform/k8s-cloud-provider v1.16.1-0.20210702024009-ea6160c1d0e3
github.com/JeffAshton/win_pdh v0.0.0-20161109143554-76bb4ee9f0ab
github.com/Microsoft/go-winio v0.4.15
github.com/Microsoft/hcsshim v0.8.10-0.20200715222032-5eafd1556990
Expand Down Expand Up @@ -94,7 +94,7 @@ require (
golang.org/x/tools v0.1.2
gonum.org/v1/gonum v0.6.2
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
google.golang.org/api v0.45.0
google.golang.org/api v0.46.0
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
Expand Down Expand Up @@ -155,7 +155,7 @@ replace (
github.com/Azure/go-autorest/tracing => github.com/Azure/go-autorest/tracing v0.6.0
github.com/BurntSushi/toml => github.com/BurntSushi/toml v0.3.1
github.com/BurntSushi/xgb => github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802
github.com/GoogleCloudPlatform/k8s-cloud-provider => github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317
github.com/GoogleCloudPlatform/k8s-cloud-provider => github.com/GoogleCloudPlatform/k8s-cloud-provider v1.16.1-0.20210702024009-ea6160c1d0e3
github.com/JeffAshton/win_pdh => github.com/JeffAshton/win_pdh v0.0.0-20161109143554-76bb4ee9f0ab
github.com/MakeNowJust/heredoc => github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd
github.com/Microsoft/go-winio => github.com/Microsoft/go-winio v0.4.15
Expand Down Expand Up @@ -458,7 +458,7 @@ replace (
gonum.org/v1/gonum => gonum.org/v1/gonum v0.6.2
gonum.org/v1/netlib => gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e
gonum.org/v1/plot => gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b
google.golang.org/api => google.golang.org/api v0.45.0
google.golang.org/api => google.golang.org/api v0.46.0
google.golang.org/appengine => google.golang.org/appengine v1.6.7
google.golang.org/genproto => google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
google.golang.org/grpc => google.golang.org/grpc v1.38.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBp
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317 h1:JhyuWIqYrstW7KHMjk/fTqU0xtMpBOHuiTA2FVc7L4E=
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317/go.mod h1:DF8FZRxMHMGv/vP2lQP6h+dYzzjpuRn24VeRiYn3qjQ=
github.com/GoogleCloudPlatform/k8s-cloud-provider v1.16.1-0.20210702024009-ea6160c1d0e3 h1:FCalqNmQYSMCCHoCtAxZN/ZgLc8ufgeo5Z3wrIoJZvs=
github.com/GoogleCloudPlatform/k8s-cloud-provider v1.16.1-0.20210702024009-ea6160c1d0e3/go.mod h1:8XasY4ymP2V/tn2OOV9ZadmiTE1FIB/h3W+yNlPttKw=
github.com/JeffAshton/win_pdh v0.0.0-20161109143554-76bb4ee9f0ab h1:UKkYhof1njT1/xq4SEg5z+VpTgjmNeHwPGRQl7takDI=
github.com/JeffAshton/win_pdh v0.0.0-20161109143554-76bb4ee9f0ab/go.mod h1:3VYc5hodBMJ5+l/7J4xAyMeuM2PNuepvHlGs8yilUCA=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd h1:sjQovDkwrZp8u+gxLtPgKGjk5hCxuy2hrRejBTA9xFU=
Expand Down Expand Up @@ -542,8 +542,8 @@ gonum.org/v1/gonum v0.6.2/go.mod h1:9mxDZsDKxgMAuccQkewq682L+0eCu4dCN2yonUJTCLU=
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e h1:jRyg0XfpwWlhEV8mDfdNGBeSJM2fuyh9Yjrnd8kF2Ts=
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e/go.mod h1:kS+toOQn6AQKjmKJ7gzohV1XkqsFehRA2FbsbkopSuQ=
gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
google.golang.org/api v0.45.0 h1:pqMffJFLBVUDIoYsHcqtxgQVTsmxMDpYLOc5MT4Jrww=
google.golang.org/api v0.45.0/go.mod h1:ISLIJCedJolbZvDfAk+Ctuq5hf+aJ33WgtUsfyFoLXA=
google.golang.org/api v0.46.0 h1:jkDWHOBIoNSD0OQpq4rtBVu+Rh325MPjXG1rakAp8JU=
google.golang.org/api v0.46.0/go.mod h1:ceL4oozhkAiTID8XMmJBsIxID/9wMXJVVFXPg4ylg3I=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c h1:wtujag7C+4D6KMoulW9YauvK2lgdvCMS260jsqqBXr0=
Expand Down
21 changes: 21 additions & 0 deletions staging/src/k8s.io/legacy-cloud-providers/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,27 @@ func (g *Cloud) HasClusterID() bool {
return true
}

// GetBasePath returns the compute API endpoint with the `projects/` element
// compute API v0.36 changed basepath and dropped the `projects/` suffix, therefore suffix
// must be added back when generating compute resource urls.
func (g *Cloud) GetBasePath() string {
basePath := g.ComputeServices().GA.BasePath
return GetProjectBasePath(basePath)
}

func GetProjectBasePath(basePath string) string {
if !strings.HasSuffix(basePath, "/") {
basePath += "/"
}
// Trim the trailing /, so that split will not consider the last element as empty
elements := strings.Split(strings.TrimSuffix(basePath, "/"), "/")

if elements[len(elements)-1] != "projects" {
return fmt.Sprintf("%sprojects/", basePath)
}
return basePath
}

// Project IDs cannot have a digit for the first characeter. If the id contains a digit,
// then it must be a project number.
func isProjectNumber(idOrNumber string) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func makeHostURL(projectsAPIEndpoint, projectID, zone, host string) string {
// ToInstanceReferences returns instance references by links
func (g *Cloud) ToInstanceReferences(zone string, instanceNames []string) (refs []*compute.InstanceReference) {
for _, ins := range instanceNames {
instanceLink := makeHostURL(g.service.BasePath, g.projectID, zone, ins)
instanceLink := makeHostURL(g.GetBasePath(), g.projectID, zone, ins)
refs = append(refs, &compute.InstanceReference{Instance: instanceLink})
}
return refs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func (g *Cloud) updateTargetPool(loadBalancerName string, hosts []*gceInstance)
}

func (g *Cloud) targetPoolURL(name string) string {
return g.service.BasePath + strings.Join([]string{g.projectID, "regions", g.region, "targetPools", name}, "/")
return g.GetBasePath() + strings.Join([]string{g.projectID, "regions", g.region, "targetPools", name}, "/")
}

func makeHTTPHealthCheck(name, path string, port int32) *compute.HttpHealthCheck {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestBasePath(t *testing.T) {
// See https://github.com/kubernetes/kubernetes/issues/102757, the endpoint can have mtls in some cases.
expectMtlsBasePath := "https://compute.mtls.googleapis.com/compute/v1/projects/"
require.NoError(t, err)
if gce.service.BasePath != expectBasePath && gce.service.BasePath != expectMtlsBasePath {
t.Errorf("Compute basePath has changed. Got %q, want %q or %q", gce.service.BasePath, expectBasePath, expectMtlsBasePath)
if gce.GetBasePath() != expectBasePath && gce.GetBasePath() != expectMtlsBasePath {
t.Errorf("Compute basePath has changed. Got %q, want %q or %q", gce.GetBasePath(), expectBasePath, expectMtlsBasePath)
}
}
35 changes: 35 additions & 0 deletions staging/src/k8s.io/legacy-cloud-providers/gce/gce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,38 @@ func TestLastComponent(t *testing.T) {
}
}
}

func TestGetProjectBasePath(t *testing.T) {
testCases := []struct {
basePath string
expectProjectBasePath string
}{
// basepath ends in `/projects/`
{
basePath: "path/to/api/projects/",
expectProjectBasePath: "path/to/api/projects/",
},
// basepath ends in `/projects`, without trailing /
{
basePath: "path/to/api/projects",
expectProjectBasePath: "path/to/api/projects/",
},
// basepath does not end in `/projects/`
{
basePath: "path/to/api/",
expectProjectBasePath: "path/to/api/projects/",
},
// basepath does not end in `/projects/` and does not have trailing /
{
basePath: "path/to/api",
expectProjectBasePath: "path/to/api/projects/",
},
}

for _, tc := range testCases {
projectBasePath := GetProjectBasePath(tc.basePath)
if projectBasePath != tc.expectProjectBasePath {
t.Errorf("Expected project base path %s; but got %s", tc.expectProjectBasePath, projectBasePath)
}
}
}
2 changes: 1 addition & 1 deletion staging/src/k8s.io/legacy-cloud-providers/gce/gce_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ func (g *Cloud) ListZonesInRegion(region string) ([]*compute.Zone, error) {
}

func (g *Cloud) getRegionLink(region string) string {
return g.service.BasePath + strings.Join([]string{g.projectID, "regions", region}, "/")
return g.GetBasePath() + strings.Join([]string{g.projectID, "regions", region}, "/")
}
4 changes: 2 additions & 2 deletions staging/src/k8s.io/legacy-cloud-providers/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/Azure/go-autorest/autorest/mocks v0.4.1
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/Azure/go-autorest/autorest/validation v0.1.0 // indirect
github.com/GoogleCloudPlatform/k8s-cloud-provider v0.0.0-20200415212048-7901bc822317
github.com/GoogleCloudPlatform/k8s-cloud-provider v1.16.1-0.20210702024009-ea6160c1d0e3
github.com/aws/aws-sdk-go v1.38.49
github.com/blang/semver v3.5.1+incompatible
github.com/dnaeon/go-vcr v1.0.1 // indirect
Expand All @@ -27,7 +27,7 @@ require (
github.com/vmware/govmomi v0.20.3
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
google.golang.org/api v0.45.0
google.golang.org/api v0.46.0
gopkg.in/gcfg.v1 v1.2.0
gopkg.in/warnings.v0 v0.1.1 // indirect
k8s.io/api v0.0.0
Expand Down
18 changes: 8 additions & 10 deletions staging/src/k8s.io/legacy-cloud-providers/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7a36a5b

Please sign in to comment.