diff --git a/api/v1alpha1/common.go b/api/v1alpha1/common.go index a33247de..ac2f78df 100644 --- a/api/v1alpha1/common.go +++ b/api/v1alpha1/common.go @@ -106,6 +106,19 @@ func GetRunHistoryPolicy(repository *TerraformRepository, layer *TerraformLayer) } } +func GetRemediationStrategy(repo *TerraformRepository, layer *TerraformLayer) RemediationStrategy { + result := RemediationStrategy{ + AutoApply: false, + } + if repo.Spec.RemediationStrategy.AutoApply { + result.AutoApply = true + } + if layer.Spec.RemediationStrategy.AutoApply { + result.AutoApply = true + } + return result +} + func mergeImagePullSecrets(a, b []corev1.LocalObjectReference) []corev1.LocalObjectReference { result := []corev1.LocalObjectReference{} temp := map[string]string{} diff --git a/internal/controllers/terraformlayer/states.go b/internal/controllers/terraformlayer/states.go index 72ea1c4d..a659cce1 100644 --- a/internal/controllers/terraformlayer/states.go +++ b/internal/controllers/terraformlayer/states.go @@ -67,7 +67,7 @@ type ApplyNeeded struct{} func (s *ApplyNeeded) getHandler() Handler { return func(ctx context.Context, r *Reconciler, layer *configv1alpha1.TerraformLayer, repository *configv1alpha1.TerraformRepository) ctrl.Result { log := log.WithContext(ctx) - remediationStrategy := getRemediationStrategy(repository, layer) + 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} @@ -82,19 +82,6 @@ func (s *ApplyNeeded) getHandler() Handler { } } -func getRemediationStrategy(repo *configv1alpha1.TerraformRepository, layer *configv1alpha1.TerraformLayer) configv1alpha1.RemediationStrategy { - result := configv1alpha1.RemediationStrategy{ - AutoApply: false, - } - if repo.Spec.RemediationStrategy.AutoApply { - result.AutoApply = true - } - if layer.Spec.RemediationStrategy.AutoApply { - result.AutoApply = true - } - return result -} - func getStateString(state State) string { t := strings.Split(fmt.Sprintf("%T", state), ".") return t[len(t)-1] diff --git a/internal/server/api/layers.go b/internal/server/api/layers.go index 9156f9d1..a9af0c21 100644 --- a/internal/server/api/layers.go +++ b/internal/server/api/layers.go @@ -7,6 +7,7 @@ import ( "github.com/labstack/echo/v4" configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1" + "github.com/padok-team/burrito/internal/annotations" log "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/selection" @@ -19,6 +20,7 @@ type layer struct { Repository string `json:"repository"` Branch string `json:"branch"` Path string `json:"path"` + State string `json:"state"` LastResult string `json:"lastResult"` IsRunning bool `json:"isRunning"` IsPR bool `json:"isPR"` @@ -32,8 +34,12 @@ func (a *API) LayersHandler(c echo.Context) error { layers := &configv1alpha1.TerraformLayerList{} err := a.Client.List(context.Background(), layers) if err != nil { - log.Errorf("could not list terraform repositories: %s", err) - return c.String(http.StatusInternalServerError, "could not list terraform repositories") + log.Errorf("could not list terraform layers: %s", err) + return c.String(http.StatusInternalServerError, "could not list terraform layers") + } + if err != nil { + log.Errorf("could not list terraform layers: %s", err) + return c.String(http.StatusInternalServerError, "could not list terraform layers") } results := []layer{} for _, l := range layers.Items { @@ -43,6 +49,7 @@ func (a *API) LayersHandler(c echo.Context) error { Repository: fmt.Sprintf("%s/%s", l.Spec.Repository.Namespace, l.Spec.Repository.Name), Branch: l.Spec.Branch, Path: l.Spec.Path, + State: a.getLayerState(l), LastResult: l.Status.LastResult, IsRunning: a.isLayerRunning(l), IsPR: a.isLayerPR(l), @@ -54,6 +61,34 @@ func (a *API) LayersHandler(c echo.Context) error { ) } +func (a *API) getLayerState(layer configv1alpha1.TerraformLayer) string { + repository := &configv1alpha1.TerraformRepository{} + err := a.Client.Get(context.Background(), client.ObjectKey{ + Namespace: layer.Spec.Repository.Namespace, + Name: layer.Spec.Repository.Name, + }, repository) + if err != nil { + log.Errorf("could not get terraform repository: %s", err) + return "Unknown" + } + state := "success" + switch { + case len(layer.Status.Conditions) == 0: + state = "disabled" + case layer.Status.State == "ApplyNeeded": + state = "warning" + case layer.Status.State == "PlanNeeded": + state = "warning" + } + if layer.Annotations[annotations.LastPlanSum] == "" { + state = "error" + } + if layer.Annotations[annotations.LastApplySum] != "" && layer.Annotations[annotations.LastApplySum] == "" { + state = "error" + } + return state +} + func (a *API) isLayerRunning(layer configv1alpha1.TerraformLayer) bool { runs := &configv1alpha1.TerraformRunList{} requirement, _ := labels.NewRequirement("burrito/managed-by", selection.Equals, []string{layer.Name})