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
792a2299
Commit
792a2299
authored
Dec 04, 2017
by
Di Xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
forbid unnamed context
parent
050956b0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
5 deletions
+43
-5
use_context.go
pkg/kubectl/cmd/config/use_context.go
+1
-1
webhook_test.go
...ing/src/k8s.io/apiserver/pkg/util/webhook/webhook_test.go
+9
-4
validation.go
staging/src/k8s.io/client-go/tools/clientcmd/validation.go
+4
-0
validation_test.go
...g/src/k8s.io/client-go/tools/clientcmd/validation_test.go
+29
-0
No files found.
pkg/kubectl/cmd/config/use_context.go
View file @
792a2299
...
@@ -88,7 +88,7 @@ func (o *useContextOptions) complete(cmd *cobra.Command) error {
...
@@ -88,7 +88,7 @@ func (o *useContextOptions) complete(cmd *cobra.Command) error {
func
(
o
useContextOptions
)
validate
(
config
*
clientcmdapi
.
Config
)
error
{
func
(
o
useContextOptions
)
validate
(
config
*
clientcmdapi
.
Config
)
error
{
if
len
(
o
.
contextName
)
==
0
{
if
len
(
o
.
contextName
)
==
0
{
return
errors
.
New
(
"
you must specify a current-context
"
)
return
errors
.
New
(
"
empty context names are not allowed
"
)
}
}
for
name
:=
range
config
.
Contexts
{
for
name
:=
range
config
.
Contexts
{
...
...
staging/src/k8s.io/apiserver/pkg/util/webhook/webhook_test.go
View file @
792a2299
...
@@ -114,15 +114,15 @@ func TestKubeConfigFile(t *testing.T) {
...
@@ -114,15 +114,15 @@ func TestKubeConfigFile(t *testing.T) {
errRegex
:
errNoConfiguration
,
errRegex
:
errNoConfiguration
,
},
},
{
{
test
:
"missing context (specified context is missing)"
,
test
:
"missing context (specified context is missing)"
,
cluster
:
&
namedCluster
,
cluster
:
&
namedCluster
,
currentContext
:
"missing-context"
,
errRegex
:
errNoConfiguration
,
errRegex
:
errNoConfiguration
,
},
},
{
{
test
:
"context without cluster"
,
test
:
"context without cluster"
,
context
:
&
v1
.
NamedContext
{
context
:
&
v1
.
NamedContext
{
Context
:
v1
.
Context
{},
Context
:
v1
.
Context
{},
Name
:
"testing-context"
,
},
},
currentContext
:
"testing-context"
,
currentContext
:
"testing-context"
,
errRegex
:
errNoConfiguration
,
errRegex
:
errNoConfiguration
,
...
@@ -134,6 +134,7 @@ func TestKubeConfigFile(t *testing.T) {
...
@@ -134,6 +134,7 @@ func TestKubeConfigFile(t *testing.T) {
Context
:
v1
.
Context
{
Context
:
v1
.
Context
{
Cluster
:
namedCluster
.
Name
,
Cluster
:
namedCluster
.
Name
,
},
},
Name
:
"testing-context"
,
},
},
currentContext
:
"testing-context"
,
currentContext
:
"testing-context"
,
errRegex
:
""
,
// Not an error at parse time, only when using the webhook
errRegex
:
""
,
// Not an error at parse time, only when using the webhook
...
@@ -145,6 +146,7 @@ func TestKubeConfigFile(t *testing.T) {
...
@@ -145,6 +146,7 @@ func TestKubeConfigFile(t *testing.T) {
Context
:
v1
.
Context
{
Context
:
v1
.
Context
{
Cluster
:
"missing-cluster"
,
Cluster
:
"missing-cluster"
,
},
},
Name
:
"fake"
,
},
},
errRegex
:
errNoConfiguration
,
errRegex
:
errNoConfiguration
,
},
},
...
@@ -156,6 +158,7 @@ func TestKubeConfigFile(t *testing.T) {
...
@@ -156,6 +158,7 @@ func TestKubeConfigFile(t *testing.T) {
Cluster
:
namedCluster
.
Name
,
Cluster
:
namedCluster
.
Name
,
AuthInfo
:
"missing-user"
,
AuthInfo
:
"missing-user"
,
},
},
Name
:
"testing-context"
,
},
},
currentContext
:
"testing-context"
,
currentContext
:
"testing-context"
,
errRegex
:
""
,
// Not an error at parse time, only when using the webhook
errRegex
:
""
,
// Not an error at parse time, only when using the webhook
...
@@ -267,6 +270,8 @@ func TestKubeConfigFile(t *testing.T) {
...
@@ -267,6 +270,8 @@ func TestKubeConfigFile(t *testing.T) {
kubeConfig
.
AuthInfos
=
[]
v1
.
NamedAuthInfo
{
*
tt
.
user
}
kubeConfig
.
AuthInfos
=
[]
v1
.
NamedAuthInfo
{
*
tt
.
user
}
}
}
kubeConfig
.
CurrentContext
=
tt
.
currentContext
kubeConfigFile
,
err
:=
newKubeConfigFile
(
kubeConfig
)
kubeConfigFile
,
err
:=
newKubeConfigFile
(
kubeConfig
)
if
err
==
nil
{
if
err
==
nil
{
...
...
staging/src/k8s.io/client-go/tools/clientcmd/validation.go
View file @
792a2299
...
@@ -253,6 +253,10 @@ func validateAuthInfo(authInfoName string, authInfo clientcmdapi.AuthInfo) []err
...
@@ -253,6 +253,10 @@ func validateAuthInfo(authInfoName string, authInfo clientcmdapi.AuthInfo) []err
func
validateContext
(
contextName
string
,
context
clientcmdapi
.
Context
,
config
clientcmdapi
.
Config
)
[]
error
{
func
validateContext
(
contextName
string
,
context
clientcmdapi
.
Context
,
config
clientcmdapi
.
Config
)
[]
error
{
validationErrors
:=
make
([]
error
,
0
)
validationErrors
:=
make
([]
error
,
0
)
if
len
(
contextName
)
==
0
{
validationErrors
=
append
(
validationErrors
,
fmt
.
Errorf
(
"empty context name for %#v is not allowed"
,
context
))
}
if
len
(
context
.
AuthInfo
)
==
0
{
if
len
(
context
.
AuthInfo
)
==
0
{
validationErrors
=
append
(
validationErrors
,
fmt
.
Errorf
(
"user was not specified for context %q"
,
contextName
))
validationErrors
=
append
(
validationErrors
,
fmt
.
Errorf
(
"user was not specified for context %q"
,
contextName
))
}
else
if
_
,
exists
:=
config
.
AuthInfos
[
context
.
AuthInfo
];
!
exists
{
}
else
if
_
,
exists
:=
config
.
AuthInfos
[
context
.
AuthInfo
];
!
exists
{
...
...
staging/src/k8s.io/client-go/tools/clientcmd/validation_test.go
View file @
792a2299
...
@@ -62,6 +62,7 @@ func TestConfirmUsableBadInfoButOkConfig(t *testing.T) {
...
@@ -62,6 +62,7 @@ func TestConfirmUsableBadInfoButOkConfig(t *testing.T) {
okTest
.
testConfirmUsable
(
"clean"
,
t
)
okTest
.
testConfirmUsable
(
"clean"
,
t
)
badValidation
.
testConfig
(
t
)
badValidation
.
testConfig
(
t
)
}
}
func
TestConfirmUsableBadInfoConfig
(
t
*
testing
.
T
)
{
func
TestConfirmUsableBadInfoConfig
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
Clusters
[
"missing ca"
]
=
&
clientcmdapi
.
Cluster
{
config
.
Clusters
[
"missing ca"
]
=
&
clientcmdapi
.
Cluster
{
...
@@ -83,6 +84,7 @@ func TestConfirmUsableBadInfoConfig(t *testing.T) {
...
@@ -83,6 +84,7 @@ func TestConfirmUsableBadInfoConfig(t *testing.T) {
test
.
testConfirmUsable
(
"first"
,
t
)
test
.
testConfirmUsable
(
"first"
,
t
)
}
}
func
TestConfirmUsableEmptyConfig
(
t
*
testing
.
T
)
{
func
TestConfirmUsableEmptyConfig
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
test
:=
configValidationTest
{
test
:=
configValidationTest
{
...
@@ -92,6 +94,7 @@ func TestConfirmUsableEmptyConfig(t *testing.T) {
...
@@ -92,6 +94,7 @@ func TestConfirmUsableEmptyConfig(t *testing.T) {
test
.
testConfirmUsable
(
""
,
t
)
test
.
testConfirmUsable
(
""
,
t
)
}
}
func
TestConfirmUsableMissingConfig
(
t
*
testing
.
T
)
{
func
TestConfirmUsableMissingConfig
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
test
:=
configValidationTest
{
test
:=
configValidationTest
{
...
@@ -101,6 +104,7 @@ func TestConfirmUsableMissingConfig(t *testing.T) {
...
@@ -101,6 +104,7 @@ func TestConfirmUsableMissingConfig(t *testing.T) {
test
.
testConfirmUsable
(
"not-here"
,
t
)
test
.
testConfirmUsable
(
"not-here"
,
t
)
}
}
func
TestValidateEmptyConfig
(
t
*
testing
.
T
)
{
func
TestValidateEmptyConfig
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
test
:=
configValidationTest
{
test
:=
configValidationTest
{
...
@@ -110,6 +114,7 @@ func TestValidateEmptyConfig(t *testing.T) {
...
@@ -110,6 +114,7 @@ func TestValidateEmptyConfig(t *testing.T) {
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateMissingCurrentContextConfig
(
t
*
testing
.
T
)
{
func
TestValidateMissingCurrentContextConfig
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
CurrentContext
=
"anything"
config
.
CurrentContext
=
"anything"
...
@@ -120,6 +125,7 @@ func TestValidateMissingCurrentContextConfig(t *testing.T) {
...
@@ -120,6 +125,7 @@ func TestValidateMissingCurrentContextConfig(t *testing.T) {
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestIsContextNotFound
(
t
*
testing
.
T
)
{
func
TestIsContextNotFound
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
CurrentContext
=
"anything"
config
.
CurrentContext
=
"anything"
...
@@ -172,6 +178,7 @@ func TestValidateMissingReferencesConfig(t *testing.T) {
...
@@ -172,6 +178,7 @@ func TestValidateMissingReferencesConfig(t *testing.T) {
test
.
testContext
(
"anything"
,
t
)
test
.
testContext
(
"anything"
,
t
)
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateEmptyContext
(
t
*
testing
.
T
)
{
func
TestValidateEmptyContext
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
CurrentContext
=
"anything"
config
.
CurrentContext
=
"anything"
...
@@ -185,6 +192,19 @@ func TestValidateEmptyContext(t *testing.T) {
...
@@ -185,6 +192,19 @@ func TestValidateEmptyContext(t *testing.T) {
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateEmptyContextName
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
.
CurrentContext
=
"anything"
config
.
Contexts
[
""
]
=
&
clientcmdapi
.
Context
{
Cluster
:
"missing"
,
AuthInfo
:
"missing"
}
test
:=
configValidationTest
{
config
:
config
,
expectedErrorSubstring
:
[]
string
{
"empty context name"
,
"is not allowed"
},
}
test
.
testContext
(
""
,
t
)
test
.
testConfig
(
t
)
}
func
TestValidateEmptyClusterInfo
(
t
*
testing
.
T
)
{
func
TestValidateEmptyClusterInfo
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
Clusters
[
"empty"
]
=
clientcmdapi
.
NewCluster
()
config
.
Clusters
[
"empty"
]
=
clientcmdapi
.
NewCluster
()
...
@@ -223,6 +243,7 @@ func TestValidateMissingCAFileClusterInfo(t *testing.T) {
...
@@ -223,6 +243,7 @@ func TestValidateMissingCAFileClusterInfo(t *testing.T) {
test
.
testCluster
(
"missing ca"
,
t
)
test
.
testCluster
(
"missing ca"
,
t
)
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateCleanClusterInfo
(
t
*
testing
.
T
)
{
func
TestValidateCleanClusterInfo
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
Clusters
[
"clean"
]
=
&
clientcmdapi
.
Cluster
{
config
.
Clusters
[
"clean"
]
=
&
clientcmdapi
.
Cluster
{
...
@@ -235,6 +256,7 @@ func TestValidateCleanClusterInfo(t *testing.T) {
...
@@ -235,6 +256,7 @@ func TestValidateCleanClusterInfo(t *testing.T) {
test
.
testCluster
(
"clean"
,
t
)
test
.
testCluster
(
"clean"
,
t
)
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateCleanWithCAClusterInfo
(
t
*
testing
.
T
)
{
func
TestValidateCleanWithCAClusterInfo
(
t
*
testing
.
T
)
{
tempFile
,
_
:=
ioutil
.
TempFile
(
""
,
""
)
tempFile
,
_
:=
ioutil
.
TempFile
(
""
,
""
)
defer
os
.
Remove
(
tempFile
.
Name
())
defer
os
.
Remove
(
tempFile
.
Name
())
...
@@ -262,6 +284,7 @@ func TestValidateEmptyAuthInfo(t *testing.T) {
...
@@ -262,6 +284,7 @@ func TestValidateEmptyAuthInfo(t *testing.T) {
test
.
testAuthInfo
(
"error"
,
t
)
test
.
testAuthInfo
(
"error"
,
t
)
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateCertFilesNotFoundAuthInfo
(
t
*
testing
.
T
)
{
func
TestValidateCertFilesNotFoundAuthInfo
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
AuthInfos
[
"error"
]
=
&
clientcmdapi
.
AuthInfo
{
config
.
AuthInfos
[
"error"
]
=
&
clientcmdapi
.
AuthInfo
{
...
@@ -276,6 +299,7 @@ func TestValidateCertFilesNotFoundAuthInfo(t *testing.T) {
...
@@ -276,6 +299,7 @@ func TestValidateCertFilesNotFoundAuthInfo(t *testing.T) {
test
.
testAuthInfo
(
"error"
,
t
)
test
.
testAuthInfo
(
"error"
,
t
)
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateCertDataOverridesFiles
(
t
*
testing
.
T
)
{
func
TestValidateCertDataOverridesFiles
(
t
*
testing
.
T
)
{
tempFile
,
_
:=
ioutil
.
TempFile
(
""
,
""
)
tempFile
,
_
:=
ioutil
.
TempFile
(
""
,
""
)
defer
os
.
Remove
(
tempFile
.
Name
())
defer
os
.
Remove
(
tempFile
.
Name
())
...
@@ -295,6 +319,7 @@ func TestValidateCertDataOverridesFiles(t *testing.T) {
...
@@ -295,6 +319,7 @@ func TestValidateCertDataOverridesFiles(t *testing.T) {
test
.
testAuthInfo
(
"clean"
,
t
)
test
.
testAuthInfo
(
"clean"
,
t
)
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateCleanCertFilesAuthInfo
(
t
*
testing
.
T
)
{
func
TestValidateCleanCertFilesAuthInfo
(
t
*
testing
.
T
)
{
tempFile
,
_
:=
ioutil
.
TempFile
(
""
,
""
)
tempFile
,
_
:=
ioutil
.
TempFile
(
""
,
""
)
defer
os
.
Remove
(
tempFile
.
Name
())
defer
os
.
Remove
(
tempFile
.
Name
())
...
@@ -311,6 +336,7 @@ func TestValidateCleanCertFilesAuthInfo(t *testing.T) {
...
@@ -311,6 +336,7 @@ func TestValidateCleanCertFilesAuthInfo(t *testing.T) {
test
.
testAuthInfo
(
"clean"
,
t
)
test
.
testAuthInfo
(
"clean"
,
t
)
test
.
testConfig
(
t
)
test
.
testConfig
(
t
)
}
}
func
TestValidateCleanTokenAuthInfo
(
t
*
testing
.
T
)
{
func
TestValidateCleanTokenAuthInfo
(
t
*
testing
.
T
)
{
config
:=
clientcmdapi
.
NewConfig
()
config
:=
clientcmdapi
.
NewConfig
()
config
.
AuthInfos
[
"clean"
]
=
&
clientcmdapi
.
AuthInfo
{
config
.
AuthInfos
[
"clean"
]
=
&
clientcmdapi
.
AuthInfo
{
...
@@ -363,6 +389,7 @@ func (c configValidationTest) testContext(contextName string, t *testing.T) {
...
@@ -363,6 +389,7 @@ func (c configValidationTest) testContext(contextName string, t *testing.T) {
}
}
}
}
}
}
func
(
c
configValidationTest
)
testConfirmUsable
(
contextName
string
,
t
*
testing
.
T
)
{
func
(
c
configValidationTest
)
testConfirmUsable
(
contextName
string
,
t
*
testing
.
T
)
{
err
:=
ConfirmUsable
(
*
c
.
config
,
contextName
)
err
:=
ConfirmUsable
(
*
c
.
config
,
contextName
)
...
@@ -382,6 +409,7 @@ func (c configValidationTest) testConfirmUsable(contextName string, t *testing.T
...
@@ -382,6 +409,7 @@ func (c configValidationTest) testConfirmUsable(contextName string, t *testing.T
}
}
}
}
}
}
func
(
c
configValidationTest
)
testConfig
(
t
*
testing
.
T
)
{
func
(
c
configValidationTest
)
testConfig
(
t
*
testing
.
T
)
{
err
:=
Validate
(
*
c
.
config
)
err
:=
Validate
(
*
c
.
config
)
...
@@ -404,6 +432,7 @@ func (c configValidationTest) testConfig(t *testing.T) {
...
@@ -404,6 +432,7 @@ func (c configValidationTest) testConfig(t *testing.T) {
}
}
}
}
}
}
func
(
c
configValidationTest
)
testCluster
(
clusterName
string
,
t
*
testing
.
T
)
{
func
(
c
configValidationTest
)
testCluster
(
clusterName
string
,
t
*
testing
.
T
)
{
errs
:=
validateClusterInfo
(
clusterName
,
*
c
.
config
.
Clusters
[
clusterName
])
errs
:=
validateClusterInfo
(
clusterName
,
*
c
.
config
.
Clusters
[
clusterName
])
...
...
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