Merge pull request #2457 from michelleN/hooks-bug

fix(tiller): track hooks in multi-def manifests
pull/2335/merge
Michelle Noorali 8 years ago committed by GitHub
commit 15c14194c4

@ -51,96 +51,149 @@ type manifest struct {
head *util.SimpleHead head *util.SimpleHead
} }
// sortManifests takes a map of filename/YAML contents and sorts them into hook types. type result struct {
hooks []*release.Hook
generic []manifest
}
type manifestFile struct {
entries map[string]string
path string
apis chartutil.VersionSet
}
// sortManifests takes a map of filename/YAML contents, splits the file
// by manifest entries, and sorts the entries into hook types.
// //
// The resulting hooks struct will be populated with all of the generated hooks. // The resulting hooks struct will be populated with all of the generated hooks.
// Any file that does not declare one of the hook types will be placed in the // Any file that does not declare one of the hook types will be placed in the
// 'generic' bucket. // 'generic' bucket.
// //
// To determine hook type, this looks for a YAML structure like this:
//
// kind: SomeKind
// apiVersion: v1
// metadata:
// annotations:
// helm.sh/hook: pre-install
//
// Where HOOK_NAME is one of the known hooks.
//
// If a file declares more than one hook, it will be copied into all of the applicable
// hook buckets. (Note: label keys are not unique within the labels section).
//
// Files that do not parse into the expected format are simply placed into a map and // Files that do not parse into the expected format are simply placed into a map and
// returned. // returned.
func sortManifests(files map[string]string, apis chartutil.VersionSet, sort SortOrder) ([]*release.Hook, []manifest, error) { func sortManifests(files map[string]string, apis chartutil.VersionSet, sort SortOrder) ([]*release.Hook, []manifest, error) {
hs := []*release.Hook{} result := &result{}
generic := []manifest{}
for filePath, c := range files {
for n, c := range files {
// Skip partials. We could return these as a separate map, but there doesn't // Skip partials. We could return these as a separate map, but there doesn't
// seem to be any need for that at this time. // seem to be any need for that at this time.
if strings.HasPrefix(path.Base(n), "_") { if strings.HasPrefix(path.Base(filePath), "_") {
continue continue
} }
// Skip empty files, and log this. // Skip empty files and log this.
if len(strings.TrimSpace(c)) == 0 { if len(strings.TrimSpace(c)) == 0 {
log.Printf("info: manifest %q is empty. Skipping.", n) log.Printf("info: manifest %q is empty. Skipping.", filePath)
continue continue
} }
var sh util.SimpleHead manifestFile := &manifestFile{
err := yaml.Unmarshal([]byte(c), &sh) entries: util.SplitManifests(c),
path: filePath,
apis: apis,
}
if err := manifestFile.sort(result); err != nil {
return result.hooks, result.generic, err
}
}
return result.hooks, sortByKind(result.generic, sort), nil
}
// sort takes a manifestFile object which may contain multiple resource definition
// entries and sorts each entry by hook types, and saves the resulting hooks and
// generic manifests (or non-hooks) to the result struct.
//
// To determine hook type, it looks for a YAML structure like this:
//
// kind: SomeKind
// apiVersion: v1
// metadata:
// annotations:
// helm.sh/hook: pre-install
//
func (file *manifestFile) sort(result *result) error {
for _, m := range file.entries {
var entry util.SimpleHead
err := yaml.Unmarshal([]byte(m), &entry)
if err != nil { if err != nil {
e := fmt.Errorf("YAML parse error on %s: %s", n, err) e := fmt.Errorf("YAML parse error on %s: %s", file.path, err)
return hs, generic, e return e
} }
if sh.Version != "" && !apis.Has(sh.Version) { if entry.Version != "" && !file.apis.Has(entry.Version) {
return hs, generic, fmt.Errorf("apiVersion %q in %s is not available", sh.Version, n) return fmt.Errorf("apiVersion %q in %s is not available", entry.Version, file.path)
} }
if sh.Metadata == nil || sh.Metadata.Annotations == nil || len(sh.Metadata.Annotations) == 0 { if !hasAnyAnnotation(entry) {
generic = append(generic, manifest{name: n, content: c, head: &sh}) result.generic = append(result.generic, manifest{
name: file.path,
content: m,
head: &entry,
})
continue continue
} }
hookTypes, ok := sh.Metadata.Annotations[hooks.HookAnno] hookTypes, ok := entry.Metadata.Annotations[hooks.HookAnno]
if !ok { if !ok {
generic = append(generic, manifest{name: n, content: c, head: &sh}) result.generic = append(result.generic, manifest{
name: file.path,
content: m,
head: &entry,
})
continue continue
} }
hws, _ := sh.Metadata.Annotations[hooks.HookWeightAnno] hw := calculateHookWeight(entry)
hw, err := strconv.Atoi(hws)
if err != nil {
hw = 0
}
h := &release.Hook{ h := &release.Hook{
Name: sh.Metadata.Name, Name: entry.Metadata.Name,
Kind: sh.Kind, Kind: entry.Kind,
Path: n, Path: file.path,
Manifest: c, Manifest: m,
Events: []release.Hook_Event{}, Events: []release.Hook_Event{},
Weight: int32(hw), Weight: hw,
} }
isHook := false isKnownHook := false
for _, hookType := range strings.Split(hookTypes, ",") { for _, hookType := range strings.Split(hookTypes, ",") {
hookType = strings.ToLower(strings.TrimSpace(hookType)) hookType = strings.ToLower(strings.TrimSpace(hookType))
e, ok := events[hookType] e, ok := events[hookType]
if ok { if ok {
isHook = true isKnownHook = true
h.Events = append(h.Events, e) h.Events = append(h.Events, e)
} }
} }
if !isHook { if !isKnownHook {
log.Printf("info: skipping unknown hook: %q", hookTypes) log.Printf("info: skipping unknown hook: %q", hookTypes)
continue continue
} }
hs = append(hs, h)
result.hooks = append(result.hooks, h)
}
return nil
}
func hasAnyAnnotation(entry util.SimpleHead) bool {
if entry.Metadata == nil ||
entry.Metadata.Annotations == nil ||
len(entry.Metadata.Annotations) == 0 {
return false
} }
return hs, sortByKind(generic, sort), nil
return true
}
func calculateHookWeight(entry util.SimpleHead) int32 {
hws, _ := entry.Metadata.Annotations[hooks.HookWeightAnno]
hw, err := strconv.Atoi(hws)
if err != nil {
hw = 0
}
return int32(hw)
} }

