This repository has been archived by the owner on Oct 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from rebuy-de/add-rmoldjob-interceptor
add removeOldJob interceptor
- Loading branch information
Showing
13 changed files
with
207 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.