From 8b063a34b07412d671b3a626f392446684fe7f0a Mon Sep 17 00:00:00 2001 From: deads2k Date: Wed, 30 Sep 2015 11:58:42 -0400 Subject: [PATCH] allow yaml as argument to patch --- hack/test-cmd.sh | 5 +++++ pkg/kubectl/cmd/patch.go | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index f925d72d77..f69d713f56 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -389,6 +389,11 @@ 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 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}" + # Post-condition: valid-pod POD has image nginx + kube::test::get_object_assert pods "{{range.items}}{{$image_field}}:{{end}}" 'changed-with-yaml:' ## Patch pod from JSON can change image # Command kubectl patch "${kube_flags[@]}" -f docs/admin/limitrange/valid-pod.yaml -p='{"spec":{"containers":[{"name": "kubernetes-serve-hostname", "image": "kubernetes/pause"}]}}' diff --git a/pkg/kubectl/cmd/patch.go b/pkg/kubectl/cmd/patch.go index 9b1cdf0200..13bfc8fbef 100644 --- a/pkg/kubectl/cmd/patch.go +++ b/pkg/kubectl/cmd/patch.go @@ -21,10 +21,12 @@ import ( "io" "github.com/spf13/cobra" + "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/kubectl" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/resource" + "k8s.io/kubernetes/pkg/util/yaml" ) // PatchOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of @@ -84,6 +86,10 @@ func RunPatch(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri if len(patch) == 0 { return cmdutil.UsageError(cmd, "Must specify -p to patch") } + patchBytes, err := yaml.ToJSON([]byte(patch)) + if err != nil { + return fmt.Errorf("unable to parse %q: %v", patch, err) + } mapper, typer := f.Object() r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()). @@ -114,7 +120,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.StrategicMergePatchType, []byte(patch)) + _, err = helper.Patch(namespace, name, api.StrategicMergePatchType, patchBytes) if err != nil { return err }