Skip to content

Commit eb6b5de

Browse files
authored
Merge pull request #62 from pwittrock/register
Scheme helper fn
2 parents f6f9ae2 + 8d89d61 commit eb6b5de

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

pkg/runtime/scheme/scheme.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// Builder builds a new Scheme for mapping go types to Kubernetes GroupVersionKinds.
26+
type Builder struct {
27+
GroupVersion schema.GroupVersion
28+
runtime.SchemeBuilder
29+
}
30+
31+
// Register adds one or objects to the SchemeBuilder so they can be added to a Scheme. Register mutates bld.
32+
func (bld *Builder) Register(object ...runtime.Object) *Builder {
33+
bld.SchemeBuilder.Register(func(scheme *runtime.Scheme) error {
34+
scheme.AddKnownTypes(bld.GroupVersion, object...)
35+
metav1.AddToGroupVersion(scheme, bld.GroupVersion)
36+
return nil
37+
})
38+
return bld
39+
}
40+
41+
// RegisterAll registers all types from the Builder argument. RegisterAll mutates bld.
42+
func (bld *Builder) RegisterAll(b *Builder) *Builder {
43+
bld.SchemeBuilder = append(bld.SchemeBuilder, b.SchemeBuilder...)
44+
return bld
45+
}
46+
47+
// AddToScheme adds all registered types to s.
48+
func (bld *Builder) AddToScheme(s *runtime.Scheme) error {
49+
return bld.SchemeBuilder.AddToScheme(s)
50+
}
51+
52+
// Build returns a new Scheme containing the registered types.
53+
func (bld *Builder) Build() (*runtime.Scheme, error) {
54+
s := runtime.NewScheme()
55+
return s, bld.AddToScheme(s)
56+
}
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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package scheme_test
2+
3+
import (
4+
"reflect"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
appsv1 "k8s.io/api/apps/v1"
9+
corev1 "k8s.io/api/core/v1"
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("Builder", func() {
16+
It("should provide a Scheme with the types registered", func() {
17+
gv := schema.GroupVersion{Group: "core", Version: "v1"}
18+
19+
s, err := (&scheme.Builder{GroupVersion: gv}).
20+
Register(&corev1.Pod{}, &corev1.PodList{}).
21+
Build()
22+
Expect(err).NotTo(HaveOccurred())
23+
24+
Expect(s.AllKnownTypes()).To(HaveLen(13))
25+
Expect(s.AllKnownTypes()[gv.WithKind("Pod")]).To(Equal(reflect.TypeOf(corev1.Pod{})))
26+
Expect(s.AllKnownTypes()[gv.WithKind("PodList")]).To(Equal(reflect.TypeOf(corev1.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+
internalGv := schema.GroupVersion{Group: "core", Version: "__internal"}
36+
Expect(s.AllKnownTypes()).To(HaveKey(internalGv.WithKind("WatchEvent")))
37+
38+
emptyGv := schema.GroupVersion{Group: "", Version: "v1"}
39+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroup")))
40+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroupList")))
41+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIResourceList")))
42+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIVersions")))
43+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("Status")))
44+
})
45+
46+
It("should be able to add types from other Builders", func() {
47+
gv1 := schema.GroupVersion{Group: "core", Version: "v1"}
48+
b1 := (&scheme.Builder{GroupVersion: gv1}).Register(&corev1.Pod{}, &corev1.PodList{})
49+
50+
gv2 := schema.GroupVersion{Group: "apps", Version: "v1"}
51+
s, err := (&scheme.Builder{GroupVersion: gv2}).
52+
Register(&appsv1.Deployment{}).
53+
Register(&appsv1.DeploymentList{}).
54+
RegisterAll(b1).
55+
Build()
56+
57+
Expect(err).NotTo(HaveOccurred())
58+
Expect(s.AllKnownTypes()).To(HaveLen(21))
59+
60+
// Types from b1
61+
Expect(s.AllKnownTypes()[gv1.WithKind("Pod")]).To(Equal(reflect.TypeOf(corev1.Pod{})))
62+
Expect(s.AllKnownTypes()[gv1.WithKind("PodList")]).To(Equal(reflect.TypeOf(corev1.PodList{})))
63+
64+
// Types from b2
65+
Expect(s.AllKnownTypes()[gv2.WithKind("Deployment")]).To(Equal(reflect.TypeOf(appsv1.Deployment{})))
66+
Expect(s.AllKnownTypes()[gv2.WithKind("Deployment")]).To(Equal(reflect.TypeOf(appsv1.Deployment{})))
67+
68+
// Base types
69+
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("DeleteOptions")))
70+
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("ExportOptions")))
71+
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("GetOptions")))
72+
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("ListOptions")))
73+
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("WatchEvent")))
74+
75+
internalGv1 := schema.GroupVersion{Group: "core", Version: "__internal"}
76+
Expect(s.AllKnownTypes()).To(HaveKey(internalGv1.WithKind("WatchEvent")))
77+
78+
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("DeleteOptions")))
79+
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("ExportOptions")))
80+
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("GetOptions")))
81+
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("ListOptions")))
82+
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("WatchEvent")))
83+
84+
internalGv2 := schema.GroupVersion{Group: "apps", Version: "__internal"}
85+
Expect(s.AllKnownTypes()).To(HaveKey(internalGv2.WithKind("WatchEvent")))
86+
87+
emptyGv := schema.GroupVersion{Group: "", Version: "v1"}
88+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroup")))
89+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroupList")))
90+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIResourceList")))
91+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIVersions")))
92+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("Status")))
93+
})
94+
})
95+
})

0 commit comments

Comments
 (0)