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 4, 2023
1 parent e15f12b commit d91b2fd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
11 changes: 8 additions & 3 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) 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)
Expand All @@ -252,7 +254,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
Expand All @@ -271,9 +273,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 && utilfeature.DefaultFeatureGate.Enabled(features.OpenAPIV3) {
b.OpenAPIV3Config = b.defaultOpenAPIConfig(genericapiserver.DefaultOpenAPIV3Config)
}

if errList := b.CustomMetricsAdapterServerOptions.Validate(); len(errList) > 0 {
return nil, utilerrors.NewAggregate(errList)
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
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 d91b2fd

Please sign in to comment.