1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-19 03:32:18 +00:00
steve/pkg/server/server.go

262 lines
7.2 KiB
Go
Raw Normal View History

2019-08-04 17:41:32 +00:00
package server
import (
"context"
2020-01-31 05:37:59 +00:00
"errors"
2019-08-04 17:41:32 +00:00
"net/http"
2021-01-08 09:28:24 +00:00
apiserver "github.com/rancher/apiserver/pkg/server"
"github.com/rancher/apiserver/pkg/types"
2020-01-31 05:37:59 +00:00
"github.com/rancher/dynamiclistener/server"
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/accesscontrol"
"github.com/rancher/steve/pkg/aggregation"
2020-07-24 08:42:00 +00:00
"github.com/rancher/steve/pkg/auth"
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/client"
"github.com/rancher/steve/pkg/clustercache"
2020-01-31 05:37:59 +00:00
schemacontroller "github.com/rancher/steve/pkg/controllers/schema"
"github.com/rancher/steve/pkg/resources"
"github.com/rancher/steve/pkg/resources/common"
"github.com/rancher/steve/pkg/resources/schemas"
2020-01-31 05:37:59 +00:00
"github.com/rancher/steve/pkg/schema"
"github.com/rancher/steve/pkg/schema/definitions"
2020-01-31 05:37:59 +00:00
"github.com/rancher/steve/pkg/server/handler"
2020-07-24 08:42:00 +00:00
"github.com/rancher/steve/pkg/server/router"
metricsStore "github.com/rancher/steve/pkg/stores/metrics"
"github.com/rancher/steve/pkg/stores/proxy"
"github.com/rancher/steve/pkg/stores/sqlpartition"
"github.com/rancher/steve/pkg/stores/sqlproxy"
2020-06-22 15:49:49 +00:00
"github.com/rancher/steve/pkg/summarycache"
2020-07-24 08:42:00 +00:00
"k8s.io/client-go/rest"
2019-08-04 17:41:32 +00:00
)
2020-01-31 05:37:59 +00:00
var ErrConfigRequired = errors.New("rest config is required")
2019-08-04 17:41:32 +00:00
2020-07-24 08:42:00 +00:00
type Server struct {
http.Handler
ClientFactory *client.Factory
ClusterCache clustercache.ClusterCache
SchemaFactory schema.Factory
RESTConfig *rest.Config
BaseSchemas *types.APISchemas
AccessSetLookup accesscontrol.AccessSetLookup
2021-01-08 09:28:24 +00:00
APIServer *apiserver.Server
ClusterRegistry string
2021-08-10 23:09:19 +00:00
Version string
2020-07-24 08:42:00 +00:00
authMiddleware auth.Middleware
controllers *Controllers
needControllerStart bool
next http.Handler
router router.RouterFunc
aggregationSecretNamespace string
aggregationSecretName string
SQLCache bool
2020-07-24 08:42:00 +00:00
}
type Options struct {
// Controllers If the controllers are passed in the caller must also start the controllers
Controllers *Controllers
ClientFactory *client.Factory
AccessSetLookup accesscontrol.AccessSetLookup
AuthMiddleware auth.Middleware
Next http.Handler
Router router.RouterFunc
AggregationSecretNamespace string
AggregationSecretName string
ClusterRegistry string
2021-08-10 23:09:19 +00:00
ServerVersion string
// SQLCache enables the SQLite-based caching mechanism
SQLCache bool
2020-07-24 08:42:00 +00:00
}
func New(ctx context.Context, restConfig *rest.Config, opts *Options) (*Server, error) {
if opts == nil {
opts = &Options{}
}
server := &Server{
RESTConfig: restConfig,
ClientFactory: opts.ClientFactory,
AccessSetLookup: opts.AccessSetLookup,
authMiddleware: opts.AuthMiddleware,
controllers: opts.Controllers,
next: opts.Next,
router: opts.Router,
aggregationSecretNamespace: opts.AggregationSecretNamespace,
aggregationSecretName: opts.AggregationSecretName,
ClusterRegistry: opts.ClusterRegistry,
2021-08-10 23:09:19 +00:00
Version: opts.ServerVersion,
// SQLCache enables the SQLite-based lasso caching mechanism
SQLCache: opts.SQLCache,
2020-07-24 08:42:00 +00:00
}
if err := setup(ctx, server); err != nil {
return nil, err
}
return server, server.start(ctx)
}
2020-01-31 05:37:59 +00:00
func setDefaults(server *Server) error {
2020-07-19 20:56:34 +00:00
if server.RESTConfig == nil {
2020-01-31 05:37:59 +00:00
return ErrConfigRequired
2019-08-04 17:41:32 +00:00
}
2020-07-24 08:42:00 +00:00
if server.controllers == nil {
2020-01-31 05:37:59 +00:00
var err error
2020-07-24 08:42:00 +00:00
server.controllers, err = NewController(server.RESTConfig, nil)
server.needControllerStart = true
2020-01-31 05:37:59 +00:00
if err != nil {
return err
}
2019-09-09 21:28:55 +00:00
}
2020-07-24 08:42:00 +00:00
if server.next == nil {
server.next = http.NotFoundHandler()
2019-08-04 17:41:32 +00:00
}
2020-01-31 05:37:59 +00:00
if server.BaseSchemas == nil {
server.BaseSchemas = types.EmptyAPISchemas()
2019-08-04 17:41:32 +00:00
}
2020-01-31 05:37:59 +00:00
return nil
}
2020-07-24 08:42:00 +00:00
func setup(ctx context.Context, server *Server) error {
2020-02-27 17:34:51 +00:00
err := setDefaults(server)
if err != nil {
2020-07-24 08:42:00 +00:00
return err
2019-08-04 17:41:32 +00:00
}
2020-02-27 17:34:51 +00:00
cf := server.ClientFactory
if cf == nil {
2020-07-24 08:42:00 +00:00
cf, err = client.NewFactory(server.RESTConfig, server.authMiddleware != nil)
2020-02-27 17:34:51 +00:00
if err != nil {
2020-07-24 08:42:00 +00:00
return err
2020-02-27 17:34:51 +00:00
}
server.ClientFactory = cf
2019-08-04 17:41:32 +00:00
}
asl := server.AccessSetLookup
if asl == nil {
2020-07-24 08:42:00 +00:00
asl = accesscontrol.NewAccessStore(ctx, true, server.controllers.RBAC)
}
2020-10-30 23:08:28 +00:00
ccache := clustercache.NewClusterCache(ctx, cf.AdminDynamicClient())
2020-07-19 20:56:34 +00:00
server.ClusterCache = ccache
sf := schema.NewCollection(ctx, server.BaseSchemas, asl)
2020-02-10 17:18:20 +00:00
2021-08-10 23:09:19 +00:00
if err = resources.DefaultSchemas(ctx, server.BaseSchemas, ccache, server.ClientFactory, sf, server.Version); err != nil {
2020-07-24 08:42:00 +00:00
return err
2020-06-06 03:45:26 +00:00
}
definitions.Register(ctx, server.BaseSchemas, server.controllers.K8s.Discovery(),
server.controllers.CRD.CustomResourceDefinition(), server.controllers.API.APIService())
2020-06-06 03:45:26 +00:00
2020-10-23 21:00:11 +00:00
summaryCache := summarycache.New(sf, ccache)
summaryCache.Start(ctx)
2020-07-19 20:56:34 +00:00
cols, err := common.NewDynamicColumns(server.RESTConfig)
2020-02-08 20:04:20 +00:00
if err != nil {
2020-07-24 08:42:00 +00:00
return err
2020-02-08 20:04:20 +00:00
}
var onSchemasHandler schemacontroller.SchemasHandlerFunc
if server.SQLCache {
s, err := sqlproxy.NewProxyStore(cols, cf, summaryCache, summaryCache, nil)
if err != nil {
panic(err)
}
errStore := proxy.NewErrorStore(
proxy.NewUnformatterStore(
proxy.NewWatchRefresh(
sqlpartition.NewStore(
s,
asl,
),
asl,
),
),
)
store := metricsStore.NewMetricsStore(errStore)
// end store setup code
for _, template := range resources.DefaultSchemaTemplatesForStore(store, server.BaseSchemas, summaryCache, server.controllers.K8s.Discovery()) {
sf.AddTemplate(template)
}
onSchemasHandler = func(schemas *schema.Collection) error {
if err := ccache.OnSchemas(schemas); err != nil {
return err
}
if err := s.Reset(); err != nil {
return err
}
return nil
}
} else {
for _, template := range resources.DefaultSchemaTemplates(cf, server.BaseSchemas, summaryCache, asl, server.controllers.K8s.Discovery(), server.controllers.Core.Namespace().Cache()) {
sf.AddTemplate(template)
}
onSchemasHandler = ccache.OnSchemas
}
schemas.SetupWatcher(ctx, server.BaseSchemas, asl, sf)
2020-07-24 08:42:00 +00:00
schemacontroller.Register(ctx,
2020-02-08 20:04:20 +00:00
cols,
2020-07-24 08:42:00 +00:00
server.controllers.K8s.Discovery(),
server.controllers.CRD.CustomResourceDefinition(),
server.controllers.API.APIService(),
server.controllers.K8s.AuthorizationV1().SelfSubjectAccessReviews(),
onSchemasHandler,
2019-08-13 23:36:03 +00:00
sf)
2019-08-04 17:41:32 +00:00
2021-01-08 09:28:24 +00:00
apiServer, handler, err := handler.New(server.RESTConfig, sf, server.authMiddleware, server.next, server.router)
2019-08-14 18:08:34 +00:00
if err != nil {
2020-07-24 08:42:00 +00:00
return err
2020-02-22 05:18:58 +00:00
}
2021-01-08 09:28:24 +00:00
server.APIServer = apiServer
2020-07-24 08:42:00 +00:00
server.Handler = handler
server.SchemaFactory = sf
2020-07-24 08:42:00 +00:00
return nil
2020-01-31 05:37:59 +00:00
}
2020-07-24 08:42:00 +00:00
func (c *Server) start(ctx context.Context) error {
if c.needControllerStart {
if err := c.controllers.Start(ctx); err != nil {
return err
2019-09-09 21:28:55 +00:00
}
}
2020-07-24 08:42:00 +00:00
return nil
2020-01-31 05:37:59 +00:00
}
func (c *Server) StartAggregation(ctx context.Context) {
aggregation.Watch(ctx, c.controllers.Core.Secret(), c.aggregationSecretNamespace,
c.aggregationSecretName, c)
}
2020-01-31 22:10:47 +00:00
func (c *Server) ListenAndServe(ctx context.Context, httpsPort, httpPort int, opts *server.ListenOpts) error {
2020-01-31 05:37:59 +00:00
if opts == nil {
opts = &server.ListenOpts{}
2019-08-14 18:08:34 +00:00
}
2020-01-31 22:10:47 +00:00
if opts.Storage == nil && opts.Secrets == nil {
2020-07-24 08:42:00 +00:00
opts.Secrets = c.controllers.Core.Secret()
2020-01-31 05:37:59 +00:00
}
c.StartAggregation(ctx)
if len(opts.TLSListenerConfig.SANs) == 0 {
opts.TLSListenerConfig.SANs = []string{"127.0.0.1"}
}
2020-07-24 08:42:00 +00:00
if err := server.ListenAndServe(ctx, httpsPort, httpPort, c, opts); err != nil {
2020-01-31 05:37:59 +00:00
return err
}
2019-09-11 17:32:01 +00:00
2020-01-31 22:10:47 +00:00
<-ctx.Done()
return ctx.Err()
2019-09-09 21:28:55 +00:00
}