Skip to content

Commit

Permalink
Merge pull request kubernetes#4819 from mikedanese/validate-api-version
Browse files Browse the repository at this point in the history
Validate api version in kubectl --patch and --override
  • Loading branch information
bgrant0607 committed Mar 2, 2015
2 parents 00ca735 + 25fac68 commit 97d7e7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/kubectl/cmd/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strings"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"

"github.com/evanphx/json-patch"
Expand Down Expand Up @@ -161,18 +161,21 @@ func Merge(dst runtime.Object, fragment, kind string) (runtime.Object, error) {
if !ok {
return nil, fmt.Errorf("apiVersion must be a string")
}
i, err := latest.InterfacesFor(versionString)
if err != nil {
return nil, err
}

codec := runtime.CodecFor(api.Scheme, versionString)
// encode dst into versioned json and apply fragment directly too it
target, err := codec.Encode(dst)
target, err := i.Codec.Encode(dst)
if err != nil {
return nil, err
}
patched, err := jsonpatch.MergePatch(target, []byte(fragment))
if err != nil {
return nil, err
}
out, err := codec.Decode(patched)
out, err := i.Codec.Decode(patched)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/kubectl/cmd/util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func TestMerge(t *testing.T) {
},
},
},
{
kind: "Service",
obj: &api.Service{},
fragment: `{ "apiVersion": "badVersion" }`,
expectErr: true,
},
{
kind: "Service",
obj: &api.Service{
Expand Down

0 comments on commit 97d7e7e

Please sign in to comment.