Skip to content

Commit

Permalink
Merge pull request kubernetes#15597 from caesarxuchao/annotate-patch
Browse files Browse the repository at this point in the history
let kubectl annotate use patch instead of replace
  • Loading branch information
piosz committed Oct 16, 2015
2 parents c261325 + d0d260f commit 4b98358
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
43 changes: 31 additions & 12 deletions pkg/kubectl/cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
Expand All @@ -28,6 +29,7 @@ import (
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/strategicpatch"
)

// AnnotateOptions have the data required to perform the annotate operation
Expand Down Expand Up @@ -91,7 +93,7 @@ func NewCmdAnnotate(f *cmdutil.Factory, out io.Writer) *cobra.Command {
if err := options.Validate(args); err != nil {
cmdutil.CheckErr(cmdutil.UsageError(cmd, err.Error()))
}
if err := options.RunAnnotate(); err != nil {
if err := options.RunAnnotate(f); err != nil {
cmdutil.CheckErr(err)
}
},
Expand Down Expand Up @@ -167,26 +169,43 @@ func (o AnnotateOptions) Validate(args []string) error {
}

// RunAnnotate does the work
func (o AnnotateOptions) RunAnnotate() error {
func (o AnnotateOptions) RunAnnotate(f *cmdutil.Factory) error {
r := o.builder.Do()
if err := r.Err(); err != nil {
return err
}

return r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
_, uErr := cmdutil.UpdateObject(info, func(obj runtime.Object) error {
err := o.updateAnnotations(obj)
if err != nil {
return err
}
return nil
})
if uErr != nil {
return uErr

name, namespace, obj := info.Name, info.Namespace, info.Object
oldData, err := json.Marshal(obj)
if err != nil {
return err
}
if err := o.updateAnnotations(obj); err != nil {
return err
}
newData, err := json.Marshal(obj)
if err != nil {
return err
}
return nil
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, obj)
if err != nil {
return err
}

mapping := info.ResourceMapping()
client, err := f.RESTClient(mapping)
if err != nil {
return err
}
helper := resource.NewHelper(client, mapping)

_, err = helper.Patch(namespace, name, api.StrategicMergePatchType, patchBytes)
return err
})
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/kubectl/cmd/annotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func TestAnnotateObject(t *testing.T) {
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
return nil, nil
}
case "PUT":
case "PATCH":
switch req.URL.Path {
case "/namespaces/test/pods/foo":
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
Expand All @@ -458,7 +458,7 @@ func TestAnnotateObject(t *testing.T) {
if err := options.Validate(args); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := options.RunAnnotate(); err != nil {
if err := options.RunAnnotate(f); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
Expand All @@ -480,7 +480,7 @@ func TestAnnotateObjectFromFile(t *testing.T) {
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
return nil, nil
}
case "PUT":
case "PATCH":
switch req.URL.Path {
case "/namespaces/test/pods/cassandra":
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
Expand All @@ -506,7 +506,7 @@ func TestAnnotateObjectFromFile(t *testing.T) {
if err := options.Validate(args); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := options.RunAnnotate(); err != nil {
if err := options.RunAnnotate(f); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
Expand All @@ -528,7 +528,7 @@ func TestAnnotateMultipleObjects(t *testing.T) {
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
return nil, nil
}
case "PUT":
case "PATCH":
switch req.URL.Path {
case "/namespaces/test/pods/foo":
return &http.Response{StatusCode: 200, Body: objBody(codec, &pods.Items[0])}, nil
Expand Down Expand Up @@ -556,7 +556,7 @@ func TestAnnotateMultipleObjects(t *testing.T) {
if err := options.Validate(args); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := options.RunAnnotate(); err != nil {
if err := options.RunAnnotate(f); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

0 comments on commit 4b98358

Please sign in to comment.