diff --git a/internal/controllers/terraformlayer/conditions.go b/internal/controllers/terraformlayer/conditions.go index 2a7072d9..32eb7a0c 100644 --- a/internal/controllers/terraformlayer/conditions.go +++ b/internal/controllers/terraformlayer/conditions.go @@ -23,8 +23,20 @@ func (r *Reconciler) IsPlanArtifactUpToDate(t *configv1alpha1.TerraformLayer) (m condition.Status = metav1.ConditionFalse return condition, false } - lastPlanDate, _ := time.Parse(time.UnixDate, value) - delta, _ := time.ParseDuration(r.Config.Controller.Timers.DriftDetection) + lastPlanDate, err := time.Parse(time.UnixDate, value) + if err != nil { + condition.Reason = "ParseError" + condition.Message = "Could not parse time from annotation" + condition.Status = metav1.ConditionFalse + return condition, false + } + delta, err := time.ParseDuration(r.Config.Controller.Timers.DriftDetection) + if err != nil { + condition.Reason = "ParseError" + condition.Message = "Could not parse timer drift detection period" + condition.Status = metav1.ConditionFalse + return condition, false + } nextPlanDate := lastPlanDate.Add(delta) now := time.Now() if nextPlanDate.After(now) { diff --git a/internal/controllers/terraformlayer/controller.go b/internal/controllers/terraformlayer/controller.go index f485c775..272177dd 100644 --- a/internal/controllers/terraformlayer/controller.go +++ b/internal/controllers/terraformlayer/controller.go @@ -65,16 +65,24 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu log.Error(err, "Failed to get TerraformLayer") return ctrl.Result{}, err } + deltaOnError, err := time.ParseDuration(r.Config.Controller.Timers.OnError) + if err != nil { + log.Error(err, "could not parse timer drift detection period") + return ctrl.Result{}, err + } locked, err := lock.IsLocked(ctx, r.Client, layer) if err != nil { log.Error(err, "Failed to get Lease Resource.") - delta, _ := time.ParseDuration(r.Config.Controller.Timers.OnError) - return ctrl.Result{RequeueAfter: delta}, err + return ctrl.Result{RequeueAfter: deltaOnError}, err + } + deltaWaitAction, err := time.ParseDuration(r.Config.Controller.Timers.WaitAction) + if err != nil { + log.Error(err, "could not parse timer wait action period") + return ctrl.Result{}, err } if locked { log.Info("Layer is locked, skipping reconciliation.") - delta, _ := time.ParseDuration(r.Config.Controller.Timers.WaitAction) - return ctrl.Result{RequeueAfter: delta}, nil + return ctrl.Result{RequeueAfter: deltaWaitAction}, nil } repository := &configv1alpha1.TerraformRepository{} log.Info("Getting Linked TerraformRepository") @@ -84,13 +92,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu }, repository) if errors.IsNotFound(err) { log.Info("TerraformRepository not found, ignoring layer until it's modified.") - delta, _ := time.ParseDuration(r.Config.Controller.Timers.OnError) - return ctrl.Result{RequeueAfter: delta}, err + return ctrl.Result{RequeueAfter: deltaOnError}, err } if err != nil { log.Error(err, "Failed to get TerraformRepository") - delta, _ := time.ParseDuration(r.Config.Controller.Timers.OnError) - return ctrl.Result{RequeueAfter: delta}, err + return ctrl.Result{RequeueAfter: deltaOnError}, err } state, conditions := r.GetState(ctx, layer) layer.Status = configv1alpha1.TerraformLayerStatus{Conditions: conditions} diff --git a/internal/controllers/terraformlayer/states.go b/internal/controllers/terraformlayer/states.go index 6e8ef66f..ae7c2a23 100644 --- a/internal/controllers/terraformlayer/states.go +++ b/internal/controllers/terraformlayer/states.go @@ -41,7 +41,12 @@ type IdleState struct{} func (s *IdleState) getHandler() func(ctx context.Context, t *Reconciler, r *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result { return func(ctx context.Context, t *Reconciler, r *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result { - delta, _ := time.ParseDuration(t.Config.Controller.Timers.DriftDetection) + log := log.FromContext(ctx) + delta, err := time.ParseDuration(t.Config.Controller.Timers.DriftDetection) + if err != nil { + log.Error(err, "could not parse timer drift detection period") + return ctrl.Result{} + } return ctrl.Result{RequeueAfter: delta} } } @@ -51,21 +56,28 @@ type PlanNeededState struct{} func (s *PlanNeededState) getHandler() func(ctx context.Context, t *Reconciler, r *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result { return func(ctx context.Context, t *Reconciler, r *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result { log := log.FromContext(ctx) - err := lock.CreateLock(ctx, t.Client, r) + deltaOnError, err := time.ParseDuration(t.Config.Controller.Timers.OnError) + if err != nil { + log.Error(err, "could not parse timer on error period") + return ctrl.Result{} + } + err = lock.CreateLock(ctx, t.Client, r) if err != nil { log.Error(err, "Could not set lock on layer, requeing resource") - delta, _ := time.ParseDuration(t.Config.Controller.Timers.OnError) - return ctrl.Result{RequeueAfter: delta} + return ctrl.Result{RequeueAfter: deltaOnError} } pod := t.getPod(r, repository, "plan") err = t.Client.Create(ctx, &pod) if err != nil { log.Error(err, "Failed to create pod for Plan action") _ = lock.DeleteLock(ctx, t.Client, r) - delta, _ := time.ParseDuration(t.Config.Controller.Timers.OnError) - return ctrl.Result{RequeueAfter: delta} + return ctrl.Result{RequeueAfter: deltaOnError} + } + delta, err := time.ParseDuration(t.Config.Controller.Timers.WaitAction) + if err != nil { + log.Error(err, "could not parse timer wait action period") + return ctrl.Result{} } - delta, _ := time.ParseDuration(t.Config.Controller.Timers.WaitAction) return ctrl.Result{RequeueAfter: delta} } } @@ -75,21 +87,28 @@ type ApplyNeededState struct{} func (s *ApplyNeededState) getHandler() func(ctx context.Context, t *Reconciler, r *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result { return func(ctx context.Context, t *Reconciler, r *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result { log := log.FromContext(ctx) - err := lock.CreateLock(ctx, t.Client, r) + deltaOnError, err := time.ParseDuration(t.Config.Controller.Timers.OnError) + if err != nil { + log.Error(err, "could not parse timer on error period") + return ctrl.Result{} + } + err = lock.CreateLock(ctx, t.Client, r) if err != nil { log.Error(err, "Could not set lock on layer, requeing resource") - delta, _ := time.ParseDuration(t.Config.Controller.Timers.OnError) - return ctrl.Result{RequeueAfter: delta} + return ctrl.Result{RequeueAfter: deltaOnError} } pod := t.getPod(r, repository, "apply") err = t.Client.Create(ctx, &pod) if err != nil { log.Error(err, "[TerraformApplyNeeded] Failed to create pod for Apply action") _ = lock.DeleteLock(ctx, t.Client, r) - delta, _ := time.ParseDuration(t.Config.Controller.Timers.OnError) - return ctrl.Result{RequeueAfter: delta} + return ctrl.Result{RequeueAfter: deltaOnError} + } + delta, err := time.ParseDuration(t.Config.Controller.Timers.WaitAction) + if err != nil { + log.Error(err, "could not parse timer wait action period") + return ctrl.Result{} } - delta, _ := time.ParseDuration(t.Config.Controller.Timers.WaitAction) return ctrl.Result{RequeueAfter: delta} } }