Skip to content

Commit

Permalink
Add support for OpenAPI v3
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierlemasle committed Jan 12, 2023
1 parent e15f12b commit 071e8cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
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

0 comments on commit 071e8cb

Please sign in to comment.