Skip to content

Commit

Permalink
fix: revert CronJobs to batch/v1beta1 (#1105)
Browse files Browse the repository at this point in the history
Resolves: #1102

Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
  • Loading branch information
danielpacak authored Apr 4, 2022
1 parent 1c26090 commit ac710b6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 26 deletions.
17 changes: 9 additions & 8 deletions itest/starboard-operator/behavior/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/aquasecurity/starboard/pkg/plugin/conftest"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -148,18 +149,18 @@ func VulnerabilityScannerBehavior(inputs *Inputs) func() {
Context("When CronJob is created", func() {

var ctx context.Context
var cronJob *batchv1.CronJob
var cronJob *batchv1beta1.CronJob

BeforeEach(func() {
ctx = context.Background()
cronJob = &batchv1.CronJob{
cronJob = &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Namespace: inputs.PrimaryNamespace,
Name: "hello-" + rand.String(5),
},
Spec: batchv1.CronJobSpec{
Spec: batchv1beta1.CronJobSpec{
Schedule: "*/1 * * * *",
JobTemplate: batchv1.JobTemplateSpec{
JobTemplate: batchv1beta1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -324,18 +325,18 @@ func ConfigurationCheckerBehavior(inputs *Inputs) func() {
Context("When CronJob is created", func() {

var ctx context.Context
var cronJob *batchv1.CronJob
var cronJob *batchv1beta1.CronJob

BeforeEach(func() {
ctx = context.Background()
cronJob = &batchv1.CronJob{
cronJob = &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Namespace: inputs.PrimaryNamespace,
Name: "hello-" + rand.String(5),
},
Spec: batchv1.CronJobSpec{
Spec: batchv1beta1.CronJobSpec{
Schedule: "*/1 * * * *",
JobTemplate: batchv1.JobTemplateSpec{
JobTemplate: batchv1beta1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Expand Down
7 changes: 4 additions & 3 deletions itest/starboard/starboard_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/aquasecurity/starboard/pkg/vulnerabilityreport"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1163,14 +1164,14 @@ var _ = Describe("Starboard CLI", func() {

BeforeEach(func() {
ctx = context.TODO()
object = &batchv1.CronJob{
object = &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: "hello" + "-" + rand.String(5),
Namespace: testNamespace.Name,
},
Spec: batchv1.CronJobSpec{
Spec: batchv1beta1.CronJobSpec{
Schedule: "*/1 * * * *",
JobTemplate: batchv1.JobTemplateSpec{
JobTemplate: batchv1beta1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Expand Down
3 changes: 2 additions & 1 deletion pkg/configauditreport/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (r *ResourceController) SetupWithManager(mgr ctrl.Manager) error {
{kind: kube.KindReplicationController, forObject: &corev1.ReplicationController{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindStatefulSet, forObject: &appsv1.StatefulSet{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindDaemonSet, forObject: &appsv1.DaemonSet{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindCronJob, forObject: &batchv1.CronJob{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindCronJob, forObject: &batchv1beta1.CronJob{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindJob, forObject: &batchv1.Job{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindService, forObject: &corev1.Service{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindConfigMap, forObject: &corev1.ConfigMap{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
Expand Down
19 changes: 10 additions & 9 deletions pkg/kube/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aquasecurity/starboard/pkg/starboard"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
Expand Down Expand Up @@ -227,7 +228,7 @@ func ObjectRefFromKindAndObjectKey(kind Kind, name client.ObjectKey) ObjectRef {
// security report.
func ComputeSpecHash(obj client.Object) (string, error) {
switch t := obj.(type) {
case *corev1.Pod, *appsv1.Deployment, *appsv1.ReplicaSet, *corev1.ReplicationController, *appsv1.StatefulSet, *appsv1.DaemonSet, *batchv1.CronJob, *batchv1.Job:
case *corev1.Pod, *appsv1.Deployment, *appsv1.ReplicaSet, *corev1.ReplicationController, *appsv1.StatefulSet, *appsv1.DaemonSet, *batchv1beta1.CronJob, *batchv1.Job:
spec, err := GetPodSpec(obj)
if err != nil {
return "", err
Expand Down Expand Up @@ -278,8 +279,8 @@ func GetPodSpec(obj client.Object) (corev1.PodSpec, error) {
return (obj.(*appsv1.StatefulSet)).Spec.Template.Spec, nil
case *appsv1.DaemonSet:
return (obj.(*appsv1.DaemonSet)).Spec.Template.Spec, nil
case *batchv1.CronJob:
return (obj.(*batchv1.CronJob)).Spec.JobTemplate.Spec.Template.Spec, nil
case *batchv1beta1.CronJob:
return (obj.(*batchv1beta1.CronJob)).Spec.JobTemplate.Spec.Template.Spec, nil
case *batchv1.Job:
return (obj.(*batchv1.Job)).Spec.Template.Spec, nil
default:
Expand Down Expand Up @@ -311,7 +312,7 @@ func (o *ObjectResolver) ObjectFromObjectRef(ctx context.Context, ref ObjectRef)
case KindDaemonSet:
obj = &appsv1.DaemonSet{}
case KindCronJob:
obj = &batchv1.CronJob{}
obj = &batchv1beta1.CronJob{}
case KindJob:
obj = &batchv1.Job{}
case KindService:
Expand Down Expand Up @@ -386,7 +387,7 @@ func (o *ObjectResolver) ReportOwner(ctx context.Context, obj client.Object) (cl
}
// Pod controlled by sth else (usually frameworks)
return obj, nil
case *appsv1.ReplicaSet, *corev1.ReplicationController, *appsv1.StatefulSet, *appsv1.DaemonSet, *batchv1.CronJob:
case *appsv1.ReplicaSet, *corev1.ReplicationController, *appsv1.StatefulSet, *appsv1.DaemonSet, *batchv1beta1.CronJob:
return obj, nil
default:
return obj, nil
Expand Down Expand Up @@ -475,21 +476,21 @@ func (o *ObjectResolver) ReplicaSetByPod(ctx context.Context, pod *corev1.Pod) (
return rsCopy, err
}

func (o *ObjectResolver) CronJobByJob(ctx context.Context, job *batchv1.Job) (*batchv1.CronJob, error) {
func (o *ObjectResolver) CronJobByJob(ctx context.Context, job *batchv1.Job) (*batchv1beta1.CronJob, error) {
controller := metav1.GetControllerOf(job)
if controller == nil {
return nil, fmt.Errorf("did not find a controller for job %q", job.Name)
}
if controller.Kind != "CronJob" {
return nil, fmt.Errorf("pod %q is controlled by a %q, want CronJob", job.Name, controller.Kind)
}
cj := &batchv1.CronJob{}
cj := &batchv1beta1.CronJob{}
err := o.Client.Get(ctx, client.ObjectKey{Namespace: job.Namespace, Name: controller.Name}, cj)
if err != nil {
return nil, err
}
obj, err := o.ensureGVK(cj)
return obj.(*batchv1.CronJob), err
return obj.(*batchv1beta1.CronJob), err
}

func (o *ObjectResolver) JobByPod(ctx context.Context, pod *corev1.Pod) (*batchv1.Job, error) {
Expand Down Expand Up @@ -583,7 +584,7 @@ func (o *ObjectResolver) GetNodeName(ctx context.Context, obj client.Object) (st
return "", err
}
return pods[0].Spec.NodeName, nil
case *batchv1.CronJob:
case *batchv1beta1.CronJob:
return "", ErrUnSupportedKind
case *batchv1.Job:
pods, err := o.getActivePodsByLabelSelector(ctx, obj.GetNamespace(), obj.(*batchv1.Job).Spec.Selector.MatchLabels)
Expand Down
7 changes: 4 additions & 3 deletions pkg/kube/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -465,9 +466,9 @@ func TestGetPodSpec(t *testing.T) {
},
{
name: "Should return PodSpec for CronJob",
object: &batchv1.CronJob{
Spec: batchv1.CronJobSpec{
JobTemplate: batchv1.JobTemplateSpec{
object: &batchv1beta1.CronJob{
Spec: batchv1beta1.CronJobSpec{
JobTemplate: batchv1beta1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Expand Down
3 changes: 2 additions & 1 deletion pkg/operator/controller/configauditreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (r *ConfigAuditReportReconciler) SetupWithManager(mgr ctrl.Manager) error {
{kind: kube.KindReplicationController, forObject: &corev1.ReplicationController{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindStatefulSet, forObject: &appsv1.StatefulSet{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindDaemonSet, forObject: &appsv1.DaemonSet{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindCronJob, forObject: &batchv1.CronJob{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindCronJob, forObject: &batchv1beta1.CronJob{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindJob, forObject: &batchv1.Job{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindService, forObject: &corev1.Service{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
{kind: kube.KindConfigMap, forObject: &corev1.ConfigMap{}, ownsObject: &v1alpha1.ConfigAuditReport{}},
Expand Down
2 changes: 2 additions & 0 deletions pkg/starboard/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/google/go-containerregistry/pkg/name"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
Expand All @@ -29,6 +30,7 @@ func NewScheme() *runtime.Scheme {
_ = corev1.AddToScheme(scheme)
_ = appsv1.AddToScheme(scheme)
_ = batchv1.AddToScheme(scheme)
_ = batchv1beta1.AddToScheme(scheme)
_ = rbacv1.AddToScheme(scheme)
_ = networkingv1.AddToScheme(scheme)
_ = policyv1beta1.AddToScheme(scheme)
Expand Down
3 changes: 2 additions & 1 deletion pkg/vulnerabilityreport/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
k8sapierror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (r *WorkloadController) SetupWithManager(mgr ctrl.Manager) error {
{kind: kube.KindReplicationController, forObject: &corev1.ReplicationController{}, ownsObject: &v1alpha1.VulnerabilityReport{}},
{kind: kube.KindStatefulSet, forObject: &appsv1.StatefulSet{}, ownsObject: &v1alpha1.VulnerabilityReport{}},
{kind: kube.KindDaemonSet, forObject: &appsv1.DaemonSet{}, ownsObject: &v1alpha1.VulnerabilityReport{}},
{kind: kube.KindCronJob, forObject: &batchv1.CronJob{}, ownsObject: &v1alpha1.VulnerabilityReport{}},
{kind: kube.KindCronJob, forObject: &batchv1beta1.CronJob{}, ownsObject: &v1alpha1.VulnerabilityReport{}},
{kind: kube.KindJob, forObject: &batchv1.Job{}, ownsObject: &v1alpha1.VulnerabilityReport{}},
}

Expand Down

0 comments on commit ac710b6

Please sign in to comment.