From dfd34144f87d12b485f6471b82e8ffdedc2edca9 Mon Sep 17 00:00:00 2001 From: sirkrypt0 <22522058+sirkrypt0@users.noreply.github.com> Date: Fri, 7 May 2021 10:50:09 +0200 Subject: [PATCH] Store default Nomad job in apiClient to cache it --- nomad/job.go | 8 ++++---- nomad/nomad.go | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/nomad/job.go b/nomad/job.go index 11bbf2f..472f58a 100644 --- a/nomad/job.go +++ b/nomad/job.go @@ -17,9 +17,9 @@ const ( TaskNameFormat = "%s-task" ) -func (apiClient *ApiClient) defaultJob() *nomadApi.Job { +func parseJob(jobHCL string) *nomadApi.Job { config := jobspec2.ParseConfig{ - Body: []byte(defaultJobHCL), + Body: []byte(jobHCL), AllowFS: false, Strict: true, } @@ -119,14 +119,14 @@ func (apiClient *ApiClient) createJob( networkAccess bool, exposedPorts []uint16) *nomadApi.Job { - job := apiClient.defaultJob() + job := apiClient.defaultJob job.ID = &id job.Name = &id var taskGroup = createTaskGroup(&job, fmt.Sprintf(TaskGroupNameFormat, id), prewarmingPoolSize) configureTask(taskGroup, fmt.Sprintf(TaskNameFormat, id), cpuLimit, memoryLimit, image, networkAccess, exposedPorts) - return job + return &job } // LoadJobList loads the list of jobs from the Nomad api. diff --git a/nomad/nomad.go b/nomad/nomad.go index 300278a..73bb94d 100644 --- a/nomad/nomad.go +++ b/nomad/nomad.go @@ -19,6 +19,8 @@ type ExecutorApi interface { // ApiClient implements the ExecutorApi interface and can be used to perform different operations on the real Executor API and its return values. type ApiClient struct { apiQuerier + client *nomadApi.Client + defaultJob nomadApi.Job } // NewExecutorApi creates a new api client. @@ -35,7 +37,11 @@ func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) { Address: nomadURL.String(), TLSConfig: &nomadApi.TLSConfig{}, }) - return err + if err != nil { + return err + } + apiClient.defaultJob = *parseJob(defaultJobHCL) + return nil } // LoadRunners loads the allocations of the specified job.