forked from roboll/helmfile
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
prepare
and cleanup
release event hooks
Resolves roboll#295 Resolves roboll#330 Resolves roboll#329 (Supports templating of only `releases[].hooks[].command` and `args` right now Resolves roboll#324
- Loading branch information
Showing
7 changed files
with
433 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
"github.com/roboll/helmfile/environment" | ||
"github.com/roboll/helmfile/helmexec" | ||
"github.com/roboll/helmfile/tmpl" | ||
"go.uber.org/zap" | ||
"os" | ||
) | ||
|
||
type Hook struct { | ||
Name string `yaml:"name"` | ||
Events []string `yaml:"events"` | ||
Command string `yaml:"command"` | ||
Args []string `yaml:"args"` | ||
} | ||
|
||
type event struct { | ||
Name string | ||
} | ||
|
||
type Bus struct { | ||
Runner helmexec.Runner | ||
Hooks []Hook | ||
|
||
BasePath string | ||
StateFilePath string | ||
Namespace string | ||
|
||
Env environment.Environment | ||
|
||
ReadFile func(string) ([]byte, error) | ||
Logger *zap.SugaredLogger | ||
} | ||
|
||
func (bus *Bus) Trigger(evt string, context map[string]interface{}) (bool, error) { | ||
if bus.Runner == nil { | ||
bus.Runner = helmexec.ShellRunner{ | ||
Dir: bus.BasePath, | ||
} | ||
} | ||
|
||
executed := false | ||
|
||
for _, hook := range bus.Hooks { | ||
contained := false | ||
for _, e := range hook.Events { | ||
contained = contained || e == evt | ||
} | ||
if !contained { | ||
continue | ||
} | ||
|
||
var err error | ||
|
||
name := hook.Name | ||
if name == "" { | ||
name = hook.Command | ||
} | ||
|
||
fmt.Fprintf(os.Stderr, "%s: basePath=%s\n", bus.StateFilePath, bus.BasePath) | ||
|
||
data := map[string]interface{}{ | ||
"Environment": bus.Env, | ||
"Namespace": bus.Namespace, | ||
"Event": event{ | ||
Name: evt, | ||
}, | ||
} | ||
for k, v := range context { | ||
data[k] = v | ||
} | ||
render := tmpl.NewTextRenderer(bus.ReadFile, bus.BasePath, data) | ||
|
||
bus.Logger.Debugf("hook[%s]: triggered by event \"%s\"\n", name, evt) | ||
|
||
command, err := render.RenderTemplateText(hook.Command) | ||
if err != nil { | ||
return false, fmt.Errorf("hook[%s]: %v", name, err) | ||
} | ||
|
||
args := make([]string, len(hook.Args)) | ||
for i, raw := range hook.Args { | ||
args[i], err = render.RenderTemplateText(raw) | ||
if err != nil { | ||
return false, fmt.Errorf("hook[%s]: %v", name, err) | ||
} | ||
} | ||
|
||
bytes, err := bus.Runner.Execute(command, args) | ||
bus.Logger.Debugf("hook[%s]: %s\n", name, string(bytes)) | ||
if err != nil { | ||
return false, fmt.Errorf("hook[%s]: command `%s` failed: %v", name, command, err) | ||
} | ||
|
||
executed = true | ||
} | ||
|
||
return executed, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
"github.com/roboll/helmfile/environment" | ||
"github.com/roboll/helmfile/helmexec" | ||
"os" | ||
"testing" | ||
) | ||
|
||
var logger = helmexec.NewLogger(os.Stdout, "warn") | ||
|
||
type runner struct { | ||
} | ||
|
||
func (r *runner) Execute(cmd string, args []string) ([]byte, error) { | ||
if cmd == "ng" { | ||
return nil, fmt.Errorf("cmd failed due to invalid cmd: %s", cmd) | ||
} | ||
for _, a := range args { | ||
if a == "ng" { | ||
return nil, fmt.Errorf("cmd failed due to invalid arg: %s", a) | ||
} | ||
} | ||
return []byte(""), nil | ||
} | ||
|
||
func TestTrigger(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
hook *Hook | ||
triggeredEvt string | ||
expectedResult bool | ||
expectedErr string | ||
}{ | ||
{ | ||
"okhook1", | ||
&Hook{"okhook1", []string{"foo"}, "ok", []string{}}, | ||
"foo", | ||
true, | ||
"", | ||
}, | ||
{ | ||
"missinghook1", | ||
&Hook{"okhook1", []string{"foo"}, "ok", []string{}}, | ||
"bar", | ||
false, | ||
"", | ||
}, | ||
{ | ||
"nohook1", | ||
nil, | ||
"bar", | ||
false, | ||
"", | ||
}, | ||
{ | ||
"nghook1", | ||
&Hook{"nghook1", []string{"foo"}, "ng", []string{}}, | ||
"foo", | ||
false, | ||
"hook[nghook1]: command `ng` failed: cmd failed due to invalid cmd: ng", | ||
}, | ||
{ | ||
"nghook2", | ||
&Hook{"nghook2", []string{"foo"}, "ok", []string{"ng"}}, | ||
"foo", | ||
false, | ||
"hook[nghook2]: command `ok` failed: cmd failed due to invalid arg: ng", | ||
}, | ||
} | ||
readFile := func(filename string) ([]byte, error) { | ||
return nil, fmt.Errorf("unexpected call to readFile: %s", filename) | ||
} | ||
for _, c := range cases { | ||
hooks := []Hook{} | ||
if c.hook != nil { | ||
hooks = append(hooks, *c.hook) | ||
} | ||
bus := &Bus{ | ||
Hooks: hooks, | ||
StateFilePath: "path/to/helmfile.yaml", | ||
BasePath: "path/to", | ||
Namespace: "myns", | ||
Env: environment.Environment{Name: "prod"}, | ||
Logger: logger, | ||
ReadFile: readFile, | ||
} | ||
|
||
bus.Runner = &runner{} | ||
data := map[string]interface{}{ | ||
"Release": "myrel", | ||
"HelmfileCommand": "mycmd", | ||
} | ||
ok, err := bus.Trigger(c.triggeredEvt, data) | ||
|
||
if ok != c.expectedResult { | ||
t.Errorf("unexpected result for case \"%s\": expected=%v, actual=%v", c.name, c.expectedResult, ok) | ||
} | ||
|
||
if c.expectedErr != "" { | ||
if err == nil { | ||
t.Error("error expected, but not occurred") | ||
} else if err.Error() != c.expectedErr { | ||
t.Errorf("unexpected error for case \"%s\": expected=%s, actual=%v", c.name, c.expectedErr, err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("unexpected error for case \"%s\": %v", c.name, err) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.