diff --git a/pkg/controller/compliancesuite/suitererunner.go b/pkg/controller/compliancesuite/suitererunner.go index 618c8f458..ab960e200 100644 --- a/pkg/controller/compliancesuite/suitererunner.go +++ b/pkg/controller/compliancesuite/suitererunner.go @@ -9,6 +9,7 @@ import ( cron "github.com/robfig/cron/v3" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" "sigs.k8s.io/controller-runtime/pkg/client" @@ -49,7 +50,7 @@ func (r *ReconcileComplianceSuite) validateSchedule(suite *compv1alpha1.Complian } func (r *ReconcileComplianceSuite) handleCreate(suite *compv1alpha1.ComplianceSuite, logger logr.Logger) error { - return r.cronJobCompatCreate(suite, reRunnerNamespacedName(suite.Name), logger) + return r.CreateOrUpdateRerunner(suite, reRunnerNamespacedName(suite.Name), logger) } // getPriorityClassName for rerunner from suite scan @@ -73,18 +74,12 @@ func (r *ReconcileComplianceSuite) getPriorityClassName(suite *compv1alpha1.Comp } func (r *ReconcileComplianceSuite) handleRerunnerDelete(suite *compv1alpha1.ComplianceSuite, logger logr.Logger) error { - key := reRunnerNamespacedName(suite.Name) - found, err := cronJobCompatGet(r, key) - if err != nil { - return err - } - inNs := client.InNamespace(common.GetComplianceOperatorNamespace()) withLabel := client.MatchingLabels{ compv1alpha1.SuiteLabel: suite.Name, compv1alpha1.SuiteScriptLabel: "", } - err = r.Client.DeleteAllOf(context.Background(), &corev1.Pod{}, inNs, withLabel) + err := r.Client.DeleteAllOf(context.Background(), &corev1.Pod{}, inNs, withLabel) if err != nil { return err } @@ -94,6 +89,13 @@ func (r *ReconcileComplianceSuite) handleRerunnerDelete(suite *compv1alpha1.Comp return err } + key := reRunnerNamespacedName(suite.Name) + found, err := r.getCronJob(key) + if err != nil && errors.IsNotFound(err) { + // The CronJob is already gone, so don't bother trying to clean + // it up. + return nil + } logger.Info("Deleting rerunner", "CronJob.Name", key.Name) - return cronJobCompatDelete(r, found) + return r.Client.Delete(context.TODO(), &found) } diff --git a/pkg/controller/compliancesuite/suitererunner_cron_compat.go b/pkg/controller/compliancesuite/suitererunner_cron_compat.go index cf8af755e..30232770d 100644 --- a/pkg/controller/compliancesuite/suitererunner_cron_compat.go +++ b/pkg/controller/compliancesuite/suitererunner_cron_compat.go @@ -2,18 +2,14 @@ package compliancesuite import ( "context" - "fmt" "github.com/go-logr/logr" batchv1 "k8s.io/api/batch/v1" - batchv1beta1 "k8s.io/api/batch/v1beta1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" compv1alpha1 "github.com/ComplianceAsCode/compliance-operator/pkg/apis/compliance/v1alpha1" "github.com/ComplianceAsCode/compliance-operator/pkg/controller/common" @@ -31,127 +27,51 @@ func GetRerunnerName(suiteName string) string { return suiteName + "-rerunner" } -func (r *ReconcileComplianceSuite) cronJobCompatCreate( +func (r *ReconcileComplianceSuite) CreateOrUpdateRerunner( suite *compv1alpha1.ComplianceSuite, key types.NamespacedName, logger logr.Logger, ) error { - var getObj client.Object + c := batchv1.CronJob{} + err := r.Client.Get(context.TODO(), key, &c) - priorityClassName, err := r.getPriorityClassName(suite) - if err != nil { - logger.Error(err, "Cannot get priority class name, scan will not be run with set priority class") - } - - createBeta := func() *batchv1beta1.CronJob { - getObj = &batchv1beta1.CronJob{} - return r.getBetaV1Rerunner(suite, priorityClassName) - } - - createV1 := func() *batchv1.CronJob { - getObj = &batchv1.CronJob{} - return r.getV1Rerunner(suite, priorityClassName) - } - - updateBeta := func() error { - getObjTyped, ok := getObj.(*batchv1beta1.CronJob) - if !ok { - return fmt.Errorf("failed to cast object to beta CronJob") - } - if getObjTyped.Spec.Schedule == suite.Spec.Schedule { - return nil - } - cronJobCopy := getObjTyped.DeepCopy() - cronJobCopy.Spec.Schedule = suite.Spec.Schedule - logger.Info("Updating beta rerunner", "CronJob.Name", cronJobCopy.GetName()) - return r.Client.Update(context.TODO(), cronJobCopy) + if err != nil && errors.IsNotFound(err) { + return r.createCronJob(suite, &c, logger) + } else if err != nil { + return err } - - updateV1 := func() error { - getObjTyped, ok := getObj.(*batchv1.CronJob) - if !ok { - return fmt.Errorf("failed to cast object to v1 CronJob") - } - if getObjTyped.Spec.Schedule == suite.Spec.Schedule && getObjTyped.Spec.Suspend == &suite.Spec.Suspend { - return nil - } - cronJobCopy := getObjTyped.DeepCopy() - cronJobCopy.Spec.Schedule = suite.Spec.Schedule - cronJobCopy.Spec.Suspend = &suite.Spec.Suspend - logger.Info("Updating v1 rerunner", "CronJob.Name", cronJobCopy.GetName()) - return r.Client.Update(context.TODO(), cronJobCopy) - } - - createAction := func(o client.Object) error { - err := r.Client.Get(context.TODO(), key, getObj) - if err != nil && errors.IsNotFound(err) { - // No re-runner found, create it - logger.Info("Creating rerunner", "CronJob.Name", o.GetName()) - return r.Client.Create(context.TODO(), o) - } else if err != nil { - return err - } - - switch o.(type) { - case *batchv1beta1.CronJob: - return updateBeta() - case *batchv1.CronJob: - return updateV1() - } - - return nil - } - - return doCompat(createAction, createBeta, createV1) + return r.updateCronJob(suite, &c, logger) } -func cronJobCompatGet(r *ReconcileComplianceSuite, key types.NamespacedName) (client.Object, error) { - var retObj client.Object - - getEmptyBeta := func() *batchv1beta1.CronJob { - return &batchv1beta1.CronJob{} - } - - getEmptyV1 := func() *batchv1.CronJob { - return &batchv1.CronJob{} - } - - getAction := func(o client.Object) error { - err := r.Client.Get(context.TODO(), key, o) - if err != nil && errors.IsNotFound(err) { - // No re-runner found, we're good - return nil - } else if err != nil { - return err - } - - retObj = o - return nil +func (r *ReconcileComplianceSuite) getCronJob(key types.NamespacedName) (batchv1.CronJob, error) { + c := batchv1.CronJob{} + err := r.Client.Get(context.TODO(), key, &c) + if err != nil { + return c, err } - - err := doCompat(getAction, getEmptyBeta, getEmptyV1) - return retObj, err + return c, nil } -func cronJobCompatDelete(r *ReconcileComplianceSuite, cron client.Object) error { - if cron == nil { - // for cases where cronJobCompatGet returns nil,nil - return nil +func (r *ReconcileComplianceSuite) createCronJob(suite *compv1alpha1.ComplianceSuite, c *batchv1.CronJob, logger logr.Logger) error { + logger.Info("Creating rerunner", "CronJob.Name", c.GetName()) + priorityClassName, err := r.getPriorityClassName(suite) + if err != nil { + logger.Error(err, "Cannot get priority class name, scan will not be run with set priority class") } - - return r.Client.Delete(context.TODO(), cron) + s := r.generateRerunnerSpec(suite, priorityClassName) + return r.Client.Create(context.TODO(), s) } -type compatAction func(o client.Object) error -type getBetaCron func() *batchv1beta1.CronJob -type getV1Cron func() *batchv1.CronJob - -func doCompat(what compatAction, betaCron getBetaCron, v1cron getV1Cron) error { - err := what(v1cron()) - if meta.IsNoMatchError(err) { - return what(betaCron()) +func (r *ReconcileComplianceSuite) updateCronJob(suite *compv1alpha1.ComplianceSuite, c *batchv1.CronJob, logger logr.Logger) error { + if c.Spec.Schedule == suite.Spec.Schedule && c.Spec.Suspend == &suite.Spec.Suspend { + logger.Info("Suite rerunner configuration is up-to-date, no update necessary", "CronJob.Name", c.GetName()) + return nil } - return err + logger.Info("Updating rerunner configuration", "CronJob.Name", c.GetName()) + co := c.DeepCopy() + co.Spec.Schedule = suite.Spec.Schedule + co.Spec.Suspend = &suite.Spec.Suspend + return r.Client.Update(context.TODO(), co) } func reRunnerNamespacedName(suiteName string) types.NamespacedName { @@ -170,7 +90,7 @@ func reRunnerObjectMeta(suiteName string) *metav1.ObjectMeta { } } -func (r *ReconcileComplianceSuite) getV1Rerunner( +func (r *ReconcileComplianceSuite) generateRerunnerSpec( suite *compv1alpha1.ComplianceSuite, priorityClassName string, ) *batchv1.CronJob { @@ -187,23 +107,6 @@ func (r *ReconcileComplianceSuite) getV1Rerunner( } } -func (r *ReconcileComplianceSuite) getBetaV1Rerunner( - suite *compv1alpha1.ComplianceSuite, - priorityClassName string, -) *batchv1beta1.CronJob { - return &batchv1beta1.CronJob{ - ObjectMeta: *reRunnerObjectMeta(suite.Name), - Spec: batchv1beta1.CronJobSpec{ - Schedule: suite.Spec.Schedule, - JobTemplate: batchv1beta1.JobTemplateSpec{ - Spec: batchv1.JobSpec{ - Template: *r.getRerunnerPodTemplate(suite, priorityClassName), - }, - }, - }, - } -} - func (r *ReconcileComplianceSuite) getRerunnerPodTemplate( suite *compv1alpha1.ComplianceSuite, priorityClassName string,