Skip to content

Commit

Permalink
libct: add/use configs.HasHook
Browse files Browse the repository at this point in the history
This allows to omit a call to c.currentOCIState (which can be somewhat
costly when there are many annotations) when the hooks of a given kind
won't be run.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Dec 23, 2024
1 parent 2e906e2 commit 8afeb58
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ const (
Poststop HookName = "poststop"
)

// HasHook checks if config has any hooks with any given names configured.
func (c *Config) HasHook(names ...HookName) bool {
if c.Hooks == nil {
return false
}
for _, h := range names {
if len(c.Hooks[h]) > 0 {
return true
}
}
return false
}

// KnownHookNames returns the known hook names.
// Used by `runc features`.
func KnownHookNames() []string {
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (c *Container) start(process *Process) (retErr error) {

if process.Init {
c.fifo.Close()
if c.config.Hooks != nil {
if c.config.HasHook(configs.Poststart) {
s, err := c.currentOCIState()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/criu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ func (c *Container) criuNotifications(resp *criurpc.CriuResp, process *Process,
return err
}
case "setup-namespaces":
if c.config.Hooks != nil {
if c.config.HasHook(configs.Prestart, configs.CreateRuntime) {
s, err := c.currentOCIState()
if err != nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ func (p *initProcess) start() (retErr error) {
return fmt.Errorf("error setting Intel RDT config for procHooks process: %w", err)
}
}
if len(p.config.Config.Hooks) != 0 {
if p.config.Config.HasHook(configs.Prestart, configs.CreateRuntime) {
s, err := p.container.currentOCIState()
if err != nil {
return err
Expand Down

0 comments on commit 8afeb58

Please sign in to comment.