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
56930674
Commit
56930674
authored
Sep 24, 2017
by
zhengjiajin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug(cli)fix kubectl config unset unexist map key will add this key, should tell…
bug(cli)fix kubectl config unset unexist map key will add this key, should tell user this key not exist
parent
5f8726e4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
16 deletions
+55
-16
set.go
pkg/kubectl/cmd/config/set.go
+3
-0
unset.go
pkg/kubectl/cmd/config/unset.go
+15
-10
unset_test.go
pkg/kubectl/cmd/config/unset_test.go
+37
-6
No files found.
pkg/kubectl/cmd/config/set.go
View file @
56930674
...
...
@@ -152,6 +152,9 @@ func modifyConfig(curr reflect.Value, steps *navigationSteps, propertyValue stri
needToSetNewMapValue
:=
currMapValue
.
Kind
()
==
reflect
.
Invalid
if
needToSetNewMapValue
{
if
unset
{
return
fmt
.
Errorf
(
"current map key `%v` is invalid"
,
mapKey
.
Interface
())
}
currMapValue
=
reflect
.
New
(
mapValueType
.
Elem
())
.
Elem
()
.
Addr
()
actualCurrValue
.
SetMapIndex
(
mapKey
,
currMapValue
)
}
...
...
pkg/kubectl/cmd/config/unset.go
View file @
56930674
...
...
@@ -48,16 +48,16 @@ func NewCmdConfigUnset(out io.Writer, configAccess clientcmd.ConfigAccess) *cobr
Short
:
i18n
.
T
(
"Unsets an individual value in a kubeconfig file"
),
Long
:
unset_long
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
cmdutil
.
CheckErr
(
options
.
complete
(
cmd
))
cmdutil
.
CheckErr
(
options
.
run
())
fmt
.
Fprintf
(
out
,
"Property %q unset.
\n
"
,
options
.
propertyName
)
cmdutil
.
CheckErr
(
options
.
complete
(
cmd
,
args
))
cmdutil
.
CheckErr
(
options
.
run
(
out
))
},
}
return
cmd
}
func
(
o
unsetOptions
)
run
()
error
{
func
(
o
unsetOptions
)
run
(
out
io
.
Writer
)
error
{
err
:=
o
.
validate
()
if
err
!=
nil
{
return
err
...
...
@@ -77,16 +77,21 @@ func (o unsetOptions) run() error {
return
err
}
return
clientcmd
.
ModifyConfig
(
o
.
configAccess
,
*
config
,
false
)
if
err
:=
clientcmd
.
ModifyConfig
(
o
.
configAccess
,
*
config
,
false
);
err
!=
nil
{
return
err
}
if
_
,
err
:=
fmt
.
Fprintf
(
out
,
"Property %q unset.
\n
"
,
o
.
propertyName
);
err
!=
nil
{
return
err
}
return
nil
}
func
(
o
*
unsetOptions
)
complete
(
cmd
*
cobra
.
Command
)
error
{
endingArgs
:=
cmd
.
Flags
()
.
Args
()
if
len
(
endingArgs
)
!=
1
{
return
helpErrorf
(
cmd
,
"Unexpected args: %v"
,
endingArgs
)
func
(
o
*
unsetOptions
)
complete
(
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
if
len
(
args
)
!=
1
{
return
helpErrorf
(
cmd
,
"Unexpected args: %v"
,
args
)
}
o
.
propertyName
=
endingA
rgs
[
0
]
o
.
propertyName
=
a
rgs
[
0
]
return
nil
}
...
...
pkg/kubectl/cmd/config/unset_test.go
View file @
56930674
...
...
@@ -28,9 +28,10 @@ import (
type
unsetConfigTest
struct
{
description
string
config
clientcmdapi
.
Config
//initiate kubectl config
args
[]
string
//kubectl config unset args
expected
string
//expect out
config
clientcmdapi
.
Config
args
[]
string
expected
string
expectedErr
string
}
func
TestUnsetConfigString
(
t
*
testing
.
T
)
{
...
...
@@ -79,6 +80,31 @@ func TestUnsetConfigMap(t *testing.T) {
test
.
run
(
t
)
}
func
TestUnsetUnexistConfig
(
t
*
testing
.
T
)
{
conf
:=
clientcmdapi
.
Config
{
Kind
:
"Config"
,
APIVersion
:
"v1"
,
Clusters
:
map
[
string
]
*
clientcmdapi
.
Cluster
{
"minikube"
:
{
Server
:
"https://192.168.99.100:8443"
},
"my-cluster"
:
{
Server
:
"https://192.168.0.1:3434"
},
},
Contexts
:
map
[
string
]
*
clientcmdapi
.
Context
{
"minikube"
:
{
AuthInfo
:
"minikube"
,
Cluster
:
"minikube"
},
"my-cluster"
:
{
AuthInfo
:
"mu-cluster"
,
Cluster
:
"my-cluster"
},
},
CurrentContext
:
"minikube"
,
}
test
:=
unsetConfigTest
{
description
:
"Testing for kubectl config unset a unexist map key"
,
config
:
conf
,
args
:
[]
string
{
"contexts.foo.namespace"
},
expectedErr
:
"current map key `foo` is invalid"
,
}
test
.
run
(
t
)
}
func
(
test
unsetConfigTest
)
run
(
t
*
testing
.
T
)
{
fakeKubeFile
,
err
:=
ioutil
.
TempFile
(
os
.
TempDir
(),
""
)
if
err
!=
nil
{
...
...
@@ -94,14 +120,19 @@ func (test unsetConfigTest) run(t *testing.T) {
pathOptions
.
EnvVar
=
""
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdConfigUnset
(
buf
,
pathOptions
)
cmd
.
SetArgs
(
test
.
args
)
if
err
:=
cmd
.
Execute
();
err
!=
nil
{
t
.
Fatalf
(
"unexpected error executing command: %v,kubectl unset args: %v"
,
err
,
test
.
args
)
opts
:=
&
unsetOptions
{
configAccess
:
pathOptions
}
err
=
opts
.
complete
(
cmd
,
test
.
args
)
if
err
==
nil
{
err
=
opts
.
run
(
buf
)
}
config
,
err
:=
clientcmd
.
LoadFromFile
(
fakeKubeFile
.
Name
())
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error loading kubeconfig file: %v"
,
err
)
}
if
err
!=
nil
&&
err
.
Error
()
!=
test
.
expectedErr
{
t
.
Fatalf
(
"expected error:
\n
%v
\n
but got error:
\n
%v"
,
test
.
expectedErr
,
err
)
}
if
len
(
test
.
expected
)
!=
0
{
if
buf
.
String
()
!=
test
.
expected
{
t
.
Errorf
(
"Failed in :%q
\n
expected %v
\n
but got %v"
,
test
.
description
,
test
.
expected
,
buf
.
String
())
...
...
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