Skip to content

Commit

Permalink
chore: handle all time parsing errors (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
spoukke authored Feb 7, 2023
1 parent 30d84d6 commit 4894e41
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
16 changes: 14 additions & 2 deletions internal/controllers/terraformlayer/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 14 additions & 8 deletions internal/controllers/terraformlayer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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}
Expand Down
45 changes: 32 additions & 13 deletions internal/controllers/terraformlayer/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}
Expand All @@ -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}
}
}
Expand All @@ -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}
}
}

0 comments on commit 4894e41

Please sign in to comment.