From 62af73c773663b7ad64ed523461e544d9202c682 Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Fri, 4 Aug 2017 10:06:31 +0200 Subject: [PATCH 1/3] move interceptor options into their interceptor packages --- pkg/interceptors/prestopsleep/options.go | 5 ++ pkg/interceptors/statuschecker/options.go | 6 +++ pkg/settings/default.go | 10 ++-- pkg/settings/interceptors.go | 61 ++++------------------- pkg/settings/tristate.go | 40 +++++++++++++++ 5 files changed, 67 insertions(+), 55 deletions(-) create mode 100644 pkg/interceptors/prestopsleep/options.go create mode 100644 pkg/interceptors/statuschecker/options.go create mode 100644 pkg/settings/tristate.go diff --git a/pkg/interceptors/prestopsleep/options.go b/pkg/interceptors/prestopsleep/options.go new file mode 100644 index 0000000..4cd5834 --- /dev/null +++ b/pkg/interceptors/prestopsleep/options.go @@ -0,0 +1,5 @@ +package prestopsleep + +type Options struct { + Seconds int `yaml:"seconds"` +} diff --git a/pkg/interceptors/statuschecker/options.go b/pkg/interceptors/statuschecker/options.go new file mode 100644 index 0000000..4e859ff --- /dev/null +++ b/pkg/interceptors/statuschecker/options.go @@ -0,0 +1,6 @@ +package statuschecker + +type Options struct { + TargetURLRegex string `yaml:"targetUrlRegex"` + JobRegex string `yaml:"jobRegex"` +} diff --git a/pkg/settings/default.go b/pkg/settings/default.go index 60bd693..7e94335 100644 --- a/pkg/settings/default.go +++ b/pkg/settings/default.go @@ -1,6 +1,10 @@ package settings -import "github.com/rebuy-de/kubernetes-deployment/pkg/gh" +import ( + "github.com/rebuy-de/kubernetes-deployment/pkg/gh" + "github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/prestopsleep" + "github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/statuschecker" +) var ( Defaults = Service{ @@ -10,12 +14,12 @@ var ( }, Interceptors: Interceptors{ PreStopSleep: PreStopSleepInterceptor{ - Options: PreStopSleepOptions{ + Options: prestopsleep.Options{ Seconds: 3, }, }, GHStatusChecker: GHStatusCheckerInterceptor{ - Options: GHStatusCheckerOptions{ + Options: statuschecker.Options{ TargetURLRegex: `.*`, JobRegex: `.*`, }, diff --git a/pkg/settings/interceptors.go b/pkg/settings/interceptors.go index 55cc341..6cea76f 100644 --- a/pkg/settings/interceptors.go +++ b/pkg/settings/interceptors.go @@ -1,5 +1,10 @@ package settings +import ( + "github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/prestopsleep" + "github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/statuschecker" +) + type Interceptors struct { PreStopSleep PreStopSleepInterceptor `yaml:"preStopSleep"` RemoveResourceSpecs Interceptor `yaml:"removeResourceSpecs"` @@ -11,60 +16,12 @@ type Interceptor struct { Enabled TriState `yaml:"enabled"` } -type PreStopSleepOptions struct { - Seconds int `yaml:"seconds"` -} - type PreStopSleepInterceptor struct { - Enabled TriState `yaml:"enabled"` - Options PreStopSleepOptions `yaml:"options"` -} - -type GHStatusCheckerOptions struct { - TargetURLRegex string `yaml:"targetUrlRegex"` - JobRegex string `yaml:"jobRegex"` + Enabled TriState `yaml:"enabled"` + Options prestopsleep.Options `yaml:"options"` } type GHStatusCheckerInterceptor struct { - Enabled TriState `yaml:"enabled"` - Options GHStatusCheckerOptions `yaml:"options"` -} - -type TriState int - -const ( - Unknown TriState = iota - Disabled - Enabled -) - -func (ts TriState) MarshalYAML() (interface{}, error) { - switch ts { - case Enabled: - return true, nil - case Disabled: - return false, nil - default: - return nil, nil - } -} - -func (ts *TriState) UnmarshalYAML(unmarshal func(interface{}) error) error { - var original bool - - err := unmarshal(&original) - if err != nil { - return err - } - - switch original { - case true: - (*ts) = Enabled - case false: - (*ts) = Disabled - default: - (*ts) = Unknown - } - - return nil + Enabled TriState `yaml:"enabled"` + Options statuschecker.Options `yaml:"options"` } diff --git a/pkg/settings/tristate.go b/pkg/settings/tristate.go new file mode 100644 index 0000000..40970f9 --- /dev/null +++ b/pkg/settings/tristate.go @@ -0,0 +1,40 @@ +package settings + +type TriState int + +const ( + Unknown TriState = iota + Disabled + Enabled +) + +func (ts TriState) MarshalYAML() (interface{}, error) { + switch ts { + case Enabled: + return true, nil + case Disabled: + return false, nil + default: + return nil, nil + } +} + +func (ts *TriState) UnmarshalYAML(unmarshal func(interface{}) error) error { + var original bool + + err := unmarshal(&original) + if err != nil { + return err + } + + switch original { + case true: + (*ts) = Enabled + case false: + (*ts) = Disabled + default: + (*ts) = Unknown + } + + return nil +} From 2fef488257c26521c6f0d3809b9fb7c088eced1f Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Fri, 4 Aug 2017 10:09:54 +0200 Subject: [PATCH 2/3] directly pass statuschecker options --- pkg/api/app.go | 8 +++----- pkg/interceptors/statuschecker/interceptor.go | 10 +++++----- pkg/interceptors/statuschecker/interceptors_test.go | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/pkg/api/app.go b/pkg/api/app.go index c539ee4..784523d 100644 --- a/pkg/api/app.go +++ b/pkg/api/app.go @@ -82,14 +82,12 @@ func New(p *Parameters) (*App, error) { if interceptors.GHStatusChecker.Enabled == settings.Enabled { log.WithFields(log.Fields{ - "Interceptor": "ghStatusChecker", - "TargetURLRegex": interceptors.GHStatusChecker.Options.TargetURLRegex, - "JobRegex": interceptors.GHStatusChecker.Options.JobRegex, + "Interceptor": "ghStatusChecker", + "Options": interceptors.GHStatusChecker.Options, }).Debug("enabling ghStatusChecker interceptor") app.Interceptors.Add(statuschecker.New( app.Clients.GitHub, - interceptors.GHStatusChecker.Options.TargetURLRegex, - interceptors.GHStatusChecker.Options.JobRegex, + interceptors.GHStatusChecker.Options, )) } diff --git a/pkg/interceptors/statuschecker/interceptor.go b/pkg/interceptors/statuschecker/interceptor.go index eb5dc1b..51334aa 100644 --- a/pkg/interceptors/statuschecker/interceptor.go +++ b/pkg/interceptors/statuschecker/interceptor.go @@ -30,7 +30,8 @@ var ( ) type Interceptor struct { - GitHub gh.Interface + GitHub gh.Interface + Options Options TargetURLRegex string JobRegex string @@ -38,11 +39,10 @@ type Interceptor struct { Branch *gh.Branch } -func New(gitHub gh.Interface, TargetURLRegex, JobRegex string) *Interceptor { +func New(gitHub gh.Interface, options Options) *Interceptor { return &Interceptor{ - GitHub: gitHub, - TargetURLRegex: TargetURLRegex, - JobRegex: JobRegex, + GitHub: gitHub, + Options: options, } } diff --git a/pkg/interceptors/statuschecker/interceptors_test.go b/pkg/interceptors/statuschecker/interceptors_test.go index 628fa1e..cd87893 100644 --- a/pkg/interceptors/statuschecker/interceptors_test.go +++ b/pkg/interceptors/statuschecker/interceptors_test.go @@ -8,12 +8,12 @@ import ( func TestTypePreApplier(t *testing.T) { var inter interceptors.PreApplier - inter = New(nil, "", "") + inter = New(nil, Options{}) _ = inter } func TestTypePostFetcher(t *testing.T) { var inter interceptors.PostFetcher - inter = New(nil, "", "") + inter = New(nil, Options{}) _ = inter } From 0607bb8eae824d6327b83fcbc3d68aa510a2f810 Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Fri, 4 Aug 2017 10:36:20 +0200 Subject: [PATCH 3/3] 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