Add option to configure Nomad namespace

This commit is contained in:
sirkrypt0
2021-05-11 16:04:58 +02:00
committed by Tobias Kantusch
parent d83e0e4548
commit 6da9080bce
8 changed files with 42 additions and 33 deletions

View File

@ -30,6 +30,7 @@ var (
Port: 4646, Port: 4646,
Token: "", Token: "",
TLS: false, TLS: false,
Namespace: "default",
}, },
Logger: logger{ Logger: logger{
Level: "INFO", Level: "INFO",
@ -61,6 +62,7 @@ type nomad struct {
Port int Port int
Token string Token string
TLS bool TLS bool
Namespace string
} }
// logger configures the used logger. // logger configures the used logger.

View File

@ -23,6 +23,8 @@ nomad:
token: SECRET token: SECRET
# Specifies whether to use TLS when communicating with the Nomad server # Specifies whether to use TLS when communicating with the Nomad server
tls: false tls: false
# Nomad namespace to use. If unset, 'default' is used
namespace: poseidon
# Configuration of the logger # Configuration of the logger
logger: logger:

View File

@ -69,7 +69,7 @@ func main() {
logging.InitializeLogging(config.Config.Logger.Level) logging.InitializeLogging(config.Config.Logger.Level)
// API initialization // API initialization
nomadAPIClient, err := nomad.NewExecutorApi(config.Config.NomadAPIURL()) nomadAPIClient, err := nomad.NewExecutorApi(config.Config.NomadAPIURL(), config.Config.Nomad.Namespace)
if err != nil { if err != nil {
log.WithError(err).WithField("nomad url", config.Config.NomadAPIURL()).Fatal("Error parsing the nomad url") log.WithError(err).WithField("nomad url", config.Config.NomadAPIURL()).Fatal("Error parsing the nomad url")
} }

View File

@ -8,7 +8,7 @@ import (
// apiQuerier provides access to the Nomad functionality. // apiQuerier provides access to the Nomad functionality.
type apiQuerier interface { type apiQuerier interface {
// init prepares an apiClient to be able to communicate to a provided Nomad API. // 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 loads the list of jobs from the Nomad API.
LoadJobList() (list []*nomadApi.JobListStub, err error) LoadJobList() (list []*nomadApi.JobListStub, err error)
@ -31,10 +31,11 @@ type nomadApiClient struct {
client *nomadApi.Client 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{ nc.client, err = nomadApi.NewClient(&nomadApi.Config{
Address: nomadURL.String(), Address: nomadURL.String(),
TLSConfig: &nomadApi.TLSConfig{}, TLSConfig: &nomadApi.TLSConfig{},
Namespace: nomadNamespace,
}) })
return err return err
} }

View File

@ -86,13 +86,13 @@ func (_m *apiQuerierMock) SetJobScaling(jobId string, count int, reason string)
return r0 return r0
} }
// init provides a mock function with given fields: nomadURL // init provides a mock function with given fields: nomadURL, nomadNamespace
func (_m *apiQuerierMock) init(nomadURL *url.URL) error { func (_m *apiQuerierMock) init(nomadURL *url.URL, nomadNamespace string) error {
ret := _m.Called(nomadURL) ret := _m.Called(nomadURL, nomadNamespace)
var r0 error var r0 error
if rf, ok := ret.Get(0).(func(*url.URL) error); ok { if rf, ok := ret.Get(0).(func(*url.URL, string) error); ok {
r0 = rf(nomadURL) r0 = rf(nomadURL, nomadNamespace)
} else { } else {
r0 = ret.Error(0) r0 = ret.Error(0)
} }

View File

@ -109,13 +109,13 @@ func (_m *ExecutorApiMock) SetJobScale(jobId string, count int, reason string) e
return r0 return r0
} }
// init provides a mock function with given fields: nomadURL // init provides a mock function with given fields: nomadURL, nomadNamespace
func (_m *ExecutorApiMock) init(nomadURL *url.URL) error { func (_m *ExecutorApiMock) init(nomadURL *url.URL, nomadNamespace string) error {
ret := _m.Called(nomadURL) ret := _m.Called(nomadURL, nomadNamespace)
var r0 error var r0 error
if rf, ok := ret.Get(0).(func(*url.URL) error); ok { if rf, ok := ret.Get(0).(func(*url.URL, string) error); ok {
r0 = rf(nomadURL) r0 = rf(nomadURL, nomadNamespace)
} else { } else {
r0 = ret.Error(0) r0 = ret.Error(0)
} }

View File

@ -24,15 +24,15 @@ type ApiClient struct {
// NewExecutorApi creates a new api client. // NewExecutorApi creates a new api client.
// One client is usually sufficient for the complete runtime of the API. // 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{}} client := &ApiClient{apiQuerier: &nomadApiClient{}}
err := client.init(nomadURL) err := client.init(nomadURL, nomadNamespace)
return client, err return client, err
} }
// init prepares an apiClient to be able to communicate to a provided Nomad API. // init prepares an apiClient to be able to communicate to a provided Nomad API.
func (apiClient *ApiClient) init(nomadURL *url.URL) (err error) { func (apiClient *ApiClient) init(nomadURL *url.URL, nomadNamespace string) (err error) {
err = apiClient.apiQuerier.init(nomadURL) err = apiClient.apiQuerier.init(nomadURL, nomadNamespace)
if err != nil { if err != nil {
return err return err
} }

View File

@ -116,15 +116,19 @@ func (suite *LoadRunnersTestSuite) TestReturnsAllAvailableRunners() {
suite.Contains(returnedIds, suite.anotherAvailableRunner.ID) suite.Contains(returnedIds, suite.anotherAvailableRunner.ID)
} }
var TestURL = url.URL{ var (
TestURL = url.URL{
Scheme: "http", Scheme: "http",
Host: "127.0.0.1:4646", Host: "127.0.0.1:4646",
} }
)
const TestNamespace = "unit-tests"
func TestApiClient_init(t *testing.T) { func TestApiClient_init(t *testing.T) {
client := &ApiClient{apiQuerier: &nomadApiClient{}} client := &ApiClient{apiQuerier: &nomadApiClient{}}
defaultJob := parseJob(defaultJobHCL) defaultJob := parseJob(defaultJobHCL)
err := client.init(&TestURL) err := client.init(&TestURL, TestNamespace)
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, *defaultJob, client.defaultJob) assert.Equal(t, *defaultJob, client.defaultJob)
} }
@ -134,15 +138,15 @@ func TestApiClientCanNotBeInitializedWithInvalidUrl(t *testing.T) {
err := client.init(&url.URL{ err := client.init(&url.URL{
Scheme: "http", Scheme: "http",
Host: "http://127.0.0.1:4646", Host: "http://127.0.0.1:4646",
}) }, TestNamespace)
assert.NotNil(t, err) assert.NotNil(t, err)
} }
func TestNewExecutorApiCanBeCreatedWithoutError(t *testing.T) { func TestNewExecutorApiCanBeCreatedWithoutError(t *testing.T) {
expectedClient := &ApiClient{apiQuerier: &nomadApiClient{}} expectedClient := &ApiClient{apiQuerier: &nomadApiClient{}}
err := expectedClient.init(&TestURL) err := expectedClient.init(&TestURL, TestNamespace)
require.Nil(t, err) require.Nil(t, err)
_, err = NewExecutorApi(&TestURL) _, err = NewExecutorApi(&TestURL, TestNamespace)
require.Nil(t, err) require.Nil(t, err)
} }