Unverified Commit 40a46e14 authored by Brian Downs's avatar Brian Downs Committed by GitHub

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

parent 142eed1a
...@@ -128,7 +128,11 @@ func NewEtcdSnapshotSubcommands(delete, list, prune, save func(ctx *cli.Context) ...@@ -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",
......
...@@ -86,6 +86,7 @@ type Server struct { ...@@ -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
......
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 ( ...@@ -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 ...@@ -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 { ...@@ -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,21 +186,38 @@ func list(app *cli.Context, cfg *cmds.Server) error { ...@@ -171,21 +186,38 @@ func list(app *cli.Context, cfg *cmds.Server) error {
return err return err
} }
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) if cfg.EtcdListFormat != "" && !validEtcdListFormat(cfg.EtcdListFormat) {
defer w.Flush() return errors.New("invalid output format: " + cfg.EtcdListFormat)
}
if cfg.EtcdS3 { switch cfg.EtcdListFormat {
fmt.Fprint(w, "Name\tSize\tCreated\n") case "json":
for _, s := range sf { if err := json.NewEncoder(os.Stdout).Encode(sf); err != nil {
if s.NodeName == "s3" { return err
fmt.Fprintf(w, "%s\t%d\t%s\n", s.Name, s.Size, s.CreatedAt.Format(time.RFC3339))
}
} }
} else { return nil
fmt.Fprint(w, "Name\tLocation\tSize\tCreated\n") case "yaml":
for _, s := range sf { if err := yaml.NewEncoder(os.Stdout).Encode(sf); err != nil {
if s.NodeName != "s3" { return err
fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", s.Name, s.Location, s.Size, s.CreatedAt.Format(time.RFC3339)) }
return nil
default:
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
defer w.Flush()
if cfg.EtcdS3 {
fmt.Fprint(w, "Name\tSize\tCreated\n")
for _, s := range sf {
if s.NodeName == "s3" {
fmt.Fprintf(w, "%s\t%d\t%s\n", s.Name, s.Size, s.CreatedAt.Format(time.RFC3339))
}
}
} else {
fmt.Fprint(w, "Name\tLocation\tSize\tCreated\n")
for _, s := range sf {
if s.NodeName != "s3" {
fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", s.Name, s.Location, s.Size, s.CreatedAt.Format(time.RFC3339))
}
} }
} }
} }
......
...@@ -173,6 +173,7 @@ type Control struct { ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment