|
| 1 | +package rest |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/Gthulhu/api/decisionmaker/domain" |
| 9 | +) |
| 10 | + |
| 11 | +// PodProcess represents a process information within a pod (for API response) |
| 12 | +type PodProcess struct { |
| 13 | + PID int `json:"pid"` |
| 14 | + Command string `json:"command"` |
| 15 | + PPID int `json:"ppid,omitempty"` |
| 16 | + ContainerID string `json:"container_id,omitempty"` |
| 17 | +} |
| 18 | + |
| 19 | +// PodInfo represents pod information with associated processes (for API response) |
| 20 | +type PodInfo struct { |
| 21 | + PodUID string `json:"pod_uid"` |
| 22 | + PodID string `json:"pod_id,omitempty"` |
| 23 | + Processes []PodProcess `json:"processes"` |
| 24 | +} |
| 25 | + |
| 26 | +// GetPodsPIDsResponse is the response structure for the GET /api/v1/pods/pids endpoint |
| 27 | +type GetPodsPIDsResponse struct { |
| 28 | + Pods []PodInfo `json:"pods"` |
| 29 | + Timestamp string `json:"timestamp"` |
| 30 | + NodeName string `json:"node_name"` |
| 31 | + NodeID string `json:"node_id,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +// GetPodsPIDs godoc |
| 35 | +// @Summary Get Pod to PID mappings |
| 36 | +// @Description Returns all pods running on this node with their associated process IDs |
| 37 | +// @Tags Pods |
| 38 | +// @Produce json |
| 39 | +// @Security BearerAuth |
| 40 | +// @Success 200 {object} SuccessResponse[GetPodsPIDsResponse] |
| 41 | +// @Failure 401 {object} ErrorResponse |
| 42 | +// @Failure 500 {object} ErrorResponse |
| 43 | +// @Router /api/v1/pods/pids [get] |
| 44 | +func (h *Handler) GetPodsPIDs(w http.ResponseWriter, r *http.Request) { |
| 45 | + ctx := r.Context() |
| 46 | + |
| 47 | + // Get all pod information from /proc filesystem |
| 48 | + podInfoMap, err := h.Service.GetAllPodInfos(ctx) |
| 49 | + if err != nil { |
| 50 | + h.ErrorResponse(ctx, w, http.StatusInternalServerError, "Failed to retrieve pod information", err) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + // Convert map to slice for response |
| 55 | + pods := make([]PodInfo, 0, len(podInfoMap)) |
| 56 | + for _, podInfo := range podInfoMap { |
| 57 | + processes := make([]PodProcess, 0, len(podInfo.Processes)) |
| 58 | + for _, proc := range podInfo.Processes { |
| 59 | + processes = append(processes, PodProcess{ |
| 60 | + PID: proc.PID, |
| 61 | + Command: proc.Command, |
| 62 | + PPID: proc.PPID, |
| 63 | + ContainerID: proc.ContainerID, |
| 64 | + }) |
| 65 | + } |
| 66 | + pods = append(pods, PodInfo{ |
| 67 | + PodUID: podInfo.PodUID, |
| 68 | + PodID: podInfo.PodID, |
| 69 | + Processes: processes, |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + // Get node name from hostname or environment variable |
| 74 | + nodeName, _ := os.Hostname() |
| 75 | + if envNodeName := os.Getenv("NODE_NAME"); envNodeName != "" { |
| 76 | + nodeName = envNodeName |
| 77 | + } |
| 78 | + |
| 79 | + response := GetPodsPIDsResponse{ |
| 80 | + Pods: pods, |
| 81 | + Timestamp: time.Now().UTC().Format(time.RFC3339), |
| 82 | + NodeName: nodeName, |
| 83 | + } |
| 84 | + |
| 85 | + h.JSONResponse(ctx, w, http.StatusOK, NewSuccessResponse(&response)) |
| 86 | +} |
| 87 | + |
| 88 | +// convertDomainPodProcess converts domain.PodProcess to rest.PodProcess |
| 89 | +func convertDomainPodProcess(proc domain.PodProcess) PodProcess { |
| 90 | + return PodProcess{ |
| 91 | + PID: proc.PID, |
| 92 | + Command: proc.Command, |
| 93 | + PPID: proc.PPID, |
| 94 | + ContainerID: proc.ContainerID, |
| 95 | + } |
| 96 | +} |
0 commit comments