Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional data to the /layers route #239

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions internal/server/api/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ import (
)

type layer struct {
UID string `json:"uid"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Repository string `json:"repository"`
Branch string `json:"branch"`
Path string `json:"path"`
State string `json:"state"`
RunCount int `json:"runCount"`
LastRunAt string `json:"lastRunAt"`
LastResult string `json:"lastResult"`
IsRunning bool `json:"isRunning"`
IsPR bool `json:"isPR"`
LatestRuns []run `json:"latestRuns"`
}

type run struct {
ID string `json:"id"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Action string `json:"action"`
Status string `json:"status"`
LastRun string `json:"lastRun"`
Retries int `json:"retries"`
}

type layersResponse struct {
Expand All @@ -43,16 +57,21 @@ func (a *API) LayersHandler(c echo.Context) error {
}
results := []layer{}
for _, l := range layers.Items {
layerRunning, runCount, latestRuns, lastRunAt := a.getLayerRunInfo(l)
results = append(results, layer{
UID: string(l.UID),
Name: l.Name,
Namespace: l.Namespace,
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),
RunCount: runCount,
LastRunAt: lastRunAt,
LastResult: l.Status.LastResult,
IsRunning: a.isLayerRunning(l),
IsRunning: layerRunning,
IsPR: a.isLayerPR(l),
LatestRuns: latestRuns,
})
}
return c.JSON(http.StatusOK, &layersResponse{
Expand Down Expand Up @@ -84,21 +103,39 @@ func (a *API) getLayerState(layer configv1alpha1.TerraformLayer) string {
return state
}

func (a *API) isLayerRunning(layer configv1alpha1.TerraformLayer) bool {
func (a *API) getLayerRunInfo(layer configv1alpha1.TerraformLayer) (layerRunning bool, runCount int, latestRuns []run, lastRunAt string) {
runs := &configv1alpha1.TerraformRunList{}
requirement, _ := labels.NewRequirement("burrito/managed-by", selection.Equals, []string{layer.Name})
selector := labels.NewSelector().Add(*requirement)
err := a.Client.List(context.Background(), runs, client.MatchingLabelsSelector{Selector: selector})
runCount = len(runs.Items)
layerRunning = false
if err != nil {
log.Errorf("could not list terraform runs, returning false: %s", err)
return false
return
}
for _, r := range runs.Items {
lastRunAt = runs.Items[len(runs.Items)-1].Status.LastRun
for i := len(runs.Items)-1 ; i >= 0 ; i-- {
r := runs.Items[i]
if r.Status.State == "Running" {
return true
layerRunning = true
if (len(runs.Items)-1-i) >= 5 {
return
}
}
if (len(runs.Items)-1-i) < 5 {
latestRuns = append(latestRuns, run{
ID: string(r.UID),
Name: r.Name,
Namespace: r.Namespace,
Action: r.Spec.Action,
Status: r.Status.State,
LastRun: r.Status.LastRun,
Retries: r.Status.Retries,
})
}
}
return false
return
}

func (a *API) isLayerPR(layer configv1alpha1.TerraformLayer) bool {
Expand Down
Loading