diff --git a/docs/parameter_reference.md b/docs/parameter_reference.md index b246761..52f3dd7 100644 --- a/docs/parameter_reference.md +++ b/docs/parameter_reference.md @@ -5,9 +5,9 @@ |---------------------|-----------------|---------| | helm_command | string | Indicates the operation to perform. Recommended, but not required. Valid options are `upgrade`, `uninstall`, `lint`, and `help`. | | update_dependencies | boolean | Calls `helm dependency update` before running the main command.| -| helm_repos | list\ | Calls `helm repo add $repo` before running the main command. Each string should be formatted as `repo_name=https://repo.url/`. **Not currently implemented**; see [#26](https://github.com/pelotech/drone-helm3/issues/26). | +| helm_repos | list\ | Calls `helm repo add $repo` before running the main command. Each string should be formatted as `repo_name=https://repo.url/`. | | namespace | string | Kubernetes namespace to use for this operation. | -| prefix | string | Expect environment variables to be prefixed with the given string. For more details, see "Using the prefix setting" below. **Not currently implemented**; see [#19](https://github.com/pelotech/drone-helm3/issues/19). | +| prefix | string | Expect environment variables to be prefixed with the given string. For more details, see "Using the prefix setting" below. | | debug | boolean | Generate debug output within drone-helm3 and pass `--debug` to all helm commands. Use with care, since the debug output may include secrets. | ## Linting diff --git a/internal/helm/config.go b/internal/helm/config.go index cf351ae..a4d1914 100644 --- a/internal/helm/config.go +++ b/internal/helm/config.go @@ -18,7 +18,7 @@ type Config struct { Command string `envconfig:"HELM_COMMAND"` // Helm command to run DroneEvent string `envconfig:"DRONE_BUILD_EVENT"` // Drone event that invoked this plugin. UpdateDependencies bool `split_words:"true"` // Call `helm dependency update` before the main command - Repos []string `envconfig:"HELM_REPOS"` // Call `helm repo add` before the main command + AddRepos []string `envconfig:"HELM_REPOS"` // Call `helm repo add` before the main command Prefix string `` // Prefix to use when looking up secret env vars Debug bool `` // Generate debug output and pass --debug to all helm commands Values string `` // Argument to pass to --set in applicable helm commands diff --git a/internal/helm/plan.go b/internal/helm/plan.go index 34ad93b..ebd4774 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -96,6 +96,7 @@ func (p *Plan) Execute() error { var upgrade = func(cfg Config) []Step { steps := initKube(cfg) + steps = append(steps, addRepos(cfg)...) if cfg.UpdateDependencies { steps = append(steps, depUpdate(cfg)...) } @@ -127,7 +128,7 @@ var uninstall = func(cfg Config) []Step { } var lint = func(cfg Config) []Step { - steps := make([]Step, 0) + steps := addRepos(cfg) if cfg.UpdateDependencies { steps = append(steps, depUpdate(cfg)...) } @@ -159,6 +160,17 @@ func initKube(cfg Config) []Step { } } +func addRepos(cfg Config) []Step { + steps := make([]Step, 0) + for _, repo := range cfg.AddRepos { + steps = append(steps, &run.AddRepo{ + Repo: repo, + }) + } + + return steps +} + func depUpdate(cfg Config) []Step { return []Step{ &run.DepUpdate{ diff --git a/internal/helm/plan_test.go b/internal/helm/plan_test.go index 2cdde5c..d4e4e82 100644 --- a/internal/helm/plan_test.go +++ b/internal/helm/plan_test.go @@ -177,6 +177,17 @@ func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() { suite.IsType(&run.DepUpdate{}, steps[1]) } +func (suite *PlanTestSuite) TestUpgradeWithAddRepos() { + cfg := Config{ + AddRepos: []string{ + "machine=https://github.com/harold_finch/themachine", + }, + } + steps := upgrade(cfg) + suite.Require().True(len(steps) > 1, "upgrade should generate at least two steps") + suite.IsType(&run.AddRepo{}, steps[1]) +} + func (suite *PlanTestSuite) TestUninstall() { cfg := Config{ KubeToken: "b2YgbXkgYWZmZWN0aW9u", @@ -268,6 +279,24 @@ func (suite *PlanTestSuite) TestDepUpdate() { suite.Equal(expected, update) } +func (suite *PlanTestSuite) TestAddRepos() { + cfg := Config{ + AddRepos: []string{ + "first=https://add.repos/one", + "second=https://add.repos/two", + }, + } + steps := addRepos(cfg) + suite.Require().Equal(2, len(steps), "addRepos should add one step per repo") + suite.Require().IsType(&run.AddRepo{}, steps[0]) + suite.Require().IsType(&run.AddRepo{}, steps[1]) + first := steps[0].(*run.AddRepo) + second := steps[1].(*run.AddRepo) + + suite.Equal(first.Repo, "first=https://add.repos/one") + suite.Equal(second.Repo, "second=https://add.repos/two") +} + func (suite *PlanTestSuite) TestLint() { cfg := Config{ Chart: "./flow", @@ -291,6 +320,15 @@ func (suite *PlanTestSuite) TestLintWithUpdateDependencies() { suite.IsType(&run.DepUpdate{}, steps[0]) } +func (suite *PlanTestSuite) TestLintWithAddRepos() { + cfg := Config{ + AddRepos: []string{"friendczar=https://github.com/logan_pierce/friendczar"}, + } + steps := lint(cfg) + suite.Require().True(len(steps) > 0, "lint should return at least one step") + suite.IsType(&run.AddRepo{}, steps[0]) +} + func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() { cfg := Config{ Command: "upgrade", diff --git a/internal/run/addrepo.go b/internal/run/addrepo.go new file mode 100644 index 0000000..3382957 --- /dev/null +++ b/internal/run/addrepo.go @@ -0,0 +1,52 @@ +package run + +import ( + "fmt" + "strings" +) + +// AddRepo is an execution step that calls `helm repo add` when executed. +type AddRepo struct { + Repo string + cmd cmd +} + +// Execute executes the `helm repo add` command. +func (a *AddRepo) Execute(_ Config) error { + return a.cmd.Run() +} + +// Prepare gets the AddRepo ready to execute. +func (a *AddRepo) Prepare(cfg Config) error { + if a.Repo == "" { + return fmt.Errorf("repo is required") + } + split := strings.SplitN(a.Repo, "=", 2) + if len(split) != 2 { + return fmt.Errorf("bad repo spec '%s'", a.Repo) + } + + name := split[0] + url := split[1] + + args := make([]string, 0) + + if cfg.Namespace != "" { + args = append(args, "--namespace", cfg.Namespace) + } + if cfg.Debug { + args = append(args, "--debug") + } + + args = append(args, "repo", "add", name, url) + + a.cmd = command(helmBin, args...) + a.cmd.Stdout(cfg.Stdout) + a.cmd.Stderr(cfg.Stderr) + + if cfg.Debug { + fmt.Fprintf(cfg.Stderr, "Generated command: '%s'\n", a.cmd.String()) + } + + return nil +} diff --git a/internal/run/addrepo_test.go b/internal/run/addrepo_test.go new file mode 100644 index 0000000..ad42d06 --- /dev/null +++ b/internal/run/addrepo_test.go @@ -0,0 +1,141 @@ +package run + +import ( + "fmt" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/suite" + "strings" + "testing" +) + +type AddRepoTestSuite struct { + suite.Suite + ctrl *gomock.Controller + mockCmd *Mockcmd + originalCommand func(string, ...string) cmd + commandPath string + commandArgs []string +} + +func (suite *AddRepoTestSuite) BeforeTest(_, _ string) { + suite.ctrl = gomock.NewController(suite.T()) + suite.mockCmd = NewMockcmd(suite.ctrl) + + suite.originalCommand = command + command = func(path string, args ...string) cmd { + suite.commandPath = path + suite.commandArgs = args + return suite.mockCmd + } +} + +func (suite *AddRepoTestSuite) AfterTest(_, _ string) { + suite.ctrl.Finish() + command = suite.originalCommand +} + +func TestAddRepoTestSuite(t *testing.T) { + suite.Run(t, new(AddRepoTestSuite)) +} + +func (suite *AddRepoTestSuite) TestPrepareAndExecute() { + stdout := strings.Builder{} + stderr := strings.Builder{} + cfg := Config{ + Stdout: &stdout, + Stderr: &stderr, + } + a := AddRepo{ + Repo: "edeath=https://github.com/n_marks/e-death", + } + + suite.mockCmd.EXPECT(). + Stdout(&stdout). + Times(1) + suite.mockCmd.EXPECT(). + Stderr(&stderr). + Times(1) + + suite.Require().NoError(a.Prepare(cfg)) + suite.Equal(helmBin, suite.commandPath) + suite.Equal([]string{"repo", "add", "edeath", "https://github.com/n_marks/e-death"}, suite.commandArgs) + + suite.mockCmd.EXPECT(). + Run(). + Times(1) + + suite.Require().NoError(a.Execute(cfg)) + +} + +func (suite *AddRepoTestSuite) TestPrepareRepoIsRequired() { + // 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{} + a := AddRepo{} + + err := a.Prepare(cfg) + suite.EqualError(err, "repo is required") +} + +func (suite *AddRepoTestSuite) TestPrepareMalformedRepo() { + a := AddRepo{ + Repo: "dwim", + } + err := a.Prepare(Config{}) + suite.EqualError(err, "bad repo spec 'dwim'") +} + +func (suite *AddRepoTestSuite) TestPrepareWithEqualSignInURL() { + suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() + suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes() + a := AddRepo{ + Repo: "samaritan=https://github.com/arthur_claypool/samaritan?version=2.1", + } + suite.NoError(a.Prepare(Config{})) + suite.Contains(suite.commandArgs, "https://github.com/arthur_claypool/samaritan?version=2.1") +} + +func (suite *AddRepoTestSuite) TestNamespaceFlag() { + suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() + suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes() + cfg := Config{ + Namespace: "alliteration", + } + a := AddRepo{ + Repo: "edeath=https://github.com/theater_guy/e-death", + } + + suite.NoError(a.Prepare(cfg)) + suite.Equal(suite.commandPath, helmBin) + suite.Equal(suite.commandArgs, []string{"--namespace", "alliteration", + "repo", "add", "edeath", "https://github.com/theater_guy/e-death"}) +} + +func (suite *AddRepoTestSuite) TestDebugFlag() { + suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() + suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes() + + stderr := strings.Builder{} + + command = func(path string, args ...string) cmd { + suite.mockCmd.EXPECT(). + String(). + Return(fmt.Sprintf("%s %s", path, strings.Join(args, " "))) + + return suite.mockCmd + } + + cfg := Config{ + Debug: true, + Stderr: &stderr, + } + a := AddRepo{ + Repo: "edeath=https://github.com/the_bug/e-death", + } + + suite.Require().NoError(a.Prepare(cfg)) + suite.Equal(fmt.Sprintf("Generated command: '%s --debug "+ + "repo add edeath https://github.com/the_bug/e-death'\n", helmBin), stderr.String()) +}