add ability to specify etcd snapshot list output format (#5132)

This commit is contained in:
Brian Downs 2022-02-25 14:00:00 -07:00 committed by GitHub
parent 142eed1a9f
commit 40a46e1412
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 14 deletions

View File

@ -128,7 +128,11 @@ func NewEtcdSnapshotSubcommands(delete, list, prune, save func(ctx *cli.Context)
SkipFlagParsing: false, SkipFlagParsing: false,
SkipArgReorder: true, SkipArgReorder: true,
Action: list, Action: list,
Flags: EtcdSnapshotFlags, Flags: append(EtcdSnapshotFlags, &cli.StringFlag{
Name: "o,output",
Usage: "(db) List format. Default: standard. Optional: json",
Destination: &ServerConfig.EtcdListFormat,
}),
}, },
{ {
Name: "prune", Name: "prune",

View File

@ -86,6 +86,7 @@ type Server struct {
EtcdSnapshotCron string EtcdSnapshotCron string
EtcdSnapshotRetention int EtcdSnapshotRetention int
EtcdSnapshotCompress bool EtcdSnapshotCompress bool
EtcdListFormat string
EtcdS3 bool EtcdS3 bool
EtcdS3Endpoint string EtcdS3Endpoint string
EtcdS3EndpointCA string EtcdS3EndpointCA string

View File

@ -1,10 +1,12 @@
package etcdsnapshot package etcdsnapshot
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"text/tabwriter" "text/tabwriter"
"time" "time"
@ -17,6 +19,7 @@ import (
util2 "github.com/rancher/k3s/pkg/util" util2 "github.com/rancher/k3s/pkg/util"
"github.com/rancher/wrangler/pkg/signals" "github.com/rancher/wrangler/pkg/signals"
"github.com/urfave/cli" "github.com/urfave/cli"
"gopkg.in/yaml.v2"
) )
// commandSetup setups up common things needed // commandSetup setups up common things needed
@ -40,6 +43,7 @@ func commandSetup(app *cli.Context, cfg *cmds.Server, sc *server.Config) (string
sc.ControlConfig.EtcdSnapshotName = cfg.EtcdSnapshotName sc.ControlConfig.EtcdSnapshotName = cfg.EtcdSnapshotName
sc.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir sc.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir
sc.ControlConfig.EtcdSnapshotCompress = cfg.EtcdSnapshotCompress sc.ControlConfig.EtcdSnapshotCompress = cfg.EtcdSnapshotCompress
sc.ControlConfig.EtcdListFormat = strings.ToLower(cfg.EtcdListFormat)
sc.ControlConfig.EtcdS3 = cfg.EtcdS3 sc.ControlConfig.EtcdS3 = cfg.EtcdS3
sc.ControlConfig.EtcdS3Endpoint = cfg.EtcdS3Endpoint sc.ControlConfig.EtcdS3Endpoint = cfg.EtcdS3Endpoint
sc.ControlConfig.EtcdS3EndpointCA = cfg.EtcdS3EndpointCA sc.ControlConfig.EtcdS3EndpointCA = cfg.EtcdS3EndpointCA
@ -152,6 +156,17 @@ func List(app *cli.Context) error {
return list(app, &cmds.ServerConfig) return list(app, &cmds.ServerConfig)
} }
var etcdListFormats = []string{"json", "yaml"}
func validEtcdListFormat(format string) bool {
for _, supportedFormat := range etcdListFormats {
if format == supportedFormat {
return true
}
}
return false
}
func list(app *cli.Context, cfg *cmds.Server) error { func list(app *cli.Context, cfg *cmds.Server) error {
var serverConfig server.Config var serverConfig server.Config
@ -171,6 +186,22 @@ func list(app *cli.Context, cfg *cmds.Server) error {
return err return err
} }
if cfg.EtcdListFormat != "" && !validEtcdListFormat(cfg.EtcdListFormat) {
return errors.New("invalid output format: " + cfg.EtcdListFormat)
}
switch cfg.EtcdListFormat {
case "json":
if err := json.NewEncoder(os.Stdout).Encode(sf); err != nil {
return err
}
return nil
case "yaml":
if err := yaml.NewEncoder(os.Stdout).Encode(sf); err != nil {
return err
}
return nil
default:
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
defer w.Flush() defer w.Flush()
@ -189,6 +220,7 @@ func list(app *cli.Context, cfg *cmds.Server) error {
} }
} }
} }
}
return nil return nil
} }

View File

@ -173,6 +173,7 @@ type Control struct {
EtcdSnapshotCron string EtcdSnapshotCron string
EtcdSnapshotRetention int EtcdSnapshotRetention int
EtcdSnapshotCompress bool EtcdSnapshotCompress bool
EtcdListFormat string
EtcdS3 bool EtcdS3 bool
EtcdS3Endpoint string EtcdS3Endpoint string
EtcdS3EndpointCA string EtcdS3EndpointCA string