Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
fix: clean-up pods when deleting kubernetes jobs (#154) (#155)
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Chila <[email protected]>
  • Loading branch information
pchila authored Jan 24, 2022
1 parent 34b0221 commit 38c9f19
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,13 @@ tasks:
ttlSecondsAfterFinished: 600
```

**Note:** `ttlSecondsAfterFinished` relies on setting the [same property](https://kubernetes.io/docs/concepts/workloads/controllers/job/#ttl-mechanism-for-finished-jobs)
in kubernetes job workloads spec. The TTL controller (alpha from Kubernetes v1.12-v1.20, beta in v1.21-v1.22, GA in v1.23)
will then take care of cleanup.
More information about feature stages in kubernetes (that is what alpha, beta and GA implies in that context) have a look at
the [official kubernetes documentation](https://v1-20.docs.kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/#feature-stages).


## How to validate a job configuration

`job-lint` is a simple cli tool that validates any given job configuration file and shows possible errors.
Expand Down
15 changes: 5 additions & 10 deletions pkg/eventhandler/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package eventhandler
import (
"encoding/json"
"fmt"
"keptn-contrib/job-executor-service/pkg/config"
"keptn-contrib/job-executor-service/pkg/k8sutils"
"log"
"math"
"strconv"
"time"

"keptn-contrib/job-executor-service/pkg/config"
"keptn-contrib/job-executor-service/pkg/k8sutils"

cloudevents "github.com/cloudevents/sdk-go/v2" // make sure to use v2 cloudevents here
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
)
Expand Down Expand Up @@ -146,7 +147,8 @@ func (eh *EventHandler) startK8sJob(k8s k8sutils.K8s, action *config.Action, jso
namespace = task.Namespace
}

err := k8s.CreateK8sJob(jobName, action, task, eh.EventData, eh.JobSettings, jsonEventData, namespace)
err := k8s.CreateK8sJob(jobName, action, task, eh.EventData, eh.JobSettings,
jsonEventData, namespace)

if err != nil {
log.Printf("Error while creating job: %s\n", err)
Expand All @@ -156,13 +158,6 @@ func (eh *EventHandler) startK8sJob(k8s k8sutils.K8s, action *config.Action, jso
return
}

defer func() {
err = k8s.DeleteK8sJob(jobName, namespace)
if err != nil {
log.Printf("Error while deleting job: %s\n", err.Error())
}
}()

maxPollCount := defaultMaxPollCount
if task.MaxPollDuration != nil {
maxPollCount = int(math.Ceil(float64(*task.MaxPollDuration) / pollIntervalInSeconds))
Expand Down
7 changes: 4 additions & 3 deletions pkg/k8sutils/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ type k8sImpl struct {
// K8s is used to interact with kubernetes jobs
type K8s interface {
ConnectToCluster() error
CreateK8sJob(jobName string, action *config.Action, task config.Task, eventData *keptnv2.EventData,
jobSettings JobSettings, jsonEventData interface{}, namespace string) error
CreateK8sJob(
jobName string, action *config.Action, task config.Task, eventData *keptnv2.EventData, jobSettings JobSettings,
jsonEventData interface{}, namespace string,
) error
AwaitK8sJobDone(jobName string, maxPollDuration int, pollIntervalInSeconds int, namespace string) error
DeleteK8sJob(jobName string, namespace string) error
GetLogsOfPod(jobName string, namespace string) (string, error)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/k8sutils/fake/connect_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions pkg/k8sutils/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ type JobSettings struct {
}

// CreateK8sJob creates a k8s job with the job-executor-service-initcontainer and the job image of the task
func (k8s *k8sImpl) CreateK8sJob(jobName string, action *config.Action, task config.Task, eventData *keptnv2.EventData,
jobSettings JobSettings, jsonEventData interface{}, namespace string) error {
// returns ttlSecondsAfterFinished as stored in k8s or error in case of issues creating the job
func (k8s *k8sImpl) CreateK8sJob(
jobName string, action *config.Action, task config.Task, eventData *keptnv2.EventData, jobSettings JobSettings,
jsonEventData interface{}, namespace string,
) error {

var backOffLimit int32 = 0

Expand Down Expand Up @@ -240,13 +243,6 @@ func (k8s *k8sImpl) AwaitK8sJobDone(jobName string, maxPollCount int, pollInterv
}
}

// DeleteK8sJob delete a k8s job in the given namespace
func (k8s *k8sImpl) DeleteK8sJob(jobName string, namespace string) error {

jobs := k8s.clientset.BatchV1().Jobs(namespace)
return jobs.Delete(context.TODO(), jobName, metav1.DeleteOptions{})
}

func (k8s *k8sImpl) prepareJobEnv(task config.Task, eventData *keptnv2.EventData, jsonEventData interface{}, namespace string) ([]v1.EnvVar, error) {

var jobEnv []v1.EnvVar
Expand Down

0 comments on commit 38c9f19

Please sign in to comment.