2019-08-04 17:41:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-08-13 23:36:03 +00:00
|
|
|
"strings"
|
2019-08-12 22:15:19 +00:00
|
|
|
|
2019-08-04 17:41:32 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/rancher/naok/pkg/accesscontrol"
|
|
|
|
"github.com/rancher/naok/pkg/attributes"
|
2019-08-08 05:41:31 +00:00
|
|
|
k8sproxy "github.com/rancher/naok/pkg/proxy"
|
2019-08-13 23:36:03 +00:00
|
|
|
"github.com/rancher/naok/pkg/resources/schema"
|
2019-08-04 17:41:32 +00:00
|
|
|
"github.com/rancher/norman/pkg/api"
|
|
|
|
"github.com/rancher/norman/pkg/types"
|
|
|
|
"github.com/rancher/norman/pkg/urlbuilder"
|
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2019-08-08 05:41:31 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2019-08-04 17:41:32 +00:00
|
|
|
)
|
|
|
|
|
2019-08-13 23:36:03 +00:00
|
|
|
func newAPIServer(cfg *rest.Config, sf schema.Factory) (http.Handler, error) {
|
2019-08-08 05:41:31 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2019-08-04 17:41:32 +00:00
|
|
|
a := &apiServer{
|
2019-08-13 23:36:03 +00:00
|
|
|
Router: mux.NewRouter(),
|
|
|
|
sf: sf,
|
|
|
|
server: api.NewAPIServer(),
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
2019-08-08 05:41:31 +00:00
|
|
|
|
|
|
|
a.Router.NotFoundHandler, err = k8sproxy.Handler("/", cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-04 17:41:32 +00:00
|
|
|
a.Router.StrictSlash(true)
|
|
|
|
a.server.AccessControl = accesscontrol.NewAccessControl()
|
2019-08-08 05:41:31 +00:00
|
|
|
return a, a.routes()
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type apiServer struct {
|
|
|
|
*mux.Router
|
2019-08-13 23:36:03 +00:00
|
|
|
sf schema.Factory
|
|
|
|
server *api.Server
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.APIRequest, bool) {
|
|
|
|
user := &user.DefaultInfo{
|
|
|
|
Name: "admin",
|
|
|
|
Groups: []string{"system:masters"},
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:36:03 +00:00
|
|
|
schemas, err := a.sf.Schemas(user)
|
2019-08-04 17:41:32 +00:00
|
|
|
if err != nil {
|
|
|
|
rw.Write([]byte(err.Error()))
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
urlBuilder, err := urlbuilder.New(req, a, schemas)
|
|
|
|
if err != nil {
|
|
|
|
rw.Write([]byte(err.Error()))
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.APIRequest{
|
|
|
|
Schemas: schemas,
|
|
|
|
Request: req,
|
|
|
|
Response: rw,
|
|
|
|
URLBuilder: urlBuilder,
|
|
|
|
}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *apiServer) Schema(base string, schema *types.Schema) string {
|
|
|
|
gvr := attributes.GVR(schema)
|
2019-08-13 23:36:03 +00:00
|
|
|
if gvr.Resource == "" {
|
|
|
|
return urlbuilder.ConstructBasicURL(base, "v1", schema.PluralName)
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
2019-08-13 23:36:03 +00:00
|
|
|
return urlbuilder.ConstructBasicURL(base, "v1", strings.ToLower(schema.ID))
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|