2019-08-04 10:41:32 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-30 22:37:59 -07:00
|
|
|
"errors"
|
2019-08-04 10:41:32 -07:00
|
|
|
"net/http"
|
|
|
|
|
2020-03-10 23:12:04 -07:00
|
|
|
"github.com/rancher/steve/pkg/server/resources/schemas"
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
"github.com/rancher/dynamiclistener/server"
|
2019-09-11 14:05:00 -07:00
|
|
|
"github.com/rancher/steve/pkg/accesscontrol"
|
|
|
|
"github.com/rancher/steve/pkg/client"
|
|
|
|
"github.com/rancher/steve/pkg/clustercache"
|
2020-01-30 22:37:59 -07:00
|
|
|
schemacontroller "github.com/rancher/steve/pkg/controllers/schema"
|
2020-02-21 22:18:58 -07:00
|
|
|
"github.com/rancher/steve/pkg/dashboard"
|
2020-01-30 22:37:59 -07:00
|
|
|
"github.com/rancher/steve/pkg/schema"
|
|
|
|
"github.com/rancher/steve/pkg/schemaserver/types"
|
|
|
|
"github.com/rancher/steve/pkg/server/handler"
|
|
|
|
"github.com/rancher/steve/pkg/server/resources"
|
2020-02-08 13:04:20 -07:00
|
|
|
"github.com/rancher/steve/pkg/server/resources/common"
|
2019-08-04 10:41:32 -07:00
|
|
|
)
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
var ErrConfigRequired = errors.New("rest config is required")
|
2019-08-04 10:41:32 -07:00
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
func setDefaults(server *Server) error {
|
|
|
|
if server.RestConfig == nil {
|
|
|
|
return ErrConfigRequired
|
2019-08-04 10:41:32 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
if server.Controllers == nil {
|
|
|
|
var err error
|
|
|
|
server.Controllers, err = NewController(server.RestConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-09 14:28:55 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
if server.Next == nil {
|
|
|
|
server.Next = http.NotFoundHandler()
|
2019-08-04 10:41:32 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
if server.BaseSchemas == nil {
|
|
|
|
server.BaseSchemas = types.EmptyAPISchemas()
|
2019-08-04 10:41:32 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(ctx context.Context, server *Server) (http.Handler, *schema.Collection, error) {
|
2020-02-27 10:34:51 -07:00
|
|
|
err := setDefaults(server)
|
|
|
|
if err != nil {
|
2020-01-30 22:37:59 -07:00
|
|
|
return nil, nil, err
|
2019-08-04 10:41:32 -07:00
|
|
|
}
|
|
|
|
|
2020-02-27 10:34:51 -07:00
|
|
|
cf := server.ClientFactory
|
|
|
|
if cf == nil {
|
|
|
|
cf, err = client.NewFactory(server.RestConfig, server.AuthMiddleware != nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
server.ClientFactory = cf
|
2019-08-04 10:41:32 -07:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:11:21 -07:00
|
|
|
asl := server.AccessSetLookup
|
|
|
|
if asl == nil {
|
2020-02-08 13:04:20 -07:00
|
|
|
asl = accesscontrol.NewAccessStore(ctx, true, server.RBAC)
|
2020-02-04 14:11:21 -07:00
|
|
|
}
|
|
|
|
|
2020-03-12 19:14:40 -07:00
|
|
|
ccache := clustercache.NewClusterCache(ctx, cf.DynamicClient())
|
2020-02-10 10:18:20 -07:00
|
|
|
|
2020-03-12 13:14:47 -07:00
|
|
|
server.BaseSchemas = resources.DefaultSchemas(server.BaseSchemas, ccache, cf)
|
2020-03-10 23:12:04 -07:00
|
|
|
server.SchemaTemplates = append(server.SchemaTemplates, resources.DefaultSchemaTemplates(cf, asl, server.K8s.Discovery())...)
|
2020-02-10 10:18:20 -07:00
|
|
|
|
2020-02-08 13:04:20 -07:00
|
|
|
cols, err := common.NewDynamicColumns(server.RestConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sf := schema.NewCollection(ctx, server.BaseSchemas, asl)
|
2020-03-10 23:12:04 -07:00
|
|
|
|
|
|
|
schemas.SetupWatcher(ctx, server.BaseSchemas, asl, sf)
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
sync := schemacontroller.Register(ctx,
|
2020-02-08 13:04:20 -07:00
|
|
|
cols,
|
2020-01-30 22:37:59 -07:00
|
|
|
server.K8s.Discovery(),
|
|
|
|
server.CRD.CustomResourceDefinition(),
|
|
|
|
server.API.APIService(),
|
|
|
|
server.K8s.AuthorizationV1().SelfSubjectAccessReviews(),
|
2019-09-09 14:28:55 -07:00
|
|
|
ccache,
|
2019-08-13 16:36:03 -07:00
|
|
|
sf)
|
2019-08-04 10:41:32 -07:00
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
handler, err := handler.New(server.RestConfig, sf, server.AuthMiddleware, server.Next, server.Router)
|
2019-08-14 11:08:34 -07:00
|
|
|
if err != nil {
|
2020-01-30 22:37:59 -07:00
|
|
|
return nil, nil, err
|
2019-08-14 11:08:34 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
server.PostStartHooks = append(server.PostStartHooks, func() error {
|
|
|
|
return sync()
|
|
|
|
})
|
|
|
|
|
2020-02-21 22:18:58 -07:00
|
|
|
if server.DashboardURL != nil && server.DashboardURL() != "" {
|
|
|
|
handler = dashboard.Route(handler, server.DashboardURL)
|
|
|
|
}
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
return handler, sf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Server) Handler(ctx context.Context) (http.Handler, error) {
|
|
|
|
handler, sf, err := setup(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-01 15:58:24 -07:00
|
|
|
c.Next = handler
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
for _, hook := range c.StartHooks {
|
|
|
|
if err := hook(ctx, c); err != nil {
|
|
|
|
return nil, err
|
2019-09-11 10:32:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 22:37:59 -07:00
|
|
|
for i := range c.SchemaTemplates {
|
|
|
|
sf.AddTemplate(&c.SchemaTemplates[i])
|
|
|
|
}
|
|
|
|
|
2020-01-31 15:10:47 -07:00
|
|
|
if err := c.Controllers.Start(ctx); err != nil {
|
2020-01-30 22:37:59 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range c.PostStartHooks {
|
|
|
|
if err := hook(); err != nil {
|
|
|
|
return nil, err
|
2019-09-09 14:28:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 15:58:24 -07:00
|
|
|
return c.Next, nil
|
2020-01-30 22:37:59 -07:00
|
|
|
}
|
|
|
|
|
2020-01-31 15:10:47 -07:00
|
|
|
func (c *Server) ListenAndServe(ctx context.Context, httpsPort, httpPort int, opts *server.ListenOpts) error {
|
|
|
|
handler, err := c.Handler(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-30 22:37:59 -07:00
|
|
|
|
|
|
|
if opts == nil {
|
|
|
|
opts = &server.ListenOpts{}
|
2019-08-14 11:08:34 -07:00
|
|
|
}
|
2020-01-31 15:10:47 -07:00
|
|
|
if opts.Storage == nil && opts.Secrets == nil {
|
|
|
|
opts.Secrets = c.Core.Secret()
|
2020-01-30 22:37:59 -07:00
|
|
|
}
|
|
|
|
if err := server.ListenAndServe(ctx, httpsPort, httpPort, handler, opts); err != nil {
|
2019-09-09 14:28:55 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-31 15:10:47 -07:00
|
|
|
if err := c.Controllers.Start(ctx); err != nil {
|
2020-01-30 22:37:59 -07:00
|
|
|
return err
|
|
|
|
}
|
2019-09-11 10:32:01 -07:00
|
|
|
|
2020-01-31 15:10:47 -07:00
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
2019-09-09 14:28:55 -07:00
|
|
|
}
|