Skip to content

Commit 132c980

Browse files
authored
feat(api): defaultRegion (#5212)
Signed-off-by: Yvonnick Esnault <[email protected]>
1 parent c9486c0 commit 132c980

File tree

4 files changed

+89
-8
lines changed

4 files changed

+89
-8
lines changed

engine/api/api.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,11 @@ type Configuration struct {
181181
Token string `toml:"token" comment:"Token shared between Izanami and CDS to be able to send webhooks from izanami" json:"-"`
182182
} `toml:"izanami" comment:"Feature flipping provider: https://maif.github.io/izanami" json:"izanami"`
183183
} `toml:"features" comment:"###########################\n CDS Features flipping Settings \n##########################" json:"features"`
184-
Services []sdk.ServiceConfiguration `toml:"services" comment:"###########################\n CDS Services Settings \n##########################" json:"services"`
185-
DefaultOS string `toml:"defaultOS" default:"linux" comment:"if no model and os/arch is specified in your job's requirements then spawn worker on this operating system (example: freebsd, linux, windows)" json:"defaultOS"`
186-
DefaultArch string `toml:"defaultArch" default:"amd64" comment:"if no model and no os/arch is specified in your job's requirements then spawn worker on this architecture (example: amd64, arm, 386)" json:"defaultArch"`
187-
Graylog struct {
184+
Services []sdk.ServiceConfiguration `toml:"services" comment:"###########################\n CDS Services Settings \n##########################" json:"services"`
185+
DefaultOS string `toml:"defaultOS" default:"linux" comment:"if no model and os/arch is specified in your job's requirements then spawn worker on this operating system (example: freebsd, linux, windows)" json:"defaultOS"`
186+
DefaultArch string `toml:"defaultArch" default:"amd64" comment:"if no model and no os/arch is specified in your job's requirements then spawn worker on this architecture (example: amd64, arm, 386)" json:"defaultArch"`
187+
DefaultRegion string `toml:"defaultRegion" default:"" comment:"Optional. If no region in your job's requirements then spawn worker with this region. You need to have an hatchery managing this region." json:"defaultRegion"`
188+
Graylog struct {
188189
AccessToken string `toml:"accessToken" json:"-"`
189190
Stream string `toml:"stream" json:"-"`
190191
URL string `toml:"url" comment:"Example: http://localhost:9000" json:"url"`
@@ -719,7 +720,7 @@ func (a *API) Serve(ctx context.Context) error {
719720

720721
migrate.Add(ctx, sdk.Migration{Name: "RefactorProjectVCSServerCrypto", Release: "0.44.0", Blocker: true, Automatic: true, ExecFunc: func(ctx context.Context) error {
721722
return migrate.RefactorProjectVCSServers(ctx, a.DBConnectionFactory.GetDBMap())
722-
}})
723+
}})
723724

724725
migrate.Add(ctx, sdk.Migration{Name: "RefactorWorkerModelCrypto", Release: "0.44.0", Blocker: true, Automatic: true, ExecFunc: func(ctx context.Context) error {
725726
return migrate.RefactorWorkerModelCrypto(ctx, a.DBConnectionFactory.GetDBMap())
@@ -812,7 +813,7 @@ func (a *API) Serve(ctx context.Context) error {
812813
}, a.PanicDump())
813814
sdk.GoRoutine(ctx, "workflow.Initialize",
814815
func(ctx context.Context) {
815-
workflow.Initialize(ctx, a.DBConnectionFactory.GetDBMap, a.Cache, a.Config.URL.UI, a.Config.DefaultOS, a.Config.DefaultArch)
816+
workflow.Initialize(ctx, a.DBConnectionFactory.GetDBMap, a.Cache, a.Config.URL.UI, a.Config.DefaultOS, a.Config.DefaultArch, a.Config.DefaultRegion)
816817
}, a.PanicDump())
817818
sdk.GoRoutine(ctx, "PushInElasticSearch",
818819
func(ctx context.Context) {

engine/api/workflow/gorp_model.go

+17
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ func (j JobRun) WorkflowNodeRunJob() (sdk.WorkflowNodeJobRun, error) {
189189
})
190190
}
191191
}
192+
if defaultRegion != "" {
193+
var regionFound bool
194+
for _, req := range jr.Job.Action.Requirements {
195+
if req.Type == sdk.RegionRequirement {
196+
regionFound = true
197+
break
198+
}
199+
}
200+
201+
if !regionFound {
202+
jr.Job.Action.Requirements = append(jr.Job.Action.Requirements, sdk.Requirement{
203+
Name: defaultRegion,
204+
Type: sdk.RegionRequirement,
205+
Value: defaultRegion,
206+
})
207+
}
208+
}
192209
return jr, nil
193210
}
194211

