Skip to content

Commit

Permalink
handling cronjob
Browse files Browse the repository at this point in the history
Signed-off-by: Prateek Nandle <prateeknandle@gmail.com>
  • Loading branch information
Prateeknandle committed May 7, 2024
1 parent f2f3362 commit 185220d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion KubeArmor/core/k8sHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,18 @@ func getTopLevelOwner(obj metav1.ObjectMeta, namespace string, objkind string) (
return "", "", "", err
}
if len(job.OwnerReferences) > 0 {
return getTopLevelOwner(job.ObjectMeta, namespace, "Job")
return getTopLevelOwner(job.ObjectMeta, namespace, "CronJob")
}
return job.Name, "Job", job.Namespace, nil
case "CronJob":
cronJob, err := K8s.K8sClient.BatchV1().CronJobs(namespace).Get(context.Background(), ownerRef.Name, metav1.GetOptions{})
if err != nil {
return "", "", "", err
}
if len(cronJob.OwnerReferences) > 0 {
return getTopLevelOwner(cronJob.ObjectMeta, namespace, "CronJob")
}
return cronJob.Name, "CronJob", cronJob.Namespace, nil
case "Deployment":
deployment, err := K8s.K8sClient.AppsV1().Deployments(namespace).Get(context.Background(), ownerRef.Name, metav1.GetOptions{})
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions KubeArmor/core/kubeUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,22 @@ func (dm *KubeArmorDaemon) WatchK8sPods() {
}
}

} else if dm.OwnerInfo[pod.Metadata["podName"]].Ref == "Job" {
job, err := K8s.K8sClient.BatchV1().Jobs(pod.Metadata["namespaceName"]).Get(context.Background(), podOwnerName, metav1.GetOptions{})
if err == nil {
for _, c := range job.Spec.Template.Spec.Containers {
containers = append(containers, c.Name)
}
}

} else if dm.OwnerInfo[pod.Metadata["podName"]].Ref == "CronJob" {
cronJob, err := K8s.K8sClient.BatchV1().CronJobs(pod.Metadata["namespaceName"]).Get(context.Background(), podOwnerName, metav1.GetOptions{})
if err == nil {
for _, c := range cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers {
containers = append(containers, c.Name)
}
}

}

}
Expand Down

0 comments on commit 185220d

Please sign in to comment.