Commit 1502ad25 authored by Darren Shepherd's avatar Darren Shepherd

Package serialized version of openapi

parent 93841ffb
//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
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -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)
})
}
......@@ -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)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment