Skip to content

Commit 6e78ff2

Browse files
author
Ma Shimiao
committed
support json value for hooks
As discussed in #470, there is not suitable separators for hooks. Because of path may contains them. So, decide to support json value for hooks. Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
1 parent 8e3ecf4 commit 6e78ff2

4 files changed

Lines changed: 86 additions & 244 deletions

File tree

cmd/oci-runtime-tool/generate.go

Lines changed: 22 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ var generateFlags = []cli.Flag{
2020
cli.StringSliceFlag{Name: "args", Usage: "command to run in the container"},
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"},
23-
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"},
26-
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"},
29-
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"},
23+
cli.StringSliceFlag{Name: "hooks-poststart-add", Usage: "set command to run in poststart hooks"},
24+
cli.BoolFlag{Name: "hooks-poststart-remove-all", Usage: "remove all poststart hooks"},
25+
cli.StringSliceFlag{Name: "hooks-poststop-add", Usage: "set command to run in poststop hooks"},
26+
cli.BoolFlag{Name: "hooks-poststop-remove-all", Usage: "remove all poststop hooks"},
27+
cli.StringSliceFlag{Name: "hooks-prestart-add", Usage: "set command to run in prestart hooks"},
28+
cli.BoolFlag{Name: "hooks-prestart-remove-all", Usage: "remove all prestart hooks"},
3229
cli.StringFlag{Name: "hostname", Usage: "hostname value for the container"},
3330
cli.StringSliceFlag{Name: "label", Usage: "add annotations to the configuration e.g. key=value"},
3431
cli.StringFlag{Name: "linux-apparmor", Usage: "specifies the the apparmor profile for the container"},
@@ -442,102 +439,45 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
442439
}
443440
}
444441

