Consistently add empty types where they are nil in spec

In a few places we check for nil types when modifying a spec,
but in many cases we do not so we could get a panic if the
passed in type was not filled. Because the generated spec is
filled we will not notice this but users may get unexpected
panics.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack
2018-04-03 14:30:47 +01:00
parent 8c2acf45fb
commit 0ee2f35e43
3 changed files with 51 additions and 10 deletions

View File

@@ -27,9 +27,17 @@ import (
// SpecOpts sets spec specific information to a newly generated OCI spec
type SpecOpts func(context.Context, Client, *containers.Container, *specs.Spec) error
// setProcess sets Process to empty if unset
func setProcess(s *specs.Spec) {
if s.Process == nil {
s.Process = &specs.Process{}
}
}
// WithProcessArgs replaces the args on the generated spec
func WithProcessArgs(args ...string) SpecOpts {
return func(_ context.Context, _ Client, _ *containers.Container, s *specs.Spec) error {
setProcess(s)
s.Process.Args = args
return nil
}
@@ -38,6 +46,7 @@ func WithProcessArgs(args ...string) SpecOpts {
// WithProcessCwd replaces the current working directory on the generated spec
func WithProcessCwd(cwd string) SpecOpts {
return func(_ context.Context, _ Client, _ *containers.Container, s *specs.Spec) error {
setProcess(s)
s.Process.Cwd = cwd
return nil
}
@@ -55,6 +64,7 @@ func WithHostname(name string) SpecOpts {
func WithEnv(environmentVariables []string) SpecOpts {
return func(_ context.Context, _ Client, _ *containers.Container, s *specs.Spec) error {
if len(environmentVariables) > 0 {
setProcess(s)
s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, environmentVariables)
}
return nil