Skip to content

Commit e490e5b

Browse files
committed
Scheme helper fn
1 parent d5af5c5 commit e490e5b

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

pkg/runtime/scheme/scheme.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 registers new go struct types with a Scheme as GroupVersionKinds. This allows
26+
// the structs to be mapped to GroupVersionKinds.
27+
type Builder struct {
28+
GroupVersion schema.GroupVersion
29+
runtime.SchemeBuilder
30+
}
31+
32+
// RegisterTypes adds one or objects to the SchemeBuilder so they can be added to a Scheme
33+
func (sb *Builder) RegisterTypes(object ...runtime.Object) {
34+
sb.SchemeBuilder.Register(func(scheme *runtime.Scheme) error {
35+
scheme.AddKnownTypes(sb.GroupVersion, object...)
36+
metav1.AddToGroupVersion(scheme, sb.GroupVersion)
37+
return nil
38+
})
39+
}
40+
41+
// AddToScheme adds all known objects to the Scheme
42+
func (sb *Builder) AddToScheme(s *runtime.Scheme) error {
43+
return sb.SchemeBuilder.AddToScheme(s)
44+
}
45+
46+
// NewScheme returns a new *runtime.Scheme with all known types registered
47+
func (sb *Builder) NewScheme() (*runtime.Scheme, error) {
48+
s := runtime.NewScheme()
49+
err := sb.AddToScheme(s)
50+
if err != nil {
51+
return nil, err
52+
}
53+
return s, nil
54+
}
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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/schema"
10+
"sigs.k8s.io/controller-runtime/pkg/runtime/scheme"
11+
)
12+
13+
var _ = Describe("Scheme", func() {
14+
Describe("SchemeBuilder", func() {
15+
It("should provide a Scheme with the types registered", func() {
16+
gv := schema.GroupVersion{Group: "core", Version: "v1"}
17+
a := scheme.Builder{GroupVersion: gv}
18+
a.RegisterTypes(&v1.Pod{}, &v1.PodList{})
19+
s, err := a.NewScheme()
20+
Expect(err).NotTo(HaveOccurred())
21+
22+
Expect(a.AddToScheme(s)).To(Succeed())
23+
Expect(s.AllKnownTypes()).To(HaveLen(13))
24+
Expect(s.AllKnownTypes()[gv.WithKind("Pod")]).To(Equal(reflect.TypeOf(v1.Pod{})))
25+
Expect(s.AllKnownTypes()[gv.WithKind("PodList")]).To(Equal(reflect.TypeOf(v1.PodList{})))
26+
27+
// Base types
28+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("DeleteOptions")))
29+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("ExportOptions")))
30+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("GetOptions")))
31+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("ListOptions")))
32+
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("WatchEvent")))
33+
34+
emptyGv := schema.GroupVersion{Group: "", Version: "v1"}
35+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroup")))
36+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroupList")))
37+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIResourceList")))
38+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIVersions")))
39+
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("Status")))
40+
41+
internalGv := schema.GroupVersion{Group: "core", Version: "__internal"}
42+
Expect(s.AllKnownTypes()).To(HaveKey(internalGv.WithKind("WatchEvent")))
43+
})
44+
})
45+
})

0 commit comments

Comments
 (0)