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
c5c56dcf
Commit
c5c56dcf
authored
Oct 17, 2017
by
hzxuzhonghu
Committed by
xuzhonghu
Oct 28, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kubectl apply does not send empty patch request
parent
9807360f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
0 deletions
+79
-0
apply.go
pkg/kubectl/cmd/apply.go
+10
-0
apply_test.go
pkg/kubectl/cmd/apply_test.go
+69
-0
No files found.
pkg/kubectl/cmd/apply.go
View file @
c5c56dcf
...
@@ -336,6 +336,12 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
...
@@ -336,6 +336,12 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti
}
else
{
}
else
{
visitedUids
.
Insert
(
string
(
uid
))
visitedUids
.
Insert
(
string
(
uid
))
}
}
if
string
(
patchBytes
)
==
"{}"
{
count
++
cmdutil
.
PrintSuccess
(
mapper
,
shortOutput
,
out
,
info
.
Mapping
.
Resource
,
info
.
Name
,
false
,
"unchanged"
)
return
nil
}
}
}
count
++
count
++
if
len
(
output
)
>
0
&&
!
shortOutput
{
if
len
(
output
)
>
0
&&
!
shortOutput
{
...
@@ -609,6 +615,10 @@ func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, names
...
@@ -609,6 +615,10 @@ func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, names
}
}
}
}
if
string
(
patch
)
==
"{}"
{
return
patch
,
obj
,
nil
}
patchedObj
,
err
:=
p
.
helper
.
Patch
(
namespace
,
name
,
patchType
,
patch
)
patchedObj
,
err
:=
p
.
helper
.
Patch
(
namespace
,
name
,
patchType
,
patch
)
return
patch
,
patchedObj
,
err
return
patch
,
patchedObj
,
err
}
}
...
...
pkg/kubectl/cmd/apply_test.go
View file @
c5c56dcf
...
@@ -592,6 +592,75 @@ func TestApplyNonExistObject(t *testing.T) {
...
@@ -592,6 +592,75 @@ func TestApplyNonExistObject(t *testing.T) {
}
}
}
}
func
TestApplyEmptyPatch
(
t
*
testing
.
T
)
{
initTestErrorHandler
(
t
)
nameRC
,
_
:=
readAndAnnotateReplicationController
(
t
,
filenameRC
)
pathRC
:=
"/namespaces/test/replicationcontrollers"
pathNameRC
:=
pathRC
+
"/"
+
nameRC
verifyPost
:=
false
var
body
[]
byte
f
,
tf
,
_
,
_
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
UnstructuredClient
=
&
fake
.
RESTClient
{
GroupVersion
:
schema
.
GroupVersion
{
Version
:
"v1"
},
NegotiatedSerializer
:
unstructuredSerializer
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
"/api/v1/namespaces/test"
&&
m
==
"GET"
:
return
&
http
.
Response
{
StatusCode
:
404
,
Header
:
defaultHeader
(),
Body
:
ioutil
.
NopCloser
(
bytes
.
NewReader
(
nil
))},
nil
case
p
==
pathNameRC
&&
m
==
"GET"
:
if
body
==
nil
{
return
&
http
.
Response
{
StatusCode
:
404
,
Header
:
defaultHeader
(),
Body
:
ioutil
.
NopCloser
(
bytes
.
NewReader
(
nil
))},
nil
}
bodyRC
:=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
body
))
return
&
http
.
Response
{
StatusCode
:
200
,
Header
:
defaultHeader
(),
Body
:
bodyRC
},
nil
case
p
==
pathRC
&&
m
==
"POST"
:
body
,
_
=
ioutil
.
ReadAll
(
req
.
Body
)
verifyPost
=
true
bodyRC
:=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
body
))
return
&
http
.
Response
{
StatusCode
:
201
,
Header
:
defaultHeader
(),
Body
:
bodyRC
},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
tf
.
Namespace
=
"test"
// 1. apply non exist object
buf
:=
bytes
.
NewBuffer
([]
byte
{})
errBuf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdApply
(
"kubectl"
,
f
,
buf
,
errBuf
)
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRC
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{})
expectRC
:=
"replicationcontroller/"
+
nameRC
+
"
\n
"
if
buf
.
String
()
!=
expectRC
{
t
.
Fatalf
(
"unexpected output: %s
\n
expected: %s"
,
buf
.
String
(),
expectRC
)
}
if
!
verifyPost
{
t
.
Fatal
(
"No server-side post call detected"
)
}
// 2. test apply already exist object, will not send empty patch request
buf
=
bytes
.
NewBuffer
([]
byte
{})
errBuf
=
bytes
.
NewBuffer
([]
byte
{})
cmd
=
NewCmdApply
(
"kubectl"
,
f
,
buf
,
errBuf
)
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRC
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{})
if
buf
.
String
()
!=
expectRC
{
t
.
Fatalf
(
"unexpected output: %s
\n
expected: %s"
,
buf
.
String
(),
expectRC
)
}
}
func
TestApplyMultipleObjectsAsList
(
t
*
testing
.
T
)
{
func
TestApplyMultipleObjectsAsList
(
t
*
testing
.
T
)
{
testApplyMultipleObjects
(
t
,
true
)
testApplyMultipleObjects
(
t
,
true
)
}
}
...
...
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