Skip to content

Commit 68378b1

Browse files
committed
Scheme helper fn
1 parent d5af5c5 commit 68378b1

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

pkg/runtime/scheme/scheme.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package scheme
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
)
24+
25+
// NewAddToScheme returns a function to add go struct types as GroupVersionKinds to a runtime.Scheme.
26+
// Scheme is used to map go structs to GroupVersionKinds and vice versa.
27+
func NewSchemeBuilder(gv schema.GroupVersion, types func() []runtime.Object) runtime.SchemeBuilder {
28+
sb := runtime.NewSchemeBuilder(func(scheme *runtime.Scheme) error {
29+
scheme.AddKnownTypes(gv, types()...)
30+
metav1.AddToGroupVersion(scheme, gv)
31+
return nil
32+
})
33+
return sb
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package scheme_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestScheme(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Scheme Suite")
13+
}

pkg/runtime/scheme/scheme_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package scheme_test
2+
3+
import (
4+
"reflect"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
"k8s.io/api/core/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"k8s.io/apimachinery/pkg/runtime/schema"
11+
"sigs.k8s.io/controller-runtime/pkg/runtime/scheme"
12+
)
13+
14+
var _ = Describe("Scheme", func() {
15+
Describe("NewAddToScheme", func() {
16+
It("should return a function that will add the types to the scheme", func() {
17+
gv := schema.GroupVersion{Group: "core", Version: "v1"}
18+
a := scheme.NewSchemeBuilder(gv, func() []runtime.Object {
19+
return []runtime.Object{&v1.Pod{}, &v1.PodList{}}
20+
})
21+
s := runtime.NewScheme()
22+
23+
Expect(a.AddToScheme(s)).To(Succeed())
24+
Expect(s.AllKnownTypes()).To(HaveLen(13))
25+
Expect(s.AllKnownTypes()[gv.WithKind("Pod")]).To(Equal(reflect.TypeOf(v1.Pod{})))
26+
Expect(s.AllKnownTypes()[gv.WithKind("PodList")]).To(Equal(reflect.TypeOf(v1.PodList{})))
27+
28+
// Base types
29+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("DeleteOptions")))
30+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("ExportOptions")))
31+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("GetOptions")))
32+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("ListOptions")))
33+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("WatchEvent")))
34+
35+
emptyGv := schema.GroupVersion{Group: "", Version: "v1"}
36+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroup")))
37+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroupList")))
38+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIResourceList")))
39+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIVersions")))
40+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("Status")))
41+
42+
internalGv := schema.GroupVersion{Group: "core", Version: "__internal"}
43+
Expect(s.AllKnownTypes()).To(HaveKey(internalGv.WithKind("WatchEvent")))
44+
})
45+
})
46+
})

0 commit comments

Comments
 (0)