From 8425282dd66adfc0f62f56f0a666198ff0fbfb97 Mon Sep 17 00:00:00 2001 From: Andre Sterba <andre.sterba@stud.h-da.de> Date: Wed, 6 Apr 2022 16:24:05 +0000 Subject: [PATCH] Add environment to decide which config should be loaded See merge request danet/gosdn!275 --- .gitignore | 6 +++ controller/Makefile | 14 +++--- controller/cmd/root.go | 8 +++- controller/config/environment.go | 47 +++++++++++++++++++ controller/configs/ci-testing-gosdn.toml | 3 ++ .../configs/development-gosdn.toml.example | 14 ++++++ controller/controller_test.go | 2 +- 7 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 controller/config/environment.go create mode 100644 controller/configs/ci-testing-gosdn.toml create mode 100644 controller/configs/development-gosdn.toml.example diff --git a/.gitignore b/.gitignore index 3af7552b2..bcba6b719 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,9 @@ clab-gosdn_csbi_arista_base/ coverage.out report.xml +# artifacts +controller/gosdn +cli/gosdnc + +# testing +controller/configs/testing-gosdn.toml diff --git a/controller/Makefile b/controller/Makefile index fd5cb1936..f0fc33811 100644 --- a/controller/Makefile +++ b/controller/Makefile @@ -29,16 +29,16 @@ clean: rm -f $(BINARY_NAME) start: clean build - ./$(BINARY_NAME) -l debug + ENVIRONMENT=development ./$(BINARY_NAME) -l debug start-insecure: clean build - ./$(BINARY_NAME) -l debug -s insecure + ENVIRONMENT=development ./$(BINARY_NAME) -l debug -s insecure unit-test: install-tools - ./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out + ENVIRONMENT=testing ./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out controller-test: install-tools - ./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun + ENVIRONMENT=testing ./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun lint: install-tools ./$(TOOLS_DIR)/golangci-lint run --config .gitlab/ci/.golangci-config/.golangci.yml @@ -50,12 +50,13 @@ ci-lint: golangci-lint run --config ../.gitlab/ci/.golangci-config/.golangci.yml --out-format code-climate | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"' ci-unit-test: ci-install-tools - gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out + ENVIRONMENT=testing gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out ci-controller-test: ci-install-tools - gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun + ENVIRONMENT=testing gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun integration-test-nucleus: + ENVIRONMENT=testing &&\ cd ./test/integration &&\ go test -race -v -run TestGnmi_SetIntegration &&\ go test -race -v -run TestGnmi_GetIntegration &&\ @@ -63,5 +64,6 @@ integration-test-nucleus: go test -race -v -run TestGnmi_CapabilitiesIntegration integration-test-api: + ENVIRONMENT=testing &&\ cd ./api &&\ go test -race -v -run TestApiIntegration diff --git a/controller/cmd/root.go b/controller/cmd/root.go index b4479da02..d56e963cf 100644 --- a/controller/cmd/root.go +++ b/controller/cmd/root.go @@ -33,10 +33,12 @@ package cmd import ( "context" + "fmt" "os" "path/filepath" "code.fbi.h-da.de/danet/gosdn/controller" + "code.fbi.h-da.de/danet/gosdn/controller/config" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -96,10 +98,14 @@ func initConfig() { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { + env := config.DetermineConfigEnvironment() + + log.Infof("environment is %s\n", env) + viper.AddConfigPath(configHome) viper.AddConfigPath("/usr/local/etc/gosdn/") viper.SetConfigType(configType) - viper.SetConfigName(configName) + viper.SetConfigName(fmt.Sprintf("%s-%s", env, configName)) } viper.AutomaticEnv() // read in environment variables that match diff --git a/controller/config/environment.go b/controller/config/environment.go new file mode 100644 index 000000000..9e916e7c1 --- /dev/null +++ b/controller/config/environment.go @@ -0,0 +1,47 @@ +package config + +import ( + "os" + "strings" +) + +// Environment is used to determine the environment the controller is running in. +type Environment int64 + +const ( + // Development is used for local development + Development Environment = iota + // Production is used for production deployments + Production + // Testing is used for tests + Testing +) + +func (e Environment) String() string { + switch e { + case Development: + return "development" + case Production: + return "production" + case Testing: + return "testing" + } + + return "development" +} + +// DetermineConfigEnvironment returns the environment mode the controller should use. +func DetermineConfigEnvironment() Environment { + env := os.Getenv("ENVIRONMENT") + + switch strings.ToLower(env) { + case "development": + return Development + case "production": + return Production + case "testing": + return Testing + default: + return Development + } +} diff --git a/controller/configs/ci-testing-gosdn.toml b/controller/configs/ci-testing-gosdn.toml new file mode 100644 index 000000000..7c78182ab --- /dev/null +++ b/controller/configs/ci-testing-gosdn.toml @@ -0,0 +1,3 @@ +basepnduuid = "d4dd9fcb-7fe3-4657-aa33-f9071b2ae257" +basesouthboundtype = 1 +basesouthbounduuid = "a325693a-aa16-45fc-965d-08c986c476c1" diff --git a/controller/configs/development-gosdn.toml.example b/controller/configs/development-gosdn.toml.example new file mode 100644 index 000000000..a59052974 --- /dev/null +++ b/controller/configs/development-gosdn.toml.example @@ -0,0 +1,14 @@ +basepnduuid = "5f20f34b-cbd0-4511-9ddc-c50cf6a3b49d" +basesouthboundtype = 1 +basesouthbounduuid = "ca29311a-3b17-4385-96f8-515b602a97ac" +cli_pnd = "0455b241-5863-4660-ad15-dfde7617738e" +cli_sbi = "a249f2d2-f7da-481d-8a99-b7f11471e0af" +config = "" +csbi-orchestrator = "localhost:55056" +grpc-port = "" +help = false +log-level = "debug" +pnduuid = "bf8160d4-4659-4a1b-98fd-f409a04111ec" +socket = ":55055" +databaseConnection = "mongodb://root:example@localhost:27017" + diff --git a/controller/controller_test.go b/controller/controller_test.go index f825e95d6..ce8794bb6 100644 --- a/controller/controller_test.go +++ b/controller/controller_test.go @@ -12,7 +12,7 @@ import ( const ( configHome string = "./configs" - configName string = "gosdn" + configName string = "ci-testing-gosdn" configType string = "toml" ) -- GitLab