diff --git a/go.mod b/go.mod index e86eb786eb..9efa80a61a 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,6 @@ require ( github.com/rancher/spur v0.0.0-20200617165101-8702c8e4ce7a github.com/rancher/wrangler v0.6.1 github.com/sirupsen/logrus v1.4.2 - github.com/urfave/cli v1.22.2 google.golang.org/grpc v1.26.0 k8s.io/api v0.18.0 k8s.io/apimachinery v0.18.0 diff --git a/pkg/cli/cmds/agent.go b/pkg/cli/cmds/agent.go index 8761d233c1..6445219867 100644 --- a/pkg/cli/cmds/agent.go +++ b/pkg/cli/cmds/agent.go @@ -8,13 +8,16 @@ import ( var ( k3sAgentBase = mustCmdFromK3S(cmds.NewAgentCommand(AgentRun), map[string]*K3SFlagOption{ - "config": copy, - "debug": copy, - "v": hide, - "vmodule": hide, - "log": hide, - "alsologtostderr": hide, - "data-dir": copy, + "config": copy, + "debug": copy, + "v": hide, + "vmodule": hide, + "log": hide, + "alsologtostderr": hide, + "data-dir": { + Usage: "(data) Folder to hold state", + Default: rke2Path, + }, "token": copy, "token-file": copy, "disable-selinux": drop, @@ -46,6 +49,6 @@ func NewAgentCommand() *cli.Command { return cmd } -func AgentRun(app *cli.Context) error { - return rke2.Agent(app, config) +func AgentRun(ctx *cli.Context) error { + return rke2.Agent(ctx, config) } diff --git a/pkg/cli/cmds/k3sopts.go b/pkg/cli/cmds/k3sopts.go index 662da0c86f..acfcecbd11 100644 --- a/pkg/cli/cmds/k3sopts.go +++ b/pkg/cli/cmds/k3sopts.go @@ -38,10 +38,6 @@ func mustCmdFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) *cli.C return cmd } -func flagValue(flag cli.Flag, field string) reflect.Value { - return reflect.Indirect(reflect.ValueOf(flag)).FieldByName(field) -} - func commandFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) (*cli.Command, error) { var ( newFlags []cli.Flag @@ -66,21 +62,14 @@ func commandFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) (*cli. } if opt.Usage != "" { - if v := flagValue(flag, "Usage"); v.IsValid() { - v.SetString(opt.Usage) - } + flagSetUsage(flag, opt) } if opt.Default != "" { - if v := flagValue(flag, "Default"); v.IsValid() { - v.SetString(opt.Default) - } + flagSetDefault(flag, opt) } if opt.Hide { - if v := flagValue(flag, "Hidden"); v.IsValid() { - v.SetBool(true) - } + flagSetHide(flag, opt) } - newFlags = append(newFlags, flag) } @@ -94,3 +83,60 @@ func commandFromK3S(cmd *cli.Command, flagOpts map[string]*K3SFlagOption) (*cli. cmd.Flags = newFlags return cmd, errs.Err() } + +// flagSetUsage receives a flag and a K3S flag option, parses +// both and sets the necessary fields based on the underlying +// flag type. +func flagSetUsage(flag cli.Flag, opt *K3SFlagOption) { + v := reflect.ValueOf(flag).Elem() + if v.CanSet() { + switch t := flag.(type) { + case *cli.StringFlag: + t.Usage = opt.Usage + case *cli.StringSliceFlag: + t.Usage = opt.Usage + case *cli.BoolFlag: + t.Usage = opt.Usage + } + } +} + +// flagSetDefault receives a flag and a K3S flag option, parses +// both and sets the necessary fields based on the underlying +// flag type. +func flagSetDefault(flag cli.Flag, opt *K3SFlagOption) { + v := reflect.ValueOf(flag).Elem() + if v.CanSet() { + switch t := flag.(type) { + case *cli.StringFlag: + t.DefaultText = opt.Default + t.Destination = &opt.Default + t.Value = opt.Default + case *cli.StringSliceFlag: + t.DefaultText = opt.Default + t.Destination = &([]string{opt.Default}) + t.Value = []string{opt.Default} + case *cli.BoolFlag: + t.DefaultText = opt.Default + t.Destination = &opt.Hide + t.Value = opt.Hide + } + } +} + +// flagSetHide receives a flag and a K3S flag option, parses +// both and sets the necessary fields based on the underlying +// flag type. +func flagSetHide(flag cli.Flag, opt *K3SFlagOption) { + v := reflect.ValueOf(flag).Elem() + if v.CanSet() { + switch t := flag.(type) { + case *cli.StringFlag: + t.Hidden = opt.Hide + case *cli.StringSliceFlag: + t.Hidden = opt.Hide + case *cli.BoolFlag: + t.Hidden = opt.Hide + } + } +} diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index 94e9d9e31b..b96257fbec 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -91,6 +91,6 @@ func NewServerCommand() *cli.Command { return cmd } -func ServerRun(app *cli.Context) error { - return rke2.Server(app, config) +func ServerRun(ctx *cli.Context) error { + return rke2.Server(ctx, config) } diff --git a/pkg/rke2/server.go b/pkg/rke2/server.go index 1fd5ca64b9..8fcdb58636 100644 --- a/pkg/rke2/server.go +++ b/pkg/rke2/server.go @@ -3,6 +3,7 @@ package rke2 import ( "os" "path/filepath" + "strings" "github.com/rancher/k3s/pkg/agent/config" "github.com/rancher/k3s/pkg/cli/agent" @@ -43,9 +44,14 @@ func Agent(ctx *cli.Context, cfg Config) error { } func setup(ctx *cli.Context, cfg Config) error { - dataDir := cmds.ServerConfig.DataDir - if dataDir == "" { - dataDir = cmds.AgentConfig.DataDir + var dataDir string + for _, f := range ctx.Command.Flags { + switch t := f.(type) { + case *cli.StringFlag: + if strings.Contains(t.Name, "data-dir") { + dataDir = t.DefaultText + } + } } images := images.New(cfg.Repo)