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
9e23ad71
Commit
9e23ad71
authored
Jul 01, 2015
by
Zach Loafman
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #10510 from mikedanese/kubectl-context-err
make use-context return error when context does not exist
parents
00193bf1
aea34037
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
14 deletions
+42
-14
config_test.go
pkg/kubectl/cmd/config/config_test.go
+29
-9
use_context.go
pkg/kubectl/cmd/config/use_context.go
+13
-5
No files found.
pkg/kubectl/cmd/config/config_test.go
View file @
9e23ad71
...
@@ -44,13 +44,6 @@ func newRedFederalCowHammerConfig() clientcmdapi.Config {
...
@@ -44,13 +44,6 @@ func newRedFederalCowHammerConfig() clientcmdapi.Config {
}
}
}
}
type
configCommandTest
struct
{
args
[]
string
startingConfig
clientcmdapi
.
Config
expectedConfig
clientcmdapi
.
Config
expectedOutputs
[]
string
}
func
ExampleView
()
{
func
ExampleView
()
{
expectedConfig
:=
newRedFederalCowHammerConfig
()
expectedConfig
:=
newRedFederalCowHammerConfig
()
test
:=
configCommandTest
{
test
:=
configCommandTest
{
...
@@ -83,16 +76,36 @@ func ExampleView() {
...
@@ -83,16 +76,36 @@ func ExampleView() {
func
TestSetCurrentContext
(
t
*
testing
.
T
)
{
func
TestSetCurrentContext
(
t
*
testing
.
T
)
{
expectedConfig
:=
newRedFederalCowHammerConfig
()
expectedConfig
:=
newRedFederalCowHammerConfig
()
expectedConfig
.
CurrentContext
=
"the-new-context"
startingConfig
:=
newRedFederalCowHammerConfig
()
newContextName
:=
"the-new-context"
newContext
:=
clientcmdapi
.
NewContext
()
startingConfig
.
Contexts
[
newContextName
]
=
*
newContext
expectedConfig
.
Contexts
[
newContextName
]
=
*
newContext
expectedConfig
.
CurrentContext
=
newContextName
test
:=
configCommandTest
{
test
:=
configCommandTest
{
args
:
[]
string
{
"use-context"
,
"the-new-context"
},
args
:
[]
string
{
"use-context"
,
"the-new-context"
},
startingConfig
:
newRedFederalCowHammerConfig
()
,
startingConfig
:
startingConfig
,
expectedConfig
:
expectedConfig
,
expectedConfig
:
expectedConfig
,
}
}
test
.
run
(
t
)
test
.
run
(
t
)
}
}
func
TestSetNonExistantContext
(
t
*
testing
.
T
)
{
expectedConfig
:=
newRedFederalCowHammerConfig
()
test
:=
configCommandTest
{
args
:
[]
string
{
"use-context"
,
"non-existant-config"
},
startingConfig
:
expectedConfig
,
expectedConfig
:
expectedConfig
,
expectedOutputs
:
[]
string
{
`No context exists with the name: "non-existant-config"`
},
}
test
.
run
(
t
)
}
func
TestSetIntoExistingStruct
(
t
*
testing
.
T
)
{
func
TestSetIntoExistingStruct
(
t
*
testing
.
T
)
{
expectedConfig
:=
newRedFederalCowHammerConfig
()
expectedConfig
:=
newRedFederalCowHammerConfig
()
a
:=
expectedConfig
.
AuthInfos
[
"red-user"
]
a
:=
expectedConfig
.
AuthInfos
[
"red-user"
]
...
@@ -691,6 +704,13 @@ func testConfigCommand(args []string, startingConfig clientcmdapi.Config) (strin
...
@@ -691,6 +704,13 @@ func testConfigCommand(args []string, startingConfig clientcmdapi.Config) (strin
return
buf
.
String
(),
*
config
return
buf
.
String
(),
*
config
}
}
type
configCommandTest
struct
{
args
[]
string
startingConfig
clientcmdapi
.
Config
expectedConfig
clientcmdapi
.
Config
expectedOutputs
[]
string
}
func
(
test
configCommandTest
)
run
(
t
*
testing
.
T
)
string
{
func
(
test
configCommandTest
)
run
(
t
*
testing
.
T
)
string
{
out
,
actualConfig
:=
testConfigCommand
(
test
.
args
,
test
.
startingConfig
)
out
,
actualConfig
:=
testConfigCommand
(
test
.
args
,
test
.
startingConfig
)
...
...
pkg/kubectl/cmd/config/use_context.go
View file @
9e23ad71
...
@@ -22,6 +22,8 @@ import (
...
@@ -22,6 +22,8 @@ import (
"io"
"io"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
clientcmdapi
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
)
)
type
useContextOptions
struct
{
type
useContextOptions
struct
{
...
@@ -43,7 +45,7 @@ func NewCmdConfigUseContext(out io.Writer, configAccess ConfigAccess) *cobra.Com
...
@@ -43,7 +45,7 @@ func NewCmdConfigUseContext(out io.Writer, configAccess ConfigAccess) *cobra.Com
err
:=
options
.
run
()
err
:=
options
.
run
()
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Printf
(
"%v
\n
"
,
err
)
fmt
.
Fprintf
(
out
,
"%v
\n
"
,
err
)
}
}
},
},
}
}
...
@@ -52,12 +54,12 @@ func NewCmdConfigUseContext(out io.Writer, configAccess ConfigAccess) *cobra.Com
...
@@ -52,12 +54,12 @@ func NewCmdConfigUseContext(out io.Writer, configAccess ConfigAccess) *cobra.Com
}
}
func
(
o
useContextOptions
)
run
()
error
{
func
(
o
useContextOptions
)
run
()
error
{
err
:=
o
.
validate
()
config
,
err
:=
o
.
configAccess
.
GetStartingConfig
()
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
config
,
err
:=
o
.
configAccess
.
GetStartingConfig
(
)
err
=
o
.
validate
(
config
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -82,10 +84,16 @@ func (o *useContextOptions) complete(cmd *cobra.Command) bool {
...
@@ -82,10 +84,16 @@ func (o *useContextOptions) complete(cmd *cobra.Command) bool {
return
true
return
true
}
}
func
(
o
useContextOptions
)
validate
()
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
(
"You must specify a current-context"
)
}
}
return
nil
for
name
:=
range
config
.
Contexts
{
if
name
==
o
.
contextName
{
return
nil
}
}
return
fmt
.
Errorf
(
"No context exists with the name: %q."
,
o
.
contextName
)
}
}
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