Skip to content

Commit

Permalink
Merge pull request #70 from mgar/set_string_flag
Browse files Browse the repository at this point in the history
Set string flag
  • Loading branch information
ipedrazas authored Sep 5, 2018
2 parents 057547a + 7fb4781 commit 8e7b0b3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ docker run --rm \
-e PLUGIN_RELEASE=my-release \
-e PLUGIN_CHART=stable/redis \
-e PLUGIN_VALUES="tag=TAG,api=API" \
-e PLUGIN_STRING_VALUES="long_string_value=1234567890" \
-e PLUGIN_DEBUG=true \
-e PLUGIN_DRY_RUN=true \
-e DRONE_BUILD_EVENT=push \
Expand All @@ -39,5 +40,4 @@ This repo is setup in a way that if you enable a personal drone server to build

* Build local ```DRONE_REPO_OWNER=ipedrazas DRONE_REPO_NAME=drone-helm drone exec```
* on your server just make sure you have DOCKER_USERNAME, DOCKER_PASSWORD, and PLUGIN_REPO set as secrets



14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"fmt"
"os"

"github.com/Sirupsen/logrus"
"github.com/ipedrazas/drone-helm/plugin"
"github.com/joho/godotenv"
"github.com/josmo/drone-helm/plugin"
"github.com/urfave/cli"
"os"
)

