Skip to content

Commit 79b7a87

Browse files
committed
pkg/hooks: Version the hook structure and add 1.0.0 hooks
This shifts the matching logic out of libpod/container_internal and into the hook package, where we can reuse it after vendoring into CRI-O. It also adds unit tests with almost-complete coverage. Now libpod is even more isolated from the hook internals, which makes it fairly straightforward to bump the hook config file to 1.0.0. I've dubbed the old format 0.1.0, although it doesn't specify an explicit version. Motivation for some of my changes with 1.0.0: * Add an explicit version field. This will make any future JSON structure migrations more straightforward by avoiding the need for version-guessing heuristics. * Collect the matching properties in a new When sub-structure. This makes the root Hook structure easier to understand, because you don't have to read over all the matching properties when wrapping your head around Hook. * Replace the old 'hook' and 'arguments' with a direct embedding of the runtime-spec's hook structure. This provides access to additional upstream properties (args[0], env, and timeout) and avoids the complication of a CRI-O-specific analog structure. * Add a 'when.always' property. You can usually accomplish this effect in another way (e.g. when.commands = [".*"]), but having a boolean explicitly for this use-case makes for easier reading and writing. * Replace the previous annotations array with an annotations map. The 0.1.0 approach matched only the values regardless of key, and that seems unreliable. * Replace 'cmds' with 'when.commands', because while there are a few ways to abbreviate "commands", there's only one way to write it out in full ;). This gives folks one less thing to remember when writing hook JSON. * Replace the old "inject if any specified condition matches" with "inject if all specified conditions match". This allows for more precise targeting. Users that need more generous targeting can recover the previous behavior by creating a separate 1.0.0 hook file for each specified 0.1.0 condition. I've added doc-compat support for the various pluralizations of the 0.1.0 properties. Previously, the docs and code were not in agreement. More on this particular facet in [1]. I've updated the docs to point out that the annotations being matched are the OCI config annotations. This differs from CRI-O, where the annotations used are the Kubernetes-supplied annotations [2,3]. For example, io.kubernetes.cri-o.Volumes [4] is part of CRI-O's runtime config annotations [5], but not part of the Kubernetes-supplied annotations CRI-O uses for matching hooks. The Monitor method supports the CRI-O use-case [6]. podman doesn't need it directly, but CRI-O will need it when we vendor this package there. I've used nvidia-container-runtime-hook for the annotation examples because Dan mentioned the Nvidia folks as the motivation behind annotation matching. The environment variables are documented in [7]. The 0.1.0 hook config, which does not allow for environment variables, only works because runc currently leaks the host environment into the hooks [8]. I haven't been able to find documentation for their usual annotation trigger or hook-install path, so I'm just guessing there. [1]: cri-o/cri-o#1235 [2]: https://github.com/kubernetes-incubator/cri-o/blob/v1.10.0/server/container_create.go#L760 [3]: https://github.com/kubernetes-incubator/cri-o/blob/v1.10.0/server/container_create.go#L772 [4]: https://github.com/kubernetes-incubator/cri-o/blob/v1.10.0/pkg/annotations/annotations.go#L97-L98 [5]: https://github.com/kubernetes-incubator/cri-o/blob/v1.10.0/server/container_create.go#L830-L834 [6]: cri-o/cri-o#1345 [7]: https://github.com/NVIDIA/nvidia-container-runtime/tree/v1.3.0-1#environment-variables-oci-spec [8]: opencontainers/runc#1738 Signed-off-by: W. Trevor King <[email protected]>
1 parent c339c5b commit 79b7a87

File tree

19 files changed

+1822
-255
lines changed

19 files changed

+1822
-255
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ The plan is to use OCI projects and best of breed libraries for different aspect
3737
**[Installation notes](/install.md)**
3838
Information on how to install Podman in your environment.
3939

40-
**[OCI Hooks Support](/hooks.md)**
41-
Information on how Podman configures OCI Hooks to run when launching a container.
40+
**[OCI Hooks Support](pkg/hooks/README.md)**
41+
Information on how Podman configures [OCI Hooks][spec-hooks] to run when launching a container.
4242