445-
if context.IsSet("hooks-poststart") {
446-
postStartHooks := context.StringSlice("hooks-poststart")
447-
for _, hook := range postStartHooks {
448-
path, args, err := parseHook(hook)
449-
if err != nil {
450-
return err
451-
}
452-
g.AddPostStartHook(path, args)
453-
}
442+
if context.IsSet("hooks-poststart-remove-all") {
443+
g.ClearPostStartHooks()
454444
}
455445

456-
if context.IsSet("hooks-poststart-env") {
457-
postStartEnvs := context.StringSlice("hooks-poststart-env")
458-
for _, postStartEnv := range postStartEnvs {
459-
path, env, err := parseHookEnv(postStartEnv)
446+
if context.IsSet("hooks-poststart-add") {
447+
postStartHooks := context.StringSlice("hooks-poststart-add")
448+
for _, hook := range postStartHooks {
449+
err := g.AddPostStartHook(hook)
460450
if err != nil {
461451
return err
462452
}
463-
g.AddPostStartHookEnv(path, env)
464453
}
465454
}
466455

467-
if context.IsSet("hooks-poststart-timeout") {
468-
postStartTimeouts := context.StringSlice("hooks-poststart-timeout")
469-
for _, postStartTimeout := range postStartTimeouts {
470-
path, timeout, err := parseHookTimeout(postStartTimeout)
471-
if err != nil {
472-
return err
473-
}
474-
g.AddPostStartHookTimeout(path, timeout)
475-
}
456+
if context.IsSet("hooks-poststop-remove-all") {
457+
g.ClearPostStopHooks()
476458
}
477459

478-
if context.IsSet("hooks-poststop") {
479-
postStopHooks := context.StringSlice("hooks-poststop")
460+
if context.IsSet("hooks-poststop-add") {
461+
postStopHooks := context.StringSlice("hooks-poststop-add")
480462
for _, hook := range postStopHooks {
481-
path, args, err := parseHook(hook)
482-
if err != nil {
483-
return err
484-
}
485-
g.AddPostStopHook(path, args)
486-
}
487-
}
488-
489-
if context.IsSet("hooks-poststop-env") {
490-
postStopEnvs := context.StringSlice("hooks-poststop-env")
491-
for _, postStopEnv := range postStopEnvs {
492-
path, env, err := parseHookEnv(postStopEnv)
463+
err := g.AddPostStopHook(hook)
493464
if err != nil {
494465
return err
495466
}
496-
g.AddPostStopHookEnv(path, env)
497467
}
498468
}
499469

500-
if context.IsSet("hooks-poststop-timeout") {
501-
postStopTimeouts := context.StringSlice("hooks-poststop-timeout")
502-
for _, postStopTimeout := range postStopTimeouts {
503-
path, timeout, err := parseHookTimeout(postStopTimeout)
504-
if err != nil {
505-
return err
506-
}
507-
g.AddPostStopHookTimeout(path, timeout)
508-
}
470+
if context.IsSet("hooks-prestart-remove-all") {
471+
g.ClearPreStartHooks()
509472
}
510473

511-
if context.IsSet("hooks-prestart") {
512-
preStartHooks := context.StringSlice("hooks-prestart")
474+
if context.IsSet("hooks-prestart-add") {
475+
preStartHooks := context.StringSlice("hooks-prestart-add")
513476
for _, hook := range preStartHooks {
514-
path, args, err := parseHook(hook)
515-
if err != nil {
516-
return err
517-
}
518-
g.AddPreStartHook(path, args)
519-
}
520-
}
521-
522-
if context.IsSet("hooks-prestart-env") {
523-
preStartEnvs := context.StringSlice("hooks-prestart-env")
524-
for _, preStartEnv := range preStartEnvs {
525-
path, env, err := parseHookEnv(preStartEnv)
477+
err := g.AddPreStartHook(hook)
526478
if err != nil {
527479
return err
528480
}
529-
g.AddPreStartHookEnv(path, env)
530-
}
531-
}
532-
533-
if context.IsSet("hooks-prestart-timeout") {
534-
preStartTimeouts := context.StringSlice("hooks-prestart-timeout")
535-
for _, preStartTimeout := range preStartTimeouts {
536-
path, timeout, err := parseHookTimeout(preStartTimeout)
537-
if err != nil {
538-
return err
539-
}
540-
g.AddPreStartHookTimeout(path, timeout)
541481
}
542482
}
543483

@@ -804,44 +744,6 @@ func parseHugepageLimit(pageLimit string) (string, uint64, error) {
804744
return pl[0], uint64(limit), nil
805745
}
806746

807-
func parseHook(s string) (string, []string, error) {
808-
args := []string{}
809-
parts := strings.Split(s, ":")
810-
if len(parts) > 1 && parts[0] == "" {
811-
return "", args, fmt.Errorf("invalid hook value: %s", s)
812-
}
813-
path := parts[0]
814-
if len(parts) > 1 {
815-
args = parts[1:]
816-
}
817-
return path, args, nil
818-
}
819-
820-
func parseHookEnv(s string) (string, []string, error) {
821-
parts := strings.Split(s, ":")
822-
envs := []string{}
823-
if len(parts) < 2 {
824-
return "", envs, fmt.Errorf("invalid format: %s", s)
825-
}
826-
envs = parts[1:]
827-
828-
return parts[0], envs, nil
829-
}
830-
831-
func parseHookTimeout(s string) (string, int, error) {
832-
parts := strings.Split(s, ":")
833-
if len(parts) != 2 {
834-
return "", 0, fmt.Errorf("invalid format: %s", s)
835-
}
836-
837-
timeout, err := strconv.Atoi(parts[1])
838-
if err != nil {
839-
return "", 0, err
840-
}
841-
842-
return parts[0], timeout, nil
843-
}
844-
845747
func parseNetworkPriority(np string) (string, int32, error) {
846748
var err error
847749

completions/bash/oci-runtime-tool

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,9 @@ _oci-runtime-tool_generate() {
305305
--args
306306
--env
307307
--env-file
308-
--hooks-poststart
309-
--hooks-poststart-env
310-
--hooks-poststart-timeout
311-
--hooks-poststop
312-
--hooks-poststop-env
313-
--hooks-poststop-timeout
314-
--hooks-prestart
315-
--hooks-prestart-env
316-
--hooks-prestart-timeout
308+
--hooks-poststart-add
309+
--hooks-poststop-add
310+
--hooks-prestart-add
317311
--hostname
318312
--label
319313
--linux-apparmor
@@ -387,6 +381,9 @@ _oci-runtime-tool_generate() {
387381

388382
local boolean_options="
389383
--help -h
384+
--hooks-poststart-remove-all
385+
--hooks-poststop-remove-all
386+
--hooks-prestart-remove-all
390387
--linux-device-remove-all
391388
--linux-disable-oom-kill
392389
--linux-namespace-remove-all
@@ -415,7 +412,7 @@ _oci-runtime-tool_generate() {
415412
return
416413
;;
417414

418-
--hooks-poststart|--hooks-poststop|--hooks-prestart)
415+
--hooks-poststart-add|--hooks-poststop-add|--hooks-prestart-add)
419416
COMPREPLY=( $( compgen -W "$( __oci-runtime-tool_hooks )" -- "$cur" ) )
420417
__oci-runtime-tool_nospace
421418
return

generate/generate.go

Lines changed: 30 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -743,42 +743,21 @@ func (g *Generator) ClearPreStartHooks() {
743743
}
744744

745745
// AddPreStartHook add a prestart hook into g.spec.Hooks.Prestart.
746-
func (g *Generator) AddPreStartHook(path string, args []string) {
746+
func (g *Generator) AddPreStartHook(hookObject string) error {
747747
g.initSpecHooks()
748-
hook := rspec.Hook{Path: path, Args: args}
749-
for i, hook := range g.spec.Hooks.Prestart {
750-
if hook.Path == path {
751-
g.spec.Hooks.Prestart[i] = hook
752-
return
753-
}
748+
tmpHook := rspec.Hook{}
749+
err := json.Unmarshal([]byte(hookObject), &tmpHook)
750+
if err != nil {
751+
return err
754752
}
755-
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
756-
}
757-
758-
// AddPreStartHookEnv adds envs of a prestart hook into g.spec.Hooks.Prestart.
759-
func (g *Generator) AddPreStartHookEnv(path string, envs []string) {
760-
g.initSpecHooks()
761753
for i, hook := range g.spec.Hooks.Prestart {
762-
if hook.Path == path {
763-
g.spec.Hooks.Prestart[i].Env = envs
764-
return
765-
}
766-
}
767-
hook := rspec.Hook{Path: path, Env: envs}
768-
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
769-
}
770-
771-
// AddPreStartHookTimeout adds timeout of a prestart hook into g.spec.Hooks.Prestart.
772-
func (g *Generator) AddPreStartHookTimeout(path string, timeout int) {
773-
g.initSpecHooks()
774-
for i, hook := range g.spec.Hooks.Prestart {
775-
if hook.Path == path {
776-
g.spec.Hooks.Prestart[i].Timeout = &timeout
777-
return
754+
if hook.Path == tmpHook.Path {
755+
g.spec.Hooks.Prestart[i] = tmpHook
756+
return nil
778757
}
779758
}
780-
hook := rspec.Hook{Path: path, Timeout: &timeout}
781-
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
759+
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, tmpHook)
760+
return nil
782761
}
783762

784763
// ClearPostStopHooks clear g.spec.Hooks.Poststop.
@@ -790,42 +769,21 @@ func (g *Generator) ClearPostStopHooks() {
790769
}
791770

792771
// AddPostStopHook adds a poststop hook into g.spec.Hooks.Poststop.
793-
func (g *Generator) AddPostStopHook(path string, args []string) {
772+
func (g *Generator) AddPostStopHook(hookObject string) error {
794773
g.initSpecHooks()
795-
hook := rspec.Hook{Path: path, Args: args}
796-
for i, hook := range g.spec.Hooks.Poststop {
797-
if hook.Path == path {
798-
g.spec.Hooks.Poststop[i] = hook
799-
return
800-
}
801-
}
802-
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
803-
}
804-
805-
// AddPostStopHookEnv adds envs of a poststop hook into g.spec.Hooks.Poststop.
806-
func (g *Generator) AddPostStopHookEnv(path string, envs []string) {
807-
g.initSpecHooks()
808-
for i, hook := range g.spec.Hooks.Poststop {
809-
if hook.Path == path {
810-
g.spec.Hooks.Poststop[i].Env = envs
811-
return
812-
}
774+
tmpHook := rspec.Hook{}
775+
err := json.Unmarshal([]byte(hookObject), &tmpHook)
776+
if err != nil {
777+
return err
813778
}
814-
hook := rspec.Hook{Path: path, Env: envs}
815-
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
816-
}
817-
818-
// AddPostStopHookTimeout adds timeout of a poststop hook into g.spec.Hooks.Poststop.
819-
func (g *Generator) AddPostStopHookTimeout(path string, timeout int) {
820-
g.initSpecHooks()
821779
for i, hook := range g.spec.Hooks.Poststop {
822-
if hook.Path == path {
823-
g.spec.Hooks.Poststop[i].Timeout = &timeout
824-
return
780+
if hook.Path == tmpHook.Path {
781+
g.spec.Hooks.Poststop[i] = tmpHook
782+
return nil
825783
}
826784
}
827-
hook := rspec.Hook{Path: path, Timeout: &timeout}
828-
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
785+
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, tmpHook)
786+
return nil
829787
}
830788

831789
// ClearPostStartHooks clear g.spec.Hooks.Poststart.
@@ -837,42 +795,21 @@ func (g *Generator) ClearPostStartHooks() {
837795
}
838796

839797
// AddPostStartHook adds a poststart hook into g.spec.Hooks.Poststart.
840-
func (g *Generator) AddPostStartHook(path string, args []string) {
798+
func (g *Generator) AddPostStartHook(hookObject string) error {
841799
g.initSpecHooks()
842-
hook := rspec.Hook{Path: path, Args: args}
843-
for i, hook := range g.spec.Hooks.Poststart {
844-
if hook.Path == path {
845-
g.spec.Hooks.Poststart[i] = hook
846-
return
847-
}
848-
}
849-
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
850-
}
851-
852-
// AddPostStartHookEnv adds envs of a poststart hook into g.spec.Hooks.Poststart.
853-
func (g *Generator) AddPostStartHookEnv(path string, envs []string) {
854-
g.initSpecHooks()
855-
for i, hook := range g.spec.Hooks.Poststart {
856-
if hook.Path == path {
857-
g.spec.Hooks.Poststart[i].Env = envs
858-
return
859-
}
800+
tmpHook := rspec.Hook{}
801+
err := json.Unmarshal([]byte(hookObject), &tmpHook)
802+
if err != nil {
803+
return err
860804
}
861-
hook := rspec.Hook{Path: path, Env: envs}
862-
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
863-
}
864-
865-
// AddPostStartHookTimeout adds timeout of a poststart hook into g.spec.Hooks.Poststart.
866-
func (g *Generator) AddPostStartHookTimeout(path string, timeout int) {
867-
g.initSpecHooks()
868805
for i, hook := range g.spec.Hooks.Poststart {
869-
if hook.Path == path {
870-
g.spec.Hooks.Poststart[i].Timeout = &timeout
871-
return
806+
if hook.Path == tmpHook.Path {
807+
g.spec.Hooks.Poststart[i] = tmpHook
808+
return nil
872809
}
873810
}
874-
hook := rspec.Hook{Path: path, Timeout: &timeout}
875-
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
811+
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, tmpHook)
812+
return nil
876813
}
877814

878815
// AddTmpfsMount adds a tmpfs mount into g.spec.Mounts.

0 commit comments

Comments
 (0)