Merge pull request #43 from nmaki/feature/custom_helm_command

allowing configurable helm_command
This commit is contained in:
Ivan Pedrazas
2018-03-13 20:12:41 +00:00
committed by GitHub
3 changed files with 74 additions and 45 deletions
+2 -2
View File
@@ -18,7 +18,7 @@ func main() {
app.Action = run
app.Version = fmt.Sprintf("1.0.%s", build)
app.Flags = []cli.Flag{
cli.StringSliceFlag{
cli.StringFlag{
Name: "helm_command",
Usage: "add the command Helm has to execute",
EnvVar: "PLUGIN_HELM_COMMAND,HELM_COMMAND",
@@ -145,7 +145,7 @@ func run(c *cli.Context) error {
Token: c.String("token"),
ServiceAccount: c.String("service-account"),
KubeConfig: c.String("kube-config"),
HelmCommand: c.StringSlice("helm_command"),
HelmCommand: c.String("helm_command"),
Namespace: c.String("namespace"),
SkipTLSVerify: c.Bool("skip_tls_verify"),
Values: c.String("values"),
+28 -24
View File
@@ -23,7 +23,7 @@ type (
Token string `json:"token"`
ServiceAccount string `json:"service_account"`
KubeConfig string `json:"kube_config"`
HelmCommand []string `json:"helm_command"`
HelmCommand string `json:"helm_command"`
SkipTLSVerify bool `json:"tls_skip_verify"`
Namespace string `json:"namespace"`
Release string `json:"release"`
@@ -48,22 +48,23 @@ type (
}
// Plugin default
Plugin struct {
Config Config
Config Config
command []string
}
)
func setHelmHelp(p *Plugin) {
p.Config.HelmCommand = []string{""}
func setHelpCommand(p *Plugin) {
p.command = []string{""}
}
func setDeleteEventCommand(p *Plugin) {
upgrade := make([]string, 2)
upgrade[0] = "delete"
upgrade[1] = p.Config.Release
func setDeleteCommand(p *Plugin) {
delete := make([]string, 2)
delete[0] = "delete"
delete[1] = p.Config.Release
p.Config.HelmCommand = upgrade
p.command = delete
}
func setPushEventCommand(p *Plugin) {
func setUpgradeCommand(p *Plugin) {
upgrade := make([]string, 2)
upgrade[0] = "upgrade"
upgrade[1] = "--install"
@@ -116,23 +117,26 @@ func setPushEventCommand(p *Plugin) {
if p.Config.Force {
upgrade = append(upgrade, "--force")
}
p.Config.HelmCommand = upgrade
p.command = upgrade
}
func setHelmCommand(p *Plugin) {
buildEvent := os.Getenv("DRONE_BUILD_EVENT")
switch buildEvent {
case "push":
setPushEventCommand(p)
case "tag":
setPushEventCommand(p)
case "deployment":
setPushEventCommand(p)
switch p.Config.HelmCommand {
case "upgrade":
setUpgradeCommand(p)
case "delete":
setDeleteEventCommand(p)
setDeleteCommand(p)
default:
setHelmHelp(p)
switch os.Getenv("DRONE_BUILD_EVENT") {
case "push", "tag", "deployment":
setUpgradeCommand(p)
case "delete":
setDeleteCommand(p)
default:
setHelpCommand(p)
}
}
}
@@ -238,12 +242,12 @@ func (p *Plugin) Exec() error {
setHelmCommand(p)
if p.Config.Debug {
log.Println("helm command: " + strings.Join(p.Config.HelmCommand[:], " "))
log.Println("helm command: " + strings.Join(p.command, " "))
}
err = runCommand(p.Config.HelmCommand)
err = runCommand(p.command)
if err != nil {
return fmt.Errorf("Error running helm command: " + strings.Join(p.Config.HelmCommand[:], " "))
return fmt.Errorf("Error running helm command: " + strings.Join(p.command[:], " "))
}
return nil
+44 -19
View File
@@ -10,16 +10,12 @@ import (
func TestInitialiseKubeconfig(t *testing.T) {
cmd := make([]string, 2)
cmd[0] = "install"
cmd[1] = "--debug"
plugin := Plugin{
Config: Config{
APIServer: "http://myapiserver",
Token: "secret-token",
ServiceAccount: "default-account",
HelmCommand: cmd,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
},
@@ -45,13 +41,13 @@ func TestInitialiseKubeconfig(t *testing.T) {
}
func TestGetHelmCommand(t *testing.T) {
func TestGetHelmCommandEmptyPushEvent(t *testing.T) {
os.Setenv("DRONE_BUILD_EVENT", "push")
plugin := &Plugin{
Config: Config{
APIServer: "http://myapiserver",
Token: "secret-token",
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -67,7 +63,36 @@ func TestGetHelmCommand(t *testing.T) {
},
}
setHelmCommand(plugin)
res := strings.Join(plugin.Config.HelmCommand[:], " ")
res := strings.Join(plugin.command[:], " ")
expected := "upgrade --install test-release ./chart/test --version 1.2.3 --set image.tag=v.0.1.0,nameOverride=my-over-app --namespace default --dry-run --debug --wait --reuse-values --timeout 500 --force"
if res != expected {
t.Errorf("Result is %s and we expected %s", res, expected)
}
}
func TestGetHelmCommandUpgrade(t *testing.T) {
os.Setenv("DRONE_BUILD_EVENT", "push")
plugin := &Plugin{
Config: Config{
APIServer: "http://myapiserver",
Token: "secret-token",
HelmCommand: "upgrade",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
DryRun: true,
Chart: "./chart/test",
Version: "1.2.3",
Release: "test-release",
Values: `"image.tag=v.0.1.0,nameOverride=my-over-app"`,
Wait: true,
ReuseValues: true,
Timeout: "500",
Force: true,
},
}
setHelmCommand(plugin)
res := strings.Join(plugin.command[:], " ")
expected := "upgrade --install test-release ./chart/test --version 1.2.3 --set image.tag=v.0.1.0,nameOverride=my-over-app --namespace default --dry-run --debug --wait --reuse-values --timeout 500 --force"
if res != expected {
t.Errorf("Result is %s and we expected %s", res, expected)
@@ -100,7 +125,7 @@ func TestResolveSecrets(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -142,7 +167,7 @@ func TestResolveSecrets(t *testing.T) {
func TestDetHelmRepoAdd(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -210,10 +235,10 @@ func TestReplaceEnvvars(t *testing.T) {
}
}
func TestSetHelmHelp(t *testing.T) {
func TestSetHelpCommand(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -224,8 +249,8 @@ func TestSetHelmHelp(t *testing.T) {
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
},
}
setHelmHelp(plugin)
if plugin.Config.HelmCommand == nil {
setHelpCommand(plugin)
if plugin.command == nil {
t.Error("Helm help is not displayed")
}
}
@@ -233,7 +258,7 @@ func TestSetHelmHelp(t *testing.T) {
func TestDetHelmInit(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -257,7 +282,7 @@ func TestDetHelmInit(t *testing.T) {
func TestDetHelmInitClient(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -284,7 +309,7 @@ func TestDetHelmInitClient(t *testing.T) {
func TestDetHelmInitUpgrade(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -311,7 +336,7 @@ func TestDetHelmInitUpgrade(t *testing.T) {
func TestDetHelmInitCanary(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
@@ -344,7 +369,7 @@ func TestResolveSecretsFallback(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
HelmCommand: "",
Namespace: "default",
SkipTLSVerify: true,
Debug: true,