Skip to content

Commit 8f34ca0

Browse files
authored
Merge pull request #278 from Mashimiao/generate-add-hooks-timeout-options
Generate: add hooks timeout and env options
2 parents 0b03b0e + df3a46f commit 8f34ca0

File tree

4 files changed

+231
-8
lines changed

4 files changed

+231
-8
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ var generateFlags = []cli.Flag{
2121
cli.StringSliceFlag{Name: "env", Usage: "add environment variable e.g. key=value"},
2222
cli.StringSliceFlag{Name: "env-file", Usage: "read in a file of environment variables"},
2323
cli.StringSliceFlag{Name: "hooks-poststart", Usage: "set command to run in poststart hooks"},
24+
cli.StringSliceFlag{Name: "hooks-poststart-env", Usage: "set environment variables for commands to run in poststart hooks"},
25+
cli.StringSliceFlag{Name: "hooks-poststart-timeout", Usage: "set timeout for commands to run in poststart hooks"},
2426
cli.StringSliceFlag{Name: "hooks-poststop", Usage: "set command to run in poststop hooks"},
27+
cli.StringSliceFlag{Name: "hooks-poststop-env", Usage: "set environment variables for commands to run in poststop hooks"},
28+
cli.StringSliceFlag{Name: "hooks-poststop-timeout", Usage: "set timeout for commands to run in poststop hooks"},
2529
cli.StringSliceFlag{Name: "hooks-prestart", Usage: "set command to run in prestart hooks"},
30+
cli.StringSliceFlag{Name: "hooks-prestart-env", Usage: "set environment variables for commands to run in prestart hooks"},
31+
cli.StringSliceFlag{Name: "hooks-prestart-timeout", Usage: "set timeout for commands to run in prestart hooks"},
2632
cli.StringFlag{Name: "hostname", Usage: "hostname value for the container"},
2733
cli.StringSliceFlag{Name: "label", Usage: "add annotations to the configuration e.g. key=value"},
2834
cli.StringFlag{Name: "linux-apparmor", Usage: "specifies the the apparmor profile for the container"},
@@ -332,14 +338,36 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
332338
}
333339
}
334340

335-
if context.IsSet("hooks-prestart") {
336-
preStartHooks := context.StringSlice("hooks-prestart")
337-
for _, hook := range preStartHooks {
341+
if context.IsSet("hooks-poststart") {
342+
postStartHooks := context.StringSlice("hooks-poststart")
343+
for _, hook := range postStartHooks {
338344
path, args, err := parseHook(hook)
339345
if err != nil {
340346
return err
341347
}
342-
g.AddPreStartHook(path, args)
348+
g.AddPostStartHook(path, args)
349+
}
350+
}
351+
352+
if context.IsSet("hooks-poststart-env") {
353+
postStartEnvs := context.StringSlice("hooks-poststart-env")
354+
for _, postStartEnv := range postStartEnvs {
355+
path, env, err := parseHookEnv(postStartEnv)
356+
if err != nil {
357+
return err
358+
}
359+
g.AddPostStartHookEnv(path, env)
360+
}
361+
}
362+
363+
if context.IsSet("hooks-poststart-timeout") {
364+
postStartTimeouts := context.StringSlice("hooks-poststart-timeout")
365+
for _, postStartTimeout := range postStartTimeouts {
366+
path, timeout, err := parseHookTimeout(postStartTimeout)
367+
if err != nil {
368+
return err
369+
}
370+
g.AddPostStartHookTimeout(path, timeout)
343371
}
344372
}
345373

@@ -354,14 +382,58 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
354382
}
355383
}
356384

