Skip to content

Commit

Permalink
feat(layer): include the last TerraformRun in the status (#183)
Browse files Browse the repository at this point in the history
* feat(layer): include the last TerraformRun in the status

* fix: still had a v0.11.2 controller-gen
  • Loading branch information
AlanLonguet authored Nov 9, 2023
1 parent a37283c commit eeedb82
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/terraformlayer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type TerraformLayerStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
State string `json:"state,omitempty"`
LastResult string `json:"lastResult,omitempty"`
LastRun string `json:"lastRun,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
6 changes: 3 additions & 3 deletions cmd/controllers/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func buildControllersStartCmd(app *burrito.App) *cobra.Command {
},
}

defaultDriftDetectionTimer, _ := time.ParseDuration("20m")
defaultOnErrorTimer, _ := time.ParseDuration("1m")
defaultWaitActionTimer, _ := time.ParseDuration("1m")
defaultDriftDetectionTimer, _ := time.ParseDuration("1h")
defaultOnErrorTimer, _ := time.ParseDuration("30s")
defaultWaitActionTimer, _ := time.ParseDuration("15s")
defaultFailureGracePeriod, _ := time.ParseDuration("15s")

cmd.Flags().StringSliceVar(&app.Config.Controller.Namespaces, "namespaces", []string{"burrito-system"}, "list of namespaces to watch")
Expand Down
8 changes: 6 additions & 2 deletions internal/controllers/terraformlayer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
if err != nil {
lastResult = []byte("Error getting last Result")
}
layer.Status = configv1alpha1.TerraformLayerStatus{Conditions: conditions, State: getStateString(state), LastResult: string(lastResult)}
result := state.getHandler()(ctx, r, layer, repository)
result, run := state.getHandler()(ctx, r, layer, repository)
runStatus := ""
if run != nil {
runStatus = run.Name
}
layer.Status = configv1alpha1.TerraformLayerStatus{Conditions: conditions, State: getStateString(state), LastResult: string(lastResult), LastRun: runStatus}
err = r.Client.Status().Update(ctx, layer)
if err != nil {
log.Errorf("could not update layer %s status: %s", layer.Name, err)
Expand Down
20 changes: 10 additions & 10 deletions internal/controllers/terraformlayer/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
)

type Handler func(context.Context, *Reconciler, *configv1alpha1.TerraformLayer, *configv1alpha1.TerraformRepository) ctrl.Result
type Handler func(context.Context, *Reconciler, *configv1alpha1.TerraformLayer, *configv1alpha1.TerraformRepository) (ctrl.Result, *configv1alpha1.TerraformRun)

type State interface {
getHandler() Handler
Expand Down Expand Up @@ -42,43 +42,43 @@ func (r *Reconciler) GetState(ctx context.Context, layer *configv1alpha1.Terrafo
type Idle struct{}

func (s *Idle) getHandler() Handler {
return func(ctx context.Context, r *Reconciler, layer *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result {
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.DriftDetection}
return func(ctx context.Context, r *Reconciler, layer *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) (ctrl.Result, *configv1alpha1.TerraformRun) {
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.DriftDetection}, nil
}
}

type PlanNeeded struct{}

func (s *PlanNeeded) getHandler() Handler {
return func(ctx context.Context, r *Reconciler, layer *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result {
return func(ctx context.Context, r *Reconciler, layer *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) (ctrl.Result, *configv1alpha1.TerraformRun) {
log := log.WithContext(ctx)
run := r.getRun(layer, repository, "plan")
err := r.Client.Create(ctx, &run)
if err != nil {
log.Errorf("failed to create TerraformRun for Plan action on layer %s: %s", layer.Name, err)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}, nil
}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}, &run
}
}

type ApplyNeeded struct{}

func (s *ApplyNeeded) getHandler() Handler {
return func(ctx context.Context, r *Reconciler, layer *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result {
return func(ctx context.Context, r *Reconciler, layer *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) (ctrl.Result, *configv1alpha1.TerraformRun) {
log := log.WithContext(ctx)
remediationStrategy := configv1alpha1.GetRemediationStrategy(repository, layer)
if !remediationStrategy.AutoApply {
log.Infof("layer %s is in dry mode, no action taken", layer.Name)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.DriftDetection}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.DriftDetection}, nil
}
run := r.getRun(layer, repository, "apply")
err := r.Client.Create(ctx, &run)
if err != nil {
log.Errorf("failed to create TerraformRun for Apply action on layer %s: %s", layer.Name, err)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}, nil
}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}, &run
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,8 @@ spec:
type: array
lastResult:
type: string
lastRun:
type: string
state:
type: string
type: object
Expand Down
2 changes: 2 additions & 0 deletions manifests/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,8 @@ spec:
type: array
lastResult:
type: string
lastRun:
type: string
state:
type: string
type: object
Expand Down

0 comments on commit eeedb82

Please sign in to comment.