Package serialized version of openapi

This commit is contained in:
Darren Shepherd 2019-01-25 21:56:12 -07:00
parent 93841ffbcb
commit 1502ad2530
4 changed files with 309 additions and 0 deletions

View File

@ -1,6 +1,7 @@
//go:generate go run types/codegen/cleanup/main.go
//go:generate go run types/codegen/main.go
//go:generate go fmt pkg/deploy/zz_generated_bindata.go
//go:generate go fmt pkg/openapi/zz_generated_bindata.go
package main

File diff suppressed because one or more lines are too long

View File

@ -2,12 +2,21 @@ package server
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/rancher/k3s/pkg/daemons/config"
"github.com/rancher/k3s/pkg/openapi"
"k8s.io/apimachinery/pkg/util/json"
)
const (
jsonMediaType = "application/json"
binaryMediaType = "application/octet-stream"
pbMediaType = "application/com.github.proto-openapi.spec.v2@v1.0+protobuf"
openapiPrefix = "openapi."
)
type CACertsGetter func() (string, error)
func router(serverConfig *config.Control, tunnel http.Handler, cacertsGetter CACertsGetter) http.Handler {
@ -22,6 +31,7 @@ func router(serverConfig *config.Control, tunnel http.Handler, cacertsGetter CAC
router := mux.NewRouter()
router.NotFoundHandler = authed
router.Path("/cacerts").Handler(cacerts(cacertsGetter))
router.Path("/openapi/v2").Handler(serveOpenapi())
return router
}
@ -68,3 +78,25 @@ func configHandler(server *config.Control) http.Handler {
json.NewEncoder(resp).Encode(server)
})
}
func serveOpenapi() http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
suffix := "json"
contentType := jsonMediaType
if req.Header.Get("Accept") == pbMediaType {
suffix = "pb"
contentType = binaryMediaType
}
data, err := openapi.Asset(openapiPrefix + suffix)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
resp.Write([]byte(err.Error()))
return
}
resp.Header().Set("Content-Type", contentType)
resp.Header().Set("Content-Length", strconv.Itoa(len(data)))
resp.Write(data)
})
}

View File

@ -44,6 +44,24 @@ func main() {
logrus.Fatal(err)
}
bc = &bindata.Config{
Input: []bindata.InputConfig{
{
Path: "vendor/k8s.io/kubernetes/openapi.json",
},
{
Path: "vendor/k8s.io/kubernetes/openapi.pb",
},
},
Package: "openapi",
NoMetadata: true,
Prefix: "vendor/k8s.io/kubernetes/",
Output: "pkg/openapi/zz_generated_bindata.go",
}
if err := bindata.Translate(bc); err != nil {
logrus.Fatal(err)
}
if err := generator.DefaultGenerate(v1.Schemas, basePackage, false, nil); err != nil {
logrus.Fatal(err)
}