diff --git a/pkg/cmd/builder.go b/pkg/cmd/builder.go index 9907e54e..fd202221 100644 --- a/pkg/cmd/builder.go +++ b/pkg/cmd/builder.go @@ -243,7 +243,7 @@ func mergeOpenAPIDefinitions(definitionsGetters []openapicommon.GetOpenAPIDefini } } -func (b *AdapterBase) defaultOpenAPIConfig() *openapicommon.Config { +func (b *AdapterBase) defaultOpenAPIConfig(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) @@ -252,7 +252,7 @@ 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 @@ -271,9 +271,12 @@ func (b *AdapterBase) Config() (*apiserver.Config, error) { } if b.OpenAPIConfig == nil { - b.OpenAPIConfig = b.defaultOpenAPIConfig() + b.OpenAPIConfig = b.defaultOpenAPIConfig(genericapiserver.DefaultOpenAPIConfig) } b.CustomMetricsAdapterServerOptions.OpenAPIConfig = b.OpenAPIConfig + if b.OpenAPIV3Config == nil { + b.OpenAPIV3Config = b.defaultOpenAPIConfig(genericapiserver.DefaultOpenAPIV3Config) + } if errList := b.CustomMetricsAdapterServerOptions.Validate(); len(errList) > 0 { return nil, utilerrors.NewAggregate(errList) diff --git a/pkg/cmd/builder_test.go b/pkg/cmd/builder_test.go index 0eba2347..7e5a1b95 100644 --- a/pkg/cmd/builder_test.go +++ b/pkg/cmd/builder_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/assert" + genericapiserver "k8s.io/apiserver/pkg/server" "k8s.io/kube-openapi/pkg/builder" "sigs.k8s.io/custom-metrics-apiserver/pkg/provider/fake" @@ -29,7 +30,7 @@ import ( func TestDefaultOpenAPIConfig(t *testing.T) { t.Run("no metric", func(t *testing.T) { adapter := &AdapterBase{} - config := adapter.defaultOpenAPIConfig() + config := adapter.defaultOpenAPIConfig(genericapiserver.DefaultOpenAPIConfig) _, err1 := builder.BuildOpenAPIDefinitionsForResources(config, "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2.MetricValue") // Should err, because no provider is installed @@ -44,7 +45,7 @@ func TestDefaultOpenAPIConfig(t *testing.T) { adapter.WithCustomMetrics(fake.NewProvider()) adapter.WithExternalMetrics(fake.NewProvider()) - config := adapter.defaultOpenAPIConfig() + config := adapter.defaultOpenAPIConfig(genericapiserver.DefaultOpenAPIConfig) _, err1 := builder.BuildOpenAPIDefinitionsForResources(config, "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2.MetricValue") // Should NOT err diff --git a/pkg/cmd/options/options.go b/pkg/cmd/options/options.go index 93af05ab..16f943e9 100644 --- a/pkg/cmd/options/options.go +++ b/pkg/cmd/options/options.go @@ -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 @@ -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