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,
Token: "",
TLS: false,
Namespace: "default",
},
Logger: logger{
Level: "INFO",
@ -61,6 +62,7 @@ type nomad struct {
Port int
Token string
TLS bool
Namespace string
}
// logger configures the used logger.

View File

@ -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:

View File

@ -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")
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -116,15 +116,19 @@ func (suite *LoadRunnersTestSuite) TestReturnsAllAvailableRunners() {
suite.Contains(returnedIds, suite.anotherAvailableRunner.ID)
}
var TestURL = url.URL{
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)
}