Skip to content

Commit

Permalink
Add support for labelling 3rd party objects
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed May 23, 2016
1 parent 4215fe5 commit dac0931
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
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)
}
}
}

0 comments on commit dac0931

Please sign in to comment.