Skip to content

Commit

Permalink
fix(api): didn't send the layer state
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan-pad committed Oct 23, 2023
1 parent feb79f0 commit 983ad07
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
13 changes: 13 additions & 0 deletions api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
15 changes: 1 addition & 14 deletions internal/controllers/terraformlayer/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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]
Expand Down
39 changes: 37 additions & 2 deletions internal/server/api/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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})
Expand Down

0 comments on commit 983ad07

Please sign in to comment.