Skip to content

Commit

Permalink
Merge pull request #6393 from ykakarap/runtime-sdk-catalog
Browse files Browse the repository at this point in the history
✨ Implement Catalog for Runtime SDK
  • Loading branch information
k8s-ci-robot authored Apr 27, 2022
2 parents 520baa3 + 52669e4 commit 3bff7da
Show file tree
Hide file tree
Showing 10 changed files with 878 additions and 1 deletion.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/cli-runtime v0.23.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/kustomize/api v0.10.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
)

require github.com/emicklei/go-restful v2.15.0+incompatible // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7fo
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful v2.15.0+incompatible h1:8KpYO/Xl/ZudZs5RNOEhWMBY4hmzlZhhRd9cu+jrZP4=
github.com/emicklei/go-restful v2.15.0+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
1 change: 1 addition & 0 deletions hack/tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/emicklei/go-restful v2.15.0+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
85 changes: 85 additions & 0 deletions internal/runtime/catalog/builder.go
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)
}
Loading

0 comments on commit 3bff7da

Please sign in to comment.