Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 13 additions & 37 deletions cmd/nerdctl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/containerd/containerd/errdefs"
dockerreference "github.com/containerd/containerd/reference/docker"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/buildkitutil"
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/defaults"
Expand Down Expand Up @@ -77,7 +78,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
return buildCommand
}

func getBuildkitHost(cmd *cobra.Command) (string, error) {
func getBuildkitHost(cmd *cobra.Command, ns string) (string, error) {
if cmd.Flags().Changed("buildkit-host") || os.Getenv("BUILDKIT_HOST") != "" {
// If address is explicitly specified, use it.
buildkitHost, err := cmd.Flags().GetString("buildkit-host")
Expand All @@ -89,10 +90,6 @@ func getBuildkitHost(cmd *cobra.Command) (string, error) {
}
return buildkitHost, nil
}
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
return "", err
}
return buildkitutil.GetBuildkitHost(ns)
}

Expand Down Expand Up @@ -126,18 +123,21 @@ func isImageSharable(buildkitHost string, namespace, uuid, snapshotter string, p
}

func buildAction(cmd *cobra.Command, args []string) error {
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
platform, err := cmd.Flags().GetStringSlice("platform")
if err != nil {
return err
}
platform = strutil.DedupeStrSlice(platform)

buildkitHost, err := getBuildkitHost(cmd)
buildkitHost, err := getBuildkitHost(cmd, globalOptions.Namespace)
if err != nil {
return err
}

buildctlBinary, buildctlArgs, needsLoading, metaFile, tags, cleanup, err := generateBuildctlArgs(cmd, buildkitHost, platform, args)
buildctlBinary, buildctlArgs, needsLoading, metaFile, tags, cleanup, err := generateBuildctlArgs(cmd, globalOptions, buildkitHost, platform, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func buildAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err = loadImage(buildctlStdout, cmd, platMC, quiet); err != nil {
if err = loadImage(buildctlStdout, cmd, globalOptions, platMC, quiet); err != nil {
return err
}
}
Expand All @@ -195,15 +195,7 @@ func buildAction(cmd *cobra.Command, args []string) error {
return err
}
}
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
address, err := cmd.Flags().GetString("address")
if err != nil {
return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if len(tags) > 1 {
logrus.Debug("Found more than 1 tag")
if err != nil {
Expand All @@ -230,7 +222,7 @@ func buildAction(cmd *cobra.Command, args []string) error {
return nil
}

func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, args []string) (buildCtlBinary string,
func generateBuildctlArgs(cmd *cobra.Command, globalOptions *types.GlobalCommandOptions, buildkitHost string, platform, args []string) (buildCtlBinary string,
buildctlArgs []string, needsLoading bool, metaFile string, tags []string, cleanup func(), err error) {
if len(args) < 1 {
return "", nil, false, "", nil, nil, errors.New("context needs to be specified")
Expand All @@ -249,15 +241,7 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg
if err != nil {
return "", nil, false, "", nil, nil, err
}
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return "", nil, false, "", nil, nil, err
}
address, err := cmd.Flags().GetString("address")
if err != nil {
return "", nil, false, "", nil, nil, err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if output == "" {
if err != nil {
return "", nil, false, "", nil, nil, err
Expand All @@ -267,15 +251,7 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg
if err != nil {
return "", nil, false, "", nil, nil, err
}
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
return "", nil, false, "", nil, nil, err
}
snapshotter, err := cmd.Flags().GetString("snapshotter")
if err != nil {
return "", nil, false, "", nil, nil, err
}
sharable, err := isImageSharable(buildkitHost, ns, info.UUID, snapshotter, platform)
sharable, err := isImageSharable(buildkitHost, globalOptions.Namespace, info.UUID, globalOptions.Snapshotter, platform)
if err != nil {
return "", nil, false, "", nil, nil, err
}
Expand Down
13 changes: 10 additions & 3 deletions cmd/nerdctl/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ func newBuilderPruneCommand() *cobra.Command {
}

func builderPruneAction(cmd *cobra.Command, _ []string) error {
buildkitHost, err := getBuildkitHost(cmd)
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
buildkitHost, err := getBuildkitHost(cmd, globalOptions.Namespace)
if err != nil {
return err
}
Expand Down Expand Up @@ -97,6 +101,10 @@ func newBuilderDebugCommand() *cobra.Command {
}

func builderDebugAction(cmd *cobra.Command, args []string) error {
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
if len(args) < 1 {
return fmt.Errorf("context needs to be specified")
}
Expand All @@ -106,10 +114,9 @@ func builderDebugAction(cmd *cobra.Command, args []string) error {
return err
}
buildgArgs := []string{"debug"}
debugLog, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
} else if debugLog {
} else if globalOptions.Debug {
buildgArgs = append([]string{"--debug"}, buildgArgs...)
}

Expand Down
14 changes: 3 additions & 11 deletions cmd/nerdctl/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,15 @@ func newCommitCommand() *cobra.Command {
}

func commitAction(cmd *cobra.Command, args []string) error {
opts, err := newCommitOpts(cmd, args)
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
address, err := cmd.Flags().GetString("address")
if err != nil {
return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)

opts, err := newCommitOpts(cmd, args)
if err != nil {
return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if err != nil {
return err
}
Expand Down
36 changes: 14 additions & 22 deletions cmd/nerdctl/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ import (
)

func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
namespace, err := cmd.Flags().GetString("namespace")
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
address, err := cmd.Flags().GetString("address")
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand All @@ -55,15 +51,11 @@ func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirec
}

func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) {
namespace, err := cmd.Flags().GetString("namespace")
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
address, err := cmd.Flags().GetString("address")
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down Expand Up @@ -108,20 +100,16 @@ func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.

// shellCompleteNetworkNames includes {"bridge","host","none"}
func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) {
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
excludeMap := make(map[string]struct{}, len(exclude))
for _, ex := range exclude {
excludeMap[ex] = struct{}{}
}

cniPath, err := cmd.Flags().GetString("cni-path")
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
cniNetconfpath, err := cmd.Flags().GetString("cni-netconfpath")
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
e, err := netutil.NewCNIEnv(cniPath, cniNetconfpath)
e, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand All @@ -144,7 +132,11 @@ func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string,
}

func shellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
vols, err := getVolumes(cmd)
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
vols, err := getVolumes(cmd, globalOptions)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down
40 changes: 10 additions & 30 deletions cmd/nerdctl/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/platforms"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/composer"
"github.com/containerd/nerdctl/pkg/composer/serviceparser"
"github.com/containerd/nerdctl/pkg/cosignutil"
Expand Down Expand Up @@ -80,7 +81,7 @@ func newComposeCommand() *cobra.Command {
return composeCommand
}

func getComposer(cmd *cobra.Command, client *containerd.Client) (*composer.Composer, error) {
func getComposer(cmd *cobra.Command, client *containerd.Client, globalOptions *types.GlobalCommandOptions) (*composer.Composer, error) {
nerdctlCmd, nerdctlArgs := globalFlags(cmd)
projectDirectory, err := cmd.Flags().GetString("project-directory")
if err != nil {
Expand All @@ -94,38 +95,17 @@ func getComposer(cmd *cobra.Command, client *containerd.Client) (*composer.Compo
if err != nil {
return nil, err
}
debugFull, err := cmd.Flags().GetBool("debug-full")
if err != nil {
return nil, err
}
debugFull := globalOptions.DebugFull
snapshotter := globalOptions.Snapshotter
files, err := cmd.Flags().GetStringArray("file")
if err != nil {
return nil, err
}
insecure, err := cmd.Flags().GetBool("insecure-registry")
if err != nil {
return nil, err
}
cniPath, err := cmd.Flags().GetString("cni-path")
if err != nil {
return nil, err
}
cniNetconfpath, err := cmd.Flags().GetString("cni-netconfpath")
if err != nil {
return nil, err
}
snapshotter, err := cmd.Flags().GetString("snapshotter")
if err != nil {
return nil, err
}
hostsDirs, err := cmd.Flags().GetStringSlice("hosts-dir")
if err != nil {
return nil, err
}
experimental, err := cmd.Flags().GetBool("experimental")
if err != nil {
return nil, err
}
insecure := globalOptions.InsecureRegistry
cniPath := globalOptions.CNIPath
cniNetconfpath := globalOptions.CNINetConfPath
hostsDirs := globalOptions.HostsDir
experimental := globalOptions.Experimental

o := composer.Options{
Project: projectName,
Expand Down Expand Up @@ -156,7 +136,7 @@ func getComposer(cmd *cobra.Command, client *containerd.Client) (*composer.Compo
return false, nil
}

volStore, err := getVolumeStore(cmd)
volStore, err := getVolumeStore(globalOptions)
if err != nil {
return nil, err
}
Expand Down
16 changes: 6 additions & 10 deletions cmd/nerdctl/compose_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,30 @@ func newComposeBuildCommand() *cobra.Command {
}

func composeBuildAction(cmd *cobra.Command, args []string) error {
buildArg, err := cmd.Flags().GetStringArray("build-arg")
if err != nil {
return err
}
noCache, err := cmd.Flags().GetBool("no-cache")
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
progress, err := cmd.Flags().GetString("progress")
buildArg, err := cmd.Flags().GetStringArray("build-arg")
if err != nil {
return err
}
namespace, err := cmd.Flags().GetString("namespace")
noCache, err := cmd.Flags().GetBool("no-cache")
if err != nil {
return err
}
address, err := cmd.Flags().GetString("address")
progress, err := cmd.Flags().GetString("progress")
if err != nil {
return err
}

client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if err != nil {
return err
}
defer cancel()

c, err := getComposer(cmd, client)
c, err := getComposer(cmd, client, globalOptions)
if err != nil {
return err
}
Expand Down
Loading