Skip to content

Commit

Permalink
adds --with-pull option and disable-pull-on-run config
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Brown <brownwm@us.ibm.com>
  • Loading branch information
mikebrow committed Jun 29, 2020
1 parent c482c7a commit 4197d94
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 30 deletions.
15 changes: 12 additions & 3 deletions cmd/crictl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ EXAMPLE:
CRICTL OPTIONS:
runtime-endpoint: Container runtime endpoint
image-endpoint: Image endpoint
timeout: Timeout of connecting to server
debug: Enable debug output
pull-image-on-create: Enable pulling image on create requests`,
timeout: Timeout of connecting to server (default: 2s)
debug: Enable debug output (default: false)
pull-image-on-create: Enable pulling image on create requests (default: false)
disable-pull-on-run: Disable pulling image on run requests (default: false)`,
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down Expand Up @@ -78,6 +79,8 @@ CRICTL OPTIONS:
fmt.Println(config.Debug)
case "pull-image-on-create":
fmt.Println(config.PullImageOnCreate)
case "disable-pull-on-run":
fmt.Println(config.DisablePullOnRun)
default:
return errors.Errorf("no configuration option named %s", get)
}
Expand Down Expand Up @@ -137,6 +140,12 @@ func setValue(key, value string, config *common.Config) error {
return errors.Wrapf(err, "parse pull-image-on-create value '%s'", value)
}
config.PullImageOnCreate = pi
case "disable-pull-on-run":
pi, err := strconv.ParseBool(value)
if err != nil {
return errors.Wrapf(err, "parse disable-pull-on-run value '%s'", value)
}
config.DisablePullOnRun = pi
default:
return errors.Errorf("no configuration option named %s", key)
}
Expand Down
89 changes: 68 additions & 21 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

Expand Down Expand Up @@ -61,8 +62,8 @@ type runOptions struct {
}

