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
b6cfa9aa
Commit
b6cfa9aa
authored
Nov 11, 2016
by
ymqytw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
warn user if they try to apply on an object without the annotation
parent
88209229
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
9 deletions
+81
-9
apply.go
pkg/kubectl/cmd/apply.go
+12
-3
apply_test.go
pkg/kubectl/cmd/apply_test.go
+68
-5
cmd.go
pkg/kubectl/cmd/cmd.go
+1
-1
No files found.
pkg/kubectl/cmd/apply.go
View file @
b6cfa9aa
...
...
@@ -61,6 +61,8 @@ const (
backOffPeriod
=
1
*
time
.
Second
// how many times we can retry before back off
triesBeforeBackOff
=
1
warningNoLastAppliedConfigAnnotation
=
"Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
\n
"
)
var
(
...
...
@@ -88,7 +90,7 @@ var (
kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap`
)
)
func
NewCmdApply
(
f
cmdutil
.
Factory
,
out
io
.
Writer
)
*
cobra
.
Command
{
func
NewCmdApply
(
f
cmdutil
.
Factory
,
out
,
errOut
io
.
Writer
)
*
cobra
.
Command
{
var
options
ApplyOptions
cmd
:=
&
cobra
.
Command
{
...
...
@@ -100,7 +102,7 @@ func NewCmdApply(f cmdutil.Factory, out io.Writer) *cobra.Command {
cmdutil
.
CheckErr
(
validateArgs
(
cmd
,
args
))
cmdutil
.
CheckErr
(
cmdutil
.
ValidateOutputArgs
(
cmd
))
cmdutil
.
CheckErr
(
validatePruneAll
(
options
.
Prune
,
cmdutil
.
GetFlagBool
(
cmd
,
"all"
),
options
.
Selector
))
cmdutil
.
CheckErr
(
RunApply
(
f
,
cmd
,
out
,
&
options
))
cmdutil
.
CheckErr
(
RunApply
(
f
,
cmd
,
out
,
errOut
,
&
options
))
},
}
...
...
@@ -161,7 +163,7 @@ func parsePruneResources(gvks []string) ([]pruneResource, error) {
return
pruneResources
,
nil
}
func
RunApply
(
f
cmdutil
.
Factory
,
cmd
*
cobra
.
Command
,
out
io
.
Writer
,
options
*
ApplyOptions
)
error
{
func
RunApply
(
f
cmdutil
.
Factory
,
cmd
*
cobra
.
Command
,
out
,
errOut
io
.
Writer
,
options
*
ApplyOptions
)
error
{
shortOutput
:=
cmdutil
.
GetFlagString
(
cmd
,
"output"
)
==
"name"
schema
,
err
:=
f
.
Validator
(
cmdutil
.
GetFlagBool
(
cmd
,
"validate"
),
cmdutil
.
GetFlagString
(
cmd
,
"schema-cache-dir"
))
if
err
!=
nil
{
...
...
@@ -256,6 +258,13 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *App
}
if
!
dryRun
{
annotationMap
,
err
:=
info
.
Mapping
.
MetadataAccessor
.
Annotations
(
info
.
Object
)
if
err
!=
nil
{
return
err
}
if
_
,
ok
:=
annotationMap
[
annotations
.
LastAppliedConfigAnnotation
];
!
ok
{
fmt
.
Fprintf
(
errOut
,
warningNoLastAppliedConfigAnnotation
)
}
overwrite
:=
cmdutil
.
GetFlagBool
(
cmd
,
"overwrite"
)
helper
:=
resource
.
NewHelper
(
info
.
Client
,
info
.
Mapping
)
patcher
:=
&
patcher
{
...
...
pkg/kubectl/cmd/apply_test.go
View file @
b6cfa9aa
...
...
@@ -41,9 +41,10 @@ import (
func
TestApplyExtraArgsFail
(
t
*
testing
.
T
)
{
buf
:=
bytes
.
NewBuffer
([]
byte
{})
errBuf
:=
bytes
.
NewBuffer
([]
byte
{})
f
,
_
,
_
,
_
:=
cmdtesting
.
NewAPIFactory
()
c
:=
NewCmdApply
(
f
,
buf
)
c
:=
NewCmdApply
(
f
,
buf
,
errBuf
)
if
validateApplyArgs
(
c
,
[]
string
{
"rc"
})
==
nil
{
t
.
Fatalf
(
"unexpected non-error"
)
}
...
...
@@ -77,6 +78,20 @@ func readBytesFromFile(t *testing.T, filename string) []byte {
return
data
}
func
readReplicationController
(
t
*
testing
.
T
,
filenameRC
string
)
(
string
,
[]
byte
)
{
rcObj
:=
readReplicationControllerFromFile
(
t
,
filenameRC
)
metaAccessor
,
err
:=
meta
.
Accessor
(
rcObj
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
rcBytes
,
err
:=
runtime
.
Encode
(
testapi
.
Default
.
Codec
(),
rcObj
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
return
metaAccessor
.
GetName
(),
rcBytes
}
func
readReplicationControllerFromFile
(
t
*
testing
.
T
,
filename
string
)
*
api
.
ReplicationController
{
data
:=
readBytesFromFile
(
t
,
filename
)
rc
:=
api
.
ReplicationController
{}
...
...
@@ -177,6 +192,50 @@ func walkMapPath(t *testing.T, start map[string]interface{}, path []string) map[
return
finish
}
func
TestApplyObjectWithoutAnnotation
(
t
*
testing
.
T
)
{
initTestErrorHandler
(
t
)
nameRC
,
rcBytes
:=
readReplicationController
(
t
,
filenameRC
)
pathRC
:=
"/namespaces/test/replicationcontrollers/"
+
nameRC
f
,
tf
,
_
,
ns
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Printer
=
&
testPrinter
{}
tf
.
Client
=
&
fake
.
RESTClient
{
NegotiatedSerializer
:
ns
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
pathRC
&&
m
==
"GET"
:
bodyRC
:=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
rcBytes
))
return
&
http
.
Response
{
StatusCode
:
200
,
Header
:
defaultHeader
(),
Body
:
bodyRC
},
nil
case
p
==
pathRC
&&
m
==
"PATCH"
:
bodyRC
:=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
rcBytes
))
return
&
http
.
Response
{
StatusCode
:
200
,
Header
:
defaultHeader
(),
Body
:
bodyRC
},
nil
default
:
t
.
Fatalf
(
"unexpected request: %#v
\n
%#v"
,
req
.
URL
,
req
)
return
nil
,
nil
}
}),
}
tf
.
Namespace
=
"test"
tf
.
ClientConfig
=
defaultClientConfig
()
buf
:=
bytes
.
NewBuffer
([]
byte
{})
errBuf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdApply
(
f
,
buf
,
errBuf
)
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRC
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{})
// uses the name from the file, not the response
expectRC
:=
"replicationcontroller/"
+
nameRC
+
"
\n
"
expectWarning
:=
warningNoLastAppliedConfigAnnotation
if
errBuf
.
String
()
!=
expectWarning
{
t
.
Fatalf
(
"unexpected non-warning: %s
\n
expected: %s"
,
errBuf
.
String
(),
expectWarning
)
}
if
buf
.
String
()
!=
expectRC
{
t
.
Fatalf
(
"unexpected output: %s
\n
expected: %s"
,
buf
.
String
(),
expectRC
)
}
}
func
TestApplyObject
(
t
*
testing
.
T
)
{
initTestErrorHandler
(
t
)
nameRC
,
currentRC
:=
readAndAnnotateReplicationController
(
t
,
filenameRC
)
...
...
@@ -203,8 +262,9 @@ func TestApplyObject(t *testing.T) {
}
tf
.
Namespace
=
"test"
buf
:=
bytes
.
NewBuffer
([]
byte
{})
errBuf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdApply
(
f
,
buf
)
cmd
:=
NewCmdApply
(
f
,
buf
,
errBuf
)
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRC
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{})
...
...
@@ -254,8 +314,9 @@ func TestApplyRetry(t *testing.T) {
}
tf
.
Namespace
=
"test"
buf
:=
bytes
.
NewBuffer
([]
byte
{})
errBuf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdApply
(
f
,
buf
)
cmd
:=
NewCmdApply
(
f
,
buf
,
errBuf
)
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRC
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{})
...
...
@@ -297,8 +358,9 @@ func TestApplyNonExistObject(t *testing.T) {
}
tf
.
Namespace
=
"test"
buf
:=
bytes
.
NewBuffer
([]
byte
{})
errBuf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdApply
(
f
,
buf
)
cmd
:=
NewCmdApply
(
f
,
buf
,
errBuf
)
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRC
)
cmd
.
Flags
()
.
Set
(
"output"
,
"name"
)
cmd
.
Run
(
cmd
,
[]
string
{})
...
...
@@ -353,8 +415,9 @@ func testApplyMultipleObjects(t *testing.T, asList bool) {
}
tf
.
Namespace
=
"test"
buf
:=
bytes
.
NewBuffer
([]
byte
{})
errBuf
:=
bytes
.
NewBuffer
([]
byte
{})
cmd
:=
NewCmdApply
(
f
,
buf
)
cmd
:=
NewCmdApply
(
f
,
buf
,
errBuf
)
if
asList
{
cmd
.
Flags
()
.
Set
(
"filename"
,
filenameRCSVC
)
}
else
{
...
...
pkg/kubectl/cmd/cmd.go
View file @
b6cfa9aa
...
...
@@ -281,7 +281,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
{
Message
:
"Advanced Commands:"
,
Commands
:
[]
*
cobra
.
Command
{
NewCmdApply
(
f
,
out
),
NewCmdApply
(
f
,
out
,
err
),
NewCmdPatch
(
f
,
out
),
NewCmdReplace
(
f
,
out
),
NewCmdConvert
(
f
,
out
),
...
...
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