Skip to content

Commit 5aa69bc

Browse files
committed
added new method
1 parent e790594 commit 5aa69bc

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

internal/api/handlers.go

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,68 +1209,51 @@ func (h *Handler) GetScenarioRunLogs(w http.ResponseWriter, r *http.Request) {
12091209
}
12101210

12111211
// ListScenarioRuns handles GET /api/v1/scenarios/run endpoint
1212-
// It returns a list of all scenario jobs
1212+
// It returns a list of all scenario runs (KrknScenarioRun CRs)
12131213
func (h *Handler) ListScenarioRuns(w http.ResponseWriter, r *http.Request) {
12141214
ctx := context.Background()
12151215

12161216
// Parse query parameters for filtering
1217-
statusFilter := r.URL.Query().Get("status")
1217+
phaseFilter := r.URL.Query().Get("phase") // e.g., Running, Succeeded, Failed
12181218
scenarioNameFilter := r.URL.Query().Get("scenarioName")
1219-
clusterNameFilter := r.URL.Query().Get("clusterName")
12201219

1221-
// List all pods with krkn-scenario label
1222-
var podList corev1.PodList
1223-
err := h.client.List(ctx, &podList, client.InNamespace(h.namespace), client.MatchingLabels{
1224-
"app": "krkn-scenario",
1225-
})
1226-
1227-
if err != nil {
1220+
// List all KrknScenarioRun CRs in the namespace
1221+
var scenarioRunList krknv1alpha1.KrknScenarioRunList
1222+
if err := h.client.List(ctx, &scenarioRunList, client.InNamespace(h.namespace)); err != nil {
12281223
writeJSONError(w, http.StatusInternalServerError, ErrorResponse{
12291224
Error: "internal_error",
1230-
Message: "Failed to list pods: " + err.Error(),
1225+
Message: "Failed to list scenario runs: " + err.Error(),
12311226
})
12321227
return
12331228
}
12341229

1235-
var jobs []JobStatusResponse
1236-
1237-
for _, pod := range podList.Items {
1238-
jobId := pod.Labels["krkn-job-id"]
1239-
clusterName := pod.Labels["krkn-cluster-name"]
1240-
scenarioName := pod.Labels["krkn-scenario-name"]
1241-
1242-
if (statusFilter != "" && string(pod.Status.Phase) != statusFilter) ||
1243-
(scenarioNameFilter != "" && scenarioName != scenarioNameFilter) ||
1244-
(clusterNameFilter != "" && clusterName != clusterNameFilter) {
1230+
// Convert to response format with optional filtering
1231+
var runs []ScenarioRunListItem
1232+
for _, sr := range scenarioRunList.Items {
1233+
// Apply filters
1234+
if phaseFilter != "" && sr.Status.Phase != phaseFilter {
12451235
continue
12461236
}
1247-
1248-
jobStatus := JobStatusResponse{
1249-
JobId: jobId,
1250-
ClusterName: clusterName,
1251-
ScenarioName: scenarioName,
1252-
Status: string(pod.Status.Phase),
1253-
PodName: pod.Name,
1254-
}
1255-
1256-
if pod.Status.StartTime != nil {
1257-
startTime := pod.Status.StartTime.Time
1258-
jobStatus.StartTime = &startTime
1237+
if scenarioNameFilter != "" && sr.Spec.ScenarioName != scenarioNameFilter {
1238+
continue
12591239
}
12601240

1261-
for _, condition := range pod.Status.Conditions {
1262-
if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionFalse &&
1263-
(pod.Status.Phase == "Succeeded" || pod.Status.Phase == "Failed") {
1264-
completionTime := condition.LastTransitionTime.Time
1265-
jobStatus.CompletionTime = &completionTime
1266-
}
1241+
run := ScenarioRunListItem{
1242+
ScenarioRunName: sr.Name,
1243+
ScenarioName: sr.Spec.ScenarioName,
1244+
Phase: sr.Status.Phase,
1245+
TotalTargets: sr.Status.TotalTargets,
1246+
SuccessfulJobs: sr.Status.SuccessfulJobs,
1247+
FailedJobs: sr.Status.FailedJobs,
1248+
RunningJobs: sr.Status.RunningJobs,
1249+
CreatedAt: sr.CreationTimestamp.Time,
12671250
}
12681251

1269-
jobs = append(jobs, jobStatus)
1252+
runs = append(runs, run)
12701253
}
12711254

1272-
response := JobsListResponse{
1273-
Jobs: jobs,
1255+
response := ScenarioRunListResponse{
1256+
ScenarioRuns: runs,
12741257
}
12751258

12761259
writeJSON(w, http.StatusOK, response)

internal/api/types.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,29 @@ type ClusterJobStatusResponse struct {
323323
// Message contains additional information about the job status
324324
Message string `json:"message,omitempty"`
325325
}
326+
327+
// ScenarioRunListItem represents a single scenario run in the list view
328+
type ScenarioRunListItem struct {
329+
// ScenarioRunName is the name of the KrknScenarioRun CR
330+
ScenarioRunName string `json:"scenarioRunName"`
331+
// ScenarioName is the name of the scenario being executed
332+
ScenarioName string `json:"scenarioName"`
333+
// Phase is the overall phase of the scenario run
334+
Phase string `json:"phase"`
335+
// TotalTargets is the total number of target clusters
336+
TotalTargets int `json:"totalTargets"`
337+
// SuccessfulJobs is the number of successfully completed jobs
338+
SuccessfulJobs int `json:"successfulJobs"`
339+
// FailedJobs is the number of failed jobs
340+
FailedJobs int `json:"failedJobs"`
341+
// RunningJobs is the number of currently running jobs
342+
RunningJobs int `json:"runningJobs"`
343+
// CreatedAt is the creation timestamp
344+
CreatedAt time.Time `json:"createdAt"`
345+
}
346+
347+
// ScenarioRunListResponse represents the response for GET /scenarios/run
348+
type ScenarioRunListResponse struct {
349+
// ScenarioRuns is the list of scenario runs
350+
ScenarioRuns []ScenarioRunListItem `json:"scenarioRuns"`
351+
}

0 commit comments

Comments
 (0)