Skip to content

etcdctl: Split SaveWithVersion and client io#21248

Open
sauerburger wants to merge 1 commit intoetcd-io:mainfrom
sauerburger:main
Open

etcdctl: Split SaveWithVersion and client io#21248
sauerburger wants to merge 1 commit intoetcd-io:mainfrom
sauerburger:main

Conversation

@sauerburger
Copy link

This commit splits the SaveWithVersion for snapshots into one part that takes care of opening, flushing, and closing the file while SaveWithVersionStream handles loading the snapshot and copying it to an io.Writer.

This allows the CLI to call SaveWithVersionStream and write the snapshot to StdOut.

Writing the snapshot to StdOut allows for secure, on-the-fly compression and encryption of the snapshot without first saving it to a filesystem.

Closes #16242


This is my first public MR in go. Feedback how to improve the MR highly welcome.

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sauerburger
Once this PR has been reviewed and has the lgtm label, please assign fuweid for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot
Copy link

Hi @sauerburger. Thanks for your PR.

I'm waiting for a etcd-io member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

func SaveWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, dbPath string) (string, error) {
// SaveWithVersionStream fetches an etcd snapshot and writes it to the provided writer.
// It does NOT fsync or close the writer, which makes it safe for stdout and other streams.
func SaveWithVersionStream(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, w io.Writer) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend changing order of functions with SaveWithVersionStream after SaveWithVersion to make diff more readable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return &cobra.Command{
Use: "save <filename>",
Short: "Stores an etcd node backend snapshot to a given file",
Use: "save [<filename>]",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is too much risk of forgetting argument to result in terminal getting filled with binary gibberish.

Instead of making filename argument optional, it would be safer to - as indicator to write to stdout.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated the PR accordingly.

@sauerburger sauerburger force-pushed the main branch 2 times, most recently from bfe3f32 to bebac77 Compare February 6, 2026 17:43
This commit splits the SaveWithVersion for snapshots into one part that takes
care of opening, flushing, and closing the file while SaveWithVersionStream
handles loading the snapshot and copying it to an io.Writer.

This allows the CLI to call SaveWithVersionStream and write the snapshot to
StdOut.

Closes etcd-io#16242

Signed-off-by: Frank Sauerburger <frank@sauerburger.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Add the ability to do etcd snapshot to a pipe or a named pipe as a pseudo file

3 participants