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 contains utilities for gradually building Schemes, 18 // which contain information associating Go types with Kubernetes 19 // groups, versions, and kinds. 20 // 21 // Each API group should define a utility function 22 // called AddToScheme for adding its types to a Scheme: 23 // 24 // // in package myapigroupv1... 25 // var ( 26 // SchemeGroupVersion = schema.GroupVersion{Group: "my.api.group", Version: "v1"} 27 // SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} 28 // AddToScheme = SchemeBuilder.AddToScheme 29 // ) 30 // 31 // func init() { 32 // SchemeBuilder.Register(&MyType{}, &MyTypeList) 33 // } 34 // var ( 35 // scheme *runtime.Scheme = runtime.NewScheme() 36 // ) 37 // 38 // This also true of the built-in Kubernetes types. Then, in the entrypoint for 39 // your manager, assemble the scheme containing exactly the types you need, 40 // panicing if scheme registration failed. For instance, if our controller needs 41 // types from the core/v1 API group (e.g. Pod), plus types from my.api.group/v1: 42 // 43 // func init() { 44 // utilruntime.Must(myapigroupv1.AddToScheme(scheme)) 45 // utilruntime.Must(kubernetesscheme.AddToScheme(scheme)) 46 // } 47 // 48 // func main() { 49 // mgr := controllers.NewManager(context.Background(), controllers.GetConfigOrDie(), manager.Options{ 50 // Scheme: scheme, 51 // }) 52 // // ... 53 // } 54 package scheme 55 56 import ( 57 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 58 "k8s.io/apimachinery/pkg/runtime" 59 "k8s.io/apimachinery/pkg/runtime/schema" 60 ) 61 62 // Builder builds a new Scheme for mapping go types to Kubernetes GroupVersionKinds. 63 type Builder struct { 64 GroupVersion schema.GroupVersion 65 runtime.SchemeBuilder 66 } 67 68 // Register adds one or more objects to the SchemeBuilder so they can be added to a Scheme. Register mutates bld. 69 func (bld *Builder) Register(object ...runtime.Object) *Builder { 70 bld.SchemeBuilder.Register(func(scheme *runtime.Scheme) error { 71 scheme.AddKnownTypes(bld.GroupVersion, object...) 72 metav1.AddToGroupVersion(scheme, bld.GroupVersion) 73 return nil 74 }) 75 return bld 76 } 77 78 // RegisterAll registers all types from the Builder argument. RegisterAll mutates bld. 79 func (bld *Builder) RegisterAll(b *Builder) *Builder { 80 bld.SchemeBuilder = append(bld.SchemeBuilder, b.SchemeBuilder...) 81 return bld 82 } 83 84 // AddToScheme adds all registered types to s. 85 func (bld *Builder) AddToScheme(s *runtime.Scheme) error { 86 return bld.SchemeBuilder.AddToScheme(s) 87 } 88 89 // Build returns a new Scheme containing the registered types. 90 func (bld *Builder) Build() (*runtime.Scheme, error) { 91 s := runtime.NewScheme() 92 return s, bld.AddToScheme(s) 93 } 94