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
56d14185
Unverified
Commit
56d14185
authored
Feb 20, 2017
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Honor output formats in kubectl patch
parent
7f626cf8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
17 deletions
+66
-17
patch.go
pkg/kubectl/cmd/patch.go
+12
-11
patch_test.go
pkg/kubectl/cmd/patch_test.go
+54
-6
No files found.
pkg/kubectl/cmd/patch.go
View file @
56d14185
...
@@ -191,13 +191,19 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
...
@@ -191,13 +191,19 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
infoCopy
.
Object
=
patchedObj
infoCopy
.
Object
=
patchedObj
infoCopy
.
VersionedObject
=
patchedObj
infoCopy
.
VersionedObject
=
patchedObj
if
patch
,
patchType
,
err
:=
cmdutil
.
ChangeResourcePatch
(
&
infoCopy
,
f
.
Command
(
cmd
,
true
));
err
==
nil
{
if
patch
,
patchType
,
err
:=
cmdutil
.
ChangeResourcePatch
(
&
infoCopy
,
f
.
Command
(
cmd
,
true
));
err
==
nil
{
if
_
,
err
=
helper
.
Patch
(
info
.
Namespace
,
info
.
Name
,
patchType
,
patch
);
err
!=
nil
{
if
recordedObj
,
err
:
=
helper
.
Patch
(
info
.
Namespace
,
info
.
Name
,
patchType
,
patch
);
err
!=
nil
{
glog
.
V
(
4
)
.
Infof
(
"error recording reason: %v"
,
err
)
glog
.
V
(
4
)
.
Infof
(
"error recording reason: %v"
,
err
)
}
else
{
patchedObj
=
recordedObj
}
}
}
}
}
}
count
++
count
++
if
err
:=
info
.
Refresh
(
patchedObj
,
true
);
err
!=
nil
{
return
err
}
oldData
,
err
:=
json
.
Marshal
(
info
.
Object
)
oldData
,
err
:=
json
.
Marshal
(
info
.
Object
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
@@ -210,9 +216,10 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
...
@@ -210,9 +216,10 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
dataChangedMsg
=
"patched"
dataChangedMsg
=
"patched"
}
}
if
options
.
OutputFormat
==
"name"
||
len
(
options
.
OutputFormat
)
==
0
{
if
len
(
options
.
OutputFormat
)
>
0
&&
options
.
OutputFormat
!=
"name"
{
cmdutil
.
PrintSuccess
(
mapper
,
options
.
OutputFormat
==
"name"
,
out
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
dataChangedMsg
)
return
cmdutil
.
PrintResourceInfoForCommand
(
cmd
,
info
,
f
,
out
)
}
}
cmdutil
.
PrintSuccess
(
mapper
,
options
.
OutputFormat
==
"name"
,
out
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
dataChangedMsg
)
return
nil
return
nil
}
}
...
@@ -234,16 +241,10 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
...
@@ -234,16 +241,10 @@ func RunPatch(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin
// rawExtension := &runtime.Unknown{
// rawExtension := &runtime.Unknown{
// Raw: originalPatchedObjJS,
// Raw: originalPatchedObjJS,
// }
// }
if
err
:=
info
.
Refresh
(
targetObj
,
true
);
err
!=
nil
{
printer
,
err
:=
f
.
PrinterForMapping
(
cmd
,
mapping
,
false
)
if
err
!=
nil
{
return
err
}
if
err
:=
printer
.
PrintObj
(
targetObj
,
out
);
err
!=
nil
{
return
err
return
err
}
}
return
cmdutil
.
PrintResourceInfoForCommand
(
cmd
,
info
,
f
,
out
)
return
nil
})
})
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
...
pkg/kubectl/cmd/patch_test.go
View file @
56d14185
...
@@ -19,18 +19,19 @@ package cmd
...
@@ -19,18 +19,19 @@ package cmd
import
(
import
(
"bytes"
"bytes"
"net/http"
"net/http"
"strings"
"testing"
"testing"
"k8s.io/client-go/rest/fake"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
cmdtesting
"k8s.io/kubernetes/pkg/kubectl/cmd/testing"
cmdtesting
"k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/printers"
)
)
func
TestPatchObject
(
t
*
testing
.
T
)
{
func
TestPatchObject
(
t
*
testing
.
T
)
{
_
,
svc
,
_
:=
testData
()
_
,
svc
,
_
:=
testData
()
f
,
tf
,
codec
,
_
:=
cmdtesting
.
NewAPIFactory
()
f
,
tf
,
codec
,
_
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
UnstructuredClient
=
&
fake
.
RESTClient
{
tf
.
UnstructuredClient
=
&
fake
.
RESTClient
{
APIRegistry
:
api
.
Registry
,
APIRegistry
:
api
.
Registry
,
NegotiatedSerializer
:
unstructuredSerializer
,
NegotiatedSerializer
:
unstructuredSerializer
,
...
@@ -53,8 +54,8 @@ func TestPatchObject(t *testing.T) {
...
@@ -53,8 +54,8 @@ func TestPatchObject(t *testing.T) {
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{
"services/frontend"
})
cmd
.
Run
(
cmd
,
[]
string
{
"services/frontend"
})
// uses the name from the
file, not the
response
// uses the name from the response
if
buf
.
String
()
!=
"service/
frontend
\n
"
{
if
buf
.
String
()
!=
"service/
baz
\n
"
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
}
}
...
@@ -63,7 +64,6 @@ func TestPatchObjectFromFile(t *testing.T) {
...
@@ -63,7 +64,6 @@ func TestPatchObjectFromFile(t *testing.T) {
_
,
svc
,
_
:=
testData
()
_
,
svc
,
_
:=
testData
()
f
,
tf
,
codec
,
_
:=
cmdtesting
.
NewAPIFactory
()
f
,
tf
,
codec
,
_
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
UnstructuredClient
=
&
fake
.
RESTClient
{
tf
.
UnstructuredClient
=
&
fake
.
RESTClient
{
APIRegistry
:
api
.
Registry
,
APIRegistry
:
api
.
Registry
,
NegotiatedSerializer
:
unstructuredSerializer
,
NegotiatedSerializer
:
unstructuredSerializer
,
...
@@ -87,8 +87,56 @@ func TestPatchObjectFromFile(t *testing.T) {
...
@@ -87,8 +87,56 @@ func TestPatchObjectFromFile(t *testing.T) {
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/frontend-service.yaml"
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/frontend-service.yaml"
)
cmd
.
Run
(
cmd
,
[]
string
{})
cmd
.
Run
(
cmd
,
[]
string
{})
// uses the name from the file, not the response
// uses the name from the response
if
buf
.
String
()
!=
"service/frontend
\n
"
{
if
buf
.
String
()
!=
"service/baz
\n
"
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
}
}
func
TestPatchObjectFromFileOutput
(
t
*
testing
.
T
)
{
_
,
svc
,
_
:=
testData
()
svcCopyObj
,
err
:=
api
.
Scheme
.
DeepCopy
(
&
svc
.
Items
[
0
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
svcCopy
:=
svcCopyObj
.
(
*
api
.
Service
)
if
svcCopy
.
Labels
==
nil
{
svcCopy
.
Labels
=
map
[
string
]
string
{}
}
svcCopy
.
Labels
[
"post-patch"
]
=
"post-patch-value"
f
,
tf
,
codec
,
_
:=
cmdtesting
.
NewAPIFactory
()
tf
.
CommandPrinter
=
&
printers
.
YAMLPrinter
{}
tf
.
GenericPrinter
=
true
tf
.
UnstructuredClient
=
&
fake
.
RESTClient
{
APIRegistry
:
api
.
Registry
,
NegotiatedSerializer
:
unstructuredSerializer
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
"/namespaces/test/services/frontend"
&&
m
==
"GET"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Header
:
defaultHeader
(),
Body
:
objBody
(
codec
,
&
svc
.
Items
[
0
])},
nil
case
p
==
"/namespaces/test/services/frontend"
&&
m
==
"PATCH"
:
return
&
http
.
Response
{
StatusCode
:
200
,
Header
:
defaultHeader
(),
Body
:
objBody
(
codec
,
svcCopy
)},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
tf
.
Namespace
=
"test"
buf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdPatch
(
f
,
buf
)
cmd
.
Flags
()
.
Set
(
"namespace"
,
"test"
)
cmd
.
Flags
()
.
Set
(
"patch"
,
`{"spec":{"type":"NodePort"}}`
)
cmd
.
Flags
()
.
Set
(
"output"
,
"yaml"
)
cmd
.
Flags
()
.
Set
(
"filename"
,
"../../../examples/guestbook/frontend-service.yaml"
)
cmd
.
Run
(
cmd
,
[]
string
{})
t
.
Log
(
buf
.
String
())
// make sure the value returned by the server is used
if
!
strings
.
Contains
(
buf
.
String
(),
"post-patch: post-patch-value"
)
{
t
.
Errorf
(
"unexpected output: %s"
,
buf
.
String
())
t
.
Errorf
(
"unexpected output: %s"
,
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