var (
Expand All @@ -19,7 +20,7 @@ func main() {
app.Name = "helm plugin"
app.Usage = "helm plugin"
app.Action = run
app.Version = fmt.Sprintf("%s+%s",version, build)
app.Version = fmt.Sprintf("%s+%s", version, build)
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "helm_command",
Expand Down Expand Up @@ -62,6 +63,11 @@ func main() {
Usage: "Kubernetes helm release",
EnvVar: "PLUGIN_VALUES,VALUES",
},
cli.StringFlag{
Name: "string_values",
Usage: "Kubernetes helm release",
EnvVar: "PLUGIN_STRING_VALUES,STRING_VALUES",
},
cli.StringFlag{
Name: "values_files",
Usage: "Helm values override files",
Expand Down Expand Up @@ -160,10 +166,10 @@ func run(c *cli.Context) error {
ServiceAccount: c.String("service-account"),
KubeConfig: c.String("kube-config"),
HelmCommand: c.String("helm_command"),
Purge: c.Bool("purge"),
Namespace: c.String("namespace"),
SkipTLSVerify: c.Bool("skip_tls_verify"),
Values: c.String("values"),
StringValues: c.String("string_values"),
ValuesFiles: c.String("values_files"),
Release: c.String("release"),
HelmRepos: c.StringSlice("helm_repos"),
Expand Down
8 changes: 8 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type (
Chart string `json:"chart"`
Version string `json:"version"`
Values string `json:"values"`
StringValues string `json:"string_values"`
ValuesFiles string `json:"values_files"`
Debug bool `json:"debug"`
DryRun bool `json:"dry_run"`
Expand Down Expand Up @@ -95,6 +96,10 @@ func setUpgradeCommand(p *Plugin) {
upgrade = append(upgrade, "--set")
upgrade = append(upgrade, unQuote(p.Config.Values))
}
if p.Config.StringValues != "" {
upgrade = append(upgrade, "--set-string")
upgrade = append(upgrade, unQuote(p.Config.StringValues))
}
if p.Config.ValuesFiles != "" {
for _, valuesFile := range strings.Split(p.Config.ValuesFiles, ",") {
upgrade = append(upgrade, "--values")
Expand Down Expand Up @@ -307,6 +312,8 @@ func runCommand(params []string) error {

func resolveSecrets(p *Plugin) {
p.Config.Values = resolveEnvVar(p.Config.Values, p.Config.Prefix, p.Config.Debug)
p.Config.StringValues = resolveEnvVar(p.Config.StringValues, p.Config.Prefix, p.Config.Debug)

if p.Config.APIServer == "" {
p.Config.APIServer = resolveEnvVar("${API_SERVER}", p.Config.Prefix, p.Config.Debug)
}
Expand Down Expand Up @@ -380,6 +387,7 @@ func (p *Plugin) debug() {
// debug plugin obj
fmt.Printf("Api server: %s \n", p.Config.APIServer)
fmt.Printf("Values: %s \n", p.Config.Values)
fmt.Printf("StringValues: %s \n", p.Config.StringValues)
fmt.Printf("Secrets: %s \n", p.Config.Secrets)
fmt.Printf("Helm Repos: %s \n", p.Config.HelmRepos)
fmt.Printf("ValuesFiles: %s \n", p.Config.ValuesFiles)
Expand Down
31 changes: 24 additions & 7 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestGetHelmCommandEmptyPushEvent(t *testing.T) {
Version: "1.2.3",
Release: "test-release",
Values: `"image.tag=v.0.1.0,nameOverride=my-over-app"`,
StringValues: `"long_string_value=1234567890"`,
Wait: true,
ReuseValues: true,
Timeout: "500",
Expand All @@ -67,7 +68,7 @@ func TestGetHelmCommandEmptyPushEvent(t *testing.T) {
}
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"
expected := "upgrade --install test-release ./chart/test --version 1.2.3 --set image.tag=v.0.1.0,nameOverride=my-over-app --set-string long_string_value=1234567890 --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)
}
Expand All @@ -88,6 +89,7 @@ func TestGetHelmCommandUpgrade(t *testing.T) {
Version: "1.2.3",
Release: "test-release",
Values: `"image.tag=v.0.1.0,nameOverride=my-over-app"`,
StringValues: `"long_string_value=1234567890"`,
Wait: true,
ReuseValues: true,
Timeout: "500",
Expand All @@ -96,7 +98,7 @@ func TestGetHelmCommandUpgrade(t *testing.T) {
}
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"
expected := "upgrade --install test-release ./chart/test --version 1.2.3 --set image.tag=v.0.1.0,nameOverride=my-over-app --set-string long_string_value=1234567890 --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)
}
Expand All @@ -115,6 +117,7 @@ func TestGetHelmDeleteCommand(t *testing.T) {
Chart: "./chart/test",
Release: "test-release",
Values: "image.tag=v.0.1.0,nameOverride=my-over-app",
StringValues: "long_string_value=1234567890",
Wait: true,
},
}
Expand All @@ -141,6 +144,7 @@ func TestGetHelmDeleteCommandOverried(t *testing.T) {
Chart: "./chart/test",
Release: "test-release",
Values: "image.tag=v.0.1.0,nameOverride=my-over-app",
StringValues: "long_string_value=1234567890",
Wait: true,
Purge: true,
},
Expand Down Expand Up @@ -189,6 +193,7 @@ func TestResolveSecrets(t *testing.T) {
Release: "test-release",
Prefix: env.prefix,
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
},
}

Expand Down Expand Up @@ -220,12 +225,13 @@ func TestResolveSecrets(t *testing.T) {

// Test resolving provided values
testInput := []struct {
server string
values string
token string
account string
server string
values string
stringValues string
token string
account string
}{
{server: "http://apiserver2", token: "123456", account: "helm2", values: "aval=test"},
{server: "http://apiserver2", token: "123456", account: "helm2", values: "aval=test", stringValues: "long_string_value=1234567890"},
}
for _, input := range testInput {
plugin := &Plugin{
Expand All @@ -234,6 +240,7 @@ func TestResolveSecrets(t *testing.T) {
ServiceAccount: input.account,
Token: input.token,
Values: input.values,
StringValues: input.stringValues,
},
}

Expand All @@ -250,6 +257,9 @@ func TestResolveSecrets(t *testing.T) {
if plugin.Config.Values != input.values {
t.Errorf("failed to keep Values '%s' got '%s'", input.values, plugin.Config.Values)
}
if plugin.Config.StringValues != input.stringValues {
t.Errorf("failed to keep StringValues '%s' got '%s'", input.stringValues, plugin.Config.StringValues)
}
}
}

Expand All @@ -265,6 +275,7 @@ func TestDetHelmRepoAdd(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
ClientOnly: true,
HelmRepos: []string{
`"r1=http://r1.example.com"`, //handle quoted strings
Expand Down Expand Up @@ -346,6 +357,7 @@ func TestSetHelpCommand(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
},
}
setHelpCommand(plugin)
Expand All @@ -366,6 +378,7 @@ func TestDetHelmInit(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
TillerNs: "system-test",
},
}
Expand All @@ -390,6 +403,7 @@ func TestDetHelmInitClient(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
ClientOnly: true,
},
}
Expand Down Expand Up @@ -417,6 +431,7 @@ func TestDetHelmInitUpgrade(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
Upgrade: true,
},
}
Expand Down Expand Up @@ -444,6 +459,7 @@ func TestDetHelmInitCanary(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
CanaryImage: true,
},
}
Expand Down Expand Up @@ -477,6 +493,7 @@ func TestResolveSecretsFallback(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nottoken=${NOTTOKEN},nameOverride=my-over-app,second.tag=${TAG}",
StringValues: "long_string_value=1234567890",
},
}

Expand Down

0 comments on commit 8e7b0b3

Please sign in to comment.