From d603a8ebb0c238276d00d838f513fc125a00a7c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Pa=C3=9F?= <22845248+mpass99@users.noreply.github.com> Date: Wed, 2 Feb 2022 10:22:16 +0100 Subject: [PATCH] Refactor static AWS functions from a magic number in the code to a configurable list in configuration.yaml --- configuration.example.yaml | 2 ++ internal/config/config.go | 10 ++++++---- internal/environment/aws_manager.go | 22 ++++++++++++---------- internal/environment/aws_manager_test.go | 19 +++++++------------ internal/runner/aws_manager.go | 2 -- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/configuration.example.yaml b/configuration.example.yaml index 42b04e0..e268cd1 100644 --- a/configuration.example.yaml +++ b/configuration.example.yaml @@ -48,6 +48,8 @@ aws: enabled: false # The enpoint of the WebSocket API endpoint: wss://abcdef1234.execute-api.eu-central-1.amazonaws.com/production + # Currently, only static AWS environments are supported. You can list them here. + functions: "java11Exec go118Exec" # Configuration of the logger logger: diff --git a/internal/config/config.go b/internal/config/config.go index 5ae2e76..802d326 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -45,8 +45,9 @@ var ( Namespace: "default", }, AWS: AWS{ - Enabled: false, - Endpoint: "", + Enabled: false, + Endpoint: "", + Functions: "", }, Logger: logger{ Level: "INFO", @@ -96,8 +97,9 @@ func (n *Nomad) URL() *url.URL { // AWS configures the AWS Lambda usage. type AWS struct { - Enabled bool - Endpoint string + Enabled bool + Endpoint string + Functions string } // TLS configures TLS on a connection. diff --git a/internal/environment/aws_manager.go b/internal/environment/aws_manager.go index 6793714..1ec4138 100644 --- a/internal/environment/aws_manager.go +++ b/internal/environment/aws_manager.go @@ -2,8 +2,10 @@ package environment import ( "fmt" + "github.com/openHPI/poseidon/internal/config" "github.com/openHPI/poseidon/internal/runner" "github.com/openHPI/poseidon/pkg/dto" + "strings" ) // AWSEnvironmentManager contains no functionality at the moment. @@ -16,7 +18,6 @@ type AWSEnvironmentManager struct { func NewAWSEnvironmentManager(runnerManager runner.Manager) *AWSEnvironmentManager { m := &AWSEnvironmentManager{&AbstractManager{nil}, runnerManager} runnerManager.Load() - m.Load() return m } @@ -43,7 +44,7 @@ func (a *AWSEnvironmentManager) Get(id dto.EnvironmentID, fetch bool) (runner.Ex func (a *AWSEnvironmentManager) CreateOrUpdate( id dto.EnvironmentID, request dto.ExecutionEnvironmentRequest) (bool, error) { - if id != runner.AwsJavaEnvironmentID { + if !isAWSEnvironment(request) { isCreated, err := a.NextHandler().CreateOrUpdate(id, request) if err != nil { return false, fmt.Errorf("aws wrapped: %w", err) @@ -59,6 +60,15 @@ func (a *AWSEnvironmentManager) CreateOrUpdate( return !ok, nil } +func isAWSEnvironment(request dto.ExecutionEnvironmentRequest) bool { + for _, function := range strings.Fields(config.Config.AWS.Functions) { + if request.Image == function { + return true + } + } + return false +} + func (a *AWSEnvironmentManager) Delete(id dto.EnvironmentID) (bool, error) { e, ok := a.runnerManager.GetEnvironment(id) if !ok { @@ -79,11 +89,3 @@ func (a *AWSEnvironmentManager) Delete(id dto.EnvironmentID) (bool, error) { func (a *AWSEnvironmentManager) Statistics() map[dto.EnvironmentID]*dto.StatisticalExecutionEnvironmentData { return a.NextHandler().Statistics() } - -// Load fetches all remote environments in the local storage. ToDo: Fetch dynamically. -func (a *AWSEnvironmentManager) Load() { - _, err := a.CreateOrUpdate(runner.AwsJavaEnvironmentID, dto.ExecutionEnvironmentRequest{Image: "java11Exec"}) - if err != nil { - log.WithError(err).Warn("Could not load aws environment.") - } -} diff --git a/internal/environment/aws_manager_test.go b/internal/environment/aws_manager_test.go index 8453c51..43c50b5 100644 --- a/internal/environment/aws_manager_test.go +++ b/internal/environment/aws_manager_test.go @@ -1,6 +1,7 @@ package environment import ( + "github.com/openHPI/poseidon/internal/config" "github.com/openHPI/poseidon/internal/runner" "github.com/openHPI/poseidon/pkg/dto" "github.com/openHPI/poseidon/tests" @@ -13,15 +14,16 @@ import ( func TestAWSEnvironmentManager_CreateOrUpdate(t *testing.T) { runnerManager := runner.NewAWSRunnerManager() m := NewAWSEnvironmentManager(runnerManager) - uniqueImage := "random image string" + uniqueImage := "java11Exec" t.Run("can create default Java environment", func(t *testing.T) { - _, err := m.CreateOrUpdate(runner.AwsJavaEnvironmentID, dto.ExecutionEnvironmentRequest{Image: uniqueImage}) + config.Config.AWS.Functions = uniqueImage + _, err := m.CreateOrUpdate(tests.AnotherEnvironmentIDAsInteger, dto.ExecutionEnvironmentRequest{Image: uniqueImage}) assert.NoError(t, err) }) t.Run("can retrieve added environment", func(t *testing.T) { - environment, err := m.Get(runner.AwsJavaEnvironmentID, false) + environment, err := m.Get(tests.AnotherEnvironmentIDAsInteger, false) assert.NoError(t, err) assert.Equal(t, environment.Image(), uniqueImage) }) @@ -78,13 +80,6 @@ func TestAWSEnvironmentManager_List(t *testing.T) { runnerManager := runner.NewAWSRunnerManager() m := NewAWSEnvironmentManager(runnerManager) - t.Run("contains the \"Load\"-ed environments", func(t *testing.T) { - environments, err := m.List(false) - assert.NoError(t, err) - require.Len(t, environments, 1) - assert.Equal(t, environments[0].ID(), dto.EnvironmentID(runner.AwsJavaEnvironmentID)) - }) - t.Run("returs also environments of the rest of the manager chain", func(t *testing.T) { nextHandler := &ManagerHandlerMock{} existingEnvironment := NewAWSEnvironment() @@ -94,7 +89,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) { environments, err := m.List(false) assert.NoError(t, err) - require.Len(t, environments, 2) + require.Len(t, environments, 1) assert.Contains(t, environments, existingEnvironment) }) m.SetNextHandler(nil) @@ -106,7 +101,7 @@ func TestAWSEnvironmentManager_List(t *testing.T) { environments, err := m.List(false) assert.NoError(t, err) - assert.Len(t, environments, 2) + assert.Len(t, environments, 1) assert.Contains(t, environments, localEnvironment) }) } diff --git a/internal/runner/aws_manager.go b/internal/runner/aws_manager.go index 9596d66..d7889a3 100644 --- a/internal/runner/aws_manager.go +++ b/internal/runner/aws_manager.go @@ -10,8 +10,6 @@ type AWSRunnerManager struct { *AbstractManager } -const AwsJavaEnvironmentID = 2142 - // NewAWSRunnerManager creates a new runner manager that keeps track of all runners at AWS. func NewAWSRunnerManager() *AWSRunnerManager { return &AWSRunnerManager{NewAbstractManager()}