4343
**[Podman Commands](/commands.md)**
4444
A list of the Podman commands with links to their man pages and in many cases videos
@@ -64,3 +64,5 @@ Information about contributing to this project.
6464
1. Pod commands for Podman
6565
1. Rootless containers
6666
1. Support for cleaning up containers via post-run hooks
67+
68+
[spec-hooks]: https://github.com/opencontainers/runtime-spec/blob/v1.0.1/config.md#posix-platform-hooks

cmd/podman/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/containers/storage/pkg/reexec"
99
"github.com/pkg/errors"
1010
"github.com/projectatomic/libpod/pkg/hooks"
11+
_ "github.com/projectatomic/libpod/pkg/hooks/0.1.0"
1112
"github.com/projectatomic/libpod/version"
1213
"github.com/sirupsen/logrus"
1314
"github.com/urfave/cli"
@@ -136,7 +137,7 @@ func main() {
136137
cli.StringFlag{
137138
Name: "hooks-dir-path",
138139
Usage: "set the OCI hooks directory path",
139-
Value: hooks.DefaultHooksDir,
140+
Value: hooks.DefaultDir,
140141
Hidden: true,
141142
},
142143
cli.StringFlag{

hooks.md

Lines changed: 0 additions & 91 deletions
This file was deleted.

libpod/container_internal.go

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"path"
1111
"path/filepath"
12-
"regexp"
1312
"strings"
1413
"syscall"
1514
"time"
@@ -1064,7 +1063,7 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
10641063
}
10651064
}
10661065

