|
| 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 | +// For instance, if our controller needs types from the core/v1 API group (e.g. Pod), |
| 41 | +// plus types from my.api.group/v1: |
| 42 | +// |
| 43 | +// func init() { |
| 44 | +// myapigroupv1.AddToScheme(scheme) |
| 45 | +// kubernetesscheme.AddToScheme(scheme) |
| 46 | +// } |
| 47 | +// |
| 48 | +// func main() { |
| 49 | +// mgr := controllers.NewManager(controllers.GetConfigOrDie(), manager.Options{ |
| 50 | +// Scheme: scheme, |
| 51 | +// }) |
| 52 | +// // ... |
| 53 | +// } |
| 54 | +// |
| 55 | +package scheme |
| 56 | + |
| 57 | +import ( |
| 58 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 59 | + "k8s.io/apimachinery/pkg/runtime" |
| 60 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 61 | +) |
| 62 | + |
| 63 | +// Builder builds a new Scheme for mapping go types to Kubernetes GroupVersionKinds. |
| 64 | +type Builder struct { |
| 65 | + GroupVersion schema.GroupVersion |
| 66 | + runtime.SchemeBuilder |
| 67 | +} |
| 68 | + |
| 69 | +// Register adds one or objects to the SchemeBuilder so they can be added to a Scheme. Register mutates bld. |
| 70 | +func (bld *Builder) Register(object ...runtime.Object) *Builder { |
| 71 | + bld.SchemeBuilder.Register(func(scheme *runtime.Scheme) error { |
| 72 | + scheme.AddKnownTypes(bld.GroupVersion, object...) |
| 73 | + metav1.AddToGroupVersion(scheme, bld.GroupVersion) |
| 74 | + return nil |
| 75 | + }) |
| 76 | + return bld |
| 77 | +} |
| 78 | + |
| 79 | +// RegisterAll registers all types from the Builder argument. RegisterAll mutates bld. |
| 80 | +func (bld *Builder) RegisterAll(b *Builder) *Builder { |
| 81 | + bld.SchemeBuilder = append(bld.SchemeBuilder, b.SchemeBuilder...) |
| 82 | + return bld |
| 83 | +} |
| 84 | + |
| 85 | +// AddToScheme adds all registered types to s. |
| 86 | +func (bld *Builder) AddToScheme(s *runtime.Scheme) error { |
| 87 | + return bld.SchemeBuilder.AddToScheme(s) |
| 88 | +} |
| 89 | + |
| 90 | +// Build returns a new Scheme containing the registered types. |
| 91 | +func (bld *Builder) Build() (*runtime.Scheme, error) { |
| 92 | + s := runtime.NewScheme() |
| 93 | + return s, bld.AddToScheme(s) |
| 94 | +} |
0 commit comments