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
189d4a51
Commit
189d4a51
authored
Dec 19, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make CIdentifier return error strings
parent
d07328dc
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
18 additions
and
11 deletions
+18
-11
validation.go
pkg/api/validation/validation.go
+4
-3
validation_test.go
pkg/api/validation/validation_test.go
+1
-1
run.go
pkg/kubectl/run.go
+5
-2
validation.go
pkg/util/validation/validation.go
+5
-2
validation_test.go
pkg/util/validation/validation_test.go
+3
-3
No files found.
pkg/api/validation/validation.go
View file @
189d4a51
...
...
@@ -51,7 +51,6 @@ var RepairMalformedUpdates bool = true
const
isNegativeErrorMsg
string
=
`must be greater than or equal to 0`
const
isInvalidQuotaResource
string
=
`must be a standard resource for quota`
const
fieldImmutableErrorMsg
string
=
`field is immutable`
const
cIdentifierErrorMsg
string
=
`must be a C identifier (matching regex `
+
validation
.
CIdentifierFmt
+
`): e.g. "my_name" or "MyName"`
const
isNotIntegerErrorMsg
string
=
`must be an integer`
func
InclusiveRangeErrorMsg
(
lo
,
hi
int
)
string
{
...
...
@@ -1087,8 +1086,10 @@ func validateEnv(vars []api.EnvVar, fldPath *field.Path) field.ErrorList {
idxPath
:=
fldPath
.
Index
(
i
)
if
len
(
ev
.
Name
)
==
0
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
idxPath
.
Child
(
"name"
),
""
))
}
else
if
!
validation
.
IsCIdentifier
(
ev
.
Name
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
idxPath
.
Child
(
"name"
),
ev
.
Name
,
cIdentifierErrorMsg
))
}
else
{
for
_
,
msg
:=
range
validation
.
IsCIdentifier
(
ev
.
Name
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
idxPath
.
Child
(
"name"
),
ev
.
Name
,
msg
))
}
}
allErrs
=
append
(
allErrs
,
validateEnvVarValueFrom
(
ev
,
idxPath
.
Child
(
"valueFrom"
))
...
)
}
...
...
pkg/api/validation/validation_test.go
View file @
189d4a51
...
...
@@ -1155,7 +1155,7 @@ func TestValidateEnv(t *testing.T) {
{
name
:
"name not a C identifier"
,
envs
:
[]
api
.
EnvVar
{{
Name
:
"a.b.c"
}},
expectedError
:
`[0].name: Invalid value: "a.b.c": must
be a C identifier (matching regex [A-Za-z_][A-Za-z0-9_]*): e.g. "my_name" or "MyName"
`
,
expectedError
:
`[0].name: Invalid value: "a.b.c": must
match the regex
`
,
},
{
name
:
"value and valueFrom specified"
,
...
...
pkg/kubectl/run.go
View file @
189d4a51
...
...
@@ -835,7 +835,10 @@ func parseEnvs(envArray []string) ([]api.EnvVar, error) {
}
name
:=
env
[
:
pos
]
value
:=
env
[
pos
+
1
:
]
if
len
(
name
)
==
0
||
!
validation
.
IsCIdentifier
(
name
)
||
len
(
value
)
==
0
{
if
len
(
name
)
==
0
||
len
(
value
)
==
0
{
return
nil
,
fmt
.
Errorf
(
"invalid env: %v"
,
env
)
}
if
len
(
validation
.
IsCIdentifier
(
name
))
!=
0
{
return
nil
,
fmt
.
Errorf
(
"invalid env: %v"
,
env
)
}
envVar
:=
api
.
EnvVar
{
Name
:
name
,
Value
:
value
}
...
...
@@ -853,7 +856,7 @@ func parseV1Envs(envArray []string) ([]v1.EnvVar, error) {
}
name
:=
env
[
:
pos
]
value
:=
env
[
pos
+
1
:
]
if
len
(
name
)
==
0
||
!
validation
.
IsCIdentifier
(
name
)
||
len
(
value
)
==
0
{
if
len
(
name
)
==
0
||
len
(
validation
.
IsCIdentifier
(
name
))
!=
0
||
len
(
value
)
==
0
{
return
nil
,
fmt
.
Errorf
(
"invalid env: %v"
,
env
)
}
envVar
:=
v1
.
EnvVar
{
Name
:
name
,
Value
:
value
}
...
...
pkg/util/validation/validation.go
View file @
189d4a51
...
...
@@ -145,8 +145,11 @@ var cIdentifierRegexp = regexp.MustCompile("^" + CIdentifierFmt + "$")
// IsCIdentifier tests for a string that conforms the definition of an identifier
// in C. This checks the format, but not the length.
func
IsCIdentifier
(
value
string
)
bool
{
return
cIdentifierRegexp
.
MatchString
(
value
)
func
IsCIdentifier
(
value
string
)
[]
string
{
if
!
cIdentifierRegexp
.
MatchString
(
value
)
{
return
[]
string
{
RegexError
(
CIdentifierFmt
,
"my_name"
,
"MY_NAME"
,
"MyName"
)}
}
return
nil
}
// IsValidPortNum tests that the argument is a valid, non-zero port number.
...
...
pkg/util/validation/validation_test.go
View file @
189d4a51
...
...
@@ -119,8 +119,8 @@ func TestIsCIdentifier(t *testing.T) {
"A"
,
"AB"
,
"AbC"
,
"A1"
,
"_A"
,
"A_"
,
"A_B"
,
"A_1"
,
"A__1__2__B"
,
"__123_ABC"
,
}
for
_
,
val
:=
range
goodValues
{
if
!
IsCIdentifier
(
val
)
{
t
.
Errorf
(
"expected true for '%s'
"
,
val
)
if
msgs
:=
IsCIdentifier
(
val
);
len
(
msgs
)
!=
0
{
t
.
Errorf
(
"expected true for '%s'
: %v"
,
val
,
msgs
)
}
}
...
...
@@ -132,7 +132,7 @@ func TestIsCIdentifier(t *testing.T) {
"#a#"
,
}
for
_
,
val
:=
range
badValues
{
if
IsCIdentifier
(
val
)
{
if
msgs
:=
IsCIdentifier
(
val
);
len
(
msgs
)
==
0
{
t
.
Errorf
(
"expected false for '%s'"
,
val
)
}
}
...
...
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