Skip to content

Scheme helper fn #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions pkg/runtime/scheme/scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2018 The Kubernetes 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"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// Builder builds a new Scheme for mapping go types to Kubernetes GroupVersionKinds.
type Builder struct {
GroupVersion schema.GroupVersion
runtime.SchemeBuilder
}

// Register adds one or objects to the SchemeBuilder so they can be added to a Scheme. Register mutates bld.
func (bld *Builder) Register(object ...runtime.Object) *Builder {
bld.SchemeBuilder.Register(func(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(bld.GroupVersion, object...)
metav1.AddToGroupVersion(scheme, bld.GroupVersion)
return nil
})
return bld
}

// RegisterAll registers all types from the Builder argument. RegisterAll mutates bld.
func (bld *Builder) RegisterAll(b *Builder) *Builder {
bld.SchemeBuilder = append(bld.SchemeBuilder, b.SchemeBuilder...)
return bld
}

// AddToScheme adds all registered types to s.
func (bld *Builder) AddToScheme(s *runtime.Scheme) error {
return bld.SchemeBuilder.AddToScheme(s)
}

// Build returns a new Scheme containing the registered types.
func (bld *Builder) Build() (*runtime.Scheme, error) {
s := runtime.NewScheme()
return s, bld.AddToScheme(s)
}
13 changes: 13 additions & 0 deletions pkg/runtime/scheme/scheme_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package scheme_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestScheme(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Scheme Suite")
}
95 changes: 95 additions & 0 deletions pkg/runtime/scheme/scheme_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package scheme_test

import (
"reflect"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/runtime/scheme"
)

var _ = Describe("Scheme", func() {
Describe("Builder", func() {
It("should provide a Scheme with the types registered", func() {
gv := schema.GroupVersion{Group: "core", Version: "v1"}

s, err := (&scheme.Builder{GroupVersion: gv}).
Register(&corev1.Pod{}, &corev1.PodList{}).
Build()
Expect(err).NotTo(HaveOccurred())

Expect(s.AllKnownTypes()).To(HaveLen(13))
Expect(s.AllKnownTypes()[gv.WithKind("Pod")]).To(Equal(reflect.TypeOf(corev1.Pod{})))
Expect(s.AllKnownTypes()[gv.WithKind("PodList")]).To(Equal(reflect.TypeOf(corev1.PodList{})))

// Base types
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("DeleteOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("ExportOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("GetOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("ListOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv.WithKind("WatchEvent")))

internalGv := schema.GroupVersion{Group: "core", Version: "__internal"}
Expect(s.AllKnownTypes()).To(HaveKey(internalGv.WithKind("WatchEvent")))

emptyGv := schema.GroupVersion{Group: "", Version: "v1"}
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroup")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroupList")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIResourceList")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIVersions")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("Status")))
})

It("should be able to add types from other Builders", func() {
gv1 := schema.GroupVersion{Group: "core", Version: "v1"}
b1 := (&scheme.Builder{GroupVersion: gv1}).Register(&corev1.Pod{}, &corev1.PodList{})

gv2 := schema.GroupVersion{Group: "apps", Version: "v1"}
s, err := (&scheme.Builder{GroupVersion: gv2}).
Register(&appsv1.Deployment{}).
Register(&appsv1.DeploymentList{}).
RegisterAll(b1).
Build()

Expect(err).NotTo(HaveOccurred())
Expect(s.AllKnownTypes()).To(HaveLen(21))

// Types from b1
Expect(s.AllKnownTypes()[gv1.WithKind("Pod")]).To(Equal(reflect.TypeOf(corev1.Pod{})))
Expect(s.AllKnownTypes()[gv1.WithKind("PodList")]).To(Equal(reflect.TypeOf(corev1.PodList{})))

// Types from b2
Expect(s.AllKnownTypes()[gv2.WithKind("Deployment")]).To(Equal(reflect.TypeOf(appsv1.Deployment{})))
Expect(s.AllKnownTypes()[gv2.WithKind("Deployment")]).To(Equal(reflect.TypeOf(appsv1.Deployment{})))

// Base types
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("DeleteOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("ExportOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("GetOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("ListOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv1.WithKind("WatchEvent")))

internalGv1 := schema.GroupVersion{Group: "core", Version: "__internal"}
Expect(s.AllKnownTypes()).To(HaveKey(internalGv1.WithKind("WatchEvent")))

Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("DeleteOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("ExportOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("GetOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("ListOptions")))
Expect(s.AllKnownTypes()).To(HaveKey(gv2.WithKind("WatchEvent")))

internalGv2 := schema.GroupVersion{Group: "apps", Version: "__internal"}
Expect(s.AllKnownTypes()).To(HaveKey(internalGv2.WithKind("WatchEvent")))

emptyGv := schema.GroupVersion{Group: "", Version: "v1"}
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroup")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIGroupList")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIResourceList")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("APIVersions")))
Expect(s.AllKnownTypes()).To(HaveKey(emptyGv.WithKind("Status")))
})
})
})