Skip to content

Commit 4aa2b0b

Browse files
author
Ma Shimiao
committed
generate: add timeout options for hooks
Signed-off-by: Ma Shimiao <[email protected]>
1 parent 6bcd3b4 commit 4aa2b0b

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ var generateFlags = []cli.Flag{
7575
cli.StringSliceFlag{Name: "mount-bind", Usage: "bind mount directories src:dest[:options...]"},
7676
cli.StringFlag{Name: "mount-cgroups", Value: "no", Usage: "mount cgroups (rw,ro,no)"},
7777
cli.StringFlag{Name: "output", Usage: "output file (defaults to stdout)"},
78+
cli.StringSliceFlag{Name: "poststart", Usage: "set command to run in poststart hooks"},
79+
cli.StringSliceFlag{Name: "poststart-timeout", Usage: "set timeout for commands to run in poststart hooks"},
80+
cli.StringSliceFlag{Name: "poststop", Usage: "set command to run in poststop hooks"},
81+
cli.StringSliceFlag{Name: "poststop-timeout", Usage: "set timeout for commands to run in poststop hooks"},
82+
cli.StringSliceFlag{Name: "prestart", Usage: "set command to run in prestart hooks"},
83+
cli.StringSliceFlag{Name: "prestart-timeout", Usage: "set timeout for commands to run in prestart hooks"},
7884
cli.BoolFlag{Name: "privileged", Usage: "enable privileged container settings"},
7985
cli.StringSliceFlag{Name: "process-cap-add", Usage: "add Linux capabilities"},
8086
cli.StringSliceFlag{Name: "process-cap-drop", Usage: "drop Linux capabilities"},
@@ -332,14 +338,29 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
332338
}
333339
}
334340

341+
<<<<<<< HEAD
335342
if context.IsSet("hooks-prestart") {
336343
preStartHooks := context.StringSlice("hooks-prestart")
337344
for _, hook := range preStartHooks {
338345
path, args, err := parseHook(hook)
346+
=======
347+
if context.IsSet("poststart") {
348+
postStartHooks := context.StringSlice("poststart")
349+
for _, hook := range postStartHooks {
350+
path, args := parseHook(hook)
351+
g.AddPostStartHook(path, args)
352+
}
353+
}
354+
355+
if context.IsSet("poststart-timeout") {
356+
postStartTimeouts := context.StringSlice("poststart-timeout")
357+
for _, postStartTimeout := range postStartTimeouts {
358+
path, timeout, err := parseHookTimeout(postStartTimeout)
359+
>>>>>>> generate: add timeout options for hooks
339360
if err != nil {
340-
return err
361+
return nil
341362
}
342-
g.AddPreStartHook(path, args)
363+
g.AddPostStartHookTimeout(path, timeout)
343364
}
344365
}
345366

@@ -354,14 +375,40 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
354375
}
355376
}
356377

378+
<<<<<<< HEAD
357379
if context.IsSet("hooks-poststart") {
358380
postStartHooks := context.StringSlice("hooks-poststart")
359381
for _, hook := range postStartHooks {
360382
path, args, err := parseHook(hook)
383+
=======
384+
if context.IsSet("poststop-timeout") {
385+
postStopTimeouts := context.StringSlice("poststop-timeout")
386+
for _, postStopTimeout := range postStopTimeouts {
387+
path, timeout, err := parseHookTimeout(postStopTimeout)
388+
>>>>>>> generate: add timeout options for hooks
361389
if err != nil {
362-
return err
390+
return nil
363391
}
364-
g.AddPostStartHook(path, args)
392+
g.AddPostStopHookTimeout(path, timeout)
393+
}
394+
}
395+
396+
if context.IsSet("prestart") {
397+
preStartHooks := context.StringSlice("prestart")
398+
for _, hook := range preStartHooks {
399+
path, args := parseHook(hook)
400+
g.AddPreStartHook(path, args)
401+
}
402+
}
403+
404+
if context.IsSet("prestart-timeout") {
405+
preStartTimeouts := context.StringSlice("prestart-timeout")
406+
for _, preStartTimeout := range preStartTimeouts {
407+
path, timeout, err := parseHookTimeout(preStartTimeout)
408+
if err != nil {
409+
return nil
410+
}
411+
g.AddPreStartHookTimeout(path, timeout)
365412
}
366413
}
367414

@@ -641,6 +688,20 @@ func parseHook(s string) (string, []string, error) {
641688
return path, args, nil
642689
}
643690

691+
func parseHookTimeout(s string) (string, int, error) {
692+
parts := strings.Split(s, ":")
693+
if len(parts) != 2 {
694+
return "", 0, fmt.Errorf("invalid format: %s", s)
695+
}
696+
697+
timeout, err := strconv.Atoi(parts[1])
698+
if err != nil {
699+
return "", 0, err
700+
}
701+
702+
return parts[0], timeout, nil
703+
}
704+
644705
func parseNetworkPriority(np string) (string, int32, error) {
645706
var err error
646707

generate/generate.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,17 @@ func (g *Generator) AddPreStartHook(path string, args []string) {
747747
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
748748
}
749749

750+
// AddPreStartHookTimeout adds timeout of a prestart hook into g.spec.Hooks.Prestart.
751+
func (g *Generator) AddPreStartHookTimeout(path string, timeout int) {
752+
g.initSpec()
753+
for i, hook := range g.spec.Hooks.Prestart {
754+
if hook.Path == path {
755+
g.spec.Hooks.Prestart[i].Timeout = &timeout
756+
return
757+
}
758+
}
759+
}
760+
750761
// ClearPostStopHooks clear g.spec.Hooks.Poststop.
751762
func (g *Generator) ClearPostStopHooks() {
752763
if g.spec == nil {
@@ -765,6 +776,17 @@ func (g *Generator) AddPostStopHook(path string, args []string) {
765776
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
766777
}
767778

779+
// AddPostStopHookTimeout adds timeout of a poststop hook into g.spec.Hooks.Postsop.
780+
func (g *Generator) AddPostStopHookTimeout(path string, timeout int) {
781+
g.initSpec()
782+
for i, hook := range g.spec.Hooks.Poststop {
783+
if hook.Path == path {
784+
g.spec.Hooks.Poststop[i].Timeout = &timeout
785+
return
786+
}
787+
}
788+
}
789+
768790
// ClearPostStartHooks clear g.spec.Hooks.Poststart.
769791
func (g *Generator) ClearPostStartHooks() {
770792
if g.spec == nil {
@@ -783,6 +805,17 @@ func (g *Generator) AddPostStartHook(path string, args []string) {
783805
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
784806
}
785807

808+
// AddPostStartHookTimeout adds timeout of a poststart hook into g.spec.Hooks.Poststart.
809+
func (g *Generator) AddPostStartHookTimeout(path string, timeout int) {
810+
g.initSpec()
811+
for i, hook := range g.spec.Hooks.Poststart {
812+
if hook.Path == path {
813+
g.spec.Hooks.Poststart[i].Timeout = &timeout
814+
return
815+
}
816+
}
817+
}
818+
786819
// AddTmpfsMount adds a tmpfs mount into g.spec.Mounts.
787820
func (g *Generator) AddTmpfsMount(dest string, options []string) {
788821
mnt := rspec.Mount{

0 commit comments

Comments
 (0)