forked from k3s-io/k3s
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to perform an etcd on-demand snapshot via cli (k3s-io#2819)
* add ability to perform an etcd on-demand snapshot via cli
- Loading branch information
1 parent
84f6655
commit 1322901
Showing
15 changed files
with
205 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rancher/k3s/pkg/cli/cmds" | ||
"github.com/rancher/k3s/pkg/cli/etcdsnapshot" | ||
"github.com/rancher/k3s/pkg/configfilearg" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func main() { | ||
app := cmds.NewApp() | ||
app.Commands = []cli.Command{ | ||
cmds.NewEtcdSnapshotCommand(etcdsnapshot.Run), | ||
} | ||
|
||
if err := app.Run(configfilearg.MustParse(os.Args)); err != nil { | ||
logrus.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cmds | ||
|
||
import ( | ||
"github.com/rancher/k3s/pkg/version" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
const EtcdSnapshotCommand = "etcd-snapshot" | ||
|
||
func NewEtcdSnapshotCommand(action func(*cli.Context) error) cli.Command { | ||
return cli.Command{ | ||
Name: EtcdSnapshotCommand, | ||
Usage: "Trigger an immediate etcd snapshot", | ||
SkipFlagParsing: false, | ||
SkipArgReorder: true, | ||
Action: action, | ||
Flags: []cli.Flag{ | ||
DebugFlag, | ||
LogFile, | ||
AlsoLogToStderr, | ||
cli.StringFlag{ | ||
Name: "data-dir,d", | ||
Usage: "(data) Folder to hold state default /var/lib/rancher/" + version.Program + " or ${HOME}/.rancher/" + version.Program + " if not root", | ||
Destination: &ServerConfig.DataDir, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Usage: "(db) Set the base name of the etcd on-demand snapshot (appended with UNIX timestamp).", | ||
Destination: &ServerConfig.EtcdSnapshotName, | ||
Value: "on-demand", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "dir", | ||
Usage: "(db) Directory to save etcd on-demand snapshot. (default: ${data-dir}/db/snapshots)", | ||
Destination: &ServerConfig.EtcdSnapshotDir, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package etcdsnapshot | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/erikdubbelboer/gspt" | ||
"github.com/rancher/k3s/pkg/cli/cmds" | ||
"github.com/rancher/k3s/pkg/cluster" | ||
"github.com/rancher/k3s/pkg/daemons/config" | ||
"github.com/rancher/k3s/pkg/etcd" | ||
"github.com/rancher/k3s/pkg/server" | ||
"github.com/rancher/wrangler/pkg/signals" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func Run(app *cli.Context) error { | ||
if err := cmds.InitLogging(); err != nil { | ||
return err | ||
} | ||
return run(app, &cmds.ServerConfig) | ||
} | ||
|
||
func run(app *cli.Context, cfg *cmds.Server) error { | ||
gspt.SetProcTitle(os.Args[0]) | ||
|
||
dataDir, err := server.ResolveDataDir(cfg.DataDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var serverConfig server.Config | ||
serverConfig.DisableAgent = true | ||
serverConfig.ControlConfig.DataDir = dataDir | ||
serverConfig.ControlConfig.EtcdSnapshotName = cfg.EtcdSnapshotName | ||
serverConfig.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir | ||
serverConfig.ControlConfig.EtcdSnapshotRetention = 0 // disable retention check | ||
serverConfig.ControlConfig.Runtime = &config.ControlRuntime{} | ||
serverConfig.ControlConfig.Runtime.ETCDServerCA = filepath.Join(dataDir, "tls", "etcd", "server-ca.crt") | ||
serverConfig.ControlConfig.Runtime.ClientETCDCert = filepath.Join(dataDir, "tls", "etcd", "client.crt") | ||
serverConfig.ControlConfig.Runtime.ClientETCDKey = filepath.Join(dataDir, "tls", "etcd", "client.key") | ||
|
||
ctx := signals.SetupSignalHandler(context.Background()) | ||
|
||
initialized, err := etcd.NewETCD().IsInitialized(ctx, &serverConfig.ControlConfig) | ||
if err != nil { | ||
return err | ||
} | ||
if !initialized { | ||
return errors.New("managed etcd database has not been initialized") | ||
} | ||
|
||
cluster := cluster.New(&serverConfig.ControlConfig) | ||
|
||
if err := cluster.Bootstrap(ctx); err != nil { | ||
return err | ||
} | ||
|
||
return cluster.Snapshot(ctx, &serverConfig.ControlConfig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.