-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): worker should not be removed when hatchery is lost (#5296)
- Loading branch information
Showing
12 changed files
with
343 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package worker | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-gorp/gorp" | ||
"github.com/ovh/cds/engine/api/database/gorpmapping" | ||
"github.com/ovh/cds/sdk" | ||
"github.com/ovh/cds/sdk/log" | ||
) | ||
|
||
// ReleaseAllFromHatchery remove dependency to given given hatchery for all workers linked to it. | ||
func ReleaseAllFromHatchery(db gorp.SqlExecutor, hatcheryID int64) error { | ||
if _, err := db.Exec("UPDATE worker SET hatchery_id = NULL WHERE hatchery_id = $1", hatcheryID); err != nil { | ||
return sdk.WrapError(err, "cannot release workers for hatchery with id %d", hatcheryID) | ||
} | ||
return nil | ||
} | ||
|
||
// ReAttachAllToHatchery search for workers without hatchery an re-attach workers if the hatchery consumer match worker consumer's parent. | ||
func ReAttachAllToHatchery(ctx context.Context, db gorp.SqlExecutor, hatchery sdk.Service) error { | ||
query := gorpmapping.NewQuery(` | ||
SELECT worker.* FROM worker | ||
JOIN auth_consumer ON auth_consumer.id = worker.auth_consumer_id | ||
WHERE auth_consumer.parent_id = $1 and worker.hatchery_id IS NULL | ||
`).Args(hatchery.ConsumerID) | ||
ws, err := getAll(ctx, db, query) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := range ws { | ||
log.Info(ctx, "worker.ReAttachAllToHatchery> re-attach worker %s (%s) to hatchery %d (%s)", ws[i].ID, ws[i].Name, hatchery.ID, hatchery.Name) | ||
ws[i].HatcheryID = &hatchery.ID | ||
ws[i].HatcheryName = hatchery.Name | ||
if err := gorpmapping.UpdateAndSign(ctx, db, &dbWorker{Worker: ws[i]}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package worker_test | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ovh/cds/engine/api/authentication" | ||
"github.com/ovh/cds/engine/api/bootstrap" | ||
"github.com/ovh/cds/engine/api/test" | ||
"github.com/ovh/cds/engine/api/test/assets" | ||
"github.com/ovh/cds/engine/api/worker" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func TestReleaseAllFromHatchery(t *testing.T) { | ||
db, _ := test.SetupPG(t, bootstrap.InitiliazeDB) | ||
|
||
// Remove all existing workers in database | ||
workers, err := worker.LoadAll(context.TODO(), db) | ||
require.NoError(t, err) | ||
for _, w := range workers { | ||
require.NoError(t, worker.Delete(db, w.ID)) | ||
} | ||
|
||
g := assets.InsertGroup(t, db) | ||
m := assets.InsertWorkerModel(t, db, sdk.RandomString(5), g.ID) | ||
|
||
h1, _, h1Consumer, _ := assets.InsertHatchery(t, db, *g) | ||
h2, _, h2Consumer, _ := assets.InsertHatchery(t, db, *g) | ||
|
||
w1Consumer, err := authentication.NewConsumerWorker(context.TODO(), db, "worker-1", h1, h1Consumer, []int64{g.ID}) | ||
require.NoError(t, err) | ||
w2Consumer, err := authentication.NewConsumerWorker(context.TODO(), db, "worker-2", h2, h2Consumer, []int64{g.ID}) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, worker.Insert(context.TODO(), db, &sdk.Worker{ | ||
ID: sdk.UUID(), | ||
Name: "worker-1", | ||
ModelID: &m.ID, | ||
HatcheryID: &h1.ID, | ||
HatcheryName: h1.Name, | ||
ConsumerID: w1Consumer.ID, | ||
Status: sdk.StatusWaiting, | ||
})) | ||
require.NoError(t, worker.Insert(context.TODO(), db, &sdk.Worker{ | ||
ID: sdk.UUID(), | ||
Name: "worker-2", | ||
ModelID: &m.ID, | ||
HatcheryID: &h2.ID, | ||
HatcheryName: h2.Name, | ||
ConsumerID: w2Consumer.ID, | ||
Status: sdk.StatusWaiting, | ||
})) | ||
|
||
require.NoError(t, worker.ReleaseAllFromHatchery(db, h1.ID)) | ||
|
||
workers, err = worker.LoadAll(context.TODO(), db) | ||
require.NoError(t, err) | ||
require.Len(t, workers, 2) | ||
sort.Slice(workers, func(i, j int) bool { return workers[i].Name < workers[i].Name }) | ||
assert.Equal(t, "worker-1", workers[0].Name) | ||
assert.Nil(t, workers[0].HatcheryID) | ||
assert.Equal(t, "worker-2", workers[1].Name) | ||
assert.NotNil(t, workers[1].HatcheryID) | ||
} | ||
|
||
func TestReAttachAllToHatchery(t *testing.T) { | ||
db, _ := test.SetupPG(t, bootstrap.InitiliazeDB) | ||
|
||
// Remove all existing workers in database | ||
workers, err := worker.LoadAll(context.TODO(), db) | ||
require.NoError(t, err) | ||
for _, w := range workers { | ||
require.NoError(t, worker.Delete(db, w.ID)) | ||
} | ||
|
||
g := assets.InsertGroup(t, db) | ||
m := assets.InsertWorkerModel(t, db, sdk.RandomString(5), g.ID) | ||
|
||
h1, _, h1Consumer, _ := assets.InsertHatchery(t, db, *g) | ||
h2, _, h2Consumer, _ := assets.InsertHatchery(t, db, *g) | ||
|
||
w1Consumer, err := authentication.NewConsumerWorker(context.TODO(), db, "worker-1", h1, h1Consumer, []int64{g.ID}) | ||
require.NoError(t, err) | ||
w2Consumer, err := authentication.NewConsumerWorker(context.TODO(), db, "worker-2", h2, h2Consumer, []int64{g.ID}) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, worker.Insert(context.TODO(), db, &sdk.Worker{ | ||
ID: sdk.UUID(), | ||
Name: "worker-1", | ||
ModelID: &m.ID, | ||
HatcheryID: &h1.ID, | ||
HatcheryName: h1.Name, | ||
ConsumerID: w1Consumer.ID, | ||
Status: sdk.StatusWaiting, | ||
})) | ||
require.NoError(t, worker.Insert(context.TODO(), db, &sdk.Worker{ | ||
ID: sdk.UUID(), | ||
Name: "worker-2", | ||
ModelID: &m.ID, | ||
HatcheryName: h2.Name, | ||
ConsumerID: w2Consumer.ID, | ||
Status: sdk.StatusWaiting, | ||
})) | ||
|
||
require.NoError(t, worker.ReAttachAllToHatchery(context.TODO(), db, *h2)) | ||
|
||
workers, err = worker.LoadAll(context.TODO(), db) | ||
require.NoError(t, err) | ||
require.Len(t, workers, 2) | ||
sort.Slice(workers, func(i, j int) bool { return workers[i].Name < workers[i].Name }) | ||
require.Equal(t, "worker-1", workers[0].Name) | ||
require.NotNil(t, workers[0].HatcheryID) | ||
require.Equal(t, h1.ID, *workers[0].HatcheryID) | ||
require.Equal(t, "worker-2", workers[1].Name) | ||
require.NotNil(t, workers[1].HatcheryID) | ||
require.Equal(t, h2.ID, *workers[1].HatcheryID) | ||
} |
Oops, something went wrong.