diff options
Diffstat (limited to 'libpod/container_internal.go')
-rw-r--r-- | libpod/container_internal.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go index f75df8c28..f3247b1c0 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "strings" "syscall" "time" @@ -22,6 +23,7 @@ import ( "github.com/pkg/errors" crioAnnotations "github.com/projectatomic/libpod/pkg/annotations" "github.com/projectatomic/libpod/pkg/chrootuser" + "github.com/projectatomic/libpod/pkg/hooks" "github.com/projectatomic/libpod/pkg/secrets" "github.com/projectatomic/libpod/pkg/util" "github.com/sirupsen/logrus" @@ -931,6 +933,9 @@ func (c *Container) generateSpec() (*spec.Spec, error) { } } + if err := c.setupOCIHooks(&g); err != nil { + return nil, errors.Wrapf(err, "error setting up OCI Hooks") + } // Bind builtin image volumes if c.config.ImageVolumes { if err := c.addImageVolumes(&g); err != nil { @@ -1103,3 +1108,58 @@ func (c *Container) saveSpec(spec *spec.Spec) error { return nil } + +func (c *Container) setupOCIHooks(g *generate.Generator) error { + addedHooks := map[string]struct{}{} + ocihooks, err := hooks.SetupHooks(c.runtime.config.HooksDir) + if err != nil { + return err + } + addHook := func(hook hooks.HookParams) error { + // Only add a hook once + if _, ok := addedHooks[hook.Hook]; !ok { + if err := hooks.AddOCIHook(g, hook); err != nil { + return err + } + addedHooks[hook.Hook] = struct{}{} + } + return nil + } + for _, hook := range ocihooks { + logrus.Debugf("SetupOCIHooks", hook) + if hook.HasBindMounts && len(c.config.Spec.Mounts) > 0 { + if err := addHook(hook); err != nil { + return err + } + continue + } + for _, cmd := range hook.Cmds { + match, err := regexp.MatchString(cmd, c.config.Spec.Process.Args[0]) + if err != nil { + logrus.Errorf("Invalid regex %q:%q", cmd, err) + continue + } + if match { + if err := addHook(hook); err != nil { + return err + } + } + } + annotations := c.Spec().Annotations + for _, annotationRegex := range hook.Annotations { + for _, annotation := range annotations { + match, err := regexp.MatchString(annotationRegex, annotation) + if err != nil { + logrus.Errorf("Invalid regex %q:%q", annotationRegex, err) + continue + } + if match { + if err := addHook(hook); err != nil { + return err + } + } + } + } + } + return nil +} |