18 Commits

Author SHA1 Message Date
Erin Call
6acad85bed Merge pull request #18 from pelotech/helm-lint
Run the `helm lint` command when HELM_COMMAND=lint
2019-12-19 11:33:01 -08:00
Erin Call
c033c8c45e Format the Lint struct non-weirdly [#3]
I thought it was a golang style convention to put a blank line between
public and private struct fields, but apparently I imagined that.
2019-12-19 11:09:39 -08:00
Erin Call
30e1e3b99f Assert that Lint.Prepare sets cmd.Stdout/Stderr [#3] 2019-12-19 10:26:33 -08:00
Erin Call
b93917c857 Use better expectations in lint_test [#3]
The tests need to allow calls to Stdout/Stderr so they don't get
"Unexpected call" errors from gomock, but these tests aren't meant to
assert that the calls actually happened. Using .AnyTimes allows 0 or
more calls.
2019-12-19 10:21:11 -08:00
Erin Call
84ac019838 Add the --namespace flag in Lint.Prepare [#3]
I don't know whether this is necessary; I'm just following drone-helm's
lead. At worst, helm will accept the flag, so it's at least *safe* to
include.
2019-12-18 10:38:33 -08:00
Erin Call
9b3616613b Merge pull request #17 from pelotech/no-todos
Remove the TODO list from README.md
2019-12-18 08:09:38 -08:00
Erin Call
7e24756ad8 Instantiate a Lint when cfg.Command is "lint" [#3] 2019-12-17 17:14:39 -08:00
Erin Call
a6a2d6e6a3 Require a nonempty chart in Lint.Prepare [#3] 2019-12-17 17:01:22 -08:00
Erin Call
a6b7e06bd2 Implement the debug flag in lint [#3] 2019-12-17 17:01:18 -08:00
Erin Call
51800c18d7 Implement the various values flags in lint [#3] 2019-12-17 17:01:11 -08:00
Erin Call
991bbf97b4 Create a Lint step [#3]
Still need global flags and checks for mandatory settings, but the basic
functionality is there.
2019-12-17 17:01:06 -08:00
Erin Call
f3039ee35d Merge branch 'master' into no-todos 2019-12-17 15:35:28 -08:00
Erin Call
09e4869b2c Merge pull request #2 from pelotech/flesh-out-upgrade
Build out the rest of the upgrade Step
2019-12-17 15:34:08 -08:00
Erin Call
c641c1a326 Remove the TODO list from README.md
Everything on the checklist has been converted to a github issue, so
it's redundant.
2019-12-17 15:23:42 -08:00
Erin Call
aa04830600 Populate DryRun when building an Upgrade step 2019-12-17 09:23:44 -08:00
Erin Call
c585d8a22b More TODOs in the README 2019-12-16 17:07:33 -08:00
Erin Call
1560c05100 Fail early if chart or release is missing 2019-12-16 17:02:56 -08:00
Erin Call
e4fa70239e Implement config flags for helm upgrade 2019-12-16 16:55:05 -08:00
7 changed files with 371 additions and 27 deletions

View File

@@ -1,15 +1,3 @@
# Drone plugin for Helm 3 # Drone plugin for Helm 3
TODO: Dissatisfied with this empty README? Consider grabbing [the "put stuff in the README" issue](https://github.com/pelotech/drone-helm3/issues/8)!
* [x] Make a `.drone.yml` that's sufficient for building an image
* [x] Make a `Dockerfile` that's sufficient for launching the built image
* [x] Make `cmd/drone-helm/main.go` actually invoke `helm`
* [x] Make `golint` part of the build process (and make it pass)
* [x] Implement debug output
* [x] Flesh out `helm upgrade` until it's capable of working
* [ ] Implement config settings for `upgrade`
* [ ] Implement `helm lint`
* [ ] Implement `helm delete`
* [ ] EKS support
* [ ] Change `.drone.yml` to use a real docker registry

View File

@@ -62,7 +62,7 @@ func determineSteps(cfg Config) *func(Config) []Step {
case "delete": case "delete":
panic("not implemented") panic("not implemented")
case "lint": case "lint":
panic("not implemented") return &lint
case "help": case "help":
return &help return &help
default: default:
@@ -106,6 +106,7 @@ var upgrade = func(cfg Config) []Step {
Chart: cfg.Chart, Chart: cfg.Chart,
Release: cfg.Release, Release: cfg.Release,
ChartVersion: cfg.ChartVersion, ChartVersion: cfg.ChartVersion,
DryRun: cfg.DryRun,
Wait: cfg.Wait, Wait: cfg.Wait,
ReuseValues: cfg.ReuseValues, ReuseValues: cfg.ReuseValues,
Timeout: cfg.Timeout, Timeout: cfg.Timeout,
@@ -115,6 +116,14 @@ var upgrade = func(cfg Config) []Step {
return steps return steps
} }
var lint = func(cfg Config) []Step {
lint := &run.Lint{
Chart: cfg.Chart,
}
return []Step{lint}
}
var help = func(cfg Config) []Step { var help = func(cfg Config) []Step {
help := &run.Help{} help := &run.Help{}
return []Step{help} return []Step{help}

View File

@@ -93,6 +93,7 @@ func (suite *PlanTestSuite) TestUpgrade() {
APIServer: "123.456.78.9", APIServer: "123.456.78.9",
ServiceAccount: "helmet", ServiceAccount: "helmet",
ChartVersion: "seventeen", ChartVersion: "seventeen",
DryRun: true,
Wait: true, Wait: true,
ReuseValues: true, ReuseValues: true,
Timeout: "go sit in the corner", Timeout: "go sit in the corner",
@@ -126,6 +127,7 @@ func (suite *PlanTestSuite) TestUpgrade() {
Chart: cfg.Chart, Chart: cfg.Chart,
Release: cfg.Release, Release: cfg.Release,
ChartVersion: cfg.ChartVersion, ChartVersion: cfg.ChartVersion,
DryRun: true,
Wait: cfg.Wait, Wait: cfg.Wait,
ReuseValues: cfg.ReuseValues, ReuseValues: cfg.ReuseValues,
Timeout: cfg.Timeout, Timeout: cfg.Timeout,
@@ -135,6 +137,20 @@ func (suite *PlanTestSuite) TestUpgrade() {
suite.Equal(expected, upgrade) suite.Equal(expected, upgrade)
} }
func (suite *PlanTestSuite) TestLint() {
cfg := Config{
Chart: "./flow",
}
steps := lint(cfg)
suite.Equal(1, len(steps))
want := &run.Lint{
Chart: "./flow",
}
suite.Equal(want, steps[0])
}
func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() { func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
cfg := Config{ cfg := Config{
Command: "upgrade", Command: "upgrade",
@@ -154,6 +170,15 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
} }
} }
func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
cfg := Config{
Command: "lint",
}
stepsMaker := determineSteps(cfg)
suite.Same(&lint, stepsMaker)
}
func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() { func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
cfg := Config{ cfg := Config{
Command: "help", Command: "help",

56
internal/run/lint.go Normal file
View File

@@ -0,0 +1,56 @@
package run
import (
"fmt"
)
// Lint is an execution step that calls `helm lint` when executed.
type Lint struct {
Chart string
cmd cmd
}
// Execute executes the `helm lint` command.
func (l *Lint) Execute(_ Config) error {
return l.cmd.Run()
}
// Prepare gets the Lint ready to execute.
func (l *Lint) Prepare(cfg Config) error {
if l.Chart == "" {
return fmt.Errorf("chart is required")
}
args := make([]string, 0)
if cfg.Namespace != "" {
args = append(args, "--namespace", cfg.Namespace)
}
if cfg.Debug {
args = append(args, "--debug")
}
args = append(args, "lint")
if cfg.Values != "" {
args = append(args, "--set", cfg.Values)
}
if cfg.StringValues != "" {
args = append(args, "--set-string", cfg.StringValues)
}
for _, vFile := range cfg.ValuesFiles {
args = append(args, "--values", vFile)
}
args = append(args, l.Chart)
l.cmd = command(helmBin, args...)
l.cmd.Stdout(cfg.Stdout)
l.cmd.Stderr(cfg.Stderr)
if cfg.Debug {
fmt.Fprintf(cfg.Stderr, "Generated command: '%s'\n", l.cmd.String())
}
return nil
}

169
internal/run/lint_test.go Normal file
View File

@@ -0,0 +1,169 @@
package run
import (
"fmt"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"strings"
"testing"
)
type LintTestSuite struct {
suite.Suite
ctrl *gomock.Controller
mockCmd *Mockcmd
originalCommand func(string, ...string) cmd
}
func (suite *LintTestSuite) BeforeTest(_, _ string) {
suite.ctrl = gomock.NewController(suite.T())
suite.mockCmd = NewMockcmd(suite.ctrl)
suite.originalCommand = command
command = func(path string, args ...string) cmd { return suite.mockCmd }
}
func (suite *LintTestSuite) AfterTest(_, _ string) {
command = suite.originalCommand
}
func TestLintTestSuite(t *testing.T) {
suite.Run(t, new(LintTestSuite))
}
func (suite *LintTestSuite) TestPrepareAndExecute() {
defer suite.ctrl.Finish()
stdout := strings.Builder{}
stderr := strings.Builder{}
l := Lint{
Chart: "./epic/mychart",
}
cfg := Config{
Stdout: &stdout,
Stderr: &stderr,
}
command = func(path string, args ...string) cmd {
suite.Equal(helmBin, path)
suite.Equal([]string{"lint", "./epic/mychart"}, args)
return suite.mockCmd
}
suite.mockCmd.EXPECT().
Stdout(&stdout)
suite.mockCmd.EXPECT().
Stderr(&stderr)
suite.mockCmd.EXPECT().
Run().
Times(1)
err := l.Prepare(cfg)
suite.Require().Nil(err)
l.Execute(cfg)
}
func (suite *LintTestSuite) TestPrepareRequiresChart() {
// These aren't really expected, but allowing them gives clearer test-failure messages
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
cfg := Config{}
l := Lint{}
err := l.Prepare(cfg)
suite.EqualError(err, "chart is required", "Chart should be mandatory")
}
func (suite *LintTestSuite) TestPrepareWithLintFlags() {
defer suite.ctrl.Finish()
cfg := Config{
Values: "width=5",
StringValues: "version=2.0",
ValuesFiles: []string{"/usr/local/underrides", "/usr/local/overrides"},
}
l := Lint{
Chart: "./uk/top_40",
}
command = func(path string, args ...string) cmd {
suite.Equal(helmBin, path)
suite.Equal([]string{"lint",
"--set", "width=5",
"--set-string", "version=2.0",
"--values", "/usr/local/underrides",
"--values", "/usr/local/overrides",
"./uk/top_40"}, args)
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
err := l.Prepare(cfg)
suite.Require().Nil(err)
}
func (suite *LintTestSuite) TestPrepareWithDebugFlag() {
defer suite.ctrl.Finish()
stderr := strings.Builder{}
cfg := Config{
Debug: true,
Stderr: &stderr,
}
l := Lint{
Chart: "./scotland/top_40",
}
command = func(path string, args ...string) cmd {
suite.mockCmd.EXPECT().
String().
Return(fmt.Sprintf("%s %s", path, strings.Join(args, " ")))
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any())
suite.mockCmd.EXPECT().Stderr(&stderr)
err := l.Prepare(cfg)
suite.Require().Nil(err)
want := fmt.Sprintf("Generated command: '%s --debug lint ./scotland/top_40'\n", helmBin)
suite.Equal(want, stderr.String())
}
func (suite *LintTestSuite) TestPrepareWithNamespaceFlag() {
defer suite.ctrl.Finish()
cfg := Config{
Namespace: "table-service",
}
l := Lint{
Chart: "./wales/top_40",
}
actual := []string{}
command = func(path string, args ...string) cmd {
actual = args
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
err := l.Prepare(cfg)
suite.Require().Nil(err)
expected := []string{"--namespace", "table-service", "lint", "./wales/top_40"}
suite.Equal(expected, actual)
}

View File

@@ -10,6 +10,7 @@ type Upgrade struct {
Release string Release string
ChartVersion string ChartVersion string
DryRun bool
Wait bool Wait bool
ReuseValues bool ReuseValues bool
Timeout string Timeout string
@@ -25,18 +26,53 @@ func (u *Upgrade) Execute(_ Config) error {
// Prepare gets the Upgrade ready to execute. // Prepare gets the Upgrade ready to execute.
func (u *Upgrade) Prepare(cfg Config) error { func (u *Upgrade) Prepare(cfg Config) error {
if u.Chart == "" {
return fmt.Errorf("chart is required")
}
if u.Release == "" {
return fmt.Errorf("release is required")
}
args := []string{"--kubeconfig", cfg.KubeConfig} args := []string{"--kubeconfig", cfg.KubeConfig}
if cfg.Namespace != "" { if cfg.Namespace != "" {
args = append(args, "--namespace", cfg.Namespace) args = append(args, "--namespace", cfg.Namespace)
} }
args = append(args, "upgrade", "--install", u.Release, u.Chart)
if cfg.Debug { if cfg.Debug {
args = append([]string{"--debug"}, args...) args = append(args, "--debug")
} }
args = append(args, "upgrade", "--install")
if u.ChartVersion != "" {
args = append(args, "--version", u.ChartVersion)
}
if u.DryRun {
args = append(args, "--dry-run")
}
if u.Wait {
args = append(args, "--wait")
}
if u.ReuseValues {
args = append(args, "--reuse-values")
}
if u.Timeout != "" {
args = append(args, "--timeout", u.Timeout)
}
if u.Force {
args = append(args, "--force")
}
if cfg.Values != "" {
args = append(args, "--set", cfg.Values)
}
if cfg.StringValues != "" {
args = append(args, "--set-string", cfg.StringValues)
}
for _, vFile := range cfg.ValuesFiles {
args = append(args, "--values", vFile)
}
args = append(args, u.Release, u.Chart)
u.cmd = command(helmBin, args...) u.cmd = command(helmBin, args...)
u.cmd.Stdout(cfg.Stdout) u.cmd.Stdout(cfg.Stdout)
u.cmd.Stderr(cfg.Stderr) u.cmd.Stderr(cfg.Stderr)

View File

@@ -31,7 +31,7 @@ func TestUpgradeTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite)) suite.Run(t, new(UpgradeTestSuite))
} }
func (suite *UpgradeTestSuite) TestPrepare() { func (suite *UpgradeTestSuite) TestPrepareAndExecute() {
defer suite.ctrl.Finish() defer suite.ctrl.Finish()
u := Upgrade{ u := Upgrade{
@@ -79,12 +79,8 @@ func (suite *UpgradeTestSuite) TestPrepareNamespaceFlag() {
return suite.mockCmd return suite.mockCmd
} }
suite.mockCmd.EXPECT(). suite.mockCmd.EXPECT().Stdout(gomock.Any())
Stdout(gomock.Any()) suite.mockCmd.EXPECT().Stderr(gomock.Any())
suite.mockCmd.EXPECT().
Stderr(gomock.Any())
suite.mockCmd.EXPECT().
Run()
cfg := Config{ cfg := Config{
Namespace: "melt", Namespace: "melt",
@@ -92,7 +88,72 @@ func (suite *UpgradeTestSuite) TestPrepareNamespaceFlag() {
} }
err := u.Prepare(cfg) err := u.Prepare(cfg)
suite.Require().Nil(err) suite.Require().Nil(err)
u.Execute(cfg) }
func (suite *UpgradeTestSuite) TestPrepareWithUpgradeFlags() {
defer suite.ctrl.Finish()
u := Upgrade{
Chart: "hot_ac",
Release: "maroon_5_memories",
ChartVersion: "radio_edit", //-version
DryRun: true, //-run
Wait: true, //-wait
ReuseValues: true, //-values
Timeout: "sit_in_the_corner", //-timeout
Force: true, //-force
}
cfg := Config{
KubeConfig: "/root/.kube/config",
Values: "age=35",
StringValues: "height=5ft10in",
ValuesFiles: []string{"/usr/local/stats", "/usr/local/grades"},
}
command = func(path string, args ...string) cmd {
suite.Equal(helmBin, path)
suite.Equal([]string{"--kubeconfig", "/root/.kube/config", "upgrade", "--install",
"--version", "radio_edit",
"--dry-run",
"--wait",
"--reuse-values",
"--timeout", "sit_in_the_corner",
"--force",
"--set", "age=35",
"--set-string", "height=5ft10in",
"--values", "/usr/local/stats",
"--values", "/usr/local/grades",
"maroon_5_memories", "hot_ac"}, args)
return suite.mockCmd
}
suite.mockCmd.EXPECT().Stdout(gomock.Any())
suite.mockCmd.EXPECT().Stderr(gomock.Any())
err := u.Prepare(cfg)
suite.Require().Nil(err)
}
func (suite *UpgradeTestSuite) TestRequiresChartAndRelease() {
// These aren't really expected, but allowing them gives clearer test-failure messages
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
u := Upgrade{
Release: "seth_everman_unskippable_cutscene",
}
err := u.Prepare(Config{})
suite.EqualError(err, "chart is required", "Chart should be mandatory")
u = Upgrade{
Chart: "billboard_top_zero",
}
err = u.Prepare(Config{})
suite.EqualError(err, "release is required", "Release should be mandatory")
} }
func (suite *UpgradeTestSuite) TestPrepareDebugFlag() { func (suite *UpgradeTestSuite) TestPrepareDebugFlag() {
@@ -125,7 +186,7 @@ func (suite *UpgradeTestSuite) TestPrepareDebugFlag() {
u.Prepare(cfg) u.Prepare(cfg)
want := fmt.Sprintf("Generated command: '%s --debug --kubeconfig /root/.kube/config upgrade "+ want := fmt.Sprintf("Generated command: '%s --kubeconfig /root/.kube/config --debug upgrade "+
"--install lewis_capaldi_someone_you_loved at40'\n", helmBin) "--install lewis_capaldi_someone_you_loved at40'\n", helmBin)
suite.Equal(want, stderr.String()) suite.Equal(want, stderr.String())
suite.Equal("", stdout.String()) suite.Equal("", stdout.String())