Skip to content
Closed
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
2 changes: 1 addition & 1 deletion pkg/hooks/1.0.0/when.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// When holds hook-injection conditions.
type When struct {
Always *bool `json:"always,omitempty"`
Annotations map[string]string `json:"annotation,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Commands []string `json:"commands,omitempty"`
HasBindMounts *bool `json:"hasBindMounts,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion pkg/hooks/1.0.0/when_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestHasBindMountsAndCommands(t *testing.T) {
match: true,
},
{
name: "both, and",
name: "both, or",
command: "/bin/sh",
hasBindMounts: true,
or: true,
Expand Down
3 changes: 2 additions & 1 deletion pkg/hooks/docs/oci-hooks.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ The following example injects `nvidia-container-runtime-hook prestart` with part
```console
$ cat /etc/containers/oci/hooks.d/nvidia.json
{
"version": "1.0.0",
"hook": {
"path": "/usr/sbin/nvidia-container-runtime-hook",
"args": ["nvidia-container-runtime-hook", "prestart"],
Expand All @@ -129,7 +130,7 @@ $ cat /etc/containers/oci/hooks.d/nvidia.json
},
"when": {
"annotations": {
"^com\.example\.department$": ".*fluid-dynamics$"
"^com\\.example\\.department$": ".*fluid-dynamics$"
}
},
"stages": ["prestart"]
Expand Down
4 changes: 4 additions & 0 deletions pkg/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
current "github.com/projectatomic/libpod/pkg/hooks/1.0.0"
"github.com/sirupsen/logrus"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -112,6 +113,7 @@ func (m *Manager) Hooks(config *rspec.Spec, annotations map[string]string, hasBi
return extensionStageHooks, errors.Wrapf(err, "matching hook %q", namedHook.name)
}
if match {
logrus.Debugf("hook %s matched; adding to stages %v", namedHook.name, namedHook.hook.Stages)
if config.Hooks == nil {
config.Hooks = &rspec.Hooks{}
}
Expand All @@ -134,6 +136,8 @@ func (m *Manager) Hooks(config *rspec.Spec, annotations map[string]string, hasBi
}
}
}
} else {
logrus.Debugf("hook %s did not match", namedHook.name)
}
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/hooks/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/pkg/errors"
current "github.com/projectatomic/libpod/pkg/hooks/1.0.0"
"github.com/sirupsen/logrus"
)

type reader func(content []byte) (*current.Hook, error)
Expand Down Expand Up @@ -61,23 +62,28 @@ func read(content []byte) (hook *current.Hook, err error) {
// ReadDir reads hook JSON files from a directory into the given map,
// clobbering any previous entries with the same filenames.
func ReadDir(path string, extensionStages []string, hooks map[string]*current.Hook) error {
logrus.Debugf("reading hooks from %s", path)
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}

for _, file := range files {
hook, err := Read(filepath.Join(path, file.Name()), extensionStages)
filePath := filepath.Join(path, file.Name())
hook, err := Read(filePath, extensionStages)
if err != nil {
if err == ErrNoJSONSuffix {
continue
}
if os.IsNotExist(err) {
continue
if err2, ok := err.(*os.PathError); ok && err2.Path == filePath {
continue
}
}
return err
}
hooks[file.Name()] = hook
logrus.Debugf("added hook %s", filePath)
}
return nil
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/hooks/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,24 @@ func TestBadDir(t *testing.T) {
}
assert.Regexp(t, "^parsing hook \"[^\"]*a.json\": unrecognized hook version: \"-1\"$", err.Error())
}

func TestHookExecutableDoesNotExit(t *testing.T) {
dir, err := ioutil.TempDir("", "hooks-test-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

jsonPath := filepath.Join(dir, "hook.json")
err = ioutil.WriteFile(jsonPath, []byte("{\"version\": \"1.0.0\", \"hook\": {\"path\": \"/does/not/exist\"}, \"when\": {\"always\": true}, \"stages\": [\"prestart\"]}"), 0644)
if err != nil {
t.Fatal(err)
}

hooks := map[string]*current.Hook{}
err = ReadDir(dir, []string{}, hooks)
if err == nil {
t.Fatal("unexpected success")
}
assert.Regexp(t, "^stat /does/not/exist: no such file or directory$", err.Error())
}