Skip to content

Commit cc169de

Browse files
committed
jump straigh to urfavecli v3
Signed-off-by: Derek Nola <[email protected]>
1 parent 66e3582 commit cc169de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+316
-282
lines changed

cmd/agent/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ import (
99
"github.com/k3s-io/k3s/pkg/cli/cmds"
1010
"github.com/k3s-io/k3s/pkg/configfilearg"
1111
"github.com/sirupsen/logrus"
12-
"github.com/urfave/cli/v2"
12+
"github.com/urfave/cli/v3"
1313
)
1414

1515
func main() {
1616
app := cmds.NewApp()
1717
app.Commands = []*cli.Command{
1818
cmds.NewAgentCommand(agent.Run),
1919
}
20+
app.DisableSliceFlagSeparator = true
2021

21-
if err := app.Run(configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
22+
if err := app.Run(context.Background(), configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
2223
logrus.Fatalf("Error: %v", err)
2324
}
2425
}

cmd/cert/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/k3s-io/k3s/pkg/cli/cmds"
1010
"github.com/k3s-io/k3s/pkg/configfilearg"
1111
"github.com/sirupsen/logrus"
12-
"github.com/urfave/cli/v2"
12+
"github.com/urfave/cli/v3"
1313
)
1414

1515
func main() {
@@ -21,8 +21,9 @@ func main() {
2121
cert.RotateCA,
2222
),
2323
}
24+
app.DisableSliceFlagSeparator = true
2425

25-
if err := app.Run(configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
26+
if err := app.Run(context.Background(), configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
2627
logrus.Fatal(err)
2728
}
2829
}

cmd/k3s/main.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/rancher/wrangler/v3/pkg/resolvehome"
2525
"github.com/sirupsen/logrus"
2626
"github.com/spf13/pflag"
27-
"github.com/urfave/cli/v2"
27+
"github.com/urfave/cli/v3"
2828
)
2929

