Remove UID for now and use machine ID to differentiate workers (#48)

* Remove UID for now and use machine ID to differentiate workers

* Rename MetadataWorkerKey back to MetadataWorkerNameKey
This commit is contained in:
Nikolay Edigaryev
2023-03-23 23:38:54 +04:00
committed by GitHub
parent 5c3b47a247
commit af074f499d
16 changed files with 120 additions and 54 deletions
+8 -5
View File
@@ -6,7 +6,6 @@ import (
"github.com/cirruslabs/orchard/internal/responder"
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"net/http"
"time"
)
@@ -31,13 +30,17 @@ func (controller *Controller) createWorker(ctx *gin.Context) responder.Responder
worker.LastSeen = currentTime
}
worker.CreatedAt = currentTime
worker.UID = uuid.New().String()
worker.Generation = 0
return controller.storeUpdate(func(txn storepkg.Transaction) responder.Responder {
// Does the worker resource with this name already exists?
_, err := txn.GetWorker(worker.Name)
if !errors.Is(err, storepkg.ErrNotFound) {
// 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
dbWorker, err := txn.GetWorker(worker.Name)
if err != nil && !errors.Is(err, storepkg.ErrNotFound) {
return responder.Code(http.StatusInternalServerError)
}
if err == nil && worker.MachineID != dbWorker.MachineID {
return responder.Code(http.StatusConflict)
}