From 6da9080bce19ff39655362ed72098e897db4b7cc Mon Sep 17 00:00:00 2001 From: sirkrypt0 <22522058+sirkrypt0@users.noreply.github.com> Date: Tue, 11 May 2021 16:04:58 +0200 Subject: [PATCH] Add option to configure Nomad namespace --- config/config.go | 18 ++++++++++-------- configuration.example.yaml | 2 ++ main.go | 2 +- nomad/api_querier.go | 5 +++-- nomad/api_querier_mock.go | 10 +++++----- nomad/executor_api_mock.go | 10 +++++----- nomad/nomad.go | 8 ++++---- nomad/nomad_test.go | 20 ++++++++++++-------- 8 files changed, 42 insertions(+), 33 deletions(-) diff --git a/config/config.go b/config/config.go index 1438389..5ffc0f8 100644 --- a/config/config.go +++ b/config/config.go @@ -26,10 +26,11 @@ var ( KeyFile: "", }, Nomad: nomad{ - Address: "127.0.0.1", - Port: 4646, - Token: "", - TLS: false, + Address: "127.0.0.1", + Port: 4646, + Token: "", + TLS: false, + Namespace: "default", }, Logger: logger{ Level: "INFO", @@ -57,10 +58,11 @@ type server struct { // nomad configures the used Nomad cluster. type nomad struct { - Address string - Port int - Token string - TLS bool + Address string + Port int + Token string + TLS bool + Namespace string } // logger configures the used logger. diff --git a/configuration.example.yaml b/configuration.example.yaml index d1c16a4..538ffeb 100644 --- a/configuration.example.yaml +++ b/configuration.example.yaml @@ -23,6 +23,8 @@ nomad: token: SECRET # Specifies whether to use TLS when communicating with the Nomad server tls: false + # Nomad namespace to use. If unset, 'default' is used + namespace: poseidon # Configuration of the logger logger: diff --git a/main.go b/main.go index 022f7f3..5f2e4e5 100644 --- a/main.go +++ b/main.go @@ -69,7 +69,7 @@ func main() { logging.InitializeLogging(config.Config.Logger.Level) // API initialization - nomadAPIClient, err := nomad.NewExecutorApi(config.Config.NomadAPIURL()) + nomadAPIClient, err := nomad.NewExecutorApi(config.Config.NomadAPIURL(), config.Config.Nomad.Namespace) if err != nil { log.WithError(err).WithField("nomad url", config.Config.NomadAPIURL()).Fatal("Error parsing the nomad url") } diff --git a/nomad/api_querier.go b/nomad/api_querier.go index cafc6a8..f8772ec 100644 --- a/nomad/api_querier.go +++ b/nomad/api_querier.go @@ -8,7 +8,7 @@ import ( // apiQuerier provides access to the Nomad functionality. type apiQuerier interface { // init prepares an apiClient to be able to communicate to a provided Nomad API. - init(nomadURL *url.URL) (err error) + init(nomadURL *url.URL, nomadNamespace string) (err error) // LoadJobList loads the list of jobs from the Nomad API. LoadJobList() (list []*nomadApi.JobListStub, err error) @@ -31,10 +31,11 @@ type nomadApiClient struct { client *nomadApi.Client } -func (nc *nomadApiClient) init(nomadURL *url.URL) (err error) { +func (nc *nomadApiClient) init(nomadURL *url.URL, nomadNamespace string) (err error) { nc.client, err = nomadApi.NewClient(&nomadApi.Config{ Address: nomadURL.String(), TLSConfig: &nomadApi.TLSConfig{}, + Namespace: nomadNamespace, }) return err } diff --git a/nomad/api_querier_mock.go b/nomad/api_querier_mock.go index 4668bd5..dc3d4a9 100644 --- a/nomad/api_querier_mock.go +++ b/nomad/api_querier_mock.go @@ -86,13 +86,13 @@ func (_m *apiQuerierMock) SetJobScaling(jobId string, count int, reason string) return r0 } -// init provides a mock function with given fields: nomadURL -func (_m *apiQuerierMock) init(nomadURL *url.URL) error { - ret := _m.Called(nomadURL) +// init provides a mock function with given fields: nomadURL, nomadNamespace +func (_m *apiQuerierMock) init(nomadURL *url.URL, nomadNamespace string) error { + ret := _m.Called(nomadURL, nomadNamespace) var r0 error - if rf, ok := ret.Get(0).(func(*url.URL) error); ok { - r0 = rf(nomadURL) + if rf, ok := ret.Get(0).(func(*url.URL, string) error); ok { + r0 = rf(nomadURL, nomadNamespace) } else { r0 = ret.Error(0) } diff --git a/nomad/executor_api_mock.go b/nomad/executor_api_mock.go index 4f81de1..522f2d5 100644 --- a/nomad/executor_api_mock.go +++ b/nomad/executor_api_mock.go @@ -109,13 +109,13 @@ func (_m *ExecutorApiMock) SetJobScale(jobId string, count int, reason string) e return r0 } -// init provides a mock function with given fields: nomadURL -func (_m *ExecutorApiMock) init(nomadURL *url.URL) error { - ret := _m.Called(nomadURL) +// init provides a mock function with given fields: nomadURL, nomadNamespace +func (_m *ExecutorApiMock) init(nomadURL *url.URL, nomadNamespace string) error { + ret := _m.Called(nomadURL, nomadNamespace) var r0 error - if rf, ok := ret.Get(0).(func(*url.URL) error); ok { - r0 = rf(nomadURL) + if rf, ok := ret.Get(0).(func(*url.URL, string) error); ok { + r0 = rf(nomadURL, nomadNamespace) } else { r0 = ret.Error(0) } diff --git a/nomad/nomad.go b/nomad/nomad.go index b2019c6..af9d801 100644 --- a/nomad/nomad.go +++ b/nomad/nomad.go @@ -24,15 +24,15 @@ type ApiClient struct { // NewExecutorApi creates a new api client. // One client is usually sufficient for the complete runtime of the API. -func NewExecutorApi(nomadURL *url.URL) (ExecutorApi, error) { +func NewExecutorApi(nomadURL *url.URL, nomadNamespace string) (ExecutorApi, error) { client := &ApiClient{apiQuerier: &nomadApiClient{}} - err := client.init(nomadURL) + err := client.init(nomadURL, nomadNamespace) return client, err } // init prepares an apiClient to be able to communicate to a provided Nomad API. -func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) { - err = apiClient.apiQuerier.init(nomadURL) +func (apiClient *ApiClient) init(nomadURL *url.URL, nomadNamespace string) (err error) { + err = apiClient.apiQuerier.init(nomadURL, nomadNamespace) if err != nil { return err } diff --git a/nomad/nomad_test.go b/nomad/nomad_test.go index ae5d98c..159e9c9 100644 --- a/nomad/nomad_test.go +++ b/nomad/nomad_test.go @@ -116,15 +116,19 @@ func (suite *LoadRunnersTestSuite) TestReturnsAllAvailableRunners() { suite.Contains(returnedIds, suite.anotherAvailableRunner.ID) } -var TestURL = url.URL{ - Scheme: "http", - Host: "127.0.0.1:4646", -} +var ( + TestURL = url.URL{ + Scheme: "http", + Host: "127.0.0.1:4646", + } +) + +const TestNamespace = "unit-tests" func TestApiClient_init(t *testing.T) { client := &ApiClient{apiQuerier: &nomadApiClient{}} defaultJob := parseJob(defaultJobHCL) - err := client.init(&TestURL) + err := client.init(&TestURL, TestNamespace) require.Nil(t, err) assert.Equal(t, *defaultJob, client.defaultJob) } @@ -134,15 +138,15 @@ func TestApiClientCanNotBeInitializedWithInvalidUrl(t *testing.T) { err := client.init(&url.URL{ Scheme: "http", Host: "http://127.0.0.1:4646", - }) + }, TestNamespace) assert.NotNil(t, err) } func TestNewExecutorApiCanBeCreatedWithoutError(t *testing.T) { expectedClient := &ApiClient{apiQuerier: &nomadApiClient{}} - err := expectedClient.init(&TestURL) + err := expectedClient.init(&TestURL, TestNamespace) require.Nil(t, err) - _, err = NewExecutorApi(&TestURL) + _, err = NewExecutorApi(&TestURL, TestNamespace) require.Nil(t, err) }