type pullOptions struct {
// do not pull the image on container creation
dontPull bool
// pull the image on container creation; overrides default
withPull bool

// creds is string in the format `USERNAME[:PASSWORD]` for accessing the
// registry during image pull
Expand All @@ -73,10 +74,35 @@ type pullOptions struct {
auth string
}

var pullFlags = []cli.Flag{
var createPullFlags = []cli.Flag{
&cli.BoolFlag{
Name: "no-pull",
Usage: "Do not pull the image on container creation (if pull-image-on-create in config is set to true use no-pull on the cli to disable pulling for this create)",
Usage: "Do not pull the image on container creation (overrides pull-image-on-create=true in config)",
},
&cli.BoolFlag{
Name: "with-pull",
Usage: "Pull the image on container creation (overrides pull-image-on-create=false in config)",
},
&cli.StringFlag{
Name: "creds",
Value: "",
Usage: "Use `USERNAME[:PASSWORD]` for accessing the registry",
},
&cli.StringFlag{
Name: "auth",
Value: "",
Usage: "Use `AUTH_STRING` for accessing the registry. AUTH_STRING is a base64 encoded 'USERNAME[:PASSWORD]'",
},
}

var runPullFlags = []cli.Flag{
&cli.BoolFlag{
Name: "no-pull",
Usage: "Do not pull the image (overrides disable-pull-on-run=false in config)",
},
&cli.BoolFlag{
Name: "with-pull",
Usage: "Pull the image (overrides disable-pull-on-run=true in config)",
},
&cli.StringFlag{
Name: "creds",
Expand All @@ -94,32 +120,42 @@ var createContainerCommand = &cli.Command{
Name: "create",
Usage: "Create a new container",
ArgsUsage: "POD container-config.[json|yaml] pod-config.[json|yaml]",
Flags: pullFlags,
Flags: createPullFlags,

Action: func(context *cli.Context) error {
if context.Args().Len() != 3 {
return cli.ShowSubcommandHelp(context)
}
if context.Bool("no-pull") == true && context.Bool("with-pull") == true {
return errors.New("confict: no-pull and with-pull are both set")
}

runtimeClient, runtimeConn, err := getRuntimeClient(context)
if err != nil {
return err
}
defer closeConnection(context, runtimeConn)

imageClient, imageConn, err := getImageClient(context)
if err != nil {
return err
withPull := (!context.Bool("no-pull") && PullImageOnCreate) || context.Bool("with-pull")

var imageClient pb.ImageServiceClient
var imageConn *grpc.ClientConn

if withPull {
imageClient, imageConn, err = getImageClient(context)
if err != nil {
return err
}
defer closeConnection(context, imageConn)
}
defer closeConnection(context, imageConn)

opts := createOptions{
podID: context.Args().Get(0),
runOptions: &runOptions{
configPath: context.Args().Get(1),
podConfig: context.Args().Get(2),
pullOptions: &pullOptions{
dontPull: context.Bool("no-pull"),
withPull: withPull,
creds: context.String("creds"),
auth: context.String("auth"),
},
Expand Down Expand Up @@ -490,7 +526,7 @@ var runContainerCommand = &cli.Command{
Name: "run",
Usage: "Run a new container inside a sandbox",
ArgsUsage: "container-config.[json|yaml] pod-config.[json|yaml]",
Flags: append(pullFlags, &cli.StringFlag{
Flags: append(runPullFlags, &cli.StringFlag{
Name: "runtime",
Aliases: []string{"r"},
Usage: "Runtime handler to use. Available options are defined by the container runtime.",
Expand All @@ -500,24 +536,34 @@ var runContainerCommand = &cli.Command{
if context.Args().Len() != 2 {
return cli.ShowSubcommandHelp(context)
}
if context.Bool("no-pull") == true && context.Bool("with-pull") == true {
return errors.New("confict: no-pull and with-pull are both set")
}

runtimeClient, runtimeConn, err := getRuntimeClient(context)
if err != nil {
return err
}
defer closeConnection(context, runtimeConn)

imageClient, imageConn, err := getImageClient(context)
if err != nil {
return err
withPull := (!DisablePullOnRun && !context.Bool("no-pull")) || context.Bool("with-pull")

var imageClient pb.ImageServiceClient
var imageConn *grpc.ClientConn

if withPull {
imageClient, imageConn, err = getImageClient(context)
if err != nil {
return err
}
defer closeConnection(context, imageConn)
}
defer closeConnection(context, imageConn)

opts := runOptions{
configPath: context.Args().Get(0),
podConfig: context.Args().Get(1),
pullOptions: &pullOptions{
dontPull: context.Bool("no-pull"),
withPull: withPull,
creds: context.String("creds"),
auth: context.String("auth"),
},
Expand Down Expand Up @@ -583,11 +629,12 @@ func CreateContainer(
}
}

// When the default mode is to pull-image-on-create(true) and the no-pull on
// create option is not set we pull the image when they ask for a create as a
// helper on the cli to reduce extra steps. As a reminder if the image is
// already in cache only the manifest will be pulled down to verify.
if !opts.dontPull && PullImageOnCreate {
// When there is a with-pull request or the image default mode is to
// pull-image-on-create(true) and no-pull was not set we pull the image when
// they ask for a create as a helper on the cli to reduce extra steps. As a
// reminder if the image is already in cache only the manifest will be pulled
// down to verify.
if opts.withPull {
auth, err := getAuth(opts.creds, opts.auth)
if err != nil {
return "", err
Expand Down
4 changes: 4 additions & 0 deletions cmd/crictl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var (
Debug bool
// PullImageOnCreate enables pulling image on create requests
PullImageOnCreate bool
// DisablePullOnRun disable pulling image on run requests
DisablePullOnRun bool
)

func getRuntimeClientConnection(context *cli.Context) (*grpc.ClientConn, error) {
Expand Down Expand Up @@ -243,6 +245,7 @@ func main() {
Timeout = context.Duration("timeout")
}
Debug = context.Bool("debug")
DisablePullOnRun = false
} else {
// Command line flags overrides config file.
if context.IsSet("runtime-endpoint") {
Expand Down Expand Up @@ -276,6 +279,7 @@ func main() {
Debug = config.Debug
}
PullImageOnCreate = config.PullImageOnCreate
DisablePullOnRun = config.DisablePullOnRun
}

if Debug {
Expand Down
17 changes: 11 additions & 6 deletions docs/crictl.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,27 @@ CRICTL OPTIONS:
```
runtime-endpoint: Container runtime endpoint
image-endpoint: Image endpoint
timeout: Timeout of connecting to server
debug: Enable debug output
pull-image-on-create: Enable pulling image on create requests (default false)
timeout: Timeout of connecting to server (default: 2s)
debug: Enable debug output (default: false)
pull-image-on-create: Enable pulling image on create requests (default: false)
disable-pull-on-run: Disable pulling image on run requests (default: false)
```
OPTIONS:
```
--get value show the option value
--set value set option (can specify multiple or separate values with commas: opt1=val1,opt2=val2)
--help, -h show help (default: false)
```
> When enabled `pull-image-on-create` modifies the create container command to first pull the container's image.
> When enabled `pull-image-on-create` modifies the create container command to first pull the container's image.
This feature is used as a helper to make creating containers easier and faster.
Some users of `crictl` may desire to not pull the image necessary to create the container.
For example, the image may have already been pulled or otherwise loaded into the container runtime, or the user may be running without a network. For this reason the default for `pull-image-on-create` is false.
When set to true a `--no-pull` option has been provided for the create command.
Thus there are two ways to avoid pulling the container image when creating the container.

> By default the run command first pulls the container image, and `disable-pull-on-run` is false.
Some users of `crictl` may desire to set `disable-pull-on-run` to true to not pull the image by default when using the run command.

> To override these default pull configuration settings, `--no-pull` and `--with-pull` options are provided for the create and run commands.
## Examples

Expand Down
3 changes: 3 additions & 0 deletions pkg/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type ServerConfiguration struct {
Debug bool
// PullImageOnCreate enables also pulling an image for create requests
PullImageOnCreate bool
// DisablePullOnRun disables pulling an image for run requests
DisablePullOnRun bool
}

// GetServerConfigFromFile returns the CRI server configuration from file
Expand Down Expand Up @@ -66,5 +68,6 @@ func GetServerConfigFromFile(configFileName, currentDir string) (*ServerConfigur
serverConfig.Timeout = time.Duration(config.Timeout) * time.Second
serverConfig.Debug = config.Debug
serverConfig.PullImageOnCreate = config.PullImageOnCreate
serverConfig.DisablePullOnRun = config.DisablePullOnRun
return &serverConfig, nil
}
9 changes: 9 additions & 0 deletions pkg/common/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
Timeout int
Debug bool
PullImageOnCreate bool
DisablePullOnRun bool
yamlData *yaml.Node //YAML representation of config
}

Expand Down Expand Up @@ -117,6 +118,11 @@ func getConfigOptions(yamlData yaml.Node) (*Config, error) {
if err != nil {
return nil, errors.Wrapf(err, "parsing config option '%s'", name)
}
case "disable-pull-on-run":
config.DisablePullOnRun, err = strconv.ParseBool(value)
if err != nil {
return nil, errors.Wrapf(err, "parsing config option '%s'", name)
}
default:
return nil, errors.Errorf("Config option '%s' is not valid", name)
}
Expand All @@ -133,6 +139,7 @@ func setConfigOptions(config *Config) {
setConfigOption("timeout", strconv.Itoa(config.Timeout), config.yamlData)
setConfigOption("debug", strconv.FormatBool(config.Debug), config.yamlData)
setConfigOption("pull-image-on-create", strconv.FormatBool(config.PullImageOnCreate), config.yamlData)
setConfigOption("disable-pull-on-run", strconv.FormatBool(config.DisablePullOnRun), config.yamlData)
}

// Set config option on yaml
Expand Down Expand Up @@ -181,6 +188,8 @@ func setConfigOption(configName, configValue string, yamlData *yaml.Node) {
tagType = "!!bool"
case "pull-image-on-create":
tagType = "!!bool"
case "disable-pull-on-run":
tagType = "!!bool"
default:
tagType = "!!str"
}
Expand Down

0 comments on commit 4197d94

Please sign in to comment.