From 25dda4d6daf1f32c08338660ed4be5d63232fca9 Mon Sep 17 00:00:00 2001 From: Richard LT Date: Mon, 22 Mar 2021 14:41:54 +0100 Subject: [PATCH] feat(hatchery): allow to set specific api url for workers (#5773) Signed-off-by: richardlt --- engine/hatchery/kubernetes/kubernetes.go | 15 ++------------- engine/hatchery/local/worker_spawn.go | 19 ++++--------------- engine/hatchery/marathon/marathon.go | 17 +++-------------- engine/hatchery/openstack/spawn.go | 16 +++------------- engine/hatchery/serve.go | 22 ++++++++++++++++++++++ engine/hatchery/swarm/swarm.go | 16 ++-------------- engine/hatchery/vsphere/spawn.go | 23 +++++++++-------------- engine/service/types.go | 6 +++++- 8 files changed, 50 insertions(+), 84 deletions(-) diff --git a/engine/hatchery/kubernetes/kubernetes.go b/engine/hatchery/kubernetes/kubernetes.go index 57da418b60..aa60835866 100644 --- a/engine/hatchery/kubernetes/kubernetes.go +++ b/engine/hatchery/kubernetes/kubernetes.go @@ -191,19 +191,8 @@ func (h *HatcheryKubernetes) SpawnWorker(ctx context.Context, spawnArgs hatchery } } - udataParam := sdk.WorkerArgs{ - API: h.Configuration().API.HTTP.URL, - Token: spawnArgs.WorkerToken, - HTTPInsecure: h.Config.API.HTTP.Insecure, - Name: spawnArgs.WorkerName, - Model: spawnArgs.Model.Group.Name + "/" + spawnArgs.Model.Name, - HatcheryName: h.Name(), - TTL: h.Config.WorkerTTL, - GraylogHost: h.Configuration().Provision.WorkerLogsOptions.Graylog.Host, - GraylogPort: h.Configuration().Provision.WorkerLogsOptions.Graylog.Port, - GraylogExtraKey: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraKey, - GraylogExtraValue: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraValue, - } + udataParam := h.GenerateWorkerArgs(h, spawnArgs) + udataParam.TTL = h.Config.WorkerTTL udataParam.WorkflowJobID = spawnArgs.JobID diff --git a/engine/hatchery/local/worker_spawn.go b/engine/hatchery/local/worker_spawn.go index 3b5a6a6aa8..41cd5a8294 100644 --- a/engine/hatchery/local/worker_spawn.go +++ b/engine/hatchery/local/worker_spawn.go @@ -71,21 +71,10 @@ func (h *HatcheryLocal) SpawnWorker(ctx context.Context, spawnArgs hatchery.Spaw log.Info(ctx, "HatcheryLocal.SpawnWorker> basedir: %s", basedir) - udataParam := sdk.WorkerArgs{ - API: h.Configuration().API.HTTP.URL, - Token: spawnArgs.WorkerToken, - BaseDir: basedir, - HTTPInsecure: h.Config.API.HTTP.Insecure, - Name: spawnArgs.WorkerName, - Model: spawnArgs.ModelName(), - HatcheryName: h.Name(), - GraylogHost: h.Configuration().Provision.WorkerLogsOptions.Graylog.Host, - GraylogPort: h.Configuration().Provision.WorkerLogsOptions.Graylog.Port, - GraylogExtraKey: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraKey, - GraylogExtraValue: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraValue, - WorkerBinary: path.Join(h.BasedirDedicated, h.getWorkerBinaryName()), - WorkflowJobID: spawnArgs.JobID, - } + udataParam := h.GenerateWorkerArgs(h, spawnArgs) + udataParam.BaseDir = basedir + udataParam.WorkerBinary = path.Join(h.BasedirDedicated, h.getWorkerBinaryName()) + udataParam.WorkflowJobID = spawnArgs.JobID tmpl, errt := template.New("cmd").Parse(workerCmdTmpl) if errt != nil { diff --git a/engine/hatchery/marathon/marathon.go b/engine/hatchery/marathon/marathon.go index a7b172a155..7686865df4 100644 --- a/engine/hatchery/marathon/marathon.go +++ b/engine/hatchery/marathon/marathon.go @@ -237,20 +237,9 @@ func (h *HatcheryMarathon) SpawnWorker(ctx context.Context, spawnArgs hatchery.S instance := 1 forcePull := strings.HasSuffix(spawnArgs.Model.ModelDocker.Image, ":latest") - udataParam := sdk.WorkerArgs{ - API: h.Configuration().API.HTTP.URL, - Token: spawnArgs.WorkerToken, - HTTPInsecure: h.Config.API.HTTP.Insecure, - Name: spawnArgs.WorkerName, - TTL: h.Config.WorkerTTL, - Model: spawnArgs.Model.Path(), - HatcheryName: h.Name(), - GraylogHost: h.Configuration().Provision.WorkerLogsOptions.Graylog.Host, - GraylogPort: h.Configuration().Provision.WorkerLogsOptions.Graylog.Port, - GraylogExtraKey: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraKey, - GraylogExtraValue: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraValue, - WorkflowJobID: spawnArgs.JobID, - } + udataParam := h.GenerateWorkerArgs(h, spawnArgs) + udataParam.TTL = h.Config.WorkerTTL + udataParam.WorkflowJobID = spawnArgs.JobID tmpl, errt := template.New("cmd").Parse(spawnArgs.Model.ModelDocker.Cmd) if errt != nil { diff --git a/engine/hatchery/openstack/spawn.go b/engine/hatchery/openstack/spawn.go index fb58828553..00853bf306 100644 --- a/engine/hatchery/openstack/spawn.go +++ b/engine/hatchery/openstack/spawn.go @@ -76,20 +76,10 @@ func (h *HatcheryOpenstack) SpawnWorker(ctx context.Context, spawnArgs hatchery. if err != nil { return err } - udataParam := sdk.WorkerArgs{ - API: h.Configuration().API.HTTP.URL, - Name: spawnArgs.WorkerName, - Token: spawnArgs.WorkerToken, - Model: spawnArgs.Model.Group.Name + "/" + spawnArgs.Model.Name, - HatcheryName: h.Name(), - TTL: h.Config.WorkerTTL, - FromWorkerImage: withExistingImage, - GraylogHost: h.Configuration().Provision.WorkerLogsOptions.Graylog.Host, - GraylogPort: h.Configuration().Provision.WorkerLogsOptions.Graylog.Port, - GraylogExtraKey: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraKey, - GraylogExtraValue: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraValue, - } + udataParam := h.GenerateWorkerArgs(h, spawnArgs) + udataParam.TTL = h.Config.WorkerTTL + udataParam.FromWorkerImage = withExistingImage udataParam.WorkflowJobID = spawnArgs.JobID var buffer bytes.Buffer diff --git a/engine/hatchery/serve.go b/engine/hatchery/serve.go index 5b22418527..effa147c09 100644 --- a/engine/hatchery/serve.go +++ b/engine/hatchery/serve.go @@ -231,3 +231,25 @@ func getStatusHandler(h hatchery.Interface) service.HandlerFunc { } } } + +func (c *Common) GenerateWorkerArgs(h hatchery.Interface, spawnArgs hatchery.SpawnArguments) sdk.WorkerArgs { + apiURL := h.Configuration().Provision.WorkerAPIHTTP.URL + httpInsecure := h.Configuration().Provision.WorkerAPIHTTP.Insecure + if apiURL == "" { + apiURL = h.Configuration().API.HTTP.URL + httpInsecure = h.Configuration().API.HTTP.Insecure + } + + return sdk.WorkerArgs{ + API: apiURL, + HTTPInsecure: httpInsecure, + Token: spawnArgs.WorkerToken, + Name: spawnArgs.WorkerName, + Model: spawnArgs.ModelName(), + HatcheryName: h.Name(), + GraylogHost: h.Configuration().Provision.WorkerLogsOptions.Graylog.Host, + GraylogPort: h.Configuration().Provision.WorkerLogsOptions.Graylog.Port, + GraylogExtraKey: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraKey, + GraylogExtraValue: h.Configuration().Provision.WorkerLogsOptions.Graylog.ExtraValue, + } +} diff --git a/engine/hatchery/swarm/swarm.go b/engine/hatchery/swarm/swarm.go index 4357f65e43..55e89278ee 100644 --- a/engine/hatchery/swarm/swarm.go +++ b/engine/hatchery/swarm/swarm.go @@ -379,20 +379,8 @@ func (h *HatcherySwarm) SpawnWorker(ctx context.Context, spawnArgs hatchery.Spaw return errDockerOpts } - udataParam := sdk.WorkerArgs{ - API: h.Config.API.HTTP.URL, - Token: spawnArgs.WorkerToken, - HTTPInsecure: h.Config.API.HTTP.Insecure, - Name: spawnArgs.WorkerName, - Model: spawnArgs.Model.Group.Name + "/" + spawnArgs.Model.Name, - TTL: h.Config.WorkerTTL, - HatcheryName: h.Name(), - GraylogHost: h.Config.Provision.WorkerLogsOptions.Graylog.Host, - GraylogPort: h.Config.Provision.WorkerLogsOptions.Graylog.Port, - GraylogExtraKey: h.Config.Provision.WorkerLogsOptions.Graylog.ExtraKey, - GraylogExtraValue: h.Config.Provision.WorkerLogsOptions.Graylog.ExtraValue, - } - + udataParam := h.GenerateWorkerArgs(h, spawnArgs) + udataParam.TTL = h.Config.WorkerTTL udataParam.WorkflowJobID = spawnArgs.JobID tmpl, errt := template.New("cmd").Parse(spawnArgs.Model.ModelDocker.Cmd) diff --git a/engine/hatchery/vsphere/spawn.go b/engine/hatchery/vsphere/spawn.go index c99c55ac7d..15084ea614 100644 --- a/engine/hatchery/vsphere/spawn.go +++ b/engine/hatchery/vsphere/spawn.go @@ -322,20 +322,15 @@ func (h *HatcheryVSphere) launchScriptWorker(ctx context.Context, name string, j if errt != nil { return errt } - udataParam := sdk.WorkerArgs{ - API: h.Config.API.HTTP.URL, - Name: name, - Token: token, - Model: model.Group.Name + "/" + model.Name, - HatcheryName: h.Name(), - TTL: h.Config.WorkerTTL, - FromWorkerImage: true, - GraylogHost: h.Config.Provision.WorkerLogsOptions.Graylog.Host, - GraylogPort: h.Config.Provision.WorkerLogsOptions.Graylog.Port, - GraylogExtraKey: h.Config.Provision.WorkerLogsOptions.Graylog.ExtraKey, - GraylogExtraValue: h.Config.Provision.WorkerLogsOptions.Graylog.ExtraValue, - WorkflowJobID: jobID, - } + + udataParam := h.GenerateWorkerArgs(h, hatchery.SpawnArguments{ + WorkerToken: token, + WorkerName: name, + Model: &model, + }) + udataParam.TTL = h.Config.WorkerTTL + udataParam.FromWorkerImage = true + udataParam.WorkflowJobID = jobID var buffer bytes.Buffer if err := tmpl.Execute(&buffer, udataParam); err != nil { diff --git a/engine/service/types.go b/engine/service/types.go index dc14ad4349..52ea80cfb6 100644 --- a/engine/service/types.go +++ b/engine/service/types.go @@ -50,7 +50,11 @@ type HatcheryCommonConfiguration struct { RegisterFrequency int `toml:"registerFrequency" default:"60" comment:"Check if some worker model have to be registered each n Seconds" json:"registerFrequency"` Region string `toml:"region" default:"" comment:"region of this hatchery - optional. With a free text as 'myregion', user can set a prerequisite 'region' with value 'myregion' on CDS Job" json:"region"` IgnoreJobWithNoRegion bool `toml:"ignoreJobWithNoRegion" default:"false" comment:"Ignore job without a region prerequisite if ignoreJobWithNoRegion=true"` - WorkerLogsOptions struct { + WorkerAPIHTTP struct { + URL string `toml:"url" default:"http://localhost:8081" commented:"true" comment:"CDS API URL for worker, let empty or commented to use the same URL that is used by the Hatchery" json:"url"` + Insecure bool `toml:"insecure" default:"false" commented:"true" comment:"sslInsecureSkipVerify, set to true if you use a self-signed SSL on CDS API" json:"insecure"` + } `toml:"http" json:"http"` + WorkerLogsOptions struct { Graylog struct { Host string `toml:"host" comment:"Example: thot.ovh.com" json:"host"` Port int `toml:"port" comment:"Example: 12202" json:"port"`