From 0607bb8eae824d6327b83fcbc3d68aa510a2f810 Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Fri, 4 Aug 2017 10:36:20 +0200 Subject: [PATCH] move statuschecker durations into config --- pkg/api/test-fixtures/deployments-golden.yaml | 35 ++++++++++++--- pkg/interceptors/statuschecker/interceptor.go | 36 ++++++--------- pkg/interceptors/statuschecker/options.go | 17 ++++++- pkg/settings/default.go | 5 +-- .../test-fixtures/services-clean-golden.yaml | 45 +++++++++++++++---- .../services-context-golden.yaml | 45 +++++++++++++++---- .../test-fixtures/services-plain-golden.yaml | 10 ++++- 7 files changed, 138 insertions(+), 55 deletions(-) diff --git a/pkg/api/test-fixtures/deployments-golden.yaml b/pkg/api/test-fixtures/deployments-golden.yaml index e96ff39..6a876e9 100644 --- a/pkg/api/test-fixtures/deployments-golden.yaml +++ b/pkg/api/test-fixtures/deployments-golden.yaml @@ -18,7 +18,10 @@ defaults: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s services: - name: foobar context: prod @@ -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 @@ -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 @@ -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 @@ -107,7 +119,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s contexts: prod: context: prod @@ -129,7 +144,10 @@ contexts: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s test: context: test owner: rebuy-de @@ -150,5 +168,8 @@ contexts: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s diff --git a/pkg/interceptors/statuschecker/interceptor.go b/pkg/interceptors/statuschecker/interceptor.go index 51334aa..b546a9e 100644 --- a/pkg/interceptors/statuschecker/interceptor.go +++ b/pkg/interceptors/statuschecker/interceptor.go @@ -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 { @@ -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() @@ -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 { @@ -101,7 +91,7 @@ func (i *Interceptor) PreApply([]runtime.Object) error { cmdutil.Exit(1) } - time.Sleep(PullInterval) + time.Sleep(i.Options.PullInterval) } } @@ -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 } diff --git a/pkg/interceptors/statuschecker/options.go b/pkg/interceptors/statuschecker/options.go index 4e859ff..540467f 100644 --- a/pkg/interceptors/statuschecker/options.go +++ b/pkg/interceptors/statuschecker/options.go @@ -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, } diff --git a/pkg/settings/default.go b/pkg/settings/default.go index 7e94335..4ec12d6 100644 --- a/pkg/settings/default.go +++ b/pkg/settings/default.go @@ -19,10 +19,7 @@ var ( }, }, GHStatusChecker: GHStatusCheckerInterceptor{ - Options: statuschecker.Options{ - TargetURLRegex: `.*`, - JobRegex: `.*`, - }, + Options: statuschecker.DefaultOptions, }, }, } diff --git a/pkg/settings/test-fixtures/services-clean-golden.yaml b/pkg/settings/test-fixtures/services-clean-golden.yaml index 7113e17..56a3279 100644 --- a/pkg/settings/test-fixtures/services-clean-golden.yaml +++ b/pkg/settings/test-fixtures/services-clean-golden.yaml @@ -19,7 +19,10 @@ defaults: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s services: - name: bish context: abc @@ -44,7 +47,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: bash context: abc owner: rebuy-de @@ -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 @@ -92,7 +101,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: foo context: abc owner: rebuy-de @@ -116,7 +128,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: kubernetes-blub context: abc owner: kubernetes @@ -140,7 +155,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: meh context: abc owner: rebuy-de @@ -164,7 +182,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s contexts: abc: context: abc @@ -188,7 +209,10 @@ contexts: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s def: context: def owner: rebuy-de @@ -211,5 +235,8 @@ contexts: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s diff --git a/pkg/settings/test-fixtures/services-context-golden.yaml b/pkg/settings/test-fixtures/services-context-golden.yaml index 6913875..c3abc3a 100644 --- a/pkg/settings/test-fixtures/services-context-golden.yaml +++ b/pkg/settings/test-fixtures/services-context-golden.yaml @@ -19,7 +19,10 @@ defaults: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s services: - name: bish context: def @@ -44,7 +47,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: bash context: def owner: rebuy-de @@ -68,7 +74,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: bosh-deployment-foo context: def owner: rebuy-de @@ -92,7 +101,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: foo context: def owner: rebuy-de @@ -116,7 +128,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: kubernetes-blub context: def owner: kubernetes @@ -140,7 +155,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s - name: meh context: def owner: rebuy-de @@ -164,7 +182,10 @@ services: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s contexts: abc: context: abc @@ -188,7 +209,10 @@ contexts: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s def: context: def owner: rebuy-de @@ -211,5 +235,8 @@ contexts: enabled: null options: targetUrlRegex: .* - jobRegex: .* + contextRegex: .* + initialDelay: 20s + pullInterval: 10s + notificationInterval: 3m0s diff --git a/pkg/settings/test-fixtures/services-plain-golden.yaml b/pkg/settings/test-fixtures/services-plain-golden.yaml index c34b94f..cc30699 100644 --- a/pkg/settings/test-fixtures/services-plain-golden.yaml +++ b/pkg/settings/test-fixtures/services-plain-golden.yaml @@ -18,7 +18,10 @@ defaults: enabled: null options: targetUrlRegex: "" - jobRegex: "" + contextRegex: "" + initialDelay: 0s + pullInterval: 0s + notificationInterval: 0s services: - repo: bish - repo: bash @@ -52,5 +55,8 @@ contexts: enabled: null options: targetUrlRegex: "" - jobRegex: "" + contextRegex: "" + initialDelay: 0s + pullInterval: 0s + notificationInterval: 0s