Skip to content

Commit

Permalink
Add context and client to setPlatformOptions()
Browse files Browse the repository at this point in the history
Because `newClient()` has a huge process to instantiate, re-use the
previously created client.

Signed-off-by: Min Uk Lee <minuk.dev@gmail.com>
  • Loading branch information
minuk-dev committed Oct 10, 2022
1 parent 5bf0535 commit 8de12b9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func createContainer(cmd *cobra.Command, ctx context.Context, client *containerd
oci.WithDefaultSpec(),
)

opts, err = setPlatformOptions(opts, cmd, id)
opts, err = setPlatformOptions(ctx, opts, cmd, client, id)
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/nerdctl/run_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"

"github.com/containerd/containerd"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
"github.com/spf13/cobra"
Expand All @@ -38,6 +39,6 @@ func runShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]s
return nil, cobra.ShellCompDirectiveNoFileComp
}

func setPlatformOptions(opts []oci.SpecOpts, cmd *cobra.Command, id string) ([]oci.SpecOpts, error) {
func setPlatformOptions(ctx context.Context, opts []oci.SpecOpts, cmd *cobra.Command, client *containerd.Client, id string) ([]oci.SpecOpts, error) {
return opts, nil
}
22 changes: 7 additions & 15 deletions cmd/nerdctl/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func runShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]s
}
}

func setPlatformOptions(opts []oci.SpecOpts, cmd *cobra.Command, id string) ([]oci.SpecOpts, error) {
func setPlatformOptions(ctx context.Context, opts []oci.SpecOpts, cmd *cobra.Command, client *containerd.Client, id string) ([]oci.SpecOpts, error) {
opts = append(opts,
oci.WithDefaultUnixDevices,
WithoutRunMount(), // unmount default tmpfs on "/run": https://github.com/containerd/nerdctl/issues/157)
Expand Down Expand Up @@ -131,7 +131,11 @@ func setPlatformOptions(opts []oci.SpecOpts, cmd *cobra.Command, id string) ([]o
opts = append(opts, oci.WithDevShmSize(shmBytes/1024))
}

pidOpts, err := generatePIDOpts(cmd)
pid, err := cmd.Flags().GetString("pid")
if err != nil {
return nil, err
}
pidOpts, err := generatePIDOpts(ctx, client, pid)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -211,13 +215,8 @@ func withOOMScoreAdj(score int) oci.SpecOpts {
}
}

func generatePIDOpts(cmd *cobra.Command) ([]oci.SpecOpts, error) {
func generatePIDOpts(ctx context.Context, client *containerd.Client, pid string) ([]oci.SpecOpts, error) {
opts := make([]oci.SpecOpts, 0)

pid, err := cmd.Flags().GetString("pid")
if err != nil {
return nil, err
}
pid = strings.ToLower(pid)

switch pid {
Expand All @@ -229,13 +228,6 @@ func generatePIDOpts(cmd *cobra.Command) ([]oci.SpecOpts, error) {
opts = append(opts, withBindMountHostProcfs)
}
default: // container:<id|name>
client, ctx, cancel, err := newClient(cmd)
if err != nil {
return nil, err
}

defer cancel()

parsed := strings.Split(pid, ":")
if len(parsed) < 2 || parsed[0] != "container" {
return nil, fmt.Errorf("invalid pid namespace. Set --pid=[host|container:<name|id>")
Expand Down
3 changes: 2 additions & 1 deletion cmd/nerdctl/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

"github.com/containerd/containerd"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
"github.com/docker/go-units"
Expand All @@ -40,7 +41,7 @@ func runShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]s
return nil, cobra.ShellCompDirectiveNoFileComp
}

func setPlatformOptions(opts []oci.SpecOpts, cmd *cobra.Command, id string) ([]oci.SpecOpts, error) {
func setPlatformOptions(ctx context.Context, opts []oci.SpecOpts, cmd *cobra.Command, client *containerd.Client, id string) ([]oci.SpecOpts, error) {
cpus, err := cmd.Flags().GetFloat64("cpus")
if err != nil {
return nil, err
Expand Down

0 comments on commit 8de12b9

Please sign in to comment.