-
Notifications
You must be signed in to change notification settings - Fork 695
Added --export command #4405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added --export command #4405
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,94 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package container | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/mattn/go-isatty" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" | ||
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" | ||
"github.com/containerd/nerdctl/v2/pkg/api/types" | ||
"github.com/containerd/nerdctl/v2/pkg/clientutil" | ||
"github.com/containerd/nerdctl/v2/pkg/cmd/container" | ||
) | ||
|
||
func ExportCommand() *cobra.Command { | ||
var exportCommand = &cobra.Command{ | ||
Use: "export [OPTIONS] CONTAINER", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Export a containers filesystem as a tar archive", | ||
Long: "Export a containers filesystem as a tar archive", | ||
RunE: exportAction, | ||
ValidArgsFunction: exportShellComplete, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
} | ||
exportCommand.Flags().StringP("output", "o", "", "Write to a file, instead of STDOUT") | ||
|
||
return exportCommand | ||
} | ||
|
||
func exportAction(cmd *cobra.Command, args []string) error { | ||
globalOptions, err := helpers.ProcessRootCmdFlags(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
if len(args) == 0 { | ||
return fmt.Errorf("requires at least 1 argument") | ||
} | ||
|
||
output, err := cmd.Flags().GetString("output") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
writer := cmd.OutOrStdout() | ||
if output != "" { | ||
f, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
writer = f | ||
} else { | ||
if isatty.IsTerminal(os.Stdout.Fd()) { | ||
return fmt.Errorf("cowardly refusing to save to a terminal. Use the -o flag or redirect") | ||
} | ||
} | ||
|
||
options := types.ContainerExportOptions{ | ||
Stdout: writer, | ||
GOptions: globalOptions, | ||
} | ||
|
||
return container.Export(ctx, client, args[0], options) | ||
} | ||
|
||
func exportShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
// show container names | ||
return completion.ContainerNames(cmd, nil) | ||
} |
This file contains hidden or 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,170 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package container | ||
|
||
import ( | ||
"archive/tar" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
|
||
"github.com/containerd/nerdctl/mod/tigron/test" | ||
"github.com/containerd/nerdctl/mod/tigron/tig" | ||
|
||
"github.com/containerd/nerdctl/v2/pkg/testutil" | ||
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" | ||
) | ||
|
||
// validateExportedTar checks that the tar file exists and contains /bin/busybox | ||
func validateExportedTar(outFile string) test.Comparator { | ||
return func(stdout string, t tig.T) { | ||
// Check if the tar file was created | ||
_, err := os.Stat(outFile) | ||
assert.Assert(t, !os.IsNotExist(err), "exported tar file %s was not created", outFile) | ||
|
||
// Open and read the tar file to check for /bin/busybox | ||
file, err := os.Open(outFile) | ||
assert.NilError(t, err, "failed to open tar file %s", outFile) | ||
defer file.Close() | ||
|
||
tarReader := tar.NewReader(file) | ||
busyboxFound := false | ||
|
||
for { | ||
header, err := tarReader.Next() | ||
if err == io.EOF { | ||
break | ||
} | ||
assert.NilError(t, err, "failed to read tar entry") | ||
|
||
if header.Name == "bin/busybox" || header.Name == "./bin/busybox" { | ||
busyboxFound = true | ||
break | ||
} | ||
} | ||
|
||
assert.Assert(t, busyboxFound, "exported tar file %s does not contain /bin/busybox", outFile) | ||
t.Log("Export validation passed: tar file exists and contains /bin/busybox") | ||
} | ||
} | ||
|
||
func TestExportStoppedContainer(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("export is not supported on Windows") | ||
} | ||
|
||
testCase := nerdtest.Setup() | ||
testCase.Setup = func(data test.Data, helpers test.Helpers) { | ||
identifier := data.Identifier("container") | ||
helpers.Ensure("create", "--name", identifier, testutil.CommonImage) | ||
data.Labels().Set("cID", identifier) | ||
data.Labels().Set("outFile", filepath.Join(os.TempDir(), identifier+".tar")) | ||
} | ||
testCase.Cleanup = func(data test.Data, helpers test.Helpers) { | ||
helpers.Anyhow("container", "rm", "-f", data.Labels().Get("cID")) | ||
helpers.Anyhow("rm", "-f", data.Labels().Get("cID")) | ||
os.Remove(data.Labels().Get("outFile")) | ||
} | ||
|
||
testCase.SubTests = []*test.Case{ | ||
{ | ||
Description: "export command succeeds", | ||
NoParallel: true, | ||
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
return helpers.Command("export", "-o", data.Labels().Get("outFile"), data.Labels().Get("cID")) | ||
}, | ||
Expected: test.Expects(0, nil, nil), | ||
}, | ||
{ | ||
Description: "tar file exists and has content", | ||
NoParallel: true, | ||
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
// Use a simple command that always succeeds to trigger the validation | ||
return helpers.Custom("echo", "validating tar file") | ||
}, | ||
Expected: func(data test.Data, helpers test.Helpers) *test.Expected { | ||
return &test.Expected{ | ||
ExitCode: 0, | ||
Output: validateExportedTar(data.Labels().Get("outFile")), | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
testCase.Run(t) | ||
} | ||
|
||
func TestExportRunningContainer(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("export is not supported on Windows") | ||
} | ||
|
||
testCase := nerdtest.Setup() | ||
testCase.Setup = func(data test.Data, helpers test.Helpers) { | ||
identifier := data.Identifier("container") | ||
helpers.Ensure("run", "-d", "--name", identifier, testutil.CommonImage, "sleep", nerdtest.Infinity) | ||
data.Labels().Set("cID", identifier) | ||
data.Labels().Set("outFile", filepath.Join(os.TempDir(), identifier+".tar")) | ||
} | ||
testCase.Cleanup = func(data test.Data, helpers test.Helpers) { | ||
helpers.Anyhow("rm", "-f", data.Labels().Get("cID")) | ||
os.Remove(data.Labels().Get("outFile")) | ||
} | ||
|
||
testCase.SubTests = []*test.Case{ | ||
{ | ||
Description: "export command succeeds", | ||
NoParallel: true, | ||
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
return helpers.Command("export", "-o", data.Labels().Get("outFile"), data.Labels().Get("cID")) | ||
}, | ||
Expected: test.Expects(0, nil, nil), | ||
}, | ||
{ | ||
Description: "tar file exists and has content", | ||
NoParallel: true, | ||
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
// Use a simple command that always succeeds to trigger the validation | ||
return helpers.Custom("echo", "validating tar file") | ||
}, | ||
Expected: func(data test.Data, helpers test.Helpers) *test.Expected { | ||
return &test.Expected{ | ||
ExitCode: 0, | ||
Output: validateExportedTar(data.Labels().Get("outFile")), | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
testCase.Run(t) | ||
} | ||
|
||
func TestExportNonexistentContainer(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("export is not supported on Windows") | ||
} | ||
|
||
testCase := nerdtest.Setup() | ||
testCase.Command = test.Command("export", "nonexistent-container") | ||
testCase.Expected = test.Expects(1, nil, nil) | ||
|
||
testCase.Run(t) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.