sigs.k8s.io/cluster-api@v1.7.1/exp/runtime/catalog/builder.go (about) 1 /* 2 Copyright 2022 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 catalog 18 19 import ( 20 "k8s.io/apimachinery/pkg/runtime" 21 "k8s.io/apimachinery/pkg/runtime/schema" 22 ) 23 24 // Builder builds a Catalog to allow mappings between Go types and the corresponding 25 // GroupVersionHooks, metadata and OpenAPIDefinitions. 26 type Builder struct { 27 // GroupVersion is the GroupVersion used when registering 28 // Hooks and OpenAPIDefinitions. 29 GroupVersion schema.GroupVersion 30 31 // catalogBuilder are the functions which are used 32 // to register Hooks and OpenAPIDefinitionsGetter into a catalog. 33 catalogBuilder []func(*Catalog) 34 35 // SchemeBuilder are the functions which are used 36 // to add the types of Hook requests and responses to 37 // the scheme of a catalog. 38 schemeBuilder runtime.SchemeBuilder 39 } 40 41 // RegisterHook registers a Hook with its request and response types and metadata. 42 // The passed in hookFunc must have the following type: func(*RequestType,*ResponseType) 43 // The name of the func becomes the "hook" field of the GroupVersionHook. 44 func (bld *Builder) RegisterHook(hookFunc Hook, hookMeta *HookMeta) *Builder { 45 bld.catalogBuilder = append(bld.catalogBuilder, func(catalog *Catalog) { 46 catalog.AddHook(bld.GroupVersion, hookFunc, hookMeta) 47 }) 48 return bld 49 } 50 51 // RegisterOpenAPIDefinitions registers a func returning the OpenAPIDefinitions for 52 // request and response types of all Runtime Hooks of a given group version. 53 // NOTE: The OpenAPIDefinitionsGetter funcs in the API packages are generated by 54 // openapi-gen. 55 func (bld *Builder) RegisterOpenAPIDefinitions(getter OpenAPIDefinitionsGetter) *Builder { 56 bld.catalogBuilder = append(bld.catalogBuilder, func(c *Catalog) { 57 c.AddOpenAPIDefinitions(getter) 58 }) 59 return bld 60 } 61 62 // AddToCatalog adds all registered Hooks with their request and response types and metadata 63 // and the OpenAPIDefinitions to the catalog. 64 func (bld *Builder) AddToCatalog(catalog *Catalog) error { 65 for _, addTo := range bld.catalogBuilder { 66 addTo(catalog) 67 } 68 return bld.schemeBuilder.AddToScheme(catalog.scheme) 69 } 70 71 // Register adds a scheme setup function to the schemeBuilder. 72 // Note: This function is used by generated conversion code. 73 // If we would just expose the func from the embedded schemeBuilder 74 // directly it would not work because it is nil at that time and 75 // appending to a nil schemeBuilder doesn't propagate to our Builder. 76 func (bld *Builder) Register(f func(*runtime.Scheme) error) { 77 bld.schemeBuilder.Register(f) 78 } 79 80 // Build returns a new Catalog containing all registered Hooks with their request and response types 81 // and metadata and the OpenAPIDefinitions. 82 func (bld *Builder) Build() (*Catalog, error) { 83 s := New() 84 return s, bld.AddToCatalog(s) 85 }