Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
a2b222c9
Commit
a2b222c9
authored
Jan 19, 2018
by
Bowei Du
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
GCE: Fix Valid() to check for proper region/zone names
parent
47b89aaf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
11 deletions
+24
-11
key.go
pkg/cloudprovider/providers/gce/cloud/meta/key.go
+13
-1
key_test.go
pkg/cloudprovider/providers/gce/cloud/meta/key_test.go
+11
-10
No files found.
pkg/cloudprovider/providers/gce/cloud/meta/key.go
View file @
a2b222c9
...
@@ -18,6 +18,7 @@ package meta
...
@@ -18,6 +18,7 @@ package meta
import
(
import
(
"fmt"
"fmt"
"regexp"
)
)
// Key for a GCP resource.
// Key for a GCP resource.
...
@@ -39,6 +40,11 @@ const (
...
@@ -39,6 +40,11 @@ const (
Global
=
"global"
Global
=
"global"
)
)
var
(
// locationRegexp is the format of regions/zone names in GCE.
locationRegexp
=
regexp
.
MustCompile
(
"^[a-z](?:[-a-z0-9]+)?$"
)
)
// ZonalKey returns the key for a zonal resource.
// ZonalKey returns the key for a zonal resource.
func
ZonalKey
(
name
,
zone
string
)
*
Key
{
func
ZonalKey
(
name
,
zone
string
)
*
Key
{
return
&
Key
{
name
,
zone
,
""
}
return
&
Key
{
name
,
zone
,
""
}
...
@@ -79,10 +85,16 @@ func (k Key) String() string {
...
@@ -79,10 +85,16 @@ func (k Key) String() string {
}
}
// Valid is true if the key is valid.
// Valid is true if the key is valid.
func
(
k
*
Key
)
Valid
(
typeName
string
)
bool
{
func
(
k
*
Key
)
Valid
()
bool
{
if
k
.
Zone
!=
""
&&
k
.
Region
!=
""
{
if
k
.
Zone
!=
""
&&
k
.
Region
!=
""
{
return
false
return
false
}
}
switch
{
case
k
.
Region
!=
""
:
return
locationRegexp
.
Match
([]
byte
(
k
.
Region
))
case
k
.
Zone
!=
""
:
return
locationRegexp
.
Match
([]
byte
(
k
.
Zone
))
}
return
true
return
true
}
}
...
...
pkg/cloudprovider/providers/gce/cloud/meta/key_test.go
View file @
a2b222c9
...
@@ -58,18 +58,19 @@ func TestKeyValid(t *testing.T) {
...
@@ -58,18 +58,19 @@ func TestKeyValid(t *testing.T) {
zone
:=
"us-central1-b"
zone
:=
"us-central1-b"
for
_
,
tc
:=
range
[]
struct
{
for
_
,
tc
:=
range
[]
struct
{
key
*
Key
key
*
Key
typeName
string
want
bool
want
bool
}{
}{
// Note: these test cases need to be synchronized with the
{
GlobalKey
(
"abc"
),
true
},
// actual settings for each type.
{
RegionalKey
(
"abc"
,
region
),
true
},
{
GlobalKey
(
"abc"
),
"UrlMap"
,
true
},
{
ZonalKey
(
"abc"
,
zone
),
true
},
{
&
Key
{
"abc"
,
zone
,
region
},
"UrlMap"
,
false
},
{
RegionalKey
(
"abc"
,
"/invalid/"
),
false
},
{
ZonalKey
(
"abc"
,
"/invalid/"
),
false
},
{
&
Key
{
"abc"
,
zone
,
region
},
false
},
}
{
}
{
valid
:=
tc
.
key
.
Valid
(
tc
.
typeName
)
got
:=
tc
.
key
.
Valid
(
)
if
valid
!=
tc
.
want
{
if
got
!=
tc
.
want
{
t
.
Errorf
(
"key %+v
, type %v; key.Valid() = %v, want %v"
,
tc
.
key
,
tc
.
typeName
,
valid
,
tc
.
want
)
t
.
Errorf
(
"key %+v
; key.Valid() = %v, want %v"
,
tc
.
key
,
got
,
tc
.
want
)
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment