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 #77 from rebuy-de/add-rmoldjob-interceptor
Browse files Browse the repository at this point in the history
add removeOldJob interceptor
  • Loading branch information
svenwltr authored Aug 17, 2017
2 parents 166ec1d + 5b8bd28 commit 4a22bf2
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pkg/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/rebuy-de/kubernetes-deployment/pkg/gh"
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors"
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/prestopsleep"
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/rmoldjob"
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/rmresspec"
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/statuschecker"
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors/waiter"
Expand Down Expand Up @@ -91,6 +92,15 @@ func New(p *Parameters) (*App, error) {
))
}

if interceptors.RemoveOldJob.Enabled == settings.Enabled {
log.WithFields(log.Fields{
"Interceptor": "removeOldJob",
}).Debug("enabling removeOldJob interceptor")
app.Interceptors.Add(rmoldjob.New(
app.Clients.Kubernetes,
))
}

return app, nil
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/api/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ func (app *App) Apply(project, branchName string) error {
}

for _, obj := range objects {
obj, err = app.Interceptors.PreManifestApply(obj)
if err != nil {
return errors.WithStack(err)
}

upstreamObj, err := app.Clients.Kubectl.Apply(obj)
if err != nil {
return errors.Wrap(err, "unable to apply manifest")
}
err = app.Interceptors.PreManifestApply(upstreamObj)

err = app.Interceptors.PostManifestApply(upstreamObj)
if err != nil {
return errors.WithStack(err)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/api/test-fixtures/deployments-golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defaults:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: null
ghStatusChecker:
Expand All @@ -38,6 +40,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: null
ghStatusChecker:
Expand All @@ -63,6 +67,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: null
ghStatusChecker:
Expand All @@ -88,6 +94,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: null
ghStatusChecker:
Expand All @@ -113,6 +121,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: null
ghStatusChecker:
Expand All @@ -138,6 +148,8 @@ contexts:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: null
ghStatusChecker:
Expand All @@ -162,6 +174,8 @@ contexts:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: null
ghStatusChecker:
Expand Down
7 changes: 6 additions & 1 deletion pkg/interceptors/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Interface interface {
PostFetcher
PreApplier
PreManifestApplier
PostManifestApplier
PostApplier
PostManifestRenderer
Closer
Expand All @@ -23,7 +24,11 @@ type PreApplier interface {
}

type PreManifestApplier interface {
PreManifestApply(runtime.Object) error
PreManifestApply(runtime.Object) (runtime.Object, error)
}

type PostManifestApplier interface {
PostManifestApply(runtime.Object) error
}

type PostApplier interface {
Expand Down
22 changes: 20 additions & 2 deletions pkg/interceptors/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,32 @@ func (m *Multi) PreApply(objs []runtime.Object) error {
return nil
}

func (m *Multi) PreManifestApply(obj runtime.Object) error {
func (m *Multi) PreManifestApply(obj runtime.Object) (runtime.Object, error) {
var err error

for _, i := range m.Interceptors {
c, ok := i.(PreManifestApplier)
if !ok {
continue
}

err := c.PreManifestApply(obj)
obj, err = c.PreManifestApply(obj)
if err != nil {
return obj, errors.WithStack(err)
}
}

return obj, nil
}

func (m *Multi) PostManifestApply(obj runtime.Object) error {
for _, i := range m.Interceptors {
c, ok := i.(PostManifestApplier)
if !ok {
continue
}

err := c.PostManifestApply(obj)
if err != nil {
return errors.WithStack(err)
}
Expand Down
93 changes: 93 additions & 0 deletions pkg/interceptors/rmoldjob/interceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package rmoldjob

import (
"fmt"
"reflect"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
k8serr "k8s.io/apimachinery/pkg/api/errors"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
v1batch "k8s.io/client-go/pkg/apis/batch/v1"
)

const Annotation = "rebuy.com/delete-on-deploy"

type Interceptor struct {
client kubernetes.Interface
}

func New(client kubernetes.Interface) *Interceptor {
return &Interceptor{
client: client,
}
}

func (i *Interceptor) PreManifestApply(obj runtime.Object) (runtime.Object, error) {
job, ok := obj.(*v1batch.Job)
if !ok {
log.WithFields(log.Fields{
"type": reflect.TypeOf(obj),
}).Debug("type doesn't support deletion of old jobs")
return obj, nil
}

name := job.ObjectMeta.Name
namespace := job.ObjectMeta.Namespace

if namespace == "" {
namespace = "default"
}

has, err := i.hasJob(namespace, name)
if !has || err != nil {
return obj, errors.WithStack(err)
}

if !v1meta.HasAnnotation(job.ObjectMeta, Annotation) ||
job.ObjectMeta.Annotations[Annotation] != "true" {
log.WithFields(log.Fields{
"Namespace": namespace,
"Name": name,
}).Debug("skip deletion of job, because the annotation is missing")
return obj, nil
}

log.WithFields(log.Fields{
"Namespace": namespace,
"Name": name,
}).Infof("deleting pending job: %s", name)

return obj, i.client.
Batch().
Jobs(namespace).
Delete(name, nil)
}

func (i *Interceptor) hasJob(namespace, name string) (bool, error) {
_, err := i.client.
Batch().
Jobs(namespace).
Get(name, v1meta.GetOptions{})

if err == nil {
return true, nil
}

status, ok := err.(*k8serr.StatusError)
if !ok {
return false, err
}

log.WithFields(log.Fields{
"Error": fmt.Sprintf("%#v", status.ErrStatus),
}).Debug("got error status")

if status.ErrStatus.Reason == v1meta.StatusReasonNotFound {
return false, nil
}

return false, err
}
13 changes: 13 additions & 0 deletions pkg/interceptors/rmoldjob/interceptors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package rmoldjob

import (
"testing"

"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors"
)

func TestTypePreManifestApplier(t *testing.T) {
var inter interceptors.PreManifestApplier
inter = &Interceptor{}
_ = inter
}
2 changes: 1 addition & 1 deletion pkg/interceptors/waiter/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (dwi *DeploymentWaitInterceptor) Close() error {
return nil
}

func (dwi *DeploymentWaitInterceptor) PreManifestApply(obj runtime.Object) error {
func (dwi *DeploymentWaitInterceptor) PostManifestApply(obj runtime.Object) error {
deployment, ok := obj.(*v1beta1.Deployment)
if !ok {
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/interceptors/waiter/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestTypePostApplier(t *testing.T) {
_ = inter
}

func TestTypePreManifestApplier(t *testing.T) {
var inter interceptors.PreManifestApplier
func TestTypePostManifestApplier(t *testing.T) {
var inter interceptors.PostManifestApplier
inter = &DeploymentWaitInterceptor{}
_ = inter
}
Expand Down
1 change: 1 addition & 0 deletions pkg/settings/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Interceptors struct {
PreStopSleep PreStopSleepInterceptor `yaml:"preStopSleep"`
RemoveResourceSpecs Interceptor `yaml:"removeResourceSpecs"`
RemoveOldJob Interceptor `yaml:"removeOldJob"`
Waiter Interceptor `yaml:"waiter"`
GHStatusChecker GHStatusCheckerInterceptor `yaml:"ghStatusChecker"`
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/settings/test-fixtures/services-clean-golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defaults:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down Expand Up @@ -41,6 +43,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down Expand Up @@ -68,6 +72,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down Expand Up @@ -95,6 +101,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down Expand Up @@ -122,6 +130,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down Expand Up @@ -149,6 +159,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down Expand Up @@ -176,6 +188,8 @@ services:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down Expand Up @@ -203,6 +217,8 @@ contexts:
seconds: 3
removeResourceSpecs:
enabled: null
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand All @@ -229,6 +245,8 @@ contexts:
seconds: 3
removeResourceSpecs:
enabled: true
removeOldJob:
enabled: null
waiter:
enabled: true
ghStatusChecker:
Expand Down
Loading

0 comments on commit 4a22bf2

Please sign in to comment.