Skip to content
This repository has been archived by the owner on Oct 5, 2020. It is now read-only.

Commit

Permalink
move statuschecker durations into config
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwltr committed Aug 4, 2017
1 parent 2fef488 commit 0607bb8
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 55 deletions.
35 changes: 28 additions & 7 deletions pkg/api/test-fixtures/deployments-golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ defaults:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
services:
- name: foobar
context: prod
Expand All @@ -41,7 +44,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: repo-no-exist
context: prod
owner: rebuy-de
Expand All @@ -63,7 +69,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: no-files
context: prod
owner: rebuy-de
Expand All @@ -85,7 +94,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: invalid-file
context: prod
owner: rebuy-de
Expand All @@ -107,7 +119,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
contexts:
prod:
context: prod
Expand All @@ -129,7 +144,10 @@ contexts:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
test:
context: test
owner: rebuy-de
Expand All @@ -150,5 +168,8 @@ contexts:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s

36 changes: 13 additions & 23 deletions pkg/interceptors/statuschecker/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,10 @@ const (
Error
)

var (
InitialDelay = 20 * time.Second
PullInterval = 10 * time.Second
NotificationInterval = 3 * time.Minute
)

type Interceptor struct {
GitHub gh.Interface
Options Options

TargetURLRegex string
JobRegex string

Branch *gh.Branch
Branch *gh.Branch
}

func New(gitHub gh.Interface, options Options) *Interceptor {
Expand All @@ -53,11 +43,11 @@ func (i *Interceptor) PostFetch(branch *gh.Branch) error {

func (i *Interceptor) PreApply([]runtime.Object) error {
age := time.Since(i.Branch.Date)
if age < InitialDelay {
if age < i.Options.InitialDelay {
log.WithFields(log.Fields{
"Delay": InitialDelay - age,
}).Info("(woah) you are deploying very fast *sleep*")
time.Sleep(InitialDelay - age)
"Delay": i.Options.InitialDelay - age,
}).Debug("the commit is very young; waiting so the build system has time to register")
time.Sleep(i.Options.InitialDelay - age)
}

worst, err := i.getWorstState()
Expand All @@ -76,7 +66,7 @@ func (i *Interceptor) PreApply([]runtime.Object) error {

log.Warn("delaying deployment, because there are pending builds")

notification := time.NewTicker(NotificationInterval)
notification := time.NewTicker(i.Options.NotificationInterval)
defer notification.Stop()
go func() {
for _ = range notification.C {
Expand All @@ -101,7 +91,7 @@ func (i *Interceptor) PreApply([]runtime.Object) error {
cmdutil.Exit(1)
}

time.Sleep(PullInterval)
time.Sleep(i.Options.PullInterval)
}
}

Expand Down Expand Up @@ -137,26 +127,26 @@ func (i *Interceptor) getState(status github.RepoStatus) (State, error) {
"Context": *status.Context,
})

ok, err := regexp.MatchString(i.TargetURLRegex, *status.TargetURL)
ok, err := regexp.MatchString(i.Options.TargetURLRegex, *status.TargetURL)
if err != nil {
return Error, errors.Wrapf(err, "failed to execute regex %v", i.TargetURLRegex)
return Error, errors.Wrapf(err, "failed to execute regex %v", i.Options.TargetURLRegex)
}

if !ok {
logger.WithFields(log.Fields{
"Regex": i.TargetURLRegex,
"Regex": i.Options.TargetURLRegex,
}).Debugf("ignoring status, since target URL doesn't match regex")
return Ignored, nil
}

ok, err = regexp.MatchString(i.JobRegex, *status.Context)
ok, err = regexp.MatchString(i.Options.ContextRegex, *status.Context)
if err != nil {
return Error, errors.Wrapf(err, "failed to execute regex %v", i.TargetURLRegex)
return Error, errors.Wrapf(err, "failed to execute regex %v", i.Options.TargetURLRegex)
}

if !ok {
logger.WithFields(log.Fields{
"Regex": i.JobRegex,
"Regex": i.Options.ContextRegex,
}).Debugf("ignoring status, since context doesn't match regex")
return Ignored, nil
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/interceptors/statuschecker/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package statuschecker

import "time"

type Options struct {
TargetURLRegex string `yaml:"targetUrlRegex"`
JobRegex string `yaml:"jobRegex"`
ContextRegex string `yaml:"contextRegex"`

InitialDelay time.Duration `yaml:"initialDelay"`
PullInterval time.Duration `yaml:"pullInterval"`
NotificationInterval time.Duration `yaml:"notificationInterval"`
}

var DefaultOptions = Options{
TargetURLRegex: `.*`,
ContextRegex: `.*`,

InitialDelay: 20 * time.Second,
PullInterval: 10 * time.Second,
NotificationInterval: 3 * time.Minute,
}
5 changes: 1 addition & 4 deletions pkg/settings/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ var (
},
},
GHStatusChecker: GHStatusCheckerInterceptor{
Options: statuschecker.Options{
TargetURLRegex: `.*`,
JobRegex: `.*`,
},
Options: statuschecker.DefaultOptions,
},
},
}
Expand Down
45 changes: 36 additions & 9 deletions pkg/settings/test-fixtures/services-clean-golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ defaults:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
services:
- name: bish
context: abc
Expand All @@ -44,7 +47,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: bash
context: abc
owner: rebuy-de
Expand All @@ -68,7 +74,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: bosh-deployment-foo
context: abc
owner: rebuy-de
Expand All @@ -92,7 +101,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: foo
context: abc
owner: rebuy-de
Expand All @@ -116,7 +128,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: kubernetes-blub
context: abc
owner: kubernetes
Expand All @@ -140,7 +155,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
- name: meh
context: abc
owner: rebuy-de
Expand All @@ -164,7 +182,10 @@ services:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
contexts:
abc:
context: abc
Expand All @@ -188,7 +209,10 @@ contexts:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s
def:
context: def
owner: rebuy-de
Expand All @@ -211,5 +235,8 @@ contexts:
enabled: null
options:
targetUrlRegex: .*
jobRegex: .*
contextRegex: .*
initialDelay: 20s
pullInterval: 10s
notificationInterval: 3m0s

Loading

0 comments on commit 0607bb8

Please sign in to comment.