2023-05-29 21:03:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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 routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
restful "github.com/emicklei/go-restful/v3"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
|
|
|
"k8s.io/apiserver/pkg/server/mux"
|
|
|
|
builder2 "k8s.io/kube-openapi/pkg/builder"
|
|
|
|
"k8s.io/kube-openapi/pkg/builder3"
|
|
|
|
"k8s.io/kube-openapi/pkg/common"
|
2023-06-01 16:58:10 +00:00
|
|
|
"k8s.io/kube-openapi/pkg/common/restfuladapter"
|
2023-05-29 21:03:29 +00:00
|
|
|
"k8s.io/kube-openapi/pkg/handler"
|
|
|
|
"k8s.io/kube-openapi/pkg/handler3"
|
|
|
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpenAPI installs spec endpoints for each web service.
|
|
|
|
type OpenAPI struct {
|
|
|
|
Config *common.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install adds the SwaggerUI webservice to the given mux.
|
|
|
|
func (oa OpenAPI) InstallV2(c *restful.Container, mux *mux.PathRecorderMux) (*handler.OpenAPIService, *spec.Swagger) {
|
2023-06-01 16:58:10 +00:00
|
|
|
spec, err := builder2.BuildOpenAPISpecFromRoutes(restfuladapter.AdaptWebServices(c.RegisteredWebServices()), oa.Config)
|
2023-05-29 21:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Fatalf("Failed to build open api spec for root: %v", err)
|
|
|
|
}
|
|
|
|
spec.Definitions = handler.PruneDefaults(spec.Definitions)
|
2023-06-01 16:58:10 +00:00
|
|
|
openAPIVersionedService := handler.NewOpenAPIService(spec)
|
2023-05-29 21:03:29 +00:00
|
|
|
err = openAPIVersionedService.RegisterOpenAPIVersionedService("/openapi/v2", mux)
|
|
|
|
if err != nil {
|
|
|
|
klog.Fatalf("Failed to register versioned open api spec for root: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return openAPIVersionedService, spec
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstallV3 adds the static group/versions defined in the RegisteredWebServices to the OpenAPI v3 spec
|
|
|
|
func (oa OpenAPI) InstallV3(c *restful.Container, mux *mux.PathRecorderMux) *handler3.OpenAPIService {
|
2023-06-01 16:58:10 +00:00
|
|
|
openAPIVersionedService := handler3.NewOpenAPIService()
|
|
|
|
err := openAPIVersionedService.RegisterOpenAPIV3VersionedService("/openapi/v3", mux)
|
2023-05-29 21:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Fatalf("Failed to register versioned open api spec for root: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
grouped := make(map[string][]*restful.WebService)
|
|
|
|
|
|
|
|
for _, t := range c.RegisteredWebServices() {
|
|
|
|
// Strip the "/" prefix from the name
|
|
|
|
gvName := t.RootPath()[1:]
|
|
|
|
grouped[gvName] = []*restful.WebService{t}
|
|
|
|
}
|
|
|
|
|
|
|
|
for gv, ws := range grouped {
|
2023-06-01 16:58:10 +00:00
|
|
|
spec, err := builder3.BuildOpenAPISpecFromRoutes(restfuladapter.AdaptWebServices(ws), oa.Config)
|
2023-05-29 21:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("Failed to build OpenAPI v3 for group %s, %q", gv, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
openAPIVersionedService.UpdateGroupVersion(gv, spec)
|
|
|
|
}
|
|
|
|
return openAPIVersionedService
|
|
|
|
}
|