engine/api/workflow/init.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010
"github.com/ovh/cds/sdk/log"
1111
)
1212

13-
var baseUIURL, defaultOS, defaultArch string
13+
var baseUIURL, defaultOS, defaultArch, defaultRegion string
1414

1515
//Initialize starts goroutines for workflows
16-
func Initialize(ctx context.Context, DBFunc func() *gorp.DbMap, store cache.Store, uiURL, confDefaultOS, confDefaultArch string) {
16+
func Initialize(ctx context.Context, DBFunc func() *gorp.DbMap, store cache.Store, uiURL, confDefaultOS, confDefaultArch, confDefaultRegion string) {
1717
baseUIURL = uiURL
1818
defaultOS = confDefaultOS
1919
defaultArch = confDefaultArch
20+
defaultRegion = confDefaultRegion
2021
tickStop := time.NewTicker(30 * time.Minute)
2122
tickHeart := time.NewTicker(10 * time.Second)
2223
defer tickHeart.Stop()
@@ -41,3 +42,8 @@ func Initialize(ctx context.Context, DBFunc func() *gorp.DbMap, store cache.Stor
4142
}
4243
}
4344
}
45+
46+
// SetDefaultRegion is used by unit tests
47+
func SetDefaultRegion(r string) {
48+
defaultRegion = r
49+
}

engine/api/workflow_queue_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,63 @@ func Test_postTakeWorkflowJobHandler(t *testing.T) {
501501
assert.Len(t, wkrDB.PrivateKey, 32)
502502
}
503503

504+
func Test_postTakeWorkflowJobHandlerDefautlRegion(t *testing.T) {
505+
api, _, router, end := newTestAPI(t)
506+
defer end()
507+
ctx := testRunWorkflow(t, api, router)
508+
testGetWorkflowJobAsWorker(t, api, router, &ctx)
509+
require.NotNil(t, ctx.job)
510+
511+
//Prepare request
512+
vars := map[string]string{
513+
"key": ctx.project.Key,
514+
"permWorkflowName": ctx.workflow.Name,
515+
"id": fmt.Sprintf("%d", ctx.job.ID),
516+
}
517+
518+
//Register the worker
519+
testRegisterWorker(t, api, router, &ctx)
520+
521+
// Add cdn config
522+
api.Config.CDN = cdn.Configuration{
523+
TCP: sdk.TCPServer{
524+
Port: 8090,
525+
Addr: "localhost",
526+
},
527+
}
528+
529+
uri := router.GetRoute("POST", api.postTakeWorkflowJobHandler, vars)
530+
require.NotEmpty(t, uri)
531+
532+
workflow.SetDefaultRegion("my-region")
533+
534+
//This call must work
535+
req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, nil)
536+
rec := httptest.NewRecorder()
537+
router.Mux.ServeHTTP(rec, req)
538+
require.Equal(t, 200, rec.Code)
539+
540+
pbji := &sdk.WorkflowNodeJobRunData{}
541+
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), pbji))
542+
543+
run, err := workflow.LoadNodeJobRun(context.TODO(), api.mustDB(), api.Cache, ctx.job.ID)
544+
require.NoError(t, err)
545+
assert.Equal(t, "Building", run.Status)
546+
assert.Equal(t, ctx.model.Name, run.Model)
547+
assert.Equal(t, ctx.worker.Name, run.WorkerName)
548+
assert.NotEmpty(t, run.HatcheryName)
549+
550+
var found bool
551+
for _, v := range run.Job.Action.Requirements {
552+
if v.Type == sdk.RegionRequirement {
553+
require.Equal(t, "my-region", v.Value)
554+
found = true
555+
}
556+
}
557+
require.Equal(t, true, found)
558+
559+
}
560+
504561
func Test_postTakeWorkflowInvalidJobHandler(t *testing.T) {
505562
api, _, router, end := newTestAPI(t)
506563
defer end()

0 commit comments

Comments
 (0)