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

Commit

Permalink
Merge pull request #72 from rebuy-de/improve-statuschecker-options
Browse files Browse the repository at this point in the history
Improve statuschecker options
  • Loading branch information
svenwltr authored Aug 4, 2017
2 parents 15d610d + 0607bb8 commit decd489
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 120 deletions.
8 changes: 3 additions & 5 deletions pkg/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}

Expand Down
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

5 changes: 5 additions & 0 deletions pkg/interceptors/prestopsleep/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package prestopsleep

type Options struct {
Seconds int `yaml:"seconds"`
}
46 changes: 18 additions & 28 deletions pkg/interceptors/statuschecker/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,16 @@ const (
Error
)

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

type Interceptor struct {
GitHub gh.Interface

TargetURLRegex string
JobRegex string

Branch *gh.Branch
GitHub gh.Interface
Options Options
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,
}
}

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
4 changes: 2 additions & 2 deletions pkg/interceptors/statuschecker/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions pkg/interceptors/statuschecker/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package statuschecker

import "time"

type Options struct {
TargetURLRegex string `yaml:"targetUrlRegex"`
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,
}
13 changes: 7 additions & 6 deletions pkg/settings/default.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -10,15 +14,12 @@ var (
},
Interceptors: Interceptors{
PreStopSleep: PreStopSleepInterceptor{
Options: PreStopSleepOptions{
Options: prestopsleep.Options{
Seconds: 3,
},
},
GHStatusChecker: GHStatusCheckerInterceptor{
Options: GHStatusCheckerOptions{
TargetURLRegex: `.*`,
JobRegex: `.*`,
},
Options: statuschecker.DefaultOptions,
},
},
}
Expand Down
61 changes: 9 additions & 52 deletions pkg/settings/interceptors.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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"`
}
Loading

0 comments on commit decd489

Please sign in to comment.