mirror of
https://github.com/niusmallnan/steve.git
synced 2025-09-05 15:11:26 +00:00
Refactor context to be sharable
This commit is contained in:
4
main.go
4
main.go
@@ -35,6 +35,6 @@ func main() {
|
|||||||
func run(_ *cli.Context) error {
|
func run(_ *cli.Context) error {
|
||||||
ctx := signals.SetupSignalHandler(context.Background())
|
ctx := signals.SetupSignalHandler(context.Background())
|
||||||
debugconfig.MustSetupDebug()
|
debugconfig.MustSetupDebug()
|
||||||
s := config.MustServerConfig().MustServer()
|
s := config.MustServer()
|
||||||
return s.ListenAndServe(ctx, nil)
|
return s.ListenAndServe(ctx, config.HTTPSListenPort, config.HTTPListenPort, nil)
|
||||||
}
|
}
|
||||||
|
@@ -16,15 +16,15 @@ type Config struct {
|
|||||||
WebhookConfig authcli.WebhookConfig
|
WebhookConfig authcli.WebhookConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) MustServerConfig() *server.Server {
|
func (c *Config) MustServer() *server.Server {
|
||||||
cc, err := c.ToServerConfig()
|
cc, err := c.ToServer()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) ToServerConfig() (*server.Server, error) {
|
func (c *Config) ToServer() (*server.Server, error) {
|
||||||
restConfig, err := kubeconfig.GetNonInteractiveClientConfig(c.KubeConfig).ClientConfig()
|
restConfig, err := kubeconfig.GetNonInteractiveClientConfig(c.KubeConfig).ClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -39,8 +39,6 @@ func (c *Config) ToServerConfig() (*server.Server, error) {
|
|||||||
Namespace: c.Namespace,
|
Namespace: c.Namespace,
|
||||||
RestConfig: restConfig,
|
RestConfig: restConfig,
|
||||||
AuthMiddleware: auth,
|
AuthMiddleware: auth,
|
||||||
HTTPPort: c.HTTPListenPort,
|
|
||||||
HTTPSPort: c.HTTPSListenPort,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -27,8 +27,6 @@ type Server struct {
|
|||||||
RestConfig *rest.Config
|
RestConfig *rest.Config
|
||||||
|
|
||||||
Namespace string
|
Namespace string
|
||||||
HTTPSPort int
|
|
||||||
HTTPPort int
|
|
||||||
BaseSchemas *types.APISchemas
|
BaseSchemas *types.APISchemas
|
||||||
SchemaTemplates []schema.Template
|
SchemaTemplates []schema.Template
|
||||||
AuthMiddleware auth.Middleware
|
AuthMiddleware auth.Middleware
|
||||||
@@ -47,6 +45,10 @@ type Controllers struct {
|
|||||||
starters []start.Starter
|
starters []start.Starter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Controllers) Start(ctx context.Context) error {
|
||||||
|
return start.All(ctx, 5, c.starters...)
|
||||||
|
}
|
||||||
|
|
||||||
func NewController(cfg *rest.Config) (*Controllers, error) {
|
func NewController(cfg *rest.Config) (*Controllers, error) {
|
||||||
c := &Controllers{}
|
c := &Controllers{}
|
||||||
|
|
||||||
|
@@ -6,8 +6,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/rancher/dynamiclistener/server"
|
"github.com/rancher/dynamiclistener/server"
|
||||||
"github.com/rancher/dynamiclistener/storage/kubernetes"
|
|
||||||
"github.com/rancher/dynamiclistener/storage/memory"
|
|
||||||
"github.com/rancher/steve/pkg/accesscontrol"
|
"github.com/rancher/steve/pkg/accesscontrol"
|
||||||
"github.com/rancher/steve/pkg/client"
|
"github.com/rancher/steve/pkg/client"
|
||||||
"github.com/rancher/steve/pkg/clustercache"
|
"github.com/rancher/steve/pkg/clustercache"
|
||||||
@@ -16,8 +14,6 @@ import (
|
|||||||
"github.com/rancher/steve/pkg/schemaserver/types"
|
"github.com/rancher/steve/pkg/schemaserver/types"
|
||||||
"github.com/rancher/steve/pkg/server/handler"
|
"github.com/rancher/steve/pkg/server/handler"
|
||||||
"github.com/rancher/steve/pkg/server/resources"
|
"github.com/rancher/steve/pkg/server/resources"
|
||||||
v1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
|
|
||||||
"github.com/rancher/wrangler/pkg/start"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrConfigRequired = errors.New("rest config is required")
|
var ErrConfigRequired = errors.New("rest config is required")
|
||||||
@@ -102,7 +98,7 @@ func (c *Server) Handler(ctx context.Context) (http.Handler, error) {
|
|||||||
sf.AddTemplate(&c.SchemaTemplates[i])
|
sf.AddTemplate(&c.SchemaTemplates[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := start.All(ctx, 5, c.starters...); err != nil {
|
if err := c.Controllers.Start(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,39 +111,26 @@ func (c *Server) Handler(ctx context.Context) (http.Handler, error) {
|
|||||||
return handler, nil
|
return handler, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenAndServe(ctx context.Context, secrets v1.SecretController, namespace string, handler http.Handler, httpsPort, httpPort int, opts *server.ListenOpts) error {
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if opts == nil {
|
|
||||||
opts = &server.ListenOpts{}
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts.CA == nil || opts.CAKey == nil {
|
|
||||||
opts.CA, opts.CAKey, err = kubernetes.LoadOrGenCA(secrets, namespace, "serving-ca")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts.Storage == nil {
|
|
||||||
storage := kubernetes.Load(ctx, secrets, namespace, "service-cert", memory.New())
|
|
||||||
opts.Storage = storage
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := server.ListenAndServe(ctx, httpsPort, httpPort, handler, opts); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Server) ListenAndServe(ctx context.Context, httpsPort, httpPort int, opts *server.ListenOpts) error {
|
func (c *Server) ListenAndServe(ctx context.Context, httpsPort, httpPort int, opts *server.ListenOpts) error {
|
||||||
handler, err := c.Handler(ctx)
|
handler, err := c.Handler(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ListenAndServe(ctx, c.Core.Secret(), c.Namespace, handler, httpsPort, httpPort, opts)
|
if opts == nil {
|
||||||
|
opts = &server.ListenOpts{}
|
||||||
|
}
|
||||||
|
if opts.Storage == nil && opts.Secrets == nil {
|
||||||
|
opts.Secrets = c.Core.Secret()
|
||||||
|
}
|
||||||
|
if err := server.ListenAndServe(ctx, httpsPort, httpPort, handler, opts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.Controllers.Start(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user