Skip to content

Commit e3c1336

Browse files
committed
libct: add/use configs.HasHook
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]>
1 parent fe461d8 commit e3c1336

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

libcontainer/configs/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ const (
332332
Poststop HookName = "poststop"
333333
)
334334

335+
// HasHook checks if config has any hooks with any given names configured.
336+
func (c *Config) HasHook(names ...HookName) bool {
337+
if c.Hooks == nil {
338+
return false
339+
}
340+
for _, h := range names {
341+
if len(c.Hooks[h]) > 0 {
342+
return true
343+
}
344+
}
345+
return false
346+
}
347+
335348
// KnownHookNames returns the known hook names.
336349
// Used by `runc features`.
337350
func KnownHookNames() []string {

libcontainer/container_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func (c *Container) start(process *Process) (retErr error) {
350350

351351
if process.Init {
352352
c.fifo.Close()
353-
if c.config.Hooks != nil {
353+
if c.config.HasHook(configs.Poststart) {
354354
s, err := c.currentOCIState()
355355
if err != nil {
356356
return err

libcontainer/criu_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ func (c *Container) criuNotifications(resp *criurpc.CriuResp, process *Process,
11101110
return err
11111111
}
11121112
case "setup-namespaces":
1113-
if c.config.Hooks != nil {
1113+
if c.config.HasHook(configs.Prestart, configs.CreateRuntime) {
11141114
s, err := c.currentOCIState()
11151115
if err != nil {
11161116
return nil

libcontainer/process_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ func (p *initProcess) start() (retErr error) {
750750
return fmt.Errorf("error setting Intel RDT config for procHooks process: %w", err)
751751
}
752752
}
753-
if len(p.config.Config.Hooks) != 0 {
753+
if p.config.Config.HasHook(configs.Prestart, configs.CreateRuntime) {
754754
s, err := p.container.currentOCIState()
755755
if err != nil {
756756
return err

0 commit comments

Comments
 (0)