mirror of
https://github.com/containers/podman.git
synced 2025-10-14 01:35:29 +08:00
hooks: Rename Hooks() output to extensionStageHooks
To more clearly distinguish between the extensionStages input to New() (a slice of strings) and the map output from Hooks(). Signed-off-by: W. Trevor King <wking@tremily.us> Closes: #855 Approved by: rhatdan
This commit is contained in:

committed by
Atomic Bot

parent
7c1434c2f7
commit
4dfe0d60f3
@ -88,28 +88,28 @@ func (m *Manager) namedHooks() (hooks []*namedHook) {
|
|||||||
|
|
||||||
// Hooks injects OCI runtime hooks for a given container configuration.
|
// Hooks injects OCI runtime hooks for a given container configuration.
|
||||||
//
|
//
|
||||||
// If the extensionStages slice was set when initializing the Manager,
|
// If extensionStages was set when initializing the Manager,
|
||||||
// matching hooks requesting those stages will be returned in the
|
// matching hooks requesting those stages will be returned in
|
||||||
// extensionStages map. This takes precedence over their inclusion in
|
// extensionStageHooks. This takes precedence over their inclusion in
|
||||||
// the OCI configuration. For example:
|
// the OCI configuration. For example:
|
||||||
//
|
//
|
||||||
// manager, err := New(ctx, []string{DefaultDir}, []string{"poststop"}, lang)
|
// manager, err := New(ctx, []string{DefaultDir}, []string{"poststop"}, lang)
|
||||||
// extensionStages, err := manager.Hooks(config, annotations, hasBindMounts)
|
// extensionStageHooks, err := manager.Hooks(config, annotations, hasBindMounts)
|
||||||
//
|
//
|
||||||
// will have any matching post-stop hooks in extensionStages and will
|
// will have any matching post-stop hooks in extensionStageHooks and
|
||||||
// not insert them into config.Hooks.Poststop.
|
// will not insert them into config.Hooks.Poststop.
|
||||||
func (m *Manager) Hooks(config *rspec.Spec, annotations map[string]string, hasBindMounts bool) (extensionStages map[string][]rspec.Hook, err error) {
|
func (m *Manager) Hooks(config *rspec.Spec, annotations map[string]string, hasBindMounts bool) (extensionStageHooks map[string][]rspec.Hook, err error) {
|
||||||
hooks := m.namedHooks()
|
hooks := m.namedHooks()
|
||||||
collator := collate.New(m.language, collate.IgnoreCase, collate.IgnoreWidth)
|
collator := collate.New(m.language, collate.IgnoreCase, collate.IgnoreWidth)
|
||||||
collator.Sort(namedHooks(hooks))
|
collator.Sort(namedHooks(hooks))
|
||||||
localStages := map[string]bool{} // stages destined for extensionStages
|
localStages := map[string]bool{} // stages destined for extensionStageHooks
|
||||||
for _, stage := range m.extensionStages {
|
for _, stage := range m.extensionStages {
|
||||||
localStages[stage] = true
|
localStages[stage] = true
|
||||||
}
|
}
|
||||||
for _, namedHook := range hooks {
|
for _, namedHook := range hooks {
|
||||||
match, err := namedHook.hook.When.Match(config, annotations, hasBindMounts)
|
match, err := namedHook.hook.When.Match(config, annotations, hasBindMounts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return extensionStages, errors.Wrapf(err, "matching hook %q", namedHook.name)
|
return extensionStageHooks, errors.Wrapf(err, "matching hook %q", namedHook.name)
|
||||||
}
|
}
|
||||||
if match {
|
if match {
|
||||||
if config.Hooks == nil {
|
if config.Hooks == nil {
|
||||||
@ -117,10 +117,10 @@ func (m *Manager) Hooks(config *rspec.Spec, annotations map[string]string, hasBi
|
|||||||
}
|
}
|
||||||
for _, stage := range namedHook.hook.Stages {
|
for _, stage := range namedHook.hook.Stages {
|
||||||
if _, ok := localStages[stage]; ok {
|
if _, ok := localStages[stage]; ok {
|
||||||
if extensionStages == nil {
|
if extensionStageHooks == nil {
|
||||||
extensionStages = map[string][]rspec.Hook{}
|
extensionStageHooks = map[string][]rspec.Hook{}
|
||||||
}
|
}
|
||||||
extensionStages[stage] = append(extensionStages[stage], namedHook.hook.Hook)
|
extensionStageHooks[stage] = append(extensionStageHooks[stage], namedHook.hook.Hook)
|
||||||
} else {
|
} else {
|
||||||
switch stage {
|
switch stage {
|
||||||
case "prestart":
|
case "prestart":
|
||||||
@ -130,14 +130,14 @@ func (m *Manager) Hooks(config *rspec.Spec, annotations map[string]string, hasBi
|
|||||||
case "poststop":
|
case "poststop":
|
||||||
config.Hooks.Poststop = append(config.Hooks.Poststop, namedHook.hook.Hook)
|
config.Hooks.Poststop = append(config.Hooks.Poststop, namedHook.hook.Hook)
|
||||||
default:
|
default:
|
||||||
return extensionStages, fmt.Errorf("hook %q: unknown stage %q", namedHook.name, stage)
|
return extensionStageHooks, fmt.Errorf("hook %q: unknown stage %q", namedHook.name, stage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return extensionStages, nil
|
return extensionStageHooks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove remove a hook by name.
|
// remove remove a hook by name.
|
||||||
|
@ -54,7 +54,7 @@ func TestGoodNew(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
config := &rspec.Spec{}
|
config := &rspec.Spec{}
|
||||||
extensionStages, err := manager.Hooks(config, map[string]string{}, false)
|
extensionStageHooks, err := manager.Hooks(config, map[string]string{}, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -91,8 +91,8 @@ func TestGoodNew(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}, config.Hooks)
|
}, config.Hooks)
|
||||||
|
|
||||||
var nilExtensionStages map[string][]rspec.Hook
|
var nilExtensionStageHooks map[string][]rspec.Hook
|
||||||
assert.Equal(t, nilExtensionStages, extensionStages)
|
assert.Equal(t, nilExtensionStageHooks, extensionStageHooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadNew(t *testing.T) {
|
func TestBadNew(t *testing.T) {
|
||||||
@ -142,14 +142,14 @@ func TestBrokenMatch(t *testing.T) {
|
|||||||
Args: []string{"/bin/sh"},
|
Args: []string{"/bin/sh"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
extensionStages, err := manager.Hooks(config, map[string]string{}, false)
|
extensionStageHooks, err := manager.Hooks(config, map[string]string{}, false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("unexpected success")
|
t.Fatal("unexpected success")
|
||||||
}
|
}
|
||||||
assert.Regexp(t, "^matching hook \"a\\.json\": command: error parsing regexp: .*", err.Error())
|
assert.Regexp(t, "^matching hook \"a\\.json\": command: error parsing regexp: .*", err.Error())
|
||||||
|
|
||||||
var nilExtensionStages map[string][]rspec.Hook
|
var nilExtensionStageHooks map[string][]rspec.Hook
|
||||||
assert.Equal(t, nilExtensionStages, extensionStages)
|
assert.Equal(t, nilExtensionStageHooks, extensionStageHooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidStage(t *testing.T) {
|
func TestInvalidStage(t *testing.T) {
|
||||||
@ -168,14 +168,14 @@ func TestInvalidStage(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
extensionStages, err := manager.Hooks(&rspec.Spec{}, map[string]string{}, false)
|
extensionStageHooks, err := manager.Hooks(&rspec.Spec{}, map[string]string{}, false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("unexpected success")
|
t.Fatal("unexpected success")
|
||||||
}
|
}
|
||||||
assert.Regexp(t, "^hook \"a\\.json\": unknown stage \"does-not-exist\"$", err.Error())
|
assert.Regexp(t, "^hook \"a\\.json\": unknown stage \"does-not-exist\"$", err.Error())
|
||||||
|
|
||||||
var nilExtensionStages map[string][]rspec.Hook
|
var nilExtensionStageHooks map[string][]rspec.Hook
|
||||||
assert.Equal(t, nilExtensionStages, extensionStages)
|
assert.Equal(t, nilExtensionStageHooks, extensionStageHooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExtensionStage(t *testing.T) {
|
func TestExtensionStage(t *testing.T) {
|
||||||
@ -197,7 +197,7 @@ func TestExtensionStage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
config := &rspec.Spec{}
|
config := &rspec.Spec{}
|
||||||
extensionStages, err := manager.Hooks(config, map[string]string{}, false)
|
extensionStageHooks, err := manager.Hooks(config, map[string]string{}, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -226,7 +226,7 @@ func TestExtensionStage(t *testing.T) {
|
|||||||
Path: "/a/b/c",
|
Path: "/a/b/c",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, extensionStages)
|
}, extensionStageHooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
Reference in New Issue
Block a user