1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-27 11:00:48 +00:00
steve/pkg/server/cli/clicontext.go

88 lines
1.9 KiB
Go
Raw Normal View History

2020-01-31 05:37:59 +00:00
package cli
import (
2020-05-31 02:02:49 +00:00
"context"
steveauth "github.com/rancher/steve/pkg/auth"
2020-01-31 05:37:59 +00:00
authcli "github.com/rancher/steve/pkg/auth/cli"
"github.com/rancher/steve/pkg/server"
2021-02-18 17:36:27 +00:00
"github.com/rancher/steve/pkg/ui"
"github.com/rancher/wrangler/v3/pkg/kubeconfig"
"github.com/rancher/wrangler/v3/pkg/ratelimit"
2020-01-31 05:37:59 +00:00
"github.com/urfave/cli"
)
type Config struct {
KubeConfig string
2021-02-18 17:36:27 +00:00
Context string
2020-01-31 05:37:59 +00:00
HTTPSListenPort int
HTTPListenPort int
2021-02-18 17:36:27 +00:00
UIPath string
2020-01-31 05:37:59 +00:00
WebhookConfig authcli.WebhookConfig
}
2020-05-31 02:02:49 +00:00
func (c *Config) MustServer(ctx context.Context) *server.Server {
cc, err := c.ToServer(ctx, false)
2020-01-31 05:37:59 +00:00
if err != nil {
panic(err)
}
return cc
}
func (c *Config) ToServer(ctx context.Context, sqlCache bool) (*server.Server, error) {
2020-05-31 02:02:49 +00:00
var (
2020-07-24 08:42:00 +00:00
auth steveauth.Middleware
2020-05-31 02:02:49 +00:00
)
2021-02-18 17:36:27 +00:00
restConfig, err := kubeconfig.GetNonInteractiveClientConfigWithContext(c.KubeConfig, c.Context).ClientConfig()
2020-01-31 05:37:59 +00:00
if err != nil {
return nil, err
}
2020-02-22 05:18:58 +00:00
restConfig.RateLimiter = ratelimit.None
2020-01-31 05:37:59 +00:00
2021-02-18 17:36:27 +00:00
if c.WebhookConfig.WebhookAuthentication {
2020-05-31 02:02:49 +00:00
auth, err = c.WebhookConfig.WebhookMiddleware()
if err != nil {
return nil, err
}
2020-01-31 05:37:59 +00:00
}
2020-07-24 08:42:00 +00:00
return server.New(ctx, restConfig, &server.Options{
2020-01-31 05:37:59 +00:00
AuthMiddleware: auth,
2021-02-18 17:36:27 +00:00
Next: ui.New(c.UIPath),
SQLCache: sqlCache,
2020-07-24 08:42:00 +00:00
})
2020-01-31 05:37:59 +00:00
}
func Flags(config *Config) []cli.Flag {
flags := []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
Destination: &config.KubeConfig,
},
2021-02-18 17:36:27 +00:00
cli.StringFlag{
Name: "context",
EnvVar: "CONTEXT",
Destination: &config.Context,
},
cli.StringFlag{
Name: "ui-path",
Destination: &config.UIPath,
},
2020-01-31 05:37:59 +00:00
cli.IntFlag{
Name: "https-listen-port",
2020-05-31 02:02:49 +00:00
Value: 9443,
2020-01-31 05:37:59 +00:00
Destination: &config.HTTPSListenPort,
},
cli.IntFlag{
Name: "http-listen-port",
2020-05-31 02:02:49 +00:00
Value: 9080,
2020-01-31 05:37:59 +00:00
Destination: &config.HTTPListenPort,
},
}
return append(flags, authcli.Flags(&config.WebhookConfig)...)
}