1067-
if err := c.setupOCIHooks(&g); err != nil {
1066+
if err := c.setupOCIHooks(ctx, &g); err != nil {
10681067
return nil, errors.Wrapf(err, "error setting up OCI Hooks")
10691068
}
10701069
// Bind builtin image volumes
@@ -1283,58 +1282,15 @@ func (c *Container) saveSpec(spec *spec.Spec) error {
12831282
return nil
12841283
}
12851284

1286-
// Add OCI hooks to a container's spec
1287-
func (c *Container) setupOCIHooks(g *generate.Generator) error {
1288-
addedHooks := map[string]struct{}{}
1289-
ocihooks, err := hooks.SetupHooks(c.runtime.config.HooksDir)
1290-
if err != nil {
1291-
return err
1292-
}
1293-
addHook := func(hook hooks.HookParams) error {
1294-
// Only add a hook once
1295-
if _, ok := addedHooks[hook.Hook]; !ok {
1296-
if err := hooks.AddOCIHook(g, hook); err != nil {
1297-
return err
1298-
}
1299-
addedHooks[hook.Hook] = struct{}{}
1300-
}
1285+
func (c *Container) setupOCIHooks(ctx context.Context, g *generate.Generator) error {
1286+
if c.runtime.config.HooksDir == "" {
13011287
return nil
13021288
}
1303-
for _, hook := range ocihooks {
1304-
logrus.Debugf("SetupOCIHooks", hook)
1305-
if hook.HasBindMounts && len(c.config.UserVolumes) > 0 {
1306-
if err := addHook(hook); err != nil {
1307-
return err
1308-
}
1309-
continue
1310-
}
1311-
for _, cmd := range hook.Cmds {
1312-
match, err := regexp.MatchString(cmd, c.config.Spec.Process.Args[0])
1313-
if err != nil {
1314-
logrus.Errorf("Invalid regex %q:%q", cmd, err)
1315-
continue
1316-
}
1317-
if match {
1318-
if err := addHook(hook); err != nil {
1319-
return err
1320-
}
1321-
}
1322-
}
1323-
annotations := c.Spec().Annotations
1324-
for _, annotationRegex := range hook.Annotations {
1325-
for _, annotation := range annotations {
1326-
match, err := regexp.MatchString(annotationRegex, annotation)
1327-
if err != nil {
1328-
logrus.Errorf("Invalid regex %q:%q", annotationRegex, err)
1329-
continue
1330-
}
1331-
if match {
1332-
if err := addHook(hook); err != nil {
1333-
return err
1334-
}
1335-
}
1336-
}
1337-
}
1289+
1290+
manager, err := hooks.New(ctx, []string{c.runtime.config.HooksDir})
1291+
if err != nil {
1292+
return err
13381293
}
1339-
return nil
1294+
1295+
return manager.Hooks(g.Spec(), c.Spec().Annotations, len(c.config.UserVolumes) > 0)
13401296
}

libpod/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ var (
159159
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
160160
},
161161
CgroupManager: CgroupfsCgroupsManager,
162-
HooksDir: hooks.DefaultHooksDir,
162+
HooksDir: hooks.DefaultDir,
163163
StaticDir: filepath.Join(storage.DefaultStoreOptions.GraphRoot, "libpod"),
164164
TmpDir: "/var/run/libpod",
165165
MaxLogSize: -1,

pkg/hooks/0.1.0/hook.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Package hook is the 0.1.0 hook configuration structure.
2+
package hook
3+
4+
import (
5+
"encoding/json"
6+
"errors"
7+
"strings"
8+
9+
rspec "github.com/opencontainers/runtime-spec/specs-go"
10+
hooks "github.com/projectatomic/libpod/pkg/hooks"
11+
current "github.com/projectatomic/libpod/pkg/hooks/1.0.0"
12+
)
13+
14+
// Version is the hook configuration version defined in this package.
15+
const Version = "0.1.0"
16+
17+
// Hook is the hook configuration structure.
18+
type Hook struct {
19+
Hook *string `json:"hook"`
20+
Arguments []string `json:"arguments,omitempty"`
21+
22+
// https://github.com/kubernetes-incubator/cri-o/pull/1235
23+
Stages []string `json:"stages"`
24+
Stage []string `json:"stage"`
25+
26+
Cmds []string `json:"cmds,omitempty"`
27+
Cmd []string `json:"cmd,omitempty"`
28+
29+
Annotations []string `json:"annotations,omitempty"`
30+
Annotation []string `json:"annotation,omitempty"`
31+
32+
HasBindMounts *bool `json:"hasbindmounts,omitempty"`
33+
}
34+
35+
func read(content []byte) (hook *current.Hook, err error) {
36+
var raw Hook
37+
if err = json.Unmarshal(content, &raw); err != nil {
38+
return nil, err
39+
}
40+
41+
if raw.Hook == nil {
42+
return nil, errors.New("missing required property: hook")
43+
}
44+
45+
if raw.Stages == nil {
46+
raw.Stages = raw.Stage
47+
} else if raw.Stage != nil {
48+
return nil, errors.New("cannot set both 'stage' and 'stages'")
49+
}
50+
if raw.Stages == nil {
51+
return nil, errors.New("missing required property: stages")
52+
}
53+
54+
if raw.Cmds == nil {
55+
raw.Cmds = raw.Cmd
56+
} else if raw.Cmd != nil {
57+
return nil, errors.New("cannot set both 'cmd' and 'cmds'")
58+
}
59+
60+
if raw.Annotations == nil {
61+
raw.Annotations = raw.Annotation
62+
} else if raw.Annotation != nil {
63+
return nil, errors.New("cannot set both 'annotation' and 'annotations'")
64+
}
65+
66+
hook = &current.Hook{
67+
Version: current.Version,
68+
Hook: rspec.Hook{
69+
Path: *raw.Hook,
70+
},
71+
When: current.When{
72+
Commands: raw.Cmds,
73+
HasBindMounts: raw.HasBindMounts,
74+
Or: true,
75+
},
76+
Stages: raw.Stages,
77+
}
78+
if raw.Arguments != nil {
79+
hook.Hook.Args = append([]string{*raw.Hook}, raw.Arguments...)
80+
}
81+
if raw.Annotations != nil {
82+
hook.When.Annotations = map[string]string{
83+
".*": strings.Join(raw.Annotations, "|"),
84+
}
85+
}
86+
87+
return hook, nil
88+
}
89+
90+
func init() {
91+
hooks.Readers[""] = read
92+
hooks.Readers[Version] = read
93+
}

0 commit comments

Comments
 (0)