Skip to content

Commit df3a46f

Browse files
author
Ma Shimiao
committed
implement add/set function for hooks items
Signed-off-by: Ma Shimiao <[email protected]>
1 parent fdcecc6 commit df3a46f

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
354354
for _, postStartEnv := range postStartEnvs {
355355
path, env, err := parseHookEnv(postStartEnv)
356356
if err != nil {
357-
return nil
357+
return err
358358
}
359359
g.AddPostStartHookEnv(path, env)
360360
}
@@ -387,7 +387,7 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
387387
for _, postStopEnv := range postStopEnvs {
388388
path, env, err := parseHookEnv(postStopEnv)
389389
if err != nil {
390-
return nil
390+
return err
391391
}
392392
g.AddPostStopHookEnv(path, env)
393393
}
@@ -398,7 +398,7 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
398398
for _, postStopTimeout := range postStopTimeouts {
399399
path, timeout, err := parseHookTimeout(postStopTimeout)
400400
if err != nil {
401-
return nil
401+
return err
402402
}
403403
g.AddPostStopHookTimeout(path, timeout)
404404
}
@@ -409,7 +409,7 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
409409
for _, hook := range preStartHooks {
410410
path, args, err := parseHook(hook)
411411
if err != nil {
412-
return nil
412+
return err
413413
}
414414
g.AddPreStartHook(path, args)
415415
}
@@ -420,7 +420,7 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
420420
for _, preStartEnv := range preStartEnvs {
421421
path, env, err := parseHookEnv(preStartEnv)
422422
if err != nil {
423-
return nil
423+
return err
424424
}
425425
g.AddPreStartHookEnv(path, env)
426426
}
@@ -431,7 +431,7 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
431431
for _, preStartTimeout := range preStartTimeouts {
432432
path, timeout, err := parseHookTimeout(preStartTimeout)
433433
if err != nil {
434-
return nil
434+
return err
435435
}
436436
g.AddPreStartHookTimeout(path, timeout)
437437
}

generate/generate.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -744,29 +744,39 @@ 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+
}
747753
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
748754
}
749755

750756
// AddPreStartHookEnv adds envs of a prestart hook into g.spec.Hooks.Prestart.
751757
func (g *Generator) AddPreStartHookEnv(path string, envs []string) {
752-
g.initSpec()
758+
g.initSpecHooks()
753759
for i, hook := range g.spec.Hooks.Prestart {
754760
if hook.Path == path {
755761
g.spec.Hooks.Prestart[i].Env = envs
756762
return
757763
}
758764
}
765+
hook := rspec.Hook{Path: path, Env: envs}
766+
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
759767
}
760768

761769
// AddPreStartHookTimeout adds timeout of a prestart hook into g.spec.Hooks.Prestart.
762770
func (g *Generator) AddPreStartHookTimeout(path string, timeout int) {
763-
g.initSpec()
771+
g.initSpecHooks()
764772
for i, hook := range g.spec.Hooks.Prestart {
765773
if hook.Path == path {
766774
g.spec.Hooks.Prestart[i].Timeout = &timeout
767775
return
768776
}
769777
}
778+
hook := rspec.Hook{Path: path, Timeout: &timeout}
779+
g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook)
770780
}
771781

772782
// ClearPostStopHooks clear g.spec.Hooks.Poststop.
@@ -784,29 +794,39 @@ func (g *Generator) ClearPostStopHooks() {
784794
func (g *Generator) AddPostStopHook(path string, args []string) {
785795
g.initSpecHooks()
786796
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+
}
787803
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
788804
}
789805

790806
// AddPostStopHookEnv adds envs of a poststop hook into g.spec.Hooks.Poststop.
791807
func (g *Generator) AddPostStopHookEnv(path string, envs []string) {
792-
g.initSpec()
808+
g.initSpecHooks()
793809
for i, hook := range g.spec.Hooks.Poststop {
794810
if hook.Path == path {
795811
g.spec.Hooks.Poststop[i].Env = envs
796812
return
797813
}
798814
}
815+
hook := rspec.Hook{Path: path, Env: envs}
816+
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
799817
}
800818

801819
// AddPostStopHookTimeout adds timeout of a poststop hook into g.spec.Hooks.Poststop.
802820
func (g *Generator) AddPostStopHookTimeout(path string, timeout int) {
803-
g.initSpec()
821+
g.initSpecHooks()
804822
for i, hook := range g.spec.Hooks.Poststop {
805823
if hook.Path == path {
806824
g.spec.Hooks.Poststop[i].Timeout = &timeout
807825
return
808826
}
809827
}
828+
hook := rspec.Hook{Path: path, Timeout: &timeout}
829+
g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook)
810830
}
811831

812832
// ClearPostStartHooks clear g.spec.Hooks.Poststart.
@@ -824,29 +844,39 @@ func (g *Generator) ClearPostStartHooks() {
824844
func (g *Generator) AddPostStartHook(path string, args []string) {
825845
g.initSpecHooks()
826846
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+
}
827853
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
828854
}
829855

830856
// AddPostStartHookEnv adds envs of a poststart hook into g.spec.Hooks.Poststart.
831857
func (g *Generator) AddPostStartHookEnv(path string, envs []string) {
832-
g.initSpec()
858+
g.initSpecHooks()
833859
for i, hook := range g.spec.Hooks.Poststart {
834860
if hook.Path == path {
835861
g.spec.Hooks.Poststart[i].Env = envs
836862
return
837863
}
838864
}
865+
hook := rspec.Hook{Path: path, Env: envs}
866+
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
839867
}
840868

841869
// AddPostStartHookTimeout adds timeout of a poststart hook into g.spec.Hooks.Poststart.
842870
func (g *Generator) AddPostStartHookTimeout(path string, timeout int) {
843-
g.initSpec()
871+
g.initSpecHooks()
844872
for i, hook := range g.spec.Hooks.Poststart {
845873
if hook.Path == path {
846874
g.spec.Hooks.Poststart[i].Timeout = &timeout
847875
return
848876
}
849877
}
878+
hook := rspec.Hook{Path: path, Timeout: &timeout}
879+
g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook)
850880
}
851881

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

0 commit comments

Comments
 (0)