diff --git a/scheme/convert.go b/scheme/convert.go new file mode 100644 index 00000000..9d6a66de --- /dev/null +++ b/scheme/convert.go @@ -0,0 +1,30 @@ +/* +Copyright 2022 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package scheme + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +// GvkToTypeMeta converts a GroupVersionKind to a TypeMeta +func GvkToTypeMeta(gvk schema.GroupVersionKind) metav1.TypeMeta { + return metav1.TypeMeta{ + Kind: gvk.Kind, + APIVersion: gvk.GroupVersion().String(), + } +} diff --git a/scheme/convert_test.go b/scheme/convert_test.go new file mode 100644 index 00000000..def9a12e --- /dev/null +++ b/scheme/convert_test.go @@ -0,0 +1,62 @@ +/* +Copyright 2022 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package scheme + +import ( + "testing" + + . "github.com/onsi/gomega" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func TestGvkToTypeMeta(t *testing.T) { + g := NewGomegaWithT(t) + tests := []struct { + gvk schema.GroupVersionKind + want v1.TypeMeta + }{ + { + gvk: schema.GroupVersionKind{}, + want: v1.TypeMeta{}, + }, + { + gvk: schema.GroupVersionKind{ + Group: "test.com", + Version: "v1", + Kind: "UnitTest", + }, + want: v1.TypeMeta{ + Kind: "UnitTest", + APIVersion: "test.com/v1", + }, + }, + { + gvk: schema.GroupVersionKind{ + Version: "v1", + Kind: "UnitTest", + }, + want: v1.TypeMeta{ + Kind: "UnitTest", + APIVersion: "v1", + }, + }, + } + for _, tt := range tests { + g.Expect(GvkToTypeMeta(tt.gvk)).To(Equal(tt.want)) + } +}