Skip to content

Commit

Permalink
feature: triage leftover cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
emirozer committed Aug 26, 2019
1 parent db1796b commit 125bdcd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ kubectl doctor
* orphan replicasets (desired number of replicas are bigger than 0 but the available replicas are 0)
* leftover replicasets (desired number of replicas and the available # of replicas are 0)
* orphan deployments (desired number of replicas are bigger than 0 but the available replicas are 0)
* lefover deployments (desired number of replicas and the available # of replicas are 0)
* leftover deployments (desired number of replicas and the available # of replicas are 0)
* leftover cronjobs (last active date is more than a month)
16 changes: 15 additions & 1 deletion pkg/plugin/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
kubectl doctor
`
longDesc = `
kubectl-doctor plugin will scan the given namespace for any kind of anomalies and reports back to its user.
kubectl-doctor plugin will scan the given k8s cluster for any kind of anomalies and reports back to its user.
example anomalies:
* deployments that are older than 30d with 0 available,
* deployments that do not have minimum availability,
Expand Down Expand Up @@ -257,6 +257,20 @@ func (o *DoctorOptions) Run() error {

// triage replicasets ends

// triage jobs starts
log.Info("---")
log.Info("Starting triage of cronjob resources across cluster")
for _, ns := range o.FetchedNamespaces {
jobsTriage, err := triage.LeftoverJobs(o.KubeCli, ns)
if err != nil {
return err
}
if len(jobsTriage.Anomalies) > 0 {
log.WithFields(log.Fields{"resource": jobsTriage.ResourceType, "Anomalies": jobsTriage.Anomalies}).Warn(jobsTriage.AnomalyType)
}
}
// triage jobs end

return nil

}
28 changes: 28 additions & 0 deletions pkg/triage/job_triage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package triage

import (
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"time"
)

// LeftoverJobs gets a kubernetes.Clientset and a specific namespace string
// then proceeds to search if there are leftover cronjobs that were inactive for more than a month
func LeftoverJobs(kubeCli *kubernetes.Clientset, namespace string) (*Triage, error) {
listOfTriages := make([]string, 0)

jobs, err := kubeCli.BatchV1beta1().CronJobs(namespace).List(v1.ListOptions{})
if err != nil {
return nil, err
}
currentTime := time.Now()
for _, i := range jobs.Items {
if i.Status.LastScheduleTime != nil {
if currentTime.Sub(i.Status.LastScheduleTime.Local()) > 31 {
listOfTriages = append(listOfTriages, i.GetName())
}
}

}
return NewTriage("CronJobs", "Found leftover cronjobs in namespace: "+namespace, listOfTriages), nil
}

0 comments on commit 125bdcd

Please sign in to comment.