2021-01-21 21:09:15 +00:00
|
|
|
package etcdsnapshot
|
|
|
|
|
|
|
|
import (
|
2024-03-28 00:48:13 +00:00
|
|
|
"bytes"
|
2024-04-19 18:00:51 +00:00
|
|
|
"context"
|
2022-02-25 21:00:00 +00:00
|
|
|
"encoding/json"
|
2021-05-11 23:59:33 +00:00
|
|
|
"fmt"
|
2021-01-21 21:09:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-03-28 00:48:13 +00:00
|
|
|
"slices"
|
2023-10-10 19:51:42 +00:00
|
|
|
"sort"
|
2021-05-11 23:59:33 +00:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
2021-01-21 21:09:15 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
k3s "github.com/k3s-io/k3s/pkg/apis/k3s.cattle.io/v1"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
2024-03-28 00:48:13 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/clientaccess"
|
|
|
|
"github.com/k3s-io/k3s/pkg/cluster/managed"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/etcd"
|
2024-05-06 16:42:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/proctitle"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/server"
|
|
|
|
util2 "github.com/k3s-io/k3s/pkg/util"
|
2024-01-16 22:43:08 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-03-28 00:48:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-21 21:09:15 +00:00
|
|
|
"github.com/urfave/cli"
|
2024-03-28 00:48:13 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/cli-runtime/pkg/printers"
|
2021-01-21 21:09:15 +00:00
|
|
|
)
|
|
|
|
|
2024-04-19 18:00:51 +00:00
|
|
|
var timeout = 2 * time.Minute
|
|
|
|
|
2021-05-07 23:10:04 +00:00
|
|
|
// commandSetup setups up common things needed
|
|
|
|
// for each etcd command.
|
2024-03-28 00:48:13 +00:00
|
|
|
func commandSetup(app *cli.Context, cfg *cmds.Server) (*etcd.SnapshotRequest, *clientaccess.Info, error) {
|
|
|
|
// hide process arguments from ps output, since they may contain
|
|
|
|
// database credentials or other secrets.
|
2024-05-06 16:42:27 +00:00
|
|
|
proctitle.SetProcTitle(os.Args[0] + " etcd-snapshot")
|
2024-03-28 00:48:13 +00:00
|
|
|
|
|
|
|
sr := &etcd.SnapshotRequest{}
|
|
|
|
// Operation and name are set by the command handler.
|
|
|
|
// Compression, dir, and retention take the server defaults if not overridden on the CLI.
|
|
|
|
if app.IsSet("etcd-snapshot-compress") {
|
|
|
|
sr.Compress = &cfg.EtcdSnapshotCompress
|
2021-05-07 23:10:04 +00:00
|
|
|
}
|
2024-03-28 00:48:13 +00:00
|
|
|
if app.IsSet("etcd-snapshot-dir") {
|
|
|
|
sr.Dir = &cfg.EtcdSnapshotDir
|
2024-01-16 22:43:08 +00:00
|
|
|
}
|
2024-03-28 00:48:13 +00:00
|
|
|
if app.IsSet("etcd-snapshot-retention") {
|
|
|
|
sr.Retention = &cfg.EtcdSnapshotRetention
|
2024-01-16 22:43:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
if cfg.EtcdS3 {
|
|
|
|
sr.S3 = &etcd.SnapshotRequestS3{}
|
|
|
|
sr.S3.AccessKey = cfg.EtcdS3AccessKey
|
|
|
|
sr.S3.Bucket = cfg.EtcdS3BucketName
|
|
|
|
sr.S3.Endpoint = cfg.EtcdS3Endpoint
|
|
|
|
sr.S3.EndpointCA = cfg.EtcdS3EndpointCA
|
|
|
|
sr.S3.Folder = cfg.EtcdS3Folder
|
|
|
|
sr.S3.Insecure = cfg.EtcdS3Insecure
|
|
|
|
sr.S3.Region = cfg.EtcdS3Region
|
|
|
|
sr.S3.SecretKey = cfg.EtcdS3SecretKey
|
|
|
|
sr.S3.SkipSSLVerify = cfg.EtcdS3SkipSSLVerify
|
|
|
|
sr.S3.Timeout = metav1.Duration{Duration: cfg.EtcdS3Timeout}
|
2024-04-19 18:00:51 +00:00
|
|
|
// extend request timeout to allow the S3 operation to complete
|
|
|
|
timeout += cfg.EtcdS3Timeout
|
2023-10-02 23:20:22 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
dataDir, err := server.ResolveDataDir(cfg.DataDir)
|
2023-10-02 23:20:22 +00:00
|
|
|
if err != nil {
|
2024-03-28 00:48:13 +00:00
|
|
|
return nil, nil, err
|
2022-03-01 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
if cfg.Token == "" {
|
|
|
|
fp := filepath.Join(dataDir, "token")
|
|
|
|
tokenByte, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cfg.Token = string(bytes.TrimRight(tokenByte, "\n"))
|
2023-10-02 23:20:22 +00:00
|
|
|
}
|
2024-03-28 00:48:13 +00:00
|
|
|
info, err := clientaccess.ParseAndValidateToken(cmds.ServerConfig.ServerURL, cfg.Token, clientaccess.WithUser("server"))
|
|
|
|
return sr, info, err
|
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
func wrapServerError(err error) error {
|
2024-04-19 18:00:51 +00:00
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
// if the request timed out the server log likely won't contain anything useful,
|
|
|
|
// since the operation may have actualy succeeded despite the client timing out the request.
|
|
|
|
return err
|
|
|
|
}
|
2024-03-28 00:48:13 +00:00
|
|
|
return errors.Wrap(err, "see server log for details")
|
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-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
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
// Save always sets retention to 0 to disable automatic pruning.
|
|
|
|
// Prune can be run manually after save, if desired.
|
|
|
|
app.Set("etcd-snapshot-retention", "0")
|
|
|
|
|
|
|
|
sr, info, err := commandSetup(app, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sr.Operation = etcd.SnapshotOperationSave
|
|
|
|
sr.Name = []string{cfg.EtcdSnapshotName}
|
|
|
|
|
|
|
|
b, err := json.Marshal(sr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-19 18:00:51 +00:00
|
|
|
r, err := info.Post("/db/snapshot", b, clientaccess.WithTimeout(timeout))
|
2021-01-21 21:09:15 +00:00
|
|
|
if err != nil {
|
2024-03-28 00:48:13 +00:00
|
|
|
return wrapServerError(err)
|
|
|
|
}
|
|
|
|
resp := &managed.SnapshotResult{}
|
|
|
|
if err := json.Unmarshal(r, resp); err != nil {
|
2021-01-21 21:09:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
for _, name := range resp.Created {
|
|
|
|
logrus.Infof("Snapshot %s saved.", name)
|
|
|
|
}
|
2021-05-01 01:26:39 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
return nil
|
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 {
|
2024-03-28 00:48:13 +00:00
|
|
|
snapshots := app.Args()
|
|
|
|
if len(snapshots) == 0 {
|
|
|
|
return errors.New("no snapshots given for removal")
|
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
sr, info, err := commandSetup(app, cfg)
|
2023-10-02 23:20:22 +00:00
|
|
|
if err != nil {
|
2021-05-07 23:10:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
sr.Operation = etcd.SnapshotOperationDelete
|
|
|
|
sr.Name = snapshots
|
|
|
|
|
|
|
|
b, err := json.Marshal(sr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-19 18:00:51 +00:00
|
|
|
r, err := info.Post("/db/snapshot", b, clientaccess.WithTimeout(timeout))
|
2024-03-28 00:48:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return wrapServerError(err)
|
|
|
|
}
|
|
|
|
resp := &managed.SnapshotResult{}
|
|
|
|
if err := json.Unmarshal(r, resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range resp.Deleted {
|
|
|
|
logrus.Infof("Snapshot %s deleted.", name)
|
|
|
|
}
|
|
|
|
for _, name := range snapshots {
|
|
|
|
if !slices.Contains(resp.Deleted, name) {
|
|
|
|
logrus.Warnf("Snapshot %s not found.", name)
|
|
|
|
}
|
2021-05-07 23:10:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
return nil
|
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 {
|
2024-03-28 00:48:13 +00:00
|
|
|
if cfg.EtcdListFormat != "" && !validEtcdListFormat(cfg.EtcdListFormat) {
|
|
|
|
return errors.New("invalid output format: " + cfg.EtcdListFormat)
|
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
sr, info, err := commandSetup(app, cfg)
|
2023-10-02 23:20:22 +00:00
|
|
|
if err != nil {
|
2022-02-24 22:35:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
sr.Operation = etcd.SnapshotOperationList
|
|
|
|
|
|
|
|
b, err := json.Marshal(sr)
|
2021-05-11 23:59:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-19 18:00:51 +00:00
|
|
|
r, err := info.Post("/db/snapshot", b, clientaccess.WithTimeout(timeout))
|
2024-03-28 00:48:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return wrapServerError(err)
|
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
sf := &k3s.ETCDSnapshotFileList{}
|
|
|
|
if err := json.Unmarshal(r, sf); err != nil {
|
|
|
|
return err
|
2022-02-25 21:00:00 +00:00
|
|
|
}
|
2021-05-11 23:59:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
sort.Slice(sf.Items, func(i, j int) bool {
|
|
|
|
if sf.Items[i].Status.CreationTime.Equal(sf.Items[j].Status.CreationTime) {
|
|
|
|
return sf.Items[i].Spec.SnapshotName < sf.Items[j].Spec.SnapshotName
|
|
|
|
}
|
|
|
|
return sf.Items[i].Status.CreationTime.Before(sf.Items[j].Status.CreationTime)
|
|
|
|
})
|
|
|
|
|
2022-02-25 21:00:00 +00:00
|
|
|
switch cfg.EtcdListFormat {
|
|
|
|
case "json":
|
2024-03-28 00:48:13 +00:00
|
|
|
json := printers.JSONPrinter{}
|
|
|
|
if err := json.PrintObj(sf, os.Stdout); err != nil {
|
2022-02-25 21:00:00 +00:00
|
|
|
return err
|
2021-11-29 18:30:04 +00:00
|
|
|
}
|
2022-02-25 21:00:00 +00:00
|
|
|
return nil
|
|
|
|
case "yaml":
|
2024-03-28 00:48:13 +00:00
|
|
|
yaml := printers.YAMLPrinter{}
|
|
|
|
if err := yaml.PrintObj(sf, os.Stdout); err != nil {
|
2022-02-25 21:00:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
|
|
|
defer w.Flush()
|
|
|
|
|
2023-10-02 23:20:22 +00:00
|
|
|
fmt.Fprint(w, "Name\tLocation\tSize\tCreated\n")
|
2024-03-28 00:48:13 +00:00
|
|
|
for _, esf := range sf.Items {
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", esf.Spec.SnapshotName, esf.Spec.Location, esf.Status.Size.Value(), esf.Status.CreationTime.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 {
|
2024-03-28 00:48:13 +00:00
|
|
|
sr, info, err := commandSetup(app, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-13 20:36:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
sr.Operation = etcd.SnapshotOperationPrune
|
|
|
|
sr.Name = []string{cfg.EtcdSnapshotName}
|
|
|
|
|
|
|
|
b, err := json.Marshal(sr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-19 18:00:51 +00:00
|
|
|
r, err := info.Post("/db/snapshot", b, clientaccess.WithTimeout(timeout))
|
2023-10-02 23:20:22 +00:00
|
|
|
if err != nil {
|
2024-03-28 00:48:13 +00:00
|
|
|
return wrapServerError(err)
|
|
|
|
}
|
|
|
|
resp := &managed.SnapshotResult{}
|
|
|
|
if err := json.Unmarshal(r, resp); err != nil {
|
2021-05-13 20:36:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
for _, name := range resp.Deleted {
|
|
|
|
logrus.Infof("Snapshot %s deleted.", name)
|
|
|
|
}
|
2021-05-13 20:36:33 +00:00
|
|
|
|
2024-03-28 00:48:13 +00:00
|
|
|
return nil
|
2021-05-13 20:36:33 +00:00
|
|
|
}
|