1
0
mirror of https://github.com/rancher/steve.git synced 2025-08-31 15:11:31 +00:00
This commit is contained in:
Darren Shepherd
2020-01-30 22:01:21 -07:00
parent 9184741c57
commit 19c6732de0
2380 changed files with 587 additions and 759101 deletions

View File

@@ -0,0 +1,94 @@
package publicapi
import (
"net/http"
"github.com/rancher/norman/v2/pkg/api"
"github.com/rancher/norman/v2/pkg/auth"
"github.com/rancher/norman/v2/pkg/types"
"github.com/rancher/norman/v2/pkg/urlbuilder"
"github.com/rancher/steve/pkg/accesscontrol"
k8sproxy "github.com/rancher/steve/pkg/proxy"
"github.com/rancher/steve/pkg/resources/schema"
"github.com/rancher/steve/pkg/server/router"
"github.com/sirupsen/logrus"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/client-go/rest"
)
func NewHandler(cfg *rest.Config, sf schema.Factory, authMiddleware auth.Middleware, next http.Handler) (http.Handler, error) {
var (
err error
)
a := &apiServer{
sf: sf,
server: api.DefaultAPIServer(),
}
a.server.AccessControl = accesscontrol.NewAccessControl()
proxy, err := k8sproxy.Handler("/", cfg)
if err != nil {
return nil, err
}
w := authMiddleware.Wrap
return router.Routes(router.Handlers{
Next: next,
K8sResource: w(a.apiHandler(k8sAPI)),
GenericResource: w(a.apiHandler(nil)),
K8sProxy: w(proxy),
APIRoot: w(a.apiHandler(apiRoot)),
}), nil
}
type apiServer struct {
sf schema.Factory
server *api.Server
}
func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.APIRequest, bool) {
user := &user.DefaultInfo{
Name: "admin",
Groups: []string{"system:masters"},
}
schemas, err := a.sf.Schemas(user)
if err != nil {
logrus.Errorf("HTTP request failed: %v", err)
rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError)
}
urlBuilder, err := urlbuilder.NewPrefixed(req, schemas, "v1")
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
}
type APIFunc func(schema.Factory, *types.APIRequest)
func (a *apiServer) apiHandler(apiFunc APIFunc) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
a.api(rw, req, apiFunc)
})
}
func (a *apiServer) api(rw http.ResponseWriter, req *http.Request, apiFunc APIFunc) {
apiOp, ok := a.common(rw, req)
if ok {
if apiFunc != nil {
apiFunc(a.sf, apiOp)
}
a.server.Handle(apiOp)
}
}

View File

@@ -0,0 +1,42 @@
package publicapi
import (
"github.com/gorilla/mux"
"github.com/rancher/norman/v2/pkg/types"
"github.com/rancher/steve/pkg/attributes"
"github.com/rancher/steve/pkg/resources/schema"
runtimeschema "k8s.io/apimachinery/pkg/runtime/schema"
)
func k8sAPI(sf schema.Factory, apiOp *types.APIRequest) {
vars := mux.Vars(apiOp.Request)
group := vars["group"]
if group == "core" {
group = ""
}
apiOp.Name = vars["name"]
apiOp.Type = sf.ByGVR(runtimeschema.GroupVersionResource{
Version: vars["version"],
Group: group,
Resource: vars["resource"],
})
nOrN := vars["nameorns"]
if nOrN != "" {
schema := apiOp.Schemas.Schema(apiOp.Type)
if attributes.Namespaced(schema) {
vars["namespace"] = nOrN
} else {
vars["name"] = nOrN
}
}
if namespace := vars["namespace"]; namespace != "" {
apiOp.Namespaces = []string{namespace}
}
}
func apiRoot(sf schema.Factory, apiOp *types.APIRequest) {
apiOp.Type = "apiRoot"
}