@ -17,6 +17,7 @@ limitations under the License.
package tiller package tiller
import ( import (
"reflect"
"testing" "testing"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
@ -29,17 +30,17 @@ import (
func TestSortManifests(t *testing.T) { func TestSortManifests(t *testing.T) {
data := []struct { data := []struct {
name string name []string
path string path string
kind string kind []string
hooks []release.Hook_Event hooks map[string][]release.Hook_Event
manifest string manifest string
}{ }{
{ {
name: "first", name: []string{"first"},
path: "one", path: "one",
kind: "Job", kind: []string{"Job"},
hooks: []release.Hook_Event{release.Hook_PRE_INSTALL}, hooks: map[string][]release.Hook_Event{"first": {release.Hook_PRE_INSTALL}},
manifest: `apiVersion: v1 manifest: `apiVersion: v1
kind: Job kind: Job
metadata: metadata:
@ -51,10 +52,10 @@ metadata:
`, `,
}, },
{ {
name: "second", name: []string{"second"},
path: "two", path: "two",
kind: "ReplicaSet", kind: []string{"ReplicaSet"},
hooks: []release.Hook_Event{release.Hook_POST_INSTALL}, hooks: map[string][]release.Hook_Event{"second": {release.Hook_POST_INSTALL}},
manifest: `kind: ReplicaSet manifest: `kind: ReplicaSet
apiVersion: v1beta1 apiVersion: v1beta1
metadata: metadata:
@ -63,10 +64,10 @@ metadata:
"helm.sh/hook": post-install "helm.sh/hook": post-install
`, `,
}, { }, {
name: "third", name: []string{"third"},
path: "three", path: "three",
kind: "ReplicaSet", kind: []string{"ReplicaSet"},
hooks: []release.Hook_Event{}, hooks: map[string][]release.Hook_Event{"third": nil},
manifest: `kind: ReplicaSet manifest: `kind: ReplicaSet
apiVersion: v1beta1 apiVersion: v1beta1
metadata: metadata:
@ -75,22 +76,21 @@ metadata:
"helm.sh/hook": no-such-hook "helm.sh/hook": no-such-hook
`, `,
}, { }, {
name: "fourth", name: []string{"fourth"},
path: "four", path: "four",
kind: "Pod", kind: []string{"Pod"},
hooks: []release.Hook_Event{}, hooks: map[string][]release.Hook_Event{"fourth": nil},
manifest: `kind: Pod manifest: `kind: Pod
apiVersion: v1 apiVersion: v1
metadata: metadata:
name: fourth name: fourth
annotations: annotations:
nothing: here nothing: here`,
`,
}, { }, {
name: "fifth", name: []string{"fifth"},
path: "five", path: "five",
kind: "ReplicaSet", kind: []string{"ReplicaSet"},
hooks: []release.Hook_Event{release.Hook_POST_DELETE, release.Hook_POST_INSTALL}, hooks: map[string][]release.Hook_Event{"fifth": {release.Hook_POST_DELETE, release.Hook_POST_INSTALL}},
manifest: `kind: ReplicaSet manifest: `kind: ReplicaSet
apiVersion: v1beta1 apiVersion: v1beta1
metadata: metadata:
@ -100,19 +100,39 @@ metadata:
`, `,
}, { }, {
// Regression test: files with an underscore in the base name should be skipped. // Regression test: files with an underscore in the base name should be skipped.
name: "sixth", name: []string{"sixth"},
path: "six/_six", path: "six/_six",
kind: "ReplicaSet", kind: []string{"ReplicaSet"},
hooks: []release.Hook_Event{}, hooks: map[string][]release.Hook_Event{"sixth": nil},
manifest: `invalid manifest`, // This will fail if partial is not skipped. manifest: `invalid manifest`, // This will fail if partial is not skipped.
}, { }, {
// Regression test: files with no content should be skipped. // Regression test: files with no content should be skipped.
name: "seventh", name: []string{"seventh"},
path: "seven", path: "seven",
kind: "ReplicaSet", kind: []string{"ReplicaSet"},
hooks: []release.Hook_Event{}, hooks: map[string][]release.Hook_Event{"seventh": nil},
manifest: "", manifest: "",
}, },
{
name: []string{"eighth", "example-test"},
path: "eight",
kind: []string{"ConfigMap", "Pod"},
hooks: map[string][]release.Hook_Event{"eighth": nil, "example-test": {release.Hook_RELEASE_TEST_SUCCESS}},
manifest: `kind: ConfigMap
apiVersion: v1
metadata:
name: eighth
data:
name: value
---
apiVersion: v1
kind: Pod
metadata:
name: example-test
annotations:
"helm.sh/hook": test-success
`,
},
} }
manifests := make(map[string]string, len(data)) manifests := make(map[string]string, len(data))
@ -126,12 +146,12 @@ metadata:
} }
// This test will fail if 'six' or 'seven' was added. // This test will fail if 'six' or 'seven' was added.
if len(generic) != 1 { if len(generic) != 2 {
t.Errorf("Expected 1 generic manifest, got %d", len(generic)) t.Errorf("Expected 2 generic manifests, got %d", len(generic))
} }
if len(hs) != 3 { if len(hs) != 4 {
t.Errorf("Expected 3 hooks, got %d", len(hs)) t.Errorf("Expected 4 hooks, got %d", len(hs))
} }
for _, out := range hs { for _, out := range hs {
@ -142,17 +162,30 @@ metadata:
if out.Path != expect.path { if out.Path != expect.path {
t.Errorf("Expected path %s, got %s", expect.path, out.Path) t.Errorf("Expected path %s, got %s", expect.path, out.Path)
} }
if out.Name != expect.name { nameFound := false
t.Errorf("Expected name %s, got %s", expect.name, out.Name) for _, expectedName := range expect.name {
if out.Name == expectedName {
nameFound = true
}
} }
if out.Kind != expect.kind { if !nameFound {
t.Errorf("Expected kind %s, got %s", expect.kind, out.Kind) t.Errorf("Got unexpected name %s", out.Name)
} }
for i := 0; i < len(out.Events); i++ { kindFound := false
if out.Events[i] != expect.hooks[i] { for _, expectedKind := range expect.kind {
t.Errorf("Expected event %d, got %d", expect.hooks[i], out.Events[i]) if out.Kind == expectedKind {
kindFound = true
} }
} }
if !kindFound {
t.Errorf("Got unexpected kind %s", out.Kind)
}
expectedHooks := expect.hooks[out.Name]
if !reflect.DeepEqual(expectedHooks, out.Events) {
t.Errorf("expected events: %v but got: %v", expectedHooks, out.Events)
}
} }
} }
if !found { if !found {
@ -161,27 +194,40 @@ metadata:
} }
// Verify the sort order // Verify the sort order
sorted := make([]manifest, len(data)) sorted := []manifest{}
for i, s := range data { for _, s := range data {
var sh util.SimpleHead manifests := util.SplitManifests(s.manifest)
err := yaml.Unmarshal([]byte(s.manifest), &sh) mCount := 0
if err != nil { for _, m := range manifests {
// This is expected for manifests that are corrupt or empty. name := s.name[mCount]
t.Log(err)
} var sh util.SimpleHead
sorted[i] = manifest{ err := yaml.Unmarshal([]byte(m), &sh)
content: s.manifest, if err != nil {
name: s.name, // This is expected for manifests that are corrupt or empty.
head: &sh, t.Log(err)
}
//only keep track of non-hook manifests
if err == nil && s.hooks[name] == nil {
another := manifest{
content: m,
name: name,
head: &sh,
}
sorted = append(sorted, another)
}
mCount++
} }
} }
sorted = sortByKind(sorted, InstallOrder) sorted = sortByKind(sorted, InstallOrder)
for i, m := range generic { for i, m := range generic {
if m.content != sorted[i].content { if m.content != sorted[i].content {
t.Errorf("Expected %q, got %q", m.content, sorted[i].content) t.Errorf("Expected %q, got %q", m.content, sorted[i].content)
} }
} }
} }
func TestVersionSet(t *testing.T) { func TestVersionSet(t *testing.T) {

@ -47,8 +47,7 @@ metadata:
annotations: annotations:
"helm.sh/hook": post-install,pre-delete "helm.sh/hook": post-install,pre-delete
data: data:
name: value name: value`
`
var manifestWithTestHook = ` var manifestWithTestHook = `
apiVersion: v1 apiVersion: v1
@ -81,8 +80,7 @@ metadata:
annotations: annotations:
"helm.sh/hook": post-upgrade,pre-upgrade "helm.sh/hook": post-upgrade,pre-upgrade
data: data:
name: value name: value`
`
var manifestWithRollbackHooks = `apiVersion: v1 var manifestWithRollbackHooks = `apiVersion: v1
kind: ConfigMap kind: ConfigMap

Loading…
Cancel
Save