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
9f003f7d
Commit
9f003f7d
authored
Feb 11, 2016
by
deads2k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow patch to handle multiple types
parent
9d776d99
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
41 additions
and
3 deletions
+41
-3
kubectl
contrib/completions/bash/kubectl
+1
-0
kubectl-patch.1
docs/man/man1/kubectl-patch.1
+7
-0
kubectl_patch.md
docs/user-guide/kubectl/kubectl_patch.md
+5
-1
test-cmd.sh
hack/test-cmd.sh
+8
-0
patch.go
pkg/kubectl/cmd/patch.go
+20
-2
No files found.
contrib/completions/bash/kubectl
View file @
9f003f7d
...
...
@@ -754,6 +754,7 @@ _kubectl_patch()
flags+
=(
"--patch="
)
two_word_flags+
=(
"-p"
)
flags+
=(
"--record"
)
flags+
=(
"--type="
)
flags+
=(
"--alsologtostderr"
)
flags+
=(
"--api-version="
)
flags+
=(
"--certificate-authority="
)
...
...
docs/man/man1/kubectl-patch.1
View file @
9f003f7d
...
...
@@ -40,6 +40,10 @@ Please refer to the models in
\fB\-\-record\fP=false
Record current kubectl command in the resource annotation.
.PP
\fB\-\-type\fP="strategic"
The type of patch being provided; one of [json merge strategic]
.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
...
...
@@ -150,6 +154,9 @@ kubectl patch \-f node.json \-p '{"spec":{"unschedulable":true}}'
# Update a container's image; spec.containers[*].name is required because it's a merge key
kubectl patch pod valid\-pod \-p '{"spec":{"containers":[{"name":"kubernetes\-serve\-hostname","image":"new image"}]}}'
# Update a container's image using a json patch with positional arrays
kubectl patch pod valid\-pod \-type='json' \-p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
.fi
.RE
...
...
docs/user-guide/kubectl/kubectl_patch.md
View file @
9f003f7d
...
...
@@ -61,6 +61,9 @@ kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'
# Update a container's image; spec.containers[*].name is required because it's a merge key
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# Update a container's image using a json patch with positional arrays
kubectl patch pod valid-pod -type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
```
### Options
...
...
@@ -70,6 +73,7 @@ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve
-o, --output="": Output mode. Use "-o name" for shorter output (resource/name).
-p, --patch="": The patch to be applied to the resource JSON file.
--record[=false]: Record current kubectl command in the resource annotation.
--type="strategic": The type of patch being provided; one of [json merge strategic]
```
### Options inherited from parent commands
...
...
@@ -104,7 +108,7 @@ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve
*
[
kubectl
](
kubectl.md
)
- kubectl controls the Kubernetes cluster manager
###### Auto generated by spf13/cobra on
22-Jan
-2016
###### Auto generated by spf13/cobra on
12-Feb
-2016
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
[

]()
...
...
hack/test-cmd.sh
View file @
9f003f7d
...
...
@@ -441,6 +441,14 @@ runTests() {
kubectl patch
"
${
kube_flags
[@]
}
"
pod valid-pod
-p
=
'{"spec":{"containers":[{"name": "kubernetes-serve-hostname", "image": "nginx"}]}}'
# Post-condition: valid-pod POD has image nginx
kube::test::get_object_assert pods
"{{range.items}}{{
$image_field
}}:{{end}}"
'nginx:'
# prove that patch can use different types
kubectl patch
"
${
kube_flags
[@]
}
"
pod valid-pod
--type
=
"json"
-p
=
'[{"op": "replace", "path": "/spec/containers/0/image", "value":"nginx2"}]'
# Post-condition: valid-pod POD has image nginx
kube::test::get_object_assert pods
"{{range.items}}{{
$image_field
}}:{{end}}"
'nginx2:'
# prove that patch can use different types
kubectl patch
"
${
kube_flags
[@]
}
"
pod valid-pod
--type
=
"json"
-p
=
'[{"op": "replace", "path": "/spec/containers/0/image", "value":"nginx"}]'
# Post-condition: valid-pod POD has image nginx
kube::test::get_object_assert pods
"{{range.items}}{{
$image_field
}}:{{end}}"
'nginx:'
# prove that yaml input works too
YAML_PATCH
=
$'spec:
\n
containers:
\n
- name: kubernetes-serve-hostname
\n
image: changed-with-yaml
\n
'
kubectl patch
"
${
kube_flags
[@]
}
"
pod valid-pod
-p
=
"
${
YAML_PATCH
}
"
...
...
pkg/kubectl/cmd/patch.go
View file @
9f003f7d
...
...
@@ -19,6 +19,7 @@ package cmd
import
(
"fmt"
"io"
"strings"
"github.com/spf13/cobra"
...
...
@@ -26,9 +27,12 @@ import (
"k8s.io/kubernetes/pkg/kubectl"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/yaml"
)
var
patchTypes
=
map
[
string
]
api
.
PatchType
{
"json"
:
api
.
JSONPatchType
,
"merge"
:
api
.
MergePatchType
,
"strategic"
:
api
.
StrategicMergePatchType
}
// PatchOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type
PatchOptions
struct
{
...
...
@@ -49,7 +53,10 @@ kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'
# Update a container's image; spec.containers[*].name is required because it's a merge key
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'`
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# Update a container's image using a json patch with positional arrays
kubectl patch pod valid-pod -type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'`
)
func
NewCmdPatch
(
f
*
cmdutil
.
Factory
,
out
io
.
Writer
)
*
cobra
.
Command
{
...
...
@@ -69,6 +76,7 @@ func NewCmdPatch(f *cmdutil.Factory, out io.Writer) *cobra.Command {
}
cmd
.
Flags
()
.
StringP
(
"patch"
,
"p"
,
""
,
"The patch to be applied to the resource JSON file."
)
cmd
.
MarkFlagRequired
(
"patch"
)
cmd
.
Flags
()
.
String
(
"type"
,
"strategic"
,
fmt
.
Sprintf
(
"The type of patch being provided; one of %v"
,
sets
.
StringKeySet
(
patchTypes
)
.
List
()))
cmdutil
.
AddOutputFlagsForMutation
(
cmd
)
cmdutil
.
AddRecordFlag
(
cmd
)
...
...
@@ -83,6 +91,16 @@ func RunPatch(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
return
err
}
patchType
:=
api
.
StrategicMergePatchType
patchTypeString
:=
strings
.
ToLower
(
cmdutil
.
GetFlagString
(
cmd
,
"type"
))
if
len
(
patchTypeString
)
!=
0
{
ok
:=
false
patchType
,
ok
=
patchTypes
[
patchTypeString
]
if
!
ok
{
return
cmdutil
.
UsageError
(
cmd
,
fmt
.
Sprintf
(
"--type must be one of %v, not %q"
,
sets
.
StringKeySet
(
patchTypes
)
.
List
(),
patchTypeString
))
}
}
patch
:=
cmdutil
.
GetFlagString
(
cmd
,
"patch"
)
if
len
(
patch
)
==
0
{
return
cmdutil
.
UsageError
(
cmd
,
"Must specify -p to patch"
)
...
...
@@ -121,7 +139,7 @@ func RunPatch(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
}
helper
:=
resource
.
NewHelper
(
client
,
mapping
)
_
,
err
=
helper
.
Patch
(
namespace
,
name
,
api
.
StrategicMergeP
atchType
,
patchBytes
)
_
,
err
=
helper
.
Patch
(
namespace
,
name
,
p
atchType
,
patchBytes
)
if
err
!=
nil
{
return
err
}
...
...
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