Skip to content

Commit

Permalink
Clean up configurations to utilize composition instead
Browse files Browse the repository at this point in the history
  • Loading branch information
t-persson committed Nov 27, 2024
1 parent fc3dc8f commit 9e806d4
Show file tree
Hide file tree
Showing 30 changed files with 257 additions and 873 deletions.
6 changes: 3 additions & 3 deletions cmd/executionspace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"runtime/debug"
"syscall"

config "github.com/eiffel-community/etos-api/internal/configs/executionspace"
"github.com/eiffel-community/etos-api/internal/config"
"github.com/eiffel-community/etos-api/internal/database/etcd"
"github.com/eiffel-community/etos-api/internal/executionspace/provider"
"github.com/eiffel-community/etos-api/internal/logging"
Expand All @@ -39,7 +39,7 @@ import (

// main sets up logging and starts up the webservice.
func main() {
cfg := config.Get()
cfg := config.NewExecutionSpaceConfig()
ctx := context.Background()

var hooks []logrus.Hook
Expand Down Expand Up @@ -120,7 +120,7 @@ func fileLogging(cfg config.Config) logrus.Hook {

// remoteLogging starts a new rabbitmq publisher if the rabbitmq parameters are set
// Warning: Must call publisher.Close() on the publisher returned from this function
func remoteLogging(cfg config.Config) *rabbitmq.Publisher {
func remoteLogging(cfg config.ExecutionSpaceConfig) *rabbitmq.Publisher {
if cfg.RabbitMQHookURL() != "" {
if cfg.RabbitMQHookExchangeName() == "" {
panic("-rabbitmq_hook_exchange (env:ETOS_RABBITMQ_EXCHANGE) must be set when using -rabbitmq_hook_url (env:ETOS_RABBITMQ_URL)")
Expand Down
4 changes: 2 additions & 2 deletions cmd/iut/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"syscall"
"time"

config "github.com/eiffel-community/etos-api/internal/configs/iut"
"github.com/eiffel-community/etos-api/internal/config"
"github.com/eiffel-community/etos-api/internal/logging"
server "github.com/eiffel-community/etos-api/internal/server"
"github.com/eiffel-community/etos-api/pkg/application"
Expand All @@ -37,7 +37,7 @@ import (

// main sets up logging and starts up the webserver.
func main() {
cfg := config.Get()
cfg := config.NewIUTConfig()
ctx := context.Background()

var hooks []logrus.Hook
Expand Down
4 changes: 2 additions & 2 deletions cmd/logarea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"syscall"
"time"

config "github.com/eiffel-community/etos-api/internal/configs/logarea"
"github.com/eiffel-community/etos-api/internal/config"
"github.com/eiffel-community/etos-api/internal/logging"
"github.com/eiffel-community/etos-api/internal/server"
"github.com/eiffel-community/etos-api/pkg/application"
Expand All @@ -36,7 +36,7 @@ import (

// main sets up logging and starts up the logarea webservice.
func main() {
cfg := config.Get()
cfg := config.NewLogAreaConfig()
ctx := context.Background()

var hooks []logrus.Hook
Expand Down
4 changes: 2 additions & 2 deletions cmd/sse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"syscall"
"time"

config "github.com/eiffel-community/etos-api/internal/configs/sse"
"github.com/eiffel-community/etos-api/internal/config"
"github.com/eiffel-community/etos-api/internal/logging"
"github.com/eiffel-community/etos-api/internal/server"
"github.com/eiffel-community/etos-api/pkg/application"
Expand All @@ -37,7 +37,7 @@ import (

// main sets up logging and starts up the sse webserver.
func main() {
cfg := config.Get()
cfg := config.NewSSEConfig()
ctx := context.Background()

var hooks []logrus.Hook
Expand Down
24 changes: 11 additions & 13 deletions internal/configs/base/config.go → internal/config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Config interface {
DatabaseURI() string
}

// cfg implements the Config interface.
type cfg struct {
// baseCfg implements the Config interface.
type baseCfg struct {
serviceHost string
servicePort string
stripPrefix string
Expand All @@ -44,9 +44,9 @@ type cfg struct {
databasePort string
}

// Get creates a config interface based on input parameters or environment variables.
func Get() Config {
var conf cfg
// load the command line vars for a base configuration.
func load() Config {
var conf baseCfg

flag.StringVar(&conf.serviceHost, "address", EnvOrDefault("SERVICE_HOST", "127.0.0.1"), "Address to serve API on")
flag.StringVar(&conf.servicePort, "port", EnvOrDefault("SERVICE_PORT", "8080"), "Port to serve API on")
Expand All @@ -56,18 +56,16 @@ func Get() Config {
flag.StringVar(&conf.etosNamespace, "etosnamespace", ReadNamespaceOrEnv("ETOS_NAMESPACE"), "Path, including filename, for the log files to create.")
flag.StringVar(&conf.databaseHost, "databasehost", EnvOrDefault("ETOS_ETCD_HOST", "etcd-client"), "Host to the database.")
flag.StringVar(&conf.databasePort, "databaseport", EnvOrDefault("ETOS_ETCD_PORT", "2379"), "Port to the database.")

flag.Parse()
return &conf
}

// ServiceHost returns the host of the service.
func (c *cfg) ServiceHost() string {
func (c *baseCfg) ServiceHost() string {
return c.serviceHost
}

// ServicePort returns the port of the service.
func (c *cfg) ServicePort() string {
func (c *baseCfg) ServicePort() string {
return c.servicePort
}

Expand All @@ -77,22 +75,22 @@ func (c *cfg) StripPrefix() string {
}

// LogLevel returns the log level.
func (c *cfg) LogLevel() string {
func (c *baseCfg) LogLevel() string {
return c.logLevel
}

// LogFilePath returns the path to where log files should be stored, including filename.
func (c *cfg) LogFilePath() string {
func (c *baseCfg) LogFilePath() string {
return c.logFilePath
}

// ETOSNamespace returns the ETOS namespace.
func (c *cfg) ETOSNamespace() string {
func (c *baseCfg) ETOSNamespace() string {
return c.etosNamespace
}

// DatabaseURI returns the URI to the ETOS database.
func (c *cfg) DatabaseURI() string {
func (c *baseCfg) DatabaseURI() string {
return fmt.Sprintf("%s:%s", c.databaseHost, c.databasePort)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -29,12 +28,12 @@ func TestGet(t *testing.T) {
logLevel := "DEBUG"
logFilePath := "path/to/a/file"

os.Setenv("SERVICE_HOST", serverHost)
os.Setenv("SERVICE_PORT", port)
os.Setenv("LOGLEVEL", logLevel)
os.Setenv("LOG_FILE_PATH", logFilePath)
t.Setenv("SERVICE_HOST", serverHost)
t.Setenv("SERVICE_PORT", port)
t.Setenv("LOGLEVEL", logLevel)
t.Setenv("LOG_FILE_PATH", logFilePath)

conf, ok := Get().(*cfg)
conf, ok := load().(*baseCfg)
assert.Truef(t, ok, "cfg returned from get is not a config interface")
assert.Equal(t, port, conf.servicePort)
assert.Equal(t, serverHost, conf.serviceHost)
Expand All @@ -46,15 +45,15 @@ type getter func() string

// Test that the getters in the Cfg struct return the values from the struct.
func TestGetters(t *testing.T) {
conf := &cfg{
conf := &baseCfg{
serviceHost: "127.0.0.1",
servicePort: "8080",
logLevel: "TRACE",
logFilePath: "a/file/path.json",
}
tests := []struct {
name string
cfg *cfg
cfg *baseCfg
function getter
value string
}{
Expand Down
110 changes: 110 additions & 0 deletions internal/config/executionspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright Axis Communications AB.
//
// For a full list of individual contributors, please see the commit history.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config

import (
"flag"
"os"
"time"

"github.com/sirupsen/logrus"
)

type ExecutionSpaceConfig interface {
Config
StripPrefix() string
Hostname() string
Timeout() time.Duration
ExecutionSpaceWaitTimeout() time.Duration
RabbitMQHookURL() string
RabbitMQHookExchangeName() string
EiffelGoerURL() string
}

// executionSpaceCfg implements the ExecutionSpaceConfig interface.
type executionSpaceCfg struct {
Config
stripPrefix string
hostname string
timeout time.Duration
executionSpaceWaitTimeout time.Duration
rabbitmqHookURL string
rabbitmqHookExchange string
eiffelGoerURL string
}

// NewExecutionSpaceConfig creates an executio nspace config interface based on input parameters or environment variables.
func NewExecutionSpaceConfig() ExecutionSpaceConfig {
var conf executionSpaceCfg

defaultTimeout, err := time.ParseDuration(EnvOrDefault("REQUEST_TIMEOUT", "1m"))
if err != nil {
logrus.Panic(err)
}

executionSpaceWaitTimeout, err := time.ParseDuration(EnvOrDefault("EXECUTION_SPACE_WAIT_TIMEOUT", "1h"))
if err != nil {
logrus.Panic(err)
}

flag.StringVar(&conf.stripPrefix, "stripprefix", EnvOrDefault("STRIP_PREFIX", ""), "Strip prefix")
flag.StringVar(&conf.hostname, "hostname", EnvOrDefault("PROVIDER_HOSTNAME", "http://localhost"), "Host to supply to ESR for starting executors")
flag.DurationVar(&conf.timeout, "timeout", defaultTimeout, "Maximum timeout for requests to Execution space provider Service.")
flag.DurationVar(&conf.executionSpaceWaitTimeout, "executionSpaceWaitTimeout", executionSpaceWaitTimeout, "Timeout duration to wait when trying to checkout execution space(s)")
flag.StringVar(&conf.rabbitmqHookURL, "rabbitmq_hook_url", os.Getenv("ETOS_RABBITMQ_URL"), "URL to the ETOS rabbitmq for logs")
flag.StringVar(&conf.rabbitmqHookExchange, "rabbitmq_hook_exchange", os.Getenv("ETOS_RABBITMQ_EXCHANGE"), "Exchange to use for the ETOS rabbitmq for logs")
flag.StringVar(&conf.eiffelGoerURL, "event_repository_host", os.Getenv("EIFFEL_GOER_URL"), "Event repository URL used for Eiffel event lookup")
base := load()
flag.Parse()
conf.Config = base

return &conf
}

// StripPrefix returns a prefix that is supposed to be stripped from URL.
func (c *executionSpaceCfg) StripPrefix() string {
return c.stripPrefix
}

// Hostname returns the hostname to use for executors.
func (c *executionSpaceCfg) Hostname() string {
return c.hostname
}

// Timeout returns the request timeout for Execution space provider Service API.
func (c *executionSpaceCfg) Timeout() time.Duration {
return c.timeout
}

// ExecutionSpaceWaitTimeout returns the timeout for checking out execution spaces.
func (c *executionSpaceCfg) ExecutionSpaceWaitTimeout() time.Duration {
return c.executionSpaceWaitTimeout
}

// RabbitMQHookURL returns the rabbitmq url for ETOS logs
func (c *executionSpaceCfg) RabbitMQHookURL() string {
return c.rabbitmqHookURL
}

// EventRepositoryURL returns the Eiffel event repository used for event lookups
func (c *executionSpaceCfg) EiffelGoerURL() string {
return c.eiffelGoerURL
}

// RabbitMQHookExchangeName returns the rabbitmq exchange name used for ETOS logs
func (c *executionSpaceCfg) RabbitMQHookExchangeName() string {
return c.rabbitmqHookExchange
}
29 changes: 29 additions & 0 deletions internal/config/iut.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Axis Communications AB.
//
// For a full list of individual contributors, please see the commit history.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config

import "flag"

type IUTConfig interface {
Config
}

// NewIUTConfig creates an iut config interface based on input parameters or environment variables.
func NewIUTConfig() IUTConfig {
cfg := load()
flag.Parse()
return cfg
}
29 changes: 29 additions & 0 deletions internal/config/logarea.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Axis Communications AB.
//
// For a full list of individual contributors, please see the commit history.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config

import "flag"

type LogAreaConfig interface {
Config
}

// NewLogAreaConfig creates a log area config interface based on input parameters or environment variables.
func NewLogAreaConfig() LogAreaConfig {
cfg := load()
flag.Parse()
return cfg
}
29 changes: 29 additions & 0 deletions internal/config/sse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Axis Communications AB.
//
// For a full list of individual contributors, please see the commit history.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config

import "flag"

type SSEConfig interface {
Config
}

// NewSSEConfig creates a sse config interface based on input parameters or environment variables.
func NewSSEConfig() SSEConfig {
cfg := load()
flag.Parse()
return cfg
}
Loading

0 comments on commit 9e806d4

Please sign in to comment.