From 87a4e432b78b150e6af846543083d47b30bba8cb Mon Sep 17 00:00:00 2001 From: yutachaos <18604471+yutachaos@users.noreply.github.com> Date: Tue, 24 Aug 2021 17:01:26 +0900 Subject: [PATCH 1/2] Added support for CronJob V1 acquisition. --- controller.go | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/controller.go b/controller.go index 62ee873..df30616 100644 --- a/controller.go +++ b/controller.go @@ -13,7 +13,6 @@ import ( "github.com/yutachaos/kube-job-notifier/pkg/notification" "golang.org/x/xerrors" batchv1 "k8s.io/api/batch/v1" - "k8s.io/api/batch/v1beta1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -84,14 +83,14 @@ func NewController( klog.Infof("Job started: %v", newJob.Status) - cronJob, err := getCronJobFromOwnerReferences(kubeclientset, newJob) + cronJob, err := getCronJobNameFromOwnerReferences(kubeclientset, newJob) if err != nil { klog.Errorf("Get cronjob failed: %v", err) } messageParam := notification.MessageTemplateParam{ JobName: newJob.Name, - CronJobName: cronJob.Name, + CronJobName: cronJob, Namespace: newJob.Namespace, StartTime: newJob.Status.StartTime, } @@ -123,20 +122,20 @@ func NewController( klog.Errorf("Get pods failed: %v", err) return } - cronJob, err := getCronJobFromOwnerReferences(kubeclientset, newJob) + cronJobName, err := getCronJobNameFromOwnerReferences(kubeclientset, newJob) if err != nil { klog.Errorf("Get cronjob failed: %v", err) return } - jobLogStr, err := getPodLogs(kubeclientset, jobPod, cronJob.Name) + jobLogStr, err := getPodLogs(kubeclientset, jobPod, cronJobName) if err != nil { klog.Errorf("Get pods failed: %v", err) } messageParam := notification.MessageTemplateParam{ JobName: newJob.Name, - CronJobName: cronJob.Name, + CronJobName: cronJobName, Namespace: newJob.Namespace, StartTime: newJob.Status.StartTime, CompletionTime: newJob.Status.CompletionTime, @@ -154,7 +153,7 @@ func NewController( err = datadogSubscription.SuccessEvent( monitoring.JobInfo{ - CronJobName: cronJob.Name, + CronJobName: cronJobName, Name: newJob.Name, Namespace: newJob.Namespace, }) @@ -172,20 +171,20 @@ func NewController( return } - cronJob, err := getCronJobFromOwnerReferences(kubeclientset, newJob) + cronJobName, err := getCronJobNameFromOwnerReferences(kubeclientset, newJob) if err != nil { klog.Errorf("Get cronjob failed: %v", err) return } - jobLogStr, err := getPodLogs(kubeclientset, jobPod, cronJob.Name) + jobLogStr, err := getPodLogs(kubeclientset, jobPod, cronJobName) if err != nil { klog.Errorf("Get pods log failed: %v", err) } messageParam := notification.MessageTemplateParam{ JobName: newJob.Name, - CronJobName: cronJob.Name, + CronJobName: cronJobName, Namespace: newJob.Namespace, StartTime: newJob.Status.StartTime, CompletionTime: newJob.Status.CompletionTime, @@ -200,7 +199,7 @@ func NewController( if os.Getenv("DATADOG_ENABLE") == "true" { err = datadogSubscription.FailEvent( monitoring.JobInfo{ - CronJobName: cronJob.Name, + CronJobName: cronJobName, Name: newJob.Name, Namespace: newJob.Namespace, }) @@ -258,14 +257,27 @@ func getPodFromControllerUID(kubeclientset kubernetes.Interface, job *batchv1.Jo return jobPod, nil } -func getCronJobFromOwnerReferences(kubeclientset kubernetes.Interface, job *batchv1.Job) (v1beta1.CronJob, error) { +func getCronJobNameFromOwnerReferences(kubeclientset kubernetes.Interface, job *batchv1.Job) (string, error) { if ownerReferences, ok := funk.Filter(job.OwnerReferences, func(ownerReference metav1.OwnerReference) bool { return ownerReference.Kind == "CronJob" }).([]metav1.OwnerReference); ok && len(ownerReferences) > 0 { - cronJob, err := kubeclientset.BatchV1beta1().CronJobs(job.Namespace).Get(context.TODO(), + cronJobBeta, err := kubeclientset.BatchV1beta1().CronJobs(job.Namespace).Get(context.TODO(), + ownerReferences[0].Name, + metav1.GetOptions{ + TypeMeta: metav1.TypeMeta{ + Kind: ownerReferences[0].Kind, + APIVersion: ownerReferences[0].APIVersion, + }, + }) + + if err == nil { + return cronJobBeta.Name, err + } + + cronJobV1, err := kubeclientset.BatchV1().CronJobs(job.Namespace).Get(context.TODO(), ownerReferences[0].Name, metav1.GetOptions{ TypeMeta: metav1.TypeMeta{ @@ -275,12 +287,12 @@ func getCronJobFromOwnerReferences(kubeclientset kubernetes.Interface, job *batc }) if err != nil { - return v1beta1.CronJob{}, err + return "", err } - return *cronJob, err + return cronJobV1.Name, err } - return v1beta1.CronJob{}, nil + return "", nil } func getPodLogs(clientset kubernetes.Interface, pod corev1.Pod, cronJobName string) (string, error) { From 407e31cc8a6910ee1991b7274a7599805613bfb7 Mon Sep 17 00:00:00 2001 From: yutachaos <18604471+yutachaos@users.noreply.github.com> Date: Wed, 25 Aug 2021 09:47:43 +0900 Subject: [PATCH 2/2] Changed var name --- controller.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/controller.go b/controller.go index df30616..efff81d 100644 --- a/controller.go +++ b/controller.go @@ -257,7 +257,7 @@ func getPodFromControllerUID(kubeclientset kubernetes.Interface, job *batchv1.Jo return jobPod, nil } -func getCronJobNameFromOwnerReferences(kubeclientset kubernetes.Interface, job *batchv1.Job) (string, error) { +func getCronJobNameFromOwnerReferences(kubeclientset kubernetes.Interface, job *batchv1.Job) (cronJobName string, err error) { if ownerReferences, ok := funk.Filter(job.OwnerReferences, func(ownerReference metav1.OwnerReference) bool { @@ -286,13 +286,11 @@ func getCronJobNameFromOwnerReferences(kubeclientset kubernetes.Interface, job * }, }) - if err != nil { - return "", err + if err == nil { + return cronJobV1.Name, err } - return cronJobV1.Name, err - } - return "", nil + return cronJobName, err } func getPodLogs(clientset kubernetes.Interface, pod corev1.Pod, cronJobName string) (string, error) {