mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-13 11:25:19 +00:00
Move swagger+openapi setup to routes and decouple from run
This commit is contained in:
58
pkg/genericapiserver/routes/openapi.go
Normal file
58
pkg/genericapiserver/routes/openapi.go
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
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 (
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/genericapiserver/mux"
|
||||
"k8s.io/kubernetes/pkg/genericapiserver/openapi"
|
||||
"k8s.io/kubernetes/pkg/genericapiserver/openapi/common"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/go-openapi/spec"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// 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) Install(c *mux.APIContainer) {
|
||||
// Install one spec per web service, an ideal client will have a ClientSet containing one client
|
||||
// per each of these specs.
|
||||
for _, w := range c.RegisteredWebServices() {
|
||||
if strings.HasPrefix(w.RootPath(), "/swaggerapi") {
|
||||
continue
|
||||
}
|
||||
|
||||
config := *oa.Config
|
||||
config.Info = new(spec.Info)
|
||||
*config.Info = *oa.Config.Info
|
||||
config.Info.Title = config.Info.Title + " " + w.RootPath()
|
||||
err := openapi.RegisterOpenAPIService(w.RootPath()+"/swagger.json", []*restful.WebService{w}, &config, c)
|
||||
if err != nil {
|
||||
glog.Fatalf("Failed to register open api spec for %v: %v", w.RootPath(), err)
|
||||
}
|
||||
}
|
||||
err := openapi.RegisterOpenAPIService("/swagger.json", c.RegisteredWebServices(), oa.Config, c)
|
||||
if err != nil {
|
||||
glog.Fatalf("Failed to register open api spec for root: %v", err)
|
||||
}
|
||||
}
|
||||
49
pkg/genericapiserver/routes/swagger.go
Normal file
49
pkg/genericapiserver/routes/swagger.go
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
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 (
|
||||
"k8s.io/kubernetes/pkg/genericapiserver/mux"
|
||||
|
||||
"github.com/emicklei/go-restful/swagger"
|
||||
)
|
||||
|
||||
// Swagger installs the /swaggerapi/ endpoint to allow schema discovery
|
||||
// and traversal. It is optional to allow consumers of the Kubernetes GenericAPIServer to
|
||||
// register their own web services into the Kubernetes mux prior to initialization
|
||||
// of swagger, so that other resource types show up in the documentation.
|
||||
type Swagger struct {
|
||||
ExternalAddress string
|
||||
}
|
||||
|
||||
// Install adds the SwaggerUI webservice to the given mux.
|
||||
func (s Swagger) Install(c *mux.APIContainer) {
|
||||
swagger.RegisterSwaggerService(swagger.Config{
|
||||
WebServicesUrl: "https://" + s.ExternalAddress,
|
||||
WebServices: c.RegisteredWebServices(),
|
||||
ApiPath: "/swaggerapi/",
|
||||
SwaggerPath: "/swaggerui/",
|
||||
SwaggerFilePath: "/swagger-ui/",
|
||||
SchemaFormatHandler: func(typeName string) string {
|
||||
switch typeName {
|
||||
case "unversioned.Time", "*unversioned.Time":
|
||||
return "date-time"
|
||||
}
|
||||
return ""
|
||||
},
|
||||
}, c.Container)
|
||||
}
|
||||
48
pkg/genericapiserver/routes/swagger_test.go
Normal file
48
pkg/genericapiserver/routes/swagger_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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 (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
genericmux "k8s.io/kubernetes/pkg/genericapiserver/mux"
|
||||
)
|
||||
|
||||
// TestInstallSwaggerAPI verifies that the swagger api is added
|
||||
// at the proper endpoint.
|
||||
func TestInstallSwaggerAPI(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// Install swagger and test
|
||||
c := genericmux.NewAPIContainer(http.NewServeMux(), nil)
|
||||
Swagger{ExternalAddress: "1.2.3.4"}.Install(c)
|
||||
ws := c.RegisteredWebServices()
|
||||
if assert.NotEqual(0, len(ws), "SwaggerAPI not installed.") {
|
||||
assert.Equal("/swaggerapi/", ws[0].RootPath(), "SwaggerAPI did not install to the proper path. %s != /swaggerapi", ws[0].RootPath())
|
||||
}
|
||||
|
||||
// Empty externalHost verification
|
||||
c = genericmux.NewAPIContainer(http.NewServeMux(), nil)
|
||||
Swagger{ExternalAddress: ""}.Install(c)
|
||||
ws = c.RegisteredWebServices()
|
||||
if assert.NotEqual(0, len(ws), "SwaggerAPI not installed.") {
|
||||
assert.Equal("/swaggerapi/", ws[0].RootPath(), "SwaggerAPI did not install to the proper path. %s != /swaggerapi", ws[0].RootPath())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user