Skip to content

Commit

Permalink
SRESOLD-1090 - adding timeouts to spinnaker stages (#56)
Browse files Browse the repository at this point in the history
* SRESOLD-1090 - adding timeouts to spinnaker stages

* Adding flag
  • Loading branch information
shraykay authored Dec 4, 2018
1 parent 4d34c06 commit 64bbb85
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ stages:
reliesOn:
- "2"
manualJudgement:
timeoutHours: 48
failPipeline: true
instructions: |
If this stage has completed QA, press proceed.
Expand Down
6 changes: 5 additions & 1 deletion cmd/k8s-pipeliner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func main() {
Name: "v2",
Usage: "Create your manifests with the v2 kubernetes provider",
},
cli.IntFlag{
Name: "timeout",
Usage: "override the default 72 hour timeout (unit: int)",
},
},
},
{
Expand Down Expand Up @@ -69,7 +73,7 @@ func createAction(ctx *cli.Context) error {
return err
}

builder := builder.New(p, builder.WithV2Provider(ctx.Bool("v2")), builder.WithLinear(ctx.Bool("linear")))
builder := builder.New(p, builder.WithV2Provider(ctx.Bool("v2")), builder.WithLinear(ctx.Bool("linear")), builder.WithTimeoutOverride(ctx.Int("timeout")))
return json.NewEncoder(os.Stdout).Encode(builder)
}

Expand Down
28 changes: 20 additions & 8 deletions pipeline/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ const (
WebhookTrigger = "webhook"
// LoadBalancerFormat creates the label selectors to attach pipeline.yml labels to deployment selectors
LoadBalancerFormat = "load-balancer-%s"
// HourInMS provides 1 hour in milliseconds
HourInMS int64 = 3600000
)

// Builder constructs a spinnaker pipeline JSON from a pipeliner config
type Builder struct {
pipeline *config.Pipeline

isLinear bool
basePath string
v2Provider bool
isLinear bool
basePath string
v2Provider bool
timeoutHours int
}

// New initializes a new builder for a pipeline config
Expand Down Expand Up @@ -508,7 +511,7 @@ func (b *Builder) buildDeployStage(index int, s config.Stage) (*types.DeployStag
VolumeSources: mg.VolumeSources,

// TODO(bobbytables): allow these to be configurable
Events: []interface{}{},
Events: []interface{}{},
InterestingHealthProviderNames: []string{"KubernetesContainer", "KubernetesPod"},
Provider: "kubernetes",
CloudProvider: "kubernetes",
Expand All @@ -525,10 +528,19 @@ func (b *Builder) buildDeployStage(index int, s config.Stage) (*types.DeployStag
func (b *Builder) buildManualJudgementStage(index int, s config.Stage) (*types.ManualJudgementStage, error) {
mjs := &types.ManualJudgementStage{
StageMetadata: buildStageMetadata(s, "manualJudgment", index, b.isLinear),

FailPipeline: s.ManualJudgement.FailPipeline,
Instructions: s.ManualJudgement.Instructions,
Inputs: s.ManualJudgement.Inputs,
FailPipeline: s.ManualJudgement.FailPipeline,
Instructions: s.ManualJudgement.Instructions,
Inputs: s.ManualJudgement.Inputs,
}
// if global timeout override has been set
if b.timeoutHours != 0 {
mjs.OverrideTimeout = true
mjs.StageTimeoutMS = int64(b.timeoutHours) * HourInMS
}
// if the timeout is actually set go
if s.ManualJudgement.Timeout != 0 {
mjs.OverrideTimeout = true
mjs.StageTimeoutMS = int64(s.ManualJudgement.Timeout) * HourInMS
}

return mjs, nil
Expand Down
7 changes: 7 additions & 0 deletions pipeline/builder/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ func WithV2Provider(v bool) OptFunc {
b.v2Provider = v
}
}

// WithTimeoutOverride overrides every stage's default 72 hour timeout
func WithTimeoutOverride(hours int) OptFunc {
return func(b *Builder) {
b.timeoutHours = hours
}
}
8 changes: 5 additions & 3 deletions pipeline/builder/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ var _ Stage = DeployStage{}
type ManualJudgementStage struct {
StageMetadata

FailPipeline bool `json:"failPipeline"`
Instructions string `json:"instructions"`
Inputs []string `json:"inputs,omitempty"`
FailPipeline bool `json:"failPipeline"`
Instructions string `json:"instructions"`
Inputs []string `json:"inputs,omitempty"`
OverrideTimeout bool `json:"overrideTimeout,omitempty"`
StageTimeoutMS int64 `json:"stageTimeoutMs,omitempty"`
}

func (mjs ManualJudgementStage) spinnakerStage() {}
Expand Down
1 change: 1 addition & 0 deletions pipeline/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ type ManualJudgementStage struct {
FailPipeline bool `yaml:"failPipeline"`
Instructions string `yaml:"instructions"`
Inputs []string `yaml:"inputs"`
Timeout int `yaml:"timeoutHours,omitempty"`
}

// ManifestFile represents a single manifest file
Expand Down
8 changes: 7 additions & 1 deletion pipeline/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ func TestNewConfig(t *testing.T) {
require.Len(t, cfg.Triggers, 1)
require.Equal(t, "nginx/job/master", cfg.Triggers[0].Jenkins.Job)

require.Len(t, cfg.Stages, 1)
require.Len(t, cfg.Stages, 3)

stage := cfg.Stages[0]
require.NotNil(t, stage.DeployEmbeddedManifests)
assert.Equal(t, stage.DeployEmbeddedManifests.Files[0].File, "manifests/nginx-deployment.yml")
assert.Equal(t, stage.Name, "Deploy nginx")
assert.Equal(t, stage.Account, "int-k8s")
stage2 := cfg.Stages[1]
require.NotNil(t, stage2.ManualJudgement)
assert.Equal(t, stage2.ManualJudgement.Timeout, 100)
stage3 := cfg.Stages[2]
require.NotNil(t, stage3.ManualJudgement)
assert.Equal(t, stage3.ManualJudgement.Timeout, 0)
}
13 changes: 13 additions & 0 deletions pipeline/config/testdata/pipeline.full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ stages:
deployEmbeddedManifests:
files:
- file: manifests/nginx-deployment.yml
- account: int-k8s
name: Deploy to staging-k8s?
manualJudgement:
failPipeline: true
instructions: Should this pipeline continue?
inputs: []
timeoutHours: 100
- account: int-k8s
name: Deploy to staging-k8s?
manualJudgement:
failPipeline: true
instructions: Should this pipeline continue?
inputs: []

0 comments on commit 64bbb85

Please sign in to comment.