2021-01-21 21:09:15 +00:00
|
|
|
package etcdsnapshot
|
|
|
|
|
|
|
|
import (
|
2023-10-02 23:20:22 +00:00
|
|
|
"context"
|
2022-02-25 21:00:00 +00:00
|
|
|
"encoding/json"
|
2021-01-21 21:09:15 +00:00
|
|
|
"errors"
|
2021-05-11 23:59:33 +00:00
|
|
|
"fmt"
|
2021-01-21 21:09:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-10-10 19:51:42 +00:00
|
|
|
"sort"
|
2022-02-25 21:00:00 +00:00
|
|
|
"strings"
|
2021-05-11 23:59:33 +00:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
2021-01-21 21:09:15 +00:00
|
|
|
|
|
|
|
"github.com/erikdubbelboer/gspt"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
2023-10-02 23:20:22 +00:00
|
|
|
daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/etcd"
|
|
|
|
"github.com/k3s-io/k3s/pkg/server"
|
|
|
|
util2 "github.com/k3s-io/k3s/pkg/util"
|
2021-01-21 21:09:15 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/signals"
|
|
|
|
"github.com/urfave/cli"
|
2022-02-25 21:00:00 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2021-01-21 21:09:15 +00:00
|
|
|
)
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
type etcdCommand struct {
|
|
|
|
etcd *etcd.ETCD
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2021-05-07 23:10:04 +00:00
|
|
|
// commandSetup setups up common things needed
|
|
|
|
// for each etcd command.
|
2023-10-02 23:20:22 +00:00
|
|
|
func commandSetup(app *cli.Context, cfg *cmds.Server, config *server.Config) (*etcdCommand, error) {
|
|
|
|
ctx := signals.SetupSignalContext()
|
2021-05-07 23:10:04 +00:00
|
|
|
gspt.SetProcTitle(os.Args[0])
|
|
|
|
|
|
|
|
nodeName := app.String("node-name")
|
|
|
|
if nodeName == "" {
|
|
|
|
h, err := os.Hostname()
|
|
|
|
if err != nil {
|
2023-10-02 23:20:22 +00:00
|
|
|
return nil, err
|
2021-05-07 23:10:04 +00:00
|
|
|
}
|
|
|
|
nodeName = h
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("NODE_NAME", nodeName)
|
|
|
|
|
2022-03-01 21:33:01 +00:00
|
|
|
dataDir, err := server.ResolveDataDir(cfg.DataDir)
|
|
|
|
if err != nil {
|
2023-10-02 23:20:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.DisableAgent = true
|
|
|
|
config.ControlConfig.DataDir = dataDir
|
|
|
|
config.ControlConfig.EtcdSnapshotName = cfg.EtcdSnapshotName
|
|
|
|
config.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir
|
|
|
|
config.ControlConfig.EtcdSnapshotCompress = cfg.EtcdSnapshotCompress
|
|
|
|
config.ControlConfig.EtcdListFormat = strings.ToLower(cfg.EtcdListFormat)
|
|
|
|
config.ControlConfig.EtcdS3 = cfg.EtcdS3
|
|
|
|
config.ControlConfig.EtcdS3Endpoint = cfg.EtcdS3Endpoint
|
|
|
|
config.ControlConfig.EtcdS3EndpointCA = cfg.EtcdS3EndpointCA
|
|
|
|
config.ControlConfig.EtcdS3SkipSSLVerify = cfg.EtcdS3SkipSSLVerify
|
|
|
|
config.ControlConfig.EtcdS3AccessKey = cfg.EtcdS3AccessKey
|
|
|
|
config.ControlConfig.EtcdS3SecretKey = cfg.EtcdS3SecretKey
|
|
|
|
config.ControlConfig.EtcdS3BucketName = cfg.EtcdS3BucketName
|
|
|
|
config.ControlConfig.EtcdS3Region = cfg.EtcdS3Region
|
|
|
|
config.ControlConfig.EtcdS3Folder = cfg.EtcdS3Folder
|
|
|
|
config.ControlConfig.EtcdS3Insecure = cfg.EtcdS3Insecure
|
|
|
|
config.ControlConfig.EtcdS3Timeout = cfg.EtcdS3Timeout
|
|
|
|
config.ControlConfig.Runtime = daemonconfig.NewRuntime(nil)
|
|
|
|
config.ControlConfig.Runtime.ETCDServerCA = filepath.Join(dataDir, "tls", "etcd", "server-ca.crt")
|
|
|
|
config.ControlConfig.Runtime.ClientETCDCert = filepath.Join(dataDir, "tls", "etcd", "client.crt")
|
|
|
|
config.ControlConfig.Runtime.ClientETCDKey = filepath.Join(dataDir, "tls", "etcd", "client.key")
|
|
|
|
config.ControlConfig.Runtime.KubeConfigAdmin = filepath.Join(dataDir, "cred", "admin.kubeconfig")
|
|
|
|
|
|
|
|
e := etcd.NewETCD()
|
|
|
|
if err := e.SetControlConfig(&config.ControlConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
initialized, err := e.IsInitialized()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !initialized {
|
|
|
|
return nil, fmt.Errorf("etcd database not found in %s", config.ControlConfig.DataDir)
|
2022-03-01 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 22:35:31 +00:00
|
|
|
sc, err := server.NewContext(ctx, config, false)
|
2023-10-02 23:20:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.ControlConfig.Runtime.K3s = sc.K3s
|
|
|
|
config.ControlConfig.Runtime.Core = sc.Core
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
return &etcdCommand{etcd: e, ctx: ctx}, nil
|
2021-05-07 23:10:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 21:33:01 +00:00
|
|
|
// Save triggers an on-demand etcd snapshot operation
|
|
|
|
func Save(app *cli.Context) error {
|
2021-01-21 21:09:15 +00:00
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-01 21:33:01 +00:00
|
|
|
return save(app, &cmds.ServerConfig)
|
2021-01-21 21:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 21:33:01 +00:00
|
|
|
func save(app *cli.Context, cfg *cmds.Server) error {
|
2021-05-11 23:59:33 +00:00
|
|
|
var serverConfig server.Config
|
|
|
|
|
2021-05-07 23:10:04 +00:00
|
|
|
if len(app.Args()) > 0 {
|
2021-06-01 19:29:46 +00:00
|
|
|
return util2.ErrCommandNoArgs
|
2021-05-01 01:26:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
ec, err := commandSetup(app, cfg, &serverConfig)
|
2021-01-21 21:09:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
serverConfig.ControlConfig.EtcdSnapshotRetention = 0 // disable retention check
|
2021-05-01 01:26:39 +00:00
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
return ec.etcd.Snapshot(ec.ctx)
|
2021-01-21 21:09:15 +00:00
|
|
|
}
|
2021-05-07 23:10:04 +00:00
|
|
|
|
|
|
|
func Delete(app *cli.Context) error {
|
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return delete(app, &cmds.ServerConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func delete(app *cli.Context, cfg *cmds.Server) error {
|
2021-05-11 23:59:33 +00:00
|
|
|
var serverConfig server.Config
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
ec, err := commandSetup(app, cfg, &serverConfig)
|
|
|
|
if err != nil {
|
2021-05-07 23:10:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshots := app.Args()
|
|
|
|
if len(snapshots) == 0 {
|
|
|
|
return errors.New("no snapshots given for removal")
|
|
|
|
}
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
return ec.etcd.DeleteSnapshots(ec.ctx, app.Args())
|
2021-05-07 23:10:04 +00:00
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
|
|
|
func List(app *cli.Context) error {
|
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return list(app, &cmds.ServerConfig)
|
|
|
|
}
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
var etcdListFormats = []string{"json", "yaml", "table"}
|
2022-02-25 21:00:00 +00:00
|
|
|
|
|
|
|
func validEtcdListFormat(format string) bool {
|
|
|
|
for _, supportedFormat := range etcdListFormats {
|
|
|
|
if format == supportedFormat {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-11 23:59:33 +00:00
|
|
|
func list(app *cli.Context, cfg *cmds.Server) error {
|
|
|
|
var serverConfig server.Config
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
ec, err := commandSetup(app, cfg, &serverConfig)
|
|
|
|
if err != nil {
|
2022-02-24 22:35:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
sf, err := ec.etcd.ListSnapshots(ec.ctx)
|
2021-05-11 23:59:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-25 21:00:00 +00:00
|
|
|
if cfg.EtcdListFormat != "" && !validEtcdListFormat(cfg.EtcdListFormat) {
|
|
|
|
return errors.New("invalid output format: " + cfg.EtcdListFormat)
|
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2022-02-25 21:00:00 +00:00
|
|
|
switch cfg.EtcdListFormat {
|
|
|
|
case "json":
|
|
|
|
if err := json.NewEncoder(os.Stdout).Encode(sf); err != nil {
|
|
|
|
return err
|
2021-11-29 18:30:04 +00:00
|
|
|
}
|
2022-02-25 21:00:00 +00:00
|
|
|
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)
|
|
|
|
defer w.Flush()
|
|
|
|
|
2023-10-10 19:51:42 +00:00
|
|
|
// Sort snapshots by creation time and key
|
|
|
|
sfKeys := make([]string, 0, len(sf))
|
|
|
|
for k := range sf {
|
|
|
|
sfKeys = append(sfKeys, k)
|
|
|
|
}
|
|
|
|
sort.Slice(sfKeys, func(i, j int) bool {
|
|
|
|
iKey := sfKeys[i]
|
|
|
|
jKey := sfKeys[j]
|
|
|
|
if sf[iKey].CreatedAt.Equal(sf[jKey].CreatedAt) {
|
|
|
|
return iKey < jKey
|
|
|
|
}
|
|
|
|
return sf[iKey].CreatedAt.Before(sf[jKey].CreatedAt)
|
|
|
|
})
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
fmt.Fprint(w, "Name\tLocation\tSize\tCreated\n")
|
2023-10-10 19:51:42 +00:00
|
|
|
for _, k := range sfKeys {
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", sf[k].Name, sf[k].Location, sf[k].Size, sf[k].CreatedAt.Format(time.RFC3339))
|
2021-05-11 23:59:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-13 20:36:33 +00:00
|
|
|
|
|
|
|
func Prune(app *cli.Context) error {
|
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return prune(app, &cmds.ServerConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prune(app *cli.Context, cfg *cmds.Server) error {
|
|
|
|
var serverConfig server.Config
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
ec, err := commandSetup(app, cfg, &serverConfig)
|
|
|
|
if err != nil {
|
2021-05-13 20:36:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serverConfig.ControlConfig.EtcdSnapshotRetention = cfg.EtcdSnapshotRetention
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
return ec.etcd.PruneSnapshots(ec.ctx)
|
2021-05-13 20:36:33 +00:00
|
|
|
}
|