Skip to content

Commit

Permalink
Adding account override command line flag (#58)
Browse files Browse the repository at this point in the history
* Adding account override commandline flag

* Govendor strangeness
  • Loading branch information
shraykay authored Jan 16, 2019
1 parent 4a76cec commit a7226ac
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
17 changes: 16 additions & 1 deletion cmd/k8s-pipeliner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/namely/k8s-pipeliner/pipeline"
"github.com/namely/k8s-pipeliner/pipeline/builder"
Expand Down Expand Up @@ -42,6 +43,10 @@ func main() {
Name: "timeout",
Usage: "override the default 72 hour timeout (unit: int)",
},
cli.StringSliceFlag{
Name: "override",
Usage: "override an environment with a different environment (example --override=int-k8s:int), --override=<old env>:<new env>, must be separated by colon",
},
},
},
{
Expand Down Expand Up @@ -73,7 +78,17 @@ func createAction(ctx *cli.Context) error {
return err
}

builder := builder.New(p, builder.WithV2Provider(ctx.Bool("v2")), builder.WithLinear(ctx.Bool("linear")), builder.WithTimeoutOverride(ctx.Int("timeout")))
overrideEnvs := map[string]string{}
for _, newEnv := range ctx.StringSlice("override") {
mapping := strings.Split(newEnv, ":")
if len(mapping) != 2 {
return fmt.Errorf("environment override flag was not formatted correctly")
}
overrideEnvs[mapping[0]] = mapping[1]
}

builder := builder.New(p, builder.WithV2Provider(ctx.Bool("v2")), builder.WithLinear(ctx.Bool("linear")), builder.WithTimeoutOverride(ctx.Int("timeout")), builder.WithAccountOverride(overrideEnvs))

return json.NewEncoder(os.Stdout).Encode(builder)
}

Expand Down
14 changes: 10 additions & 4 deletions pipeline/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ const (
type Builder struct {
pipeline *config.Pipeline

isLinear bool
basePath string
v2Provider bool
timeoutHours int
isLinear bool
basePath string
v2Provider bool
timeoutHours int
overrideAccounts map[string]string
}

// New initializes a new builder for a pipeline config
Expand Down Expand Up @@ -128,6 +129,11 @@ func (b *Builder) Pipeline() (*types.SpinnakerPipeline, error) {
var s types.Stage
var err error

// if the account has an override, switch the account name
if account, ok := b.overrideAccounts[stage.Account]; ok {
stage.Account = account
}

if b.v2Provider {
if stage.RunJob != nil {
s, err = b.buildV2RunJobStage(stageIndex, stage)
Expand Down
54 changes: 54 additions & 0 deletions pipeline/builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builder_test

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -228,7 +229,60 @@ func TestBuilderPipelineStages(t *testing.T) {
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")
assert.Equal(t, "Test V2 Deploy Stage", spinnaker.Stages[0].(*types.ManifestStage).Name)
})

t.Run("Test Account Override is working", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{Account: "int-k8s",
Name: "Test Deploy Stage",
Deploy: &config.DeployStage{
Groups: []config.Group{
{
ManifestFile: file,
PodOverrides: &config.PodOverrides{
Annotations: map[string]string{"hello": "world"},
},
},
},
},
},
},
}

builder := builder.New(pipeline, builder.WithLinear(true), builder.WithV2Provider(true), builder.WithAccountOverride(map[string]string{"int-k8s": "int"}))
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")
assert.Equal(t, "Test Deploy Stage", spinnaker.Stages[0].(*types.ManifestStage).Name)
assert.Equal(t, "int", spinnaker.Stages[0].(*types.ManifestStage).Account)
fmt.Println(spinnaker.Stages[0].(*types.ManifestStage).Account)
})

t.Run("Test account override ignores other accounts", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{Account: "int-k8s",
Name: "Test Deploy Stage",
Deploy: &config.DeployStage{
Groups: []config.Group{
{
ManifestFile: file,
PodOverrides: &config.PodOverrides{
Annotations: map[string]string{"hello": "world"},
},
},
},
},
},
},
}

builder := builder.New(pipeline, builder.WithLinear(true), builder.WithV2Provider(true), builder.WithAccountOverride(map[string]string{"staging-k8s": "staging"}))
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")
assert.Equal(t, "Test Deploy Stage", spinnaker.Stages[0].(*types.ManifestStage).Name)
assert.Equal(t, "int-k8s", spinnaker.Stages[0].(*types.ManifestStage).Account)
fmt.Println(spinnaker.Stages[0].(*types.ManifestStage).Account)
})

t.Run("RequisiteStageRefIds defaults to an empty slice", func(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions pipeline/builder/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ func WithTimeoutOverride(hours int) OptFunc {
b.timeoutHours = hours
}
}

// WithAccountOverride lets you override an account with a different account
func WithAccountOverride(accounts map[string]string) OptFunc {
return func(b *Builder) {
b.overrideAccounts = accounts
}
}
14 changes: 10 additions & 4 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@
"revisionTime": "2018-08-09T22:11:04Z"
},
{
"checksumSHA1": "UdgOEimjVVIIvrSjTGzD4oDaYjI=",
"checksumSHA1": "kWc69gh2NaXdo63gbfp77LTXiS4=",
"path": "github.com/namely/k8s-configurator",
"revision": "db775ef189eb77fdde753d1f0c447fff1f8a6e9b",
"revisionTime": "2018-09-20T16:51:51Z"
},
{
"checksumSHA1": "RS/TqWQGmuk6pxcdNXtlM4+mALU=",
"path": "github.com/namely/k8s-pipeliner/pipeline/config",
"revision": "4a76cecf31ac0d03e372bd08ce3ba1826ed39ed5",
"revisionTime": "2019-01-16T19:06:37Z"
},
{
"checksumSHA1": "ljd3FhYRJ91cLZz3wsH9BQQ2JbA=",
"path": "github.com/pkg/errors",
Expand Down Expand Up @@ -202,10 +208,10 @@
"revisionTime": "2018-01-25T14:14:21Z"
},
{
"checksumSHA1": "qOmvuDm+F+2nQQecUZBVkZrTn6Y=",
"checksumSHA1": "QqDq2x8XOU7IoOR98Cx1eiV5QY8=",
"path": "gopkg.in/yaml.v2",
"revision": "d670f9405373e636a5a2765eea47fac0c9bc91a4",
"revisionTime": "2018-01-09T11:43:31Z"
"revision": "51d6538a90f86fe93ac480b35f37b2be17fef232",
"revisionTime": "2018-11-15T11:05:04Z"
},
{
"checksumSHA1": "xwvGTst/xgcrHZB2fy3PhWHAXAk=",
Expand Down

0 comments on commit a7226ac

Please sign in to comment.