Repository navigation
Expand file tree
/
Copy pathapi_handler.go
More file actions
223 lines (185 loc) · 6.81 KB
/
Copy pathapi_handler.go
File metadata and controls
223 lines (185 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"encoding/json"
"net/http"
"time"
placementv1 "gpu-descheduler/pkg/gpu-descheduler/api/v1"
"gpu-descheduler/pkg/gpu-descheduler/cache"
clusterstate "gpu-descheduler/pkg/gpu-descheduler/cluster_state"
"k8s.io/klog/v2"
)
type ClusterInfoResponse struct {
Timestamp time.Time `json:"timestamp"`
NodeCount int `json:"nodeCount"`
TotalGPUs int `json:"totalGPUs"`
Healthy bool `json:"healthy"`
LastSyncAt time.Time `json:"lastSyncAt"`
SyncError string `json:"syncError,omitempty"`
Nodes []NodeInfoResponse `json:"nodes"`
}
type NodeInfoResponse struct {
NodeName string `json:"nodeName"`
Healthy bool `json:"healthy"`
GPUCount int `json:"gpuCount"`
Labels map[string]string `json:"labels,omitempty"`
Devices []DeviceResponse `json:"devices"`
}
type DeviceResponse struct {
ID string `json:"id"`
Index uint `json:"index"`
Type string `json:"type"`
Vendor string `json:"vendor"`
MemoryMB int32 `json:"memoryMB"`
Cores int32 `json:"cores"`
Numa int `json:"numa"`
Mode string `json:"mode,omitempty"`
Health bool `json:"health"`
}
func RegisterClusterInfoHandler(mux *http.ServeMux, clusterState *clusterstate.ClusterState) {
mux.HandleFunc("/api/v1/cluster-info", func(w http.ResponseWriter, r *http.Request) {
handleClusterInfo(w, r, clusterState)
})
klog.Info("Registered HTTP endpoint: GET /api/v1/cluster-info")
}
func handleClusterInfo(w http.ResponseWriter, r *http.Request, cs *clusterstate.ClusterState) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
nodes := cs.GetAllNodes()
resp := buildClusterInfoResponse(cs, nodes)
if err := json.NewEncoder(w).Encode(resp); err != nil {
klog.ErrorS(err, "Failed to encode cluster info response")
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
func buildClusterInfoResponse(cs *clusterstate.ClusterState, nodes map[string]*clusterstate.NodeInfo) ClusterInfoResponse {
resp := ClusterInfoResponse{
Timestamp: time.Now(),
NodeCount: len(nodes),
Healthy: true,
LastSyncAt: cs.GetLastSyncTime(),
Nodes: make([]NodeInfoResponse, 0, len(nodes)),
}
syncErr := cs.GetSyncError()
if syncErr != nil {
resp.SyncError = syncErr.Error()
resp.Healthy = false
}
for nodeName, nodeInfo := range nodes {
nodeResp := NodeInfoResponse{
NodeName: nodeName,
Healthy: nodeInfo.Healthy,
GPUCount: len(nodeInfo.Devices),
Labels: nodeInfo.Labels,
Devices: make([]DeviceResponse, 0, len(nodeInfo.Devices)),
}
if !nodeInfo.Healthy {
resp.Healthy = false
}
for _, device := range nodeInfo.Devices {
resp.TotalGPUs++
nodeResp.Devices = append(nodeResp.Devices, DeviceResponse{
ID: device.ID,
Index: device.Index,
Type: device.Type,
Vendor: device.DeviceVendor,
MemoryMB: device.Devmem,
Cores: device.Devcore,
Numa: device.Numa,
Mode: device.Mode,
Health: device.Health,
})
}
resp.Nodes = append(resp.Nodes, nodeResp)
}
return resp
}
type PlacementCacheResponse struct {
Timestamp time.Time `json:"timestamp"`
TotalCount int `json:"totalCount"`
Placements []PlacementInfoResponse `json:"placements"`
}
type PlacementInfoResponse struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
ReqID string `json:"reqID"`
NodeName string `json:"nodeName"`
GPUDevices []string `json:"gpuDevices"`
Phase string `json:"phase,omitempty"`
Message string `json:"message,omitempty"`
CreatedAt string `json:"createdAt"`
ConsumedAt string `json:"consumedAt,omitempty"`
MatchedPodName string `json:"matchedPodName,omitempty"`
Assignments []AssignmentResponse `json:"assignments,omitempty"`
}
type AssignmentResponse struct {
PodName string `json:"podName"`
GPUDevice string `json:"gpuDevice"`
}
// apiReadHeaderTimeout bounds how long the API server waits for request
// headers, so a client that never finishes them cannot hold a connection.
var apiReadHeaderTimeout = 5 * time.Second
// newAPIServer returns the server for the read-only API on addr.
func newAPIServer(addr string, handler http.Handler) *http.Server {
return &http.Server{Addr: addr, Handler: handler, ReadHeaderTimeout: apiReadHeaderTimeout}
}
func RegisterAllAPIHandlers(mux *http.ServeMux, clusterState *clusterstate.ClusterState, placementCache *cache.PlacementCache) {
RegisterClusterInfoHandler(mux, clusterState)
RegisterPlacementCacheHandler(mux, placementCache)
}
func RegisterPlacementCacheHandler(mux *http.ServeMux, placementCache *cache.PlacementCache) {
mux.HandleFunc("/api/v1/placements", func(w http.ResponseWriter, r *http.Request) {
handlePlacementCache(w, r, placementCache)
})
klog.Info("Registered HTTP endpoint: GET /api/v1/placements")
}
func handlePlacementCache(w http.ResponseWriter, r *http.Request, pc *cache.PlacementCache) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
placements := pc.ListAll()
resp := buildPlacementCacheResponse(placements)
if err := json.NewEncoder(w).Encode(resp); err != nil {
klog.ErrorS(err, "Failed to encode placement cache response")
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
func buildPlacementCacheResponse(placements []*placementv1.HamiPodPlacement) PlacementCacheResponse {
resp := PlacementCacheResponse{
Timestamp: time.Now(),
TotalCount: len(placements),
Placements: make([]PlacementInfoResponse, 0, len(placements)),
}
for _, pl := range placements {
plInfo := PlacementInfoResponse{
Namespace: pl.Namespace,
Name: pl.Name,
ReqID: pl.Spec.ReqID,
NodeName: pl.Spec.NodeName,
GPUDevices: pl.Spec.GPUDevices,
Phase: pl.Status.Phase,
Message: pl.Status.Message,
CreatedAt: pl.CreationTimestamp.Format(time.RFC3339),
MatchedPodName: pl.Status.MatchedPodName,
}
if pl.Status.ConsumedAt != nil {
plInfo.ConsumedAt = pl.Status.ConsumedAt.Format(time.RFC3339)
}
for _, assignment := range pl.Status.AssignmentDetails {
plInfo.Assignments = append(plInfo.Assignments, AssignmentResponse{
PodName: assignment.PodName,
GPUDevice: assignment.GPUDevice,
})
}
resp.Placements = append(resp.Placements, plInfo)
}
return resp
}