diff --git a/api/v1alpha1/terraformlayer_types.go b/api/v1alpha1/terraformlayer_types.go index db9bffa2..1f0411d5 100644 --- a/api/v1alpha1/terraformlayer_types.go +++ b/api/v1alpha1/terraformlayer_types.go @@ -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 diff --git a/cmd/controllers/start.go b/cmd/controllers/start.go index 4d7db593..1d7e168b 100644 --- a/cmd/controllers/start.go +++ b/cmd/controllers/start.go @@ -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") diff --git a/internal/controllers/terraformlayer/controller.go b/internal/controllers/terraformlayer/controller.go index e6c2820e..0a3c119e 100644 --- a/internal/controllers/terraformlayer/controller.go +++ b/internal/controllers/terraformlayer/controller.go @@ -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) diff --git a/internal/controllers/terraformlayer/states.go b/internal/controllers/terraformlayer/states.go index a659cce1..a83c2ac8 100644 --- a/internal/controllers/terraformlayer/states.go +++ b/internal/controllers/terraformlayer/states.go @@ -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 @@ -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 } } diff --git a/manifests/crds/config.terraform.padok.cloud_terraformlayers.yaml b/manifests/crds/config.terraform.padok.cloud_terraformlayers.yaml index 3758357e..7f9394ed 100644 --- a/manifests/crds/config.terraform.padok.cloud_terraformlayers.yaml +++ b/manifests/crds/config.terraform.padok.cloud_terraformlayers.yaml @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformlayers.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud @@ -2101,6 +2100,8 @@ spec: type: array lastResult: type: string + lastRun: + type: string state: type: string type: object diff --git a/manifests/crds/config.terraform.padok.cloud_terraformpullrequests.yaml b/manifests/crds/config.terraform.padok.cloud_terraformpullrequests.yaml index 09af6b70..2456ffe0 100644 --- a/manifests/crds/config.terraform.padok.cloud_terraformpullrequests.yaml +++ b/manifests/crds/config.terraform.padok.cloud_terraformpullrequests.yaml @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformpullrequests.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud diff --git a/manifests/crds/config.terraform.padok.cloud_terraformrepositories.yaml b/manifests/crds/config.terraform.padok.cloud_terraformrepositories.yaml index 842531c1..8fa12a7a 100644 --- a/manifests/crds/config.terraform.padok.cloud_terraformrepositories.yaml +++ b/manifests/crds/config.terraform.padok.cloud_terraformrepositories.yaml @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformrepositories.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud diff --git a/manifests/crds/config.terraform.padok.cloud_terraformruns.yaml b/manifests/crds/config.terraform.padok.cloud_terraformruns.yaml index dd3796ad..2e355076 100644 --- a/manifests/crds/config.terraform.padok.cloud_terraformruns.yaml +++ b/manifests/crds/config.terraform.padok.cloud_terraformruns.yaml @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformruns.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud diff --git a/manifests/install.yaml b/manifests/install.yaml index 915d2d9d..3b7a4cb1 100644 --- a/manifests/install.yaml +++ b/manifests/install.yaml @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformlayers.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud @@ -1323,6 +1322,8 @@ spec: type: array lastResult: type: string + lastRun: + type: string state: type: string type: object @@ -1336,8 +1337,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformpullrequests.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud @@ -1460,8 +1460,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformrepositories.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud @@ -2774,8 +2773,7 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.11.2 - creationTimestamp: null + controller-gen.kubebuilder.io/version: v0.11.4 name: terraformruns.config.terraform.padok.cloud spec: group: config.terraform.padok.cloud