diff --git a/engine/hatchery/kubernetes/kill_workers.go b/engine/hatchery/kubernetes/kill_workers.go index d645000ea7..59db56417b 100644 --- a/engine/hatchery/kubernetes/kill_workers.go +++ b/engine/hatchery/kubernetes/kill_workers.go @@ -88,7 +88,7 @@ func (h *HatcheryKubernetes) killAwolWorkers(ctx context.Context) error { } } - if err := hatchery.CheckWorkerModelRegister(h, modelPath); err != nil { + if err := hatchery.CheckWorkerModelRegister(ctx, h, modelPath); err != nil { var spawnErr = sdk.SpawnErrorForm{ Error: err.Error(), } diff --git a/engine/hatchery/marathon/marathon.go b/engine/hatchery/marathon/marathon.go index 9ceaf38a35..a7b172a155 100644 --- a/engine/hatchery/marathon/marathon.go +++ b/engine/hatchery/marathon/marathon.go @@ -578,7 +578,7 @@ func (h *HatcheryMarathon) killAwolWorkers() error { // If its a worker "register", check registration before deleting it if strings.HasPrefix(app.ID, "register-") && app.Env != nil { model := (*app.Env)["CDS_MODEL_PATH"] - if err := hatchery.CheckWorkerModelRegister(h, model); err != nil { + if err := hatchery.CheckWorkerModelRegister(ctx, h, model); err != nil { var spawnErr = sdk.SpawnErrorForm{ Error: err.Error(), } diff --git a/engine/hatchery/openstack/openstack.go b/engine/hatchery/openstack/openstack.go index ac890c8509..6773250a5d 100644 --- a/engine/hatchery/openstack/openstack.go +++ b/engine/hatchery/openstack/openstack.go @@ -465,7 +465,7 @@ func (h *HatcheryOpenstack) deleteServer(ctx context.Context, s servers.Server) if err != nil { log.Error(ctx, "killAndRemove> unable to get console log from registering server %s: %v", s.Name, err) } - if err := hatchery.CheckWorkerModelRegister(h, modelPath); err != nil { + if err := hatchery.CheckWorkerModelRegister(ctx, h, modelPath); err != nil { var spawnErr = sdk.SpawnErrorForm{ Error: err.Error(), Logs: []byte(consoleLog), diff --git a/engine/hatchery/swarm/swarm_util_kill.go b/engine/hatchery/swarm/swarm_util_kill.go index d853452331..47eed481bb 100644 --- a/engine/hatchery/swarm/swarm_util_kill.go +++ b/engine/hatchery/swarm/swarm_util_kill.go @@ -38,7 +38,7 @@ func (h *HatcherySwarm) killAndRemove(ctx context.Context, dockerClient *dockerC if strings.HasPrefix(container.Name, "/register-") { modelPath := container.Config.Labels["worker_model_path"] - if err := hatchery.CheckWorkerModelRegister(h, modelPath); err != nil { + if err := hatchery.CheckWorkerModelRegister(ctx, h, modelPath); err != nil { ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2) defer cancel() logsOpts := types.ContainerLogsOptions{ diff --git a/engine/hatchery/vsphere/client.go b/engine/hatchery/vsphere/client.go index 2f24faafd9..52906f0944 100644 --- a/engine/hatchery/vsphere/client.go +++ b/engine/hatchery/vsphere/client.go @@ -150,7 +150,7 @@ func (h *HatcheryVSphere) deleteServer(ctx context.Context, s mo.VirtualMachine) } if strings.HasPrefix(s.Name, "register-") { - if err := hatchery.CheckWorkerModelRegister(h, annot.WorkerModelPath); err != nil { + if err := hatchery.CheckWorkerModelRegister(ctx, h, annot.WorkerModelPath); err != nil { var spawnErr = sdk.SpawnErrorForm{ Error: err.Error(), } diff --git a/sdk/hatchery/register.go b/sdk/hatchery/register.go index 13d48531f1..92643d21e1 100644 --- a/sdk/hatchery/register.go +++ b/sdk/hatchery/register.go @@ -92,7 +92,7 @@ loopModels: } // CheckWorkerModelRegister checks if a model has been registered, if not it raises an error on the API -func CheckWorkerModelRegister(h Interface, modelPath string) error { +func CheckWorkerModelRegister(ctx context.Context, h Interface, modelPath string) error { var sendError bool for i := range models { if models[i].Group.Name+"/"+models[i].Name == modelPath { @@ -100,7 +100,32 @@ func CheckWorkerModelRegister(h Interface, modelPath string) error { break } } + if !sendError { + // need registration is false, no error to return + return nil + } + + // it's need registration = true -> + // perhaps that the models list is not up to date + // so, we call a fresh model list to re-check the flag need registration known by the api + hWithModels, isWithModels := h.(InterfaceWithModels) + if isWithModels { + modelsFresh, errwm := hWithModels.WorkerModelsEnabled() + if errwm != nil { + log.Error(ctx, "error on h.CheckWorkerModelRegister(): %v", errwm) + return errwm + } + + for i := range modelsFresh { + if modelsFresh[i].Group.Name+"/"+modelsFresh[i].Name == modelPath { + sendError = modelsFresh[i].NeedRegistration + break + } + } + } + if sendError { + // need registration stay to true, even after a fresh call to api -> error return sdk.WithStack(sdk.ErrWorkerModelDeploymentFailed) } return nil