Skip to content

Commit

Permalink
API: only overwrite specific worker fields when worker already exists (
Browse files Browse the repository at this point in the history
…#236)

* API: only overwrite specific worker fields when worker already exists

* Don't forget to return when creating new worker

* Return updated worker when updating the worker
  • Loading branch information
edigaryev authored Jan 16, 2025
1 parent 08769e0 commit 1fce915
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions internal/controller/api_workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,39 @@ func (controller *Controller) createWorker(ctx *gin.Context) responder.Responder

return controller.storeUpdate(func(txn storepkg.Transaction) responder.Responder {
// In case there already exist a worker with the same name,
// allow overwriting it if the request comes from a worker
// with the same machine ID
// update it (to avoid overwriting things like SchedulingPaused)
// if the request comes from a worker with the same machine ID
dbWorker, err := txn.GetWorker(worker.Name)
if err != nil && !errors.Is(err, storepkg.ErrNotFound) {
if err != nil {
if errors.Is(err, storepkg.ErrNotFound) {
// Create a new worker
if err := txn.SetWorker(worker); err != nil {
return responder.Error(err)
}

return responder.JSON(200, worker)
}

controller.logger.Errorf("failed to check if the worker "+
"with name %q exists in the DB: %v", worker.Name, err)

return responder.Code(http.StatusInternalServerError)
}
if err == nil && worker.MachineID != dbWorker.MachineID {
return responder.JSON(http.StatusConflict,
NewErrorResponse("this worker is managed from a different machine ID, "+
"delete this worker first to be able to re-create it"))

// Update an already existing worker
if worker.MachineID != dbWorker.MachineID {
return responder.JSON(http.StatusConflict, NewErrorResponse("this worker is managed "+
"from a different machine ID, delete this worker first to be able to re-create it"))
}

if err := txn.SetWorker(worker); err != nil {
dbWorker.LastSeen = worker.LastSeen
dbWorker.Resources = worker.Resources

if err := txn.SetWorker(*dbWorker); err != nil {
return responder.Error(err)
}

return responder.JSON(200, worker)
return responder.JSON(200, dbWorker)
})
}

Expand Down

0 comments on commit 1fce915

Please sign in to comment.