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