-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6393 from ykakarap/runtime-sdk-catalog
✨ Implement Catalog for Runtime SDK
- Loading branch information
Showing
10 changed files
with
878 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 and its request and response types. | ||
// 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(bld.GroupVersion, getter) | ||
}) | ||
return bld | ||
} | ||
|
||
// AddToCatalog adds all registered Hooks their request and response types 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 their request and response types | ||
// and the OpenAPIDefinitions. | ||
func (bld *Builder) Build() (*Catalog, error) { | ||
s := New() | ||
return s, bld.AddToCatalog(s) | ||
} |
Oops, something went wrong.