Cluster-autoscaler: relax url format
This commit is contained in:
parent
962035e4e8
commit
2c16e8a407
|
|
@ -22,7 +22,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
gcePrefix = "https://content.googleapis.com/compute/v1/projects/"
|
gceUrlSchema = "https"
|
||||||
|
gceDomainSufix = "googleapis.com/compute/v1/projects/"
|
||||||
|
gcePrefix = gceUrlSchema + "://content." + gceDomainSufix
|
||||||
instanceUrlTemplate = gcePrefix + "%s/zones/%s/instances/%s"
|
instanceUrlTemplate = gcePrefix + "%s/zones/%s/instances/%s"
|
||||||
migUrlTemplate = gcePrefix + "%s/zones/%s/instanceGroups/%s"
|
migUrlTemplate = gcePrefix + "%s/zones/%s/instanceGroups/%s"
|
||||||
)
|
)
|
||||||
|
|
@ -51,10 +53,13 @@ func GenerateMigUrl(project, zone, name string) string {
|
||||||
|
|
||||||
func parseGceUrl(url, expectedResource string) (project string, zone string, name string, err error) {
|
func parseGceUrl(url, expectedResource string) (project string, zone string, name string, err error) {
|
||||||
errMsg := fmt.Errorf("Wrong url: expected format https://content.googleapis.com/compute/v1/projects/<project-id>/zones/<zone>/%s/<name>, got %s", expectedResource, url)
|
errMsg := fmt.Errorf("Wrong url: expected format https://content.googleapis.com/compute/v1/projects/<project-id>/zones/<zone>/%s/<name>, got %s", expectedResource, url)
|
||||||
if !strings.HasPrefix(url, gcePrefix) {
|
if !strings.Contains(url, gceDomainSufix) {
|
||||||
return "", "", "", errMsg
|
return "", "", "", errMsg
|
||||||
}
|
}
|
||||||
splitted := strings.Split(strings.TrimLeft(url, gcePrefix), "/")
|
if !strings.HasPrefix(url, gceUrlSchema) {
|
||||||
|
return "", "", "", errMsg
|
||||||
|
}
|
||||||
|
splitted := strings.Split(strings.Split(url, gceDomainSufix)[1], "/")
|
||||||
if len(splitted) != 5 || splitted[1] != "zones" {
|
if len(splitted) != 5 || splitted[1] != "zones" {
|
||||||
return "", "", "", errMsg
|
return "", "", "", errMsg
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package gceurl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseUrl(t *testing.T) {
|
||||||
|
proj, zone, name, err := parseGceUrl("https://www.googleapis.com/compute/v1/projects/mwielgus-proj/zones/us-central1-b/instanceGroups/kubernetes-minion-group", "instanceGroups")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "mwielgus-proj", proj)
|
||||||
|
assert.Equal(t, "us-central1-b", zone)
|
||||||
|
assert.Equal(t, "kubernetes-minion-group", name)
|
||||||
|
|
||||||
|
proj, zone, name, err = parseGceUrl("https://content.googleapis.com/compute/v1/projects/mwielgus-proj/zones/us-central1-b/instanceGroups/kubernetes-minion-group", "instanceGroups")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "mwielgus-proj", proj)
|
||||||
|
assert.Equal(t, "us-central1-b", zone)
|
||||||
|
assert.Equal(t, "kubernetes-minion-group", name)
|
||||||
|
|
||||||
|
proj, zone, name, err = parseGceUrl("www.onet.pl", "instanceGroups")
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
|
proj, zone, name, err = parseGceUrl("https://content.googleapis.com/compute/vabc/projects/mwielgus-proj/zones/us-central1-b/instanceGroups/kubernetes-minion-group", "instanceGroups")
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue