Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 3rd party objects to kubectl label #24882

Merged
merged 1 commit into from
May 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (o AnnotateOptions) RunAnnotate() error {
return err
}

obj, err := info.Mapping.ConvertToVersion(info.Object, info.Mapping.GroupVersionKind.GroupVersion())
obj, err := cmdutil.MaybeConvertObject(info.Object, info.Mapping.GroupVersionKind.GroupVersion(), info.Mapping)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
}
outputObj = info.Object
} else {
obj, err := info.Mapping.ConvertToVersion(info.Object, info.Mapping.GroupVersionKind.GroupVersion())
obj, err := cmdutil.MaybeConvertObject(info.Object, info.Mapping.GroupVersionKind.GroupVersion(), info.Mapping)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/kubectl/cmd/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/client/typed/discovery"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
"k8s.io/kubernetes/pkg/kubectl"
Expand Down Expand Up @@ -594,3 +595,15 @@ func ParsePairs(pairArgs []string, pairType string, supportRemove bool) (newPair

return
}

// MaybeConvertObject attempts to convert an object to a specific group/version. If the object is
// a third party resource it is simply passed through.
func MaybeConvertObject(obj runtime.Object, gv unversioned.GroupVersion, converter runtime.ObjectConvertor) (runtime.Object, error) {
switch obj.(type) {
case *extensions.ThirdPartyResourceData:
// conversion is not supported for 3rd party objects
return obj, nil
default:
return converter.ConvertToVersion(obj, gv)
}
}
52 changes: 52 additions & 0 deletions pkg/kubectl/cmd/util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"k8s.io/kubernetes/pkg/api/testapi"
apitesting "k8s.io/kubernetes/pkg/api/testing"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/validation/field"
)
Expand Down Expand Up @@ -302,3 +304,53 @@ func TestDumpReaderToFile(t *testing.T) {
t.Fatalf("Wrong file content %s != %s", testString, stringData)
}
}

func TestMaybeConvert(t *testing.T) {
tests := []struct {
input runtime.Object
gv unversioned.GroupVersion
expected runtime.Object
}{
{
input: &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
},
gv: unversioned.GroupVersion{Group: "", Version: "v1"},
expected: &v1.Pod{
TypeMeta: unversioned.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: v1.ObjectMeta{
Name: "foo",
},
},
},
{
input: &extensions.ThirdPartyResourceData{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
Data: []byte("this is some data"),
},
expected: &extensions.ThirdPartyResourceData{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
Data: []byte("this is some data"),
},
},
}

for _, test := range tests {
obj, err := MaybeConvertObject(test.input, test.gv, testapi.Default.Converter())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(test.expected, obj) {
t.Errorf("expected:\n%#v\nsaw:\n%#v\n", test.expected, obj)
}
}
}