Skip to content

Commit 8ddc030

Browse files
authored
Add new GET Workload Status API endpoint (#1528)
Signed-off-by: lujunsan <[email protected]>
1 parent 811f6da commit 8ddc030

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

docs/server/docs.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/server/swagger.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/server/swagger.yaml

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/v1/workloads.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func WorkloadRouter(
6464
r.Post("/{name}/edit", routes.updateWorkload)
6565
r.Post("/{name}/stop", routes.stopWorkload)
6666
r.Post("/{name}/restart", routes.restartWorkload)
67+
r.Get("/{name}/status", routes.getWorkloadStatus)
6768
r.Get("/{name}/logs", routes.getLogsForWorkload)
6869
r.Get("/{name}/export", routes.exportWorkload)
6970
r.Delete("/{name}", routes.deleteWorkload)
@@ -558,6 +559,45 @@ func (s *WorkloadRoutes) getLogsForWorkload(w http.ResponseWriter, r *http.Reque
558559
}
559560
}
560561

562+
// getWorkloadStatus
563+
//
564+
// @Summary Get workload status
565+
// @Description Get the current status of a specific workload
566+
// @Tags workloads
567+
// @Produce json
568+
// @Param name path string true "Workload name"
569+
// @Success 200 {object} workloadStatusResponse
570+
// @Failure 404 {string} string "Not Found"
571+
// @Router /api/v1beta/workloads/{name}/status [get]
572+
func (s *WorkloadRoutes) getWorkloadStatus(w http.ResponseWriter, r *http.Request) {
573+
ctx := r.Context()
574+
name := chi.URLParam(r, "name")
575+
576+
workload, err := s.workloadManager.GetWorkload(ctx, name)
577+
if err != nil {
578+
if errors.Is(err, runtime.ErrWorkloadNotFound) {
579+
http.Error(w, "Workload not found", http.StatusNotFound)
580+
return
581+
} else if errors.Is(err, wt.ErrInvalidWorkloadName) {
582+
http.Error(w, "Invalid workload name: "+err.Error(), http.StatusBadRequest)
583+
return
584+
}
585+
logger.Errorf("Failed to get workload: %v", err)
586+
http.Error(w, "Failed to get workload", http.StatusInternalServerError)
587+
return
588+
}
589+
590+
response := workloadStatusResponse{
591+
Status: workload.Status,
592+
}
593+
594+
w.Header().Set("Content-Type", "application/json")
595+
if err := json.NewEncoder(w).Encode(response); err != nil {
596+
http.Error(w, "Failed to marshal workload status", http.StatusInternalServerError)
597+
return
598+
}
599+
}
600+
561601
// exportWorkload
562602
//
563603
// @Summary Export workload configuration
@@ -603,6 +643,14 @@ type workloadListResponse struct {
603643
Workloads []core.Workload `json:"workloads"`
604644
}
605645

646+
// workloadStatusResponse represents the response for getting workload status
647+
//
648+
// @Description Response containing workload status information
649+
type workloadStatusResponse struct {
650+
// Current status of the workload
651+
Status runtime.WorkloadStatus `json:"status"`
652+
}
653+
606654
// updateRequest represents the request to update an existing workload
607655
//
608656
// @Description Request to update an existing workload (name cannot be changed)

0 commit comments

Comments
 (0)