-
Notifications
You must be signed in to change notification settings - Fork 930
Expand file tree
/
Copy pathserver.go
More file actions
276 lines (234 loc) · 8.86 KB
/
server.go
File metadata and controls
276 lines (234 loc) · 8.86 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 2026 The Kubeflow Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package statusserver
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"sync/atomic"
"time"
"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
configapi "github.com/kubeflow/trainer/v2/pkg/apis/config/v1alpha1"
trainer "github.com/kubeflow/trainer/v2/pkg/apis/trainer/v1alpha1"
trainerv1alpha1ac "github.com/kubeflow/trainer/v2/pkg/client/applyconfiguration/trainer/v1alpha1"
)
const (
shutdownTimeout = 5 * time.Second
// HTTP Server timeouts to prevent resource exhaustion
readTimeout = 10 * time.Second
writeTimeout = 10 * time.Second
idleTimeout = 120 * time.Second
// Maximum request body size (64kB)
maxBodySize = 1 << 16
)
// Server for collecting runtime status updates.
type Server struct {
log logr.Logger
httpServer *http.Server
client client.Client
authorizer TokenAuthorizer
ready atomic.Bool
}
var (
_ manager.Runnable = &Server{}
_ manager.LeaderElectionRunnable = &Server{}
)
// NewServer creates a new Server for collecting runtime status updates.
func NewServer(c client.Client, cfg *configapi.StatusServer, tlsConfig *tls.Config, authorizer TokenAuthorizer) (*Server, error) {
if cfg == nil || cfg.Port == nil {
return nil, fmt.Errorf("cfg info is required")
}
if tlsConfig == nil {
return nil, fmt.Errorf("tlsConfig is required")
}
if authorizer == nil {
return nil, fmt.Errorf("authorizer is required")
}
log := ctrl.Log.WithName("runtime-status")
s := &Server{
log: log,
client: c,
authorizer: authorizer,
}
mux := http.NewServeMux()
mux.HandleFunc("POST "+StatusUrl("{namespace}", "{name}"), s.handleTrainJobRuntimeStatus)
mux.HandleFunc("/", s.handleDefault)
// Apply middleware (authentication happens in handler)
handler := chain(mux,
recoveryMiddleware(log),
loggingMiddleware(log),
bodySizeLimitMiddleware(log, maxBodySize),
)
httpServer := http.Server{
Addr: fmt.Sprintf(":%d", *cfg.Port),
Handler: handler,
TLSConfig: tlsConfig,
ErrorLog: slog.NewLogLogger(logr.ToSlogHandler(log), slog.LevelInfo),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
}
s.httpServer = &httpServer
return s, nil
}
// Start implements manager.Runnable and starts the HTTPS Server.
// It blocks until the Server stops, either due to an error or graceful shutdown.
func (s *Server) Start(ctx context.Context) error {
s.log.Info("Initializing token authorizer")
if err := s.authorizer.Init(ctx); err != nil {
return fmt.Errorf("token authorizer initialization failed: %w", err)
}
// Handle graceful shutdown in background
serverShutdown := make(chan struct{})
go func() {
defer close(serverShutdown)
<-ctx.Done()
s.ready.Store(false)
s.log.Info("Shutting down runtime status server")
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := s.httpServer.Shutdown(shutdownCtx); err != nil {
s.log.Error(err, "Error shutting down runtime status server")
}
}()
s.ready.Store(true)
defer s.ready.Store(false)
s.log.Info("Starting runtime status server with TLS", "address", s.httpServer.Addr)
if err := s.httpServer.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("runtime status server failed: %w", err)
}
<-serverShutdown
return nil
}
func (s *Server) NeedLeaderElection() bool {
// server needs to run on all replicas
return false
}
// Check implements healthz.Checker and returns an error if the status server is not ready.
func (s *Server) Check(_ *http.Request) error {
if !s.ready.Load() {
return fmt.Errorf("runtime status server is not ready")
}
return nil
}
// handleTrainJobRuntimeStatus handles POST requests to update TrainJob status.
// Expected URL format: /apis/trainer.kubeflow.org/v1alpha1/namespaces/{namespace}/trainjobs/{name}/status
func (s *Server) handleTrainJobRuntimeStatus(w http.ResponseWriter, r *http.Request) {
namespace := r.PathValue("namespace")
trainJobName := r.PathValue("name")
authorized, err := s.authorizer.Authorize(r.Context(), r.Header.Get("Authorization"), namespace, trainJobName)
if err != nil {
badRequest(w, s.log, "Internal error", metav1.StatusReasonInternalError, http.StatusInternalServerError)
return
}
if !authorized {
badRequest(w, s.log, "Forbidden", metav1.StatusReasonForbidden, http.StatusForbidden)
return
}
// Parse request body
var updateRequest trainer.UpdateTrainJobStatusRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&updateRequest); err != nil {
s.log.V(5).Error(err, "Failed to parse runtime status", "namespace", namespace, "trainJobName", trainJobName)
badRequest(w, s.log, "Invalid payload", metav1.StatusReasonInvalid, http.StatusUnprocessableEntity)
return
}
// If the update request is empty (no trainer status), return success without applying
if updateRequest.TrainerStatus == nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(updateRequest); err != nil {
s.log.Error(err, "Failed to write response", "namespace", namespace, "name", trainJobName)
}
return
}
var trainJob = trainerv1alpha1ac.TrainJob(trainJobName, namespace).WithStatus(toApplyConfig(updateRequest))
if err := s.client.Status().Apply(r.Context(), trainJob, client.ForceOwnership, client.FieldOwner("trainer-status")); err != nil {
// Check if the error is due to validation failure
if apierrors.IsInvalid(err) || apierrors.IsBadRequest(err) {
// Extract the validation error message for the user
statusErr, ok := err.(*apierrors.StatusError)
if ok && statusErr.ErrStatus.Message != "" {
badRequest(w, s.log, statusErr.ErrStatus.Message, metav1.StatusReasonInvalid, http.StatusUnprocessableEntity)
} else {
badRequest(w, s.log, "Validation failed: "+err.Error(), metav1.StatusReasonInvalid, http.StatusUnprocessableEntity)
}
return
}
// Check if the error is due to missing object
if apierrors.IsNotFound(err) {
badRequest(w, s.log, "Train job not found", metav1.StatusReasonNotFound, http.StatusNotFound)
return
}
// For other errors, return internal server error
s.log.Error(err, "Failed to update TrainJob", "namespace", namespace, "name", trainJobName)
badRequest(w, s.log, "Internal error", metav1.StatusReasonInternalError, http.StatusInternalServerError)
return
}
// Return the parsed payload
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(updateRequest); err != nil {
s.log.Error(err, "Failed to write TrainJob status", "namespace", namespace, "name", trainJobName)
}
}
// handleDefault is the default handler for unknown requests.
func (s *Server) handleDefault(w http.ResponseWriter, _ *http.Request) {
badRequest(w, s.log, "Not found", metav1.StatusReasonNotFound, http.StatusNotFound)
}
// badRequest sends a kubernetes Status response with the error message
func badRequest(w http.ResponseWriter, log logr.Logger, message string, reason metav1.StatusReason, code int32) {
status := metav1.Status{
Status: metav1.StatusFailure,
Message: message,
Reason: reason,
Code: code,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(int(code))
if err := json.NewEncoder(w).Encode(status); err != nil {
log.Error(err, "Failed to write bad request details")
}
}
func toApplyConfig(updateRequest trainer.UpdateTrainJobStatusRequest) *trainerv1alpha1ac.TrainJobStatusApplyConfiguration {
var s = trainerv1alpha1ac.TrainJobStatus()
trainerStatus := updateRequest.TrainerStatus
if trainerStatus != nil {
var ts = trainerv1alpha1ac.TrainerStatus()
if trainerStatus.ProgressPercentage != nil {
ts = ts.WithProgressPercentage(*trainerStatus.ProgressPercentage)
}
if trainerStatus.EstimatedRemainingSeconds != nil {
ts = ts.WithEstimatedRemainingSeconds(*trainerStatus.EstimatedRemainingSeconds)
}
for _, m := range trainerStatus.Metrics {
ts.WithMetrics(
trainerv1alpha1ac.Metric().
WithName(m.Name).
WithValue(m.Value),
)
}
ts = ts.WithLastUpdatedTime(trainerStatus.LastUpdatedTime)
s.WithTrainerStatus(ts)
}
return s
}