From 54df1e8ec868804a8a18a81ede18157674dbd22b Mon Sep 17 00:00:00 2001 From: Jan-Eric Hellenberg Date: Tue, 11 May 2021 08:52:02 +0200 Subject: [PATCH] Move api querier to own file --- nomad/api_querier.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ nomad/nomad.go | 74 +++----------------------------------------- 2 files changed, 78 insertions(+), 70 deletions(-) create mode 100644 nomad/api_querier.go diff --git a/nomad/api_querier.go b/nomad/api_querier.go new file mode 100644 index 0000000..2402445 --- /dev/null +++ b/nomad/api_querier.go @@ -0,0 +1,74 @@ +package nomad + +import ( + nomadApi "github.com/hashicorp/nomad/api" + "net/url" +) + +// nomadApiQuerier provides access to the Nomad functionality. +type nomadApiQuerier interface { + // init prepares an apiClient to be able to communicate to a provided Nomad API. + init(nomadURL *url.URL) (err error) + + // LoadJobList loads the list of jobs from the Nomad API. + LoadJobList() (list []*nomadApi.JobListStub, err error) + + // GetJobScale returns the scale of the passed job. + GetJobScale(jobId string) (jobScale int, err error) + + // SetJobScaling sets the scaling count of the passed job to Nomad. + SetJobScaling(jobId string, count int, reason string) (err error) + + // DeleteRunner deletes the runner with the given Id. + DeleteRunner(runnerId string) (err error) + + // loadRunners loads all allocations of the specified job. + loadRunners(jobId string) (allocationListStub []*nomadApi.AllocationListStub, err error) +} + +// nomadApiClient implements the nomadApiQuerier interface and provides access to a real Nomad API. +type nomadApiClient struct { + client *nomadApi.Client +} + +func (nc *nomadApiClient) init(nomadURL *url.URL) (err error) { + nc.client, err = nomadApi.NewClient(&nomadApi.Config{ + Address: nomadURL.String(), + TLSConfig: &nomadApi.TLSConfig{}, + }) + return err +} + +func (nc *nomadApiClient) LoadJobList() (list []*nomadApi.JobListStub, err error) { + list, _, err = nc.client.Jobs().List(nil) + return +} + +func (nc *nomadApiClient) GetJobScale(jobId string) (jobScale int, err error) { + status, _, err := nc.client.Jobs().ScaleStatus(jobId, nil) + if err != nil { + return + } + // ToDo: Consider counting also the placed and desired allocations + jobScale = status.TaskGroups[jobId].Running + return +} + +func (nc *nomadApiClient) SetJobScaling(jobId string, count int, reason string) (err error) { + _, _, err = nc.client.Jobs().Scale(jobId, jobId, &count, reason, false, nil, nil) + return +} + +func (nc *nomadApiClient) DeleteRunner(runnerId string) (err error) { + allocation, _, err := nc.client.Allocations().Info(runnerId, nil) + if err != nil { + return + } + _, err = nc.client.Allocations().Stop(allocation, nil) + return err +} + +func (nc *nomadApiClient) loadRunners(jobId string) (allocationListStub []*nomadApi.AllocationListStub, err error) { + allocationListStub, _, err = nc.client.Jobs().Allocations(jobId, true, nil) + return +} diff --git a/nomad/nomad.go b/nomad/nomad.go index 19e9e80..075e005 100644 --- a/nomad/nomad.go +++ b/nomad/nomad.go @@ -13,47 +13,23 @@ type ExecutorApi interface { LoadAvailableRunners(jobId string) (runnerIds []string, err error) } -// nomadApiQuerier provides access to the Nomad functionality -type nomadApiQuerier interface { - // init prepares an apiClient to be able to communicate to a provided Nomad API - init(nomadURL *url.URL) (err error) - - // LoadJobList loads the list of jobs from the Nomad api. - LoadJobList() (list []*nomadApi.JobListStub, err error) - - // GetJobScale returns the scale of the passed job. - GetJobScale(jobId string) (jobScale int, err error) - - // SetJobScaling sets the scaling count of the passed job to Nomad. - SetJobScaling(jobId string, count int, reason string) (err error) - - // DeleteRunner deletes the runner with the given Id. - DeleteRunner(runnerId string) (err error) - - // LoadAvailableRunners loads all allocations of the specified job which are running and not about to get stopped. - loadRunners(jobId string) (allocationListStub []*nomadApi.AllocationListStub, err error) -} - +// 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 { nomadApiQuerier } -type directNomadApiClient struct { - client *nomadApi.Client -} - // New creates a new api client. // One client is usually sufficient for the complete runtime of the API. func New(nomadURL *url.URL) (ExecutorApi, error) { client := &ApiClient{ - nomadApiQuerier: &directNomadApiClient{}, + nomadApiQuerier: &nomadApiClient{}, } err := client.init(nomadURL) return client, err } -func (apiClient *ApiClient) LoadAvailableRunners(jobId string) (runnerIds []string, err error) { - list, err := apiClient.loadRunners(jobId) +func (c *ApiClient) LoadAvailableRunners(jobId string) (runnerIds []string, err error) { + list, err := c.loadRunners(jobId) if err != nil { return nil, err } @@ -65,45 +41,3 @@ func (apiClient *ApiClient) LoadAvailableRunners(jobId string) (runnerIds []stri } return } - -func (rawApiClient *directNomadApiClient) init(nomadURL *url.URL) (err error) { - rawApiClient.client, err = nomadApi.NewClient(&nomadApi.Config{ - Address: nomadURL.String(), - TLSConfig: &nomadApi.TLSConfig{}, - }) - return err -} - -func (rawApiClient *directNomadApiClient) LoadJobList() (list []*nomadApi.JobListStub, err error) { - list, _, err = rawApiClient.client.Jobs().List(nil) - return -} - -func (rawApiClient *directNomadApiClient) GetJobScale(jobId string) (jobScale int, err error) { - status, _, err := rawApiClient.client.Jobs().ScaleStatus(jobId, nil) - if err != nil { - return - } - // ToDo: Consider counting also the placed and desired allocations - jobScale = status.TaskGroups[jobId].Running - return -} - -func (rawApiClient *directNomadApiClient) SetJobScaling(jobId string, count int, reason string) (err error) { - _, _, err = rawApiClient.client.Jobs().Scale(jobId, jobId, &count, reason, false, nil, nil) - return -} - -func (rawApiClient *directNomadApiClient) loadRunners(jobId string) (allocationListStub []*nomadApi.AllocationListStub, err error) { - allocationListStub, _, err = rawApiClient.client.Jobs().Allocations(jobId, true, nil) - return -} - -func (rawApiClient *directNomadApiClient) DeleteRunner(runnerId string) (err error) { - allocation, _, err := rawApiClient.client.Allocations().Info(runnerId, nil) - if err != nil { - return - } - _, err = rawApiClient.client.Allocations().Stop(allocation, nil) - return err -}