3030
var criDefaultConfigPath = "/etc/crictl.yaml"
@@ -50,7 +50,7 @@ func main() {
5050

5151
// Handle subcommand invocation (k3s server, k3s crictl, etc)
5252
app := cmds.NewApp()
53-
app.EnableBashCompletion = true
53+
app.EnableShellCompletion = true
5454
app.Commands = []*cli.Command{
5555
cmds.NewServerCommand(internalCLIAction(version.Program+"-server"+programPostfix, dataDir, os.Args)),
5656
cmds.NewAgentCommand(internalCLIAction(version.Program+"-agent"+programPostfix, dataDir, os.Args)),
@@ -88,7 +88,7 @@ func main() {
8888
cmds.NewCompletionCommand(internalCLIAction(version.Program+"-completion", dataDir, os.Args)),
8989
}
9090

91-
if err := app.Run(os.Args); err != nil && !errors.Is(err, context.Canceled) {
91+
if err := app.Run(context.Background(), os.Args); err != nil && !errors.Is(err, context.Canceled) {
9292
logrus.Fatalf("Error: %v", err)
9393
}
9494
}
@@ -170,8 +170,8 @@ func runCLIs(dataDir string) bool {
170170
}
171171

172172
// externalCLIAction returns a function that will call an external binary, be used as the Action of a cli.Command.
173-
func externalCLIAction(cmd, dataDir string) func(cli *cli.Context) error {
174-
return func(cli *cli.Context) error {
173+
func externalCLIAction(cmd, dataDir string) func(ctx context.Context, cli *cli.Command) error {
174+
return func(ctx context.Context, cli *cli.Command) error {
175175
return externalCLI(cmd, dataDir, cli.Args().Slice())
176176
}
177177
}
@@ -188,18 +188,18 @@ func externalCLI(cli, dataDir string, args []string) error {
188188
}
189189

190190
// internalCLIAction returns a function that will call a K3s internal command, be used as the Action of a cli.Command.
191-
func internalCLIAction(cmd, dataDir string, args []string) func(ctx *cli.Context) error {
192-
return func(ctx *cli.Context) error {
191+
func internalCLIAction(cmd, dataDir string, args []string) func(ctx context.Context, app *cli.Command) error {
192+
return func(ctx context.Context, app *cli.Command) error {
193193
// We don't want the Info logs seen when printing the autocomplete script
194194
if cmd == "k3s-completion" {
195195
logrus.SetLevel(logrus.ErrorLevel)
196196
}
197-
return stageAndRunCLI(ctx, cmd, dataDir, args)
197+
return stageAndRunCLI(app, cmd, dataDir, args)
198198
}
199199
}
200200

201201
// stageAndRunCLI calls an external binary.
202-
func stageAndRunCLI(cli *cli.Context, cmd string, dataDir string, args []string) error {
202+
func stageAndRunCLI(cli *cli.Command, cmd string, dataDir string, args []string) error {
203203
return stageAndRun(dataDir, cmd, args, true)
204204
}
205205

cmd/server/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
ctr2 "github.com/k3s-io/k3s/pkg/ctr"
2424
kubectl2 "github.com/k3s-io/k3s/pkg/kubectl"
2525
"github.com/sirupsen/logrus"
26-
"github.com/urfave/cli/v2"
26+
"github.com/urfave/cli/v3"
2727
crictl2 "sigs.k8s.io/cri-tools/cmd/crictl"
2828
)
2929

@@ -78,8 +78,11 @@ func main() {
7878
),
7979
cmds.NewCompletionCommand(completion.Run),
8080
}
81-
82-
if err := app.Run(configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
81+
// Due to https://github.com/urfave/cli/issues/1856
82+
// we need to set the SliceFlagSeparator to a value that hopefully no one ever uses
83+
// to avoid issues with kube-* flags that have "," in them
84+
app.DisableSliceFlagSeparator = true
85+
if err := app.Run(context.Background(), configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
8386
logrus.Fatalf("Error: %v", err)
8487
}
8588
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ require (
142142
github.com/spf13/pflag v1.0.5
143143
github.com/stretchr/testify v1.10.0
144144
github.com/urfave/cli/v2 v2.27.5
145+
github.com/urfave/cli/v3 v3.0.0-beta1
145146
github.com/vishvananda/netlink v1.3.1-0.20240905180732-b1ce50cfa9be
146147
github.com/yl2chen/cidranger v1.0.2
147148
go.etcd.io/etcd/api/v3 v3.5.18

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,8 @@ github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX
13291329
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
13301330
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
13311331
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
1332+
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
1333+
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
13321334
github.com/vbatts/tar-split v0.11.6 h1:4SjTW5+PU11n6fZenf2IPoV8/tz3AaYHMWjf23envGs=
13331335
github.com/vbatts/tar-split v0.11.6/go.mod h1:dqKNtesIOr2j2Qv3W/cHjnvk9I8+G7oAkFDFN6TCBEI=
13341336
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=

main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/k3s-io/k3s/pkg/cli/server"
2121
"github.com/k3s-io/k3s/pkg/configfilearg"
2222
"github.com/sirupsen/logrus"
23-
"github.com/urfave/cli/v2"
23+
"github.com/urfave/cli/v3"
2424
)
2525

2626
func main() {
@@ -52,8 +52,11 @@ func main() {
5252
),
5353
cmds.NewCompletionCommand(completion.Run),
5454
}
55-
56-
if err := app.Run(configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
55+
// Due to https://github.com/urfave/cli/issues/1856
56+
// we need to set the SliceFlagSeparator to a value that hopefully no one ever uses
57+
// to avoid issues with kube-* flags that have "," in them
58+
app.DisableSliceFlagSeparator = true
59+
if err := app.Run(context.Background(), configfilearg.MustParse(os.Args)); err != nil && !errors.Is(err, context.Canceled) {
5760
logrus.Fatal(err)
5861
}
5962
}

pkg/agent/config/config.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
441441
}
442442
// If the supervisor and externally-facing apiserver are not on the same port, tell the proxy where to find the apiserver.
443443
if controlConfig.SupervisorPort != controlConfig.HTTPSPort {
444-
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(&envInfo.NodeIP)))
444+
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(envInfo.NodeIP)))
445445
if err := proxy.SetAPIServerPort(controlConfig.HTTPSPort, isIPv6); err != nil {
446446
return nil, errors.Wrapf(err, "failed to set apiserver port to %d", controlConfig.HTTPSPort)
447447
}
@@ -482,7 +482,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
482482
newNodePasswordFile := filepath.Join(nodeConfigPath, "password")
483483
upgradeOldNodePasswordPath(oldNodePasswordFile, newNodePasswordFile)
484484

485-
nodeName, nodeIPs, err := util.GetHostnameAndIPs(envInfo.NodeName, &envInfo.NodeIP)
485+
nodeName, nodeIPs, err := util.GetHostnameAndIPs(envInfo.NodeName, envInfo.NodeIP)
486486
if err != nil {
487487
return nil, err
488488
}
@@ -514,10 +514,10 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
514514
// Overwrite nodeip and flannel interface and throw a warning if user explicitly set those parameters
515515
if len(vpnIPs) != 0 {
516516
logrus.Infof("Node-ip changed to %v due to VPN", vpnIPs)
517-
if len(envInfo.NodeIP.Value()) != 0 {
517+
if len(envInfo.NodeIP) != 0 {
518518
logrus.Warn("VPN provider overrides configured node-ip parameter")
519519
}
520-
if len(envInfo.NodeExternalIP.Value()) != 0 {
520+
if len(envInfo.NodeExternalIP) != 0 {
521521
logrus.Warn("VPN provider overrides node-external-ip parameter")
522522
}
523523
nodeIPs = vpnIPs
@@ -536,7 +536,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
536536
}
537537
}
538538

539-
nodeExternalIPs, err := util.ParseStringSliceToIPs(&envInfo.NodeExternalIP)
539+
nodeExternalIPs, err := util.ParseStringSliceToIPs(envInfo.NodeExternalIP)
540540
if err != nil {
541541
return nil, fmt.Errorf("invalid node-external-ip: %w", err)
542542
}
@@ -676,13 +676,13 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
676676
}
677677

678678
var nodeExternalDNSs []string
679-
for _, dnsString := range envInfo.NodeExternalDNS.Value() {
679+
for _, dnsString := range envInfo.NodeExternalDNS {
680680
nodeExternalDNSs = append(nodeExternalDNSs, strings.Split(dnsString, ",")...)
681681
}
682682
nodeConfig.AgentConfig.NodeExternalDNSs = nodeExternalDNSs
683683

684684
var nodeInternalDNSs []string
685-
for _, dnsString := range envInfo.NodeInternalDNS.Value() {
685+
for _, dnsString := range envInfo.NodeInternalDNS {
686686
nodeInternalDNSs = append(nodeInternalDNSs, strings.Split(dnsString, ",")...)
687687
}
688688
nodeConfig.AgentConfig.NodeInternalDNSs = nodeInternalDNSs
@@ -761,7 +761,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
761761
}
762762

763763
nodeConfig.AgentConfig.PauseImage = envInfo.PauseImage
764-
nodeConfig.AgentConfig.AirgapExtraRegistry = envInfo.AirgapExtraRegistry.Value()
764+
nodeConfig.AgentConfig.AirgapExtraRegistry = envInfo.AirgapExtraRegistry
765765
nodeConfig.AgentConfig.SystemDefaultRegistry = controlConfig.SystemDefaultRegistry
766766

767767
// Apply SystemDefaultRegistry to PauseImage and AirgapExtraRegistry
@@ -773,11 +773,10 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
773773
nodeConfig.AgentConfig.AirgapExtraRegistry = append(nodeConfig.AgentConfig.AirgapExtraRegistry, controlConfig.SystemDefaultRegistry)
774774
}
775775
}
776-
777-
nodeConfig.AgentConfig.ExtraKubeletArgs = envInfo.ExtraKubeletArgs.Value()
778-
nodeConfig.AgentConfig.ExtraKubeProxyArgs = envInfo.ExtraKubeProxyArgs.Value()
779-
nodeConfig.AgentConfig.NodeTaints = envInfo.Taints.Value()
780-
nodeConfig.AgentConfig.NodeLabels = envInfo.Labels.Value()
776+
nodeConfig.AgentConfig.ExtraKubeletArgs = envInfo.ExtraKubeletArgs
777+
nodeConfig.AgentConfig.ExtraKubeProxyArgs = envInfo.ExtraKubeProxyArgs
778+
nodeConfig.AgentConfig.NodeTaints = envInfo.Taints
779+
nodeConfig.AgentConfig.NodeLabels = envInfo.Labels
781780
nodeConfig.AgentConfig.ImageCredProvBinDir = envInfo.ImageCredProvBinDir
782781
nodeConfig.AgentConfig.ImageCredProvConfig = envInfo.ImageCredProvConfig
783782
nodeConfig.AgentConfig.DisableCCM = controlConfig.DisableCCM
@@ -788,7 +787,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
788787
nodeConfig.AgentConfig.PodManifests = filepath.Join(envInfo.DataDir, "agent", DefaultPodManifestPath)
789788
nodeConfig.AgentConfig.ProtectKernelDefaults = envInfo.ProtectKernelDefaults
790789
nodeConfig.AgentConfig.DisableServiceLB = envInfo.DisableServiceLB
791-
nodeConfig.AgentConfig.VLevel = cmds.LogConfig.VLevel
790+
nodeConfig.AgentConfig.VLevel = int(cmds.LogConfig.VLevel)
792791
nodeConfig.AgentConfig.VModule = cmds.LogConfig.VModule
793792
nodeConfig.AgentConfig.LogFile = cmds.LogConfig.LogFile
794793
nodeConfig.AgentConfig.AlsoLogToStderr = cmds.LogConfig.AlsoLogToStderr

pkg/agent/run.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func Run(ctx context.Context, cfg cmds.Agent) error {
310310
}
311311

312312
if cfg.Rootless && !cfg.RootlessAlreadyUnshared {
313-
dualNode, err := utilsnet.IsDualStackIPStrings(cfg.NodeIP.Value())
313+
dualNode, err := utilsnet.IsDualStackIPStrings(cfg.NodeIP)
314314
if err != nil {
315315
return err
316316
}
@@ -335,9 +335,9 @@ func createProxyAndValidateToken(ctx context.Context, cfg *cmds.Agent) (proxy.Pr
335335
if err := os.MkdirAll(agentDir, 0700); err != nil {
336336
return nil, err
337337
}
338-
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(&cfg.NodeIP)))
338+
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(cfg.NodeIP)))
339339

340-
proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, cfg.LBServerPort, isIPv6)
340+
proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, int(cfg.LBServerPort), isIPv6)
341341
if err != nil {
342342
return nil, err
343343
}

pkg/cli/agent/agent.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
"github.com/pkg/errors"
2525
"github.com/rancher/wrangler/v3/pkg/signals"
2626
"github.com/sirupsen/logrus"
27-
"github.com/urfave/cli/v2"
27+
"github.com/urfave/cli/v3"
2828
)
2929

30-
func Run(ctx *cli.Context) error {
30+
func Run(ctx context.Context, cmd *cli.Command) error {
3131
// Validate build env
3232
cmds.MustValidateGolang()
3333

@@ -72,23 +72,23 @@ func Run(ctx *cli.Context) error {
7272
return fmt.Errorf("--server is required")
7373
}
7474

75-
if cmds.AgentConfig.FlannelIface != "" && len(cmds.AgentConfig.NodeIP.Value()) == 0 {
75+
if cmds.AgentConfig.FlannelIface != "" && len(cmds.AgentConfig.NodeIP) == 0 {
7676
ip, err := util.GetIPFromInterface(cmds.AgentConfig.FlannelIface)
7777
if err != nil {
7878
return err
7979
}
80-
cmds.AgentConfig.NodeIP.Set(ip)
80+
cmds.AgentConfig.NodeIP = []string{ip}
8181
}
8282

83-
logrus.Info("Starting " + version.Program + " agent " + ctx.App.Version)
83+
logrus.Info("Starting " + version.Program + " agent " + cmd.Version)
8484

8585
dataDir, err := datadir.LocalHome(cmds.AgentConfig.DataDir, cmds.AgentConfig.Rootless)
8686
if err != nil {
8787
return err
8888
}
8989

9090
cfg := cmds.AgentConfig
91-
cfg.Debug = ctx.Bool("debug")
91+
cfg.Debug = cmd.Bool("debug")
9292
cfg.DataDir = dataDir
9393

9494
contextCtx := signals.SetupSignalContext()

0 commit comments

Comments
 (0)