Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OpenAPI v3 #142

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
apimeta "k8s.io/apimachinery/pkg/api/meta"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
"k8s.io/apiserver/pkg/features"
genericapiserver "k8s.io/apiserver/pkg/server"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -243,7 +245,7 @@ func mergeOpenAPIDefinitions(definitionsGetters []openapicommon.GetOpenAPIDefini
}
}

func (b *AdapterBase) defaultOpenAPIConfig() *openapicommon.Config {
func (b *AdapterBase) openAPIConfig(createConfig func(getDefinitions openapicommon.GetOpenAPIDefinitions, defNamer *openapinamer.DefinitionNamer) *openapicommon.Config) *openapicommon.Config {
definitionsGetters := []openapicommon.GetOpenAPIDefinitions{generatedcore.GetOpenAPIDefinitions}
if b.cmProvider != nil {
definitionsGetters = append(definitionsGetters, generatedcustommetrics.GetOpenAPIDefinitions)
Expand All @@ -252,12 +254,20 @@ func (b *AdapterBase) defaultOpenAPIConfig() *openapicommon.Config {
definitionsGetters = append(definitionsGetters, generatedexternalmetrics.GetOpenAPIDefinitions)
}
getAPIDefinitions := mergeOpenAPIDefinitions(definitionsGetters)
openAPIConfig := genericapiserver.DefaultOpenAPIConfig(getAPIDefinitions, openapinamer.NewDefinitionNamer(apiserver.Scheme))
openAPIConfig := createConfig(getAPIDefinitions, openapinamer.NewDefinitionNamer(apiserver.Scheme))
openAPIConfig.Info.Title = b.Name
openAPIConfig.Info.Version = "1.0.0"
return openAPIConfig
}

func (b *AdapterBase) defaultOpenAPIConfig() *openapicommon.Config {
return b.openAPIConfig(genericapiserver.DefaultOpenAPIConfig)
}

func (b *AdapterBase) defaultOpenAPIV3Config() *openapicommon.Config {
return b.openAPIConfig(genericapiserver.DefaultOpenAPIV3Config)
}

// Config fetches the configuration used to ultimately create the custom metrics adapter's
// API server. While this method is idempotent, it does "cement" values of some of the other
// fields, so make sure to only call it just before `Server` or `Run`.
Expand All @@ -274,6 +284,9 @@ func (b *AdapterBase) Config() (*apiserver.Config, error) {
b.OpenAPIConfig = b.defaultOpenAPIConfig()
}
b.CustomMetricsAdapterServerOptions.OpenAPIConfig = b.OpenAPIConfig
if b.OpenAPIV3Config == nil && utilfeature.DefaultFeatureGate.Enabled(features.OpenAPIV3) {
b.OpenAPIV3Config = b.defaultOpenAPIV3Config()
}

if errList := b.CustomMetricsAdapterServerOptions.Validate(); len(errList) > 0 {
return nil, utilerrors.NewAggregate(errList)
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type CustomMetricsAdapterServerOptions struct {
Audit *genericoptions.AuditOptions
Features *genericoptions.FeatureOptions

OpenAPIConfig *openapicommon.Config
EnableMetrics bool
OpenAPIConfig *openapicommon.Config
OpenAPIV3Config *openapicommon.Config
EnableMetrics bool
}

// NewCustomMetricsAdapterServerOptions creates a new instance of
Expand Down Expand Up @@ -106,6 +107,9 @@ func (o *CustomMetricsAdapterServerOptions) ApplyTo(serverConfig *genericapiserv
if o.OpenAPIConfig != nil {
serverConfig.OpenAPIConfig = o.OpenAPIConfig
}
if o.OpenAPIV3Config != nil {
serverConfig.OpenAPIV3Config = o.OpenAPIV3Config
}

serverConfig.EnableMetrics = o.EnableMetrics

Expand Down