Skip to content

Commit

Permalink
don't delete jobs and pods owner by cronjobs (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeylanzman authored Jun 30, 2020
1 parent cb89101 commit 7e342c1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
17 changes: 7 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,13 @@ func main() {
wg.Add(1)
go func() {
for {
select {
case <-stopCh:
log.Println("shutting http server down")
err := server.Shutdown(ctx)
if err != nil {
log.Printf("failed to shutdown metrics server: %v\n", err)
}
wg.Done()
return
<-stopCh
log.Println("shutting http server down")
err := server.Shutdown(ctx)
if err != nil {
log.Printf("failed to shutdown metrics server: %v\n", err)
}
wg.Done()
}
}()

Expand All @@ -168,7 +165,7 @@ func main() {
func newClientSet(runOutsideCluster bool) (*kubernetes.Clientset, error) {
kubeConfigLocation := ""

if runOutsideCluster == true {
if runOutsideCluster {
if os.Getenv("KUBECONFIG") != "" {
kubeConfigLocation = filepath.Join(os.Getenv("KUBECONFIG"))
} else {
Expand Down
34 changes: 32 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,18 @@ func (c *Kleaner) Process(obj interface{}) {
return
}

owners := getJobOwnerKinds(job)
if isOwnedByCronJob(owners) {
return
}

finishTime := extractJobFinishTime(job)

if finishTime.IsZero() {
return
}

timeSinceFinish := time.Now().Sub(finishTime)
timeSinceFinish := time.Since(finishTime)

if job.Status.Succeeded > 0 {
if c.deleteSuccessfulAfter > 0 && timeSinceFinish > c.deleteSuccessfulAfter {
Expand All @@ -190,7 +195,7 @@ func (c *Kleaner) Process(obj interface{}) {
if podFinishTime.IsZero() {
return
}
age := time.Now().Sub(podFinishTime)
age := time.Since(podFinishTime)
// orphaned pod: those that do not have any owner references
// - uses c.deleteOrphanedAfter
if len(owners) == 0 {
Expand All @@ -202,6 +207,14 @@ func (c *Kleaner) Process(obj interface{}) {
// owned by job, have exactly one ownerReference present and its kind is Job
// - uses the c.deleteSuccessfulAfter, c.deleteFailedAfter, c.deletePendingAfter
if isOwnedByJob(owners) {
jobOwnerName := pod.OwnerReferences[0].Name
jobOwner, exists, err := c.jobInformer.GetStore().GetByKey(pod.Namespace + "/" + jobOwnerName)
if err != nil {
log.Printf("Can't find job '%s:%s`", pod.Namespace, jobOwnerName)

} else if exists && isOwnedByCronJob(getJobOwnerKinds(jobOwner.(*batchv1.Job))) {
return
}
toDelete := c.maybeDeletePod(pod.Status.Phase, age)
if toDelete {
c.deletePods(pod)
Expand Down Expand Up @@ -275,6 +288,14 @@ func getPodOwnerKinds(pod *corev1.Pod) []string {
return kinds
}

func getJobOwnerKinds(job *batchv1.Job) []string {
var kinds []string
for _, ow := range job.OwnerReferences {
kinds = append(kinds, ow.Kind)
}
return kinds
}

// isOwnedByJob returns true if and only if pod has a single owner
// and this owners kind is Job
func isOwnedByJob(ownerKinds []string) bool {
Expand All @@ -284,6 +305,15 @@ func isOwnedByJob(ownerKinds []string) bool {
return false
}

// isOwnedByCronJob returns true if and only if job has a single owner CronJob
// and this owners kind is CronJob
func isOwnedByCronJob(ownerKinds []string) bool {
if len(ownerKinds) == 1 && ownerKinds[0] == "CronJob" {
return true
}
return false
}

func extractPodFinishTime(podObj *corev1.Pod) time.Time {
for _, pc := range podObj.Status.Conditions {
// Looking for the time when pod's condition "Ready" became "false" (equals end of execution)
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/controller_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ type PodController struct {
// CreatedByAnnotation type used to match pods created by job
type CreatedByAnnotation struct {
Kind string
ApiVersion string
APIVersion string
Reference struct {
Kind string
Namespace string
Name string
Uid string
ApiVersion string
UID string
APIVersion string
ResourceVersion string
}
}
Expand Down Expand Up @@ -210,7 +210,6 @@ func (c *PodController) deleteObjects(podObj *corev1.Pod, parentJobName string)
} else {
log.Printf("dry-run: Pod '%s' would have been deleted", podObj.Name)
}
return
}

func (c *PodController) getParentJobName(podObj *corev1.Pod) (parentJobName string) {
Expand Down

0 comments on commit 7e342c1

Please sign in to comment.