357-
if context.IsSet("hooks-poststart") {
358-
postStartHooks := context.StringSlice("hooks-poststart")
359-
for _, hook := range postStartHooks {
385+
if context.IsSet("hooks-poststop-env") {
386+
postStopEnvs := context.StringSlice("hooks-poststop-env")
387+
for _, postStopEnv := range postStopEnvs {
388+
path, env, err := parseHookEnv(postStopEnv)
389+
if err != nil {
390+
return err
391+
}
392+
g.AddPostStopHookEnv(path, env)
393+
}
394+
}
395+
396+
if context.IsSet("hooks-poststop-timeout") {
397+
postStopTimeouts := context.StringSlice("hooks-poststop-timeout")
398+
for _, postStopTimeout := range postStopTimeouts {
399+
path, timeout, err := parseHookTimeout(postStopTimeout)
400+
if err != nil {
401+
return err
402+
}
403+
g.AddPostStopHookTimeout(path, timeout)
404+
}
405+
}
406+
407+
if context.IsSet("hooks-prestart") {
408+
preStartHooks := context.StringSlice("hooks-prestart")
409+
for _, hook := range preStartHooks {
360410
path, args, err := parseHook(hook)
361411
if err != nil {
362412
return err
363413
}
364-
g.AddPostStartHook(path, args)
414+
g.AddPreStartHook(path, args)
415+
}
416+
}
417+
418+
if context.IsSet("hooks-prestart-env") {
419+
preStartEnvs := context.StringSlice("hooks-prestart-env")
420+
for _, preStartEnv := range preStartEnvs {
421+
path, env, err := parseHookEnv(preStartEnv)
422+
if err != nil {
423+
return err
424+
}
425+
g.AddPreStartHookEnv(path, env)
426+
}
427+
}
428+
429+
if context.IsSet("hooks-prestart-timeout") {
430+
preStartTimeouts := context.StringSlice("hooks-prestart-timeout")
431+
for _, preStartTimeout := range preStartTimeouts {
432+
path, timeout, err := parseHookTimeout(preStartTimeout)
433+
if err != nil {
434+
return err
435+
}
436+
g.AddPreStartHookTimeout(path, timeout)
365437
}
366438
}
367439

@@ -641,6 +713,31 @@ func parseHook(s string) (string, []string, error) {
641713
return path, args, nil
642714
}
643715

716+
func parseHookEnv(s string) (string, []string, error) {
717+
parts := strings.Split(s, ":")
718+
envs := []string{}
719+
if len(parts) < 2 {
720+
return "", envs, fmt.Errorf("invalid format: %s", s)
721+
}
722+
envs = parts[1:]
723+
724+
return parts[0], envs, nil
725+
}
726+
727+
func parseHookTimeout(s string) (string, int, error) {
728+
parts := strings.Split(s, ":")
729+
if len(parts) != 2 {
730+
return "", 0, fmt.Errorf("invalid format: %s", s)
731+
}
732+
733+
timeout, err := strconv.Atoi(parts[1])
734+
if err != nil {
735+
return "", 0, err
736+
}
737+
738+
return parts[0], timeout, nil
739+
}
740+
644741
func parseNetworkPriority(np string) (string, int32, error) {
645742
var err error
646743

completions/bash/oci-runtime-tool

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,14 @@ _oci-runtime-tool_generate() {
308308
--env
309309
--env-file
310310
--hooks-poststart
311+
--hooks-poststart-env
312+
--hooks-poststart-timeout
311313
--hooks-poststop
314+
--hooks-poststop-env
315+
--hooks-poststop-timeout
312316
--hooks-prestart
317+
--hooks-prestart-env
318+
--hooks-prestart-timeout
313319
--hostname
314320
--label
315321
--linux-apparmor

generate/generate.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,38 @@ func (g *Generator) ClearPreStartHooks() {
744744
func (g *Generator) AddPreStartHook(path string, args []string) {
745745
g.initSpecHooks()
746746
hook := rspec.Hook{Path: path, Args: args}
747+
for i, hook := range g.spec.Hooks.Prestart {
748+
if hook.Path == path {
749+
g.spec.Hooks.Prestart[i] = hook
750+
return
751+
}
752+
}
753+
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
754+
}
755+
756+
// AddPreStartHookEnv adds envs of a prestart hook into g.spec.Hooks.Prestart.
757+
func (g *Generator) AddPreStartHookEnv(path string, envs []string) {
758+
g.initSpecHooks()
759+
for i, hook := range g.spec.Hooks.Prestart {
760+
if hook.Path == path {
761+
g.spec.Hooks.Prestart[i].Env = envs
762+
return
763+
}
764+
}
765+
hook := rspec.Hook{Path: path, Env: envs}
766+
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
767+
}
768+
769+
// AddPreStartHookTimeout adds timeout of a prestart hook into g.spec.Hooks.Prestart.
770+
func (g *Generator) AddPreStartHookTimeout(path string, timeout int) {
771+
g.initSpecHooks()
772+
for i, hook := range g.spec.Hooks.Prestart {
773+
if hook.Path == path {
774+
g.spec.Hooks.Prestart[i].Timeout = &timeout
775+
return
776+
}
777+
}
778+
hook := rspec.Hook{Path: path, Timeout: &timeout}
747779
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
748780
}
749781

@@ -762,6 +794,38 @@ func (g *Generator) ClearPostStopHooks() {
762794
func (g *Generator) AddPostStopHook(path string, args []string) {
763795
g.initSpecHooks()
764796
hook := rspec.Hook{Path: path, Args: args}
797+
for i, hook := range g.spec.Hooks.Poststop {
798+
if hook.Path == path {
799+
g.spec.Hooks.Poststop[i] = hook
800+
return
801+
}
802+
}
803+
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
804+
}
805+
806+
// AddPostStopHookEnv adds envs of a poststop hook into g.spec.Hooks.Poststop.
807+
func (g *Generator) AddPostStopHookEnv(path string, envs []string) {
808+
g.initSpecHooks()
809+
for i, hook := range g.spec.Hooks.Poststop {
810+
if hook.Path == path {
811+
g.spec.Hooks.Poststop[i].Env = envs
812+
return
813+
}
814+
}
815+
hook := rspec.Hook{Path: path, Env: envs}
816+
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
817+
}
818+
819+
// AddPostStopHookTimeout adds timeout of a poststop hook into g.spec.Hooks.Poststop.
820+
func (g *Generator) AddPostStopHookTimeout(path string, timeout int) {
821+
g.initSpecHooks()
822+
for i, hook := range g.spec.Hooks.Poststop {
823+
if hook.Path == path {
824+
g.spec.Hooks.Poststop[i].Timeout = &timeout
825+
return
826+
}
827+
}
828+
hook := rspec.Hook{Path: path, Timeout: &timeout}
765829
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
766830
}
767831

@@ -780,6 +844,38 @@ func (g *Generator) ClearPostStartHooks() {
780844
func (g *Generator) AddPostStartHook(path string, args []string) {
781845
g.initSpecHooks()
782846
hook := rspec.Hook{Path: path, Args: args}
847+
for i, hook := range g.spec.Hooks.Poststart {
848+
if hook.Path == path {
849+
g.spec.Hooks.Poststart[i] = hook
850+
return
851+
}
852+
}
853+
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
854+
}
855+
856+
// AddPostStartHookEnv adds envs of a poststart hook into g.spec.Hooks.Poststart.
857+
func (g *Generator) AddPostStartHookEnv(path string, envs []string) {
858+
g.initSpecHooks()
859+
for i, hook := range g.spec.Hooks.Poststart {
860+
if hook.Path == path {
861+
g.spec.Hooks.Poststart[i].Env = envs
862+
return
863+
}
864+
}
865+
hook := rspec.Hook{Path: path, Env: envs}
866+
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
867+
}
868+
869+
// AddPostStartHookTimeout adds timeout of a poststart hook into g.spec.Hooks.Poststart.
870+
func (g *Generator) AddPostStartHookTimeout(path string, timeout int) {
871+
g.initSpecHooks()
872+
for i, hook := range g.spec.Hooks.Poststart {
873+
if hook.Path == path {
874+
g.spec.Hooks.Poststart[i].Timeout = &timeout
875+
return
876+
}
877+
}
878+
hook := rspec.Hook{Path: path, Timeout: &timeout}
783879
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
784880
}
785881

man/oci-runtime-tool-generate.1.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,40 @@ read the configuration from `config.json`.
4747
gets launched but after the container environment and main process has been
4848
created.
4949

50+
**--hooks-poststart-env**=[]
51+
Set environment variables for commands in poststart hooks, format is CMD:ENV. e.g. --hooks-poststart-env=/bin/test:key=value
52+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
53+
54+
**--hooks-poststart-timeout**=[]
55+
Set timeout for commands in poststart hooks, format is CMD:TIMEOUT. e.g. --hooks-poststart-timeout=/bin/test:5
56+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
57+
5058
**--hooks-poststop**=CMD[:ARGS...]
5159
Set command to run in poststop hooks. Can be specified multiple times.
5260
The multiple commands will be run in order after the container process
5361
is stopped.
5462

63+
**--hook-poststop-env**=[]
64+
Set environment variables for commands in poststop hooks, format is CMD:ENV. e.g. --hooks-poststop-env=/bin/test:key=value
65+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
66+
67+
**--hooks-poststop-timeout**=[]
68+
Set timeout for commands in poststop hooks, format is CMD:TIMEOUT. e.g. --hooks-poststop-timeout=/bin/test:5
69+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
70+
5571
**--hooks-prestart**=CMD[:ARGS...]
5672
Set command to run in prestart hooks. Can be specified multiple times.
5773
The multiple commands will be run in order after the container process
5874
has been created but before it executes the user-configured code.
5975

76+
**--hooks-prestart-env**=[]
77+
Set environment variables for commands in prestart hooks, format is CMD:ENV. e.g. --hooks-prestart-env=/bin/test:key=value
78+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
79+
80+
**--hooks-prestart-timeout**=[]
81+
Set timeout for commands in prestart hooks, format is CMD:TIMEOUT. e.g. --hooks-prestart-timeout=/bin/test:5
82+
This option can be specified multiple times. When same CMD specified over once, the last one make sense.
83+
6084
**--label**=[]
6185
Add annotations to the configuration e.g. key=value.
6286
Currently, key containing equals sign is not supported.

0 commit comments

Comments
 (0)