From d12c05b09dfadc7daa2f6c5341e93b5fe29662aa Mon Sep 17 00:00:00 2001 From: Hadrian Bayanulhaq S Date: Fri, 11 Oct 2019 14:08:55 +0700 Subject: [PATCH 1/3] [hadrian.siregar|bimo.horizon] Inject user email to execution argument in execution post handler --- internal/app/service/execution/handler/http.go | 10 ++++++---- internal/app/service/execution/handler/http_test.go | 10 ++++++++-- internal/pkg/constant/constant.go | 1 + 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/internal/app/service/execution/handler/http.go b/internal/app/service/execution/handler/http.go index 1d267724..99ec9b31 100644 --- a/internal/app/service/execution/handler/http.go +++ b/internal/app/service/execution/handler/http.go @@ -20,6 +20,7 @@ import ( "proctor/internal/app/service/infra/config" "proctor/internal/app/service/infra/logger" serviceNotification "proctor/internal/app/service/notification/service" + "proctor/internal/pkg/constant" "proctor/internal/pkg/model/execution" "proctor/pkg/notification/event" ) @@ -155,10 +156,10 @@ func (httpHandler *executionHTTPHandler) GetStatus() http.HandlerFunc { response.WriteHeader(http.StatusOK) - responseJson, err := json.Marshal(responseBody) + responseJSON, err := json.Marshal(responseBody) logger.LogErrors(err, "marshal json from: ", responseBody) - _, _ = response.Write(responseJson) + _, _ = response.Write(responseJSON) } } @@ -179,6 +180,7 @@ func (httpHandler *executionHTTPHandler) Post() http.HandlerFunc { _, _ = response.Write([]byte(status.MalformedRequest)) return } + job.Args[constant.AuthorEmailKey] = userEmail context, executionName, err := httpHandler.service.Execute(job.Name, userEmail, job.Args) @@ -205,10 +207,10 @@ func (httpHandler *executionHTTPHandler) Post() http.HandlerFunc { response.WriteHeader(http.StatusCreated) - responseJson, err := json.Marshal(responseBody) + responseJSON, err := json.Marshal(responseBody) logger.LogErrors(err, "marshal json from: ", responseBody) - _, _ = response.Write(responseJson) + _, _ = response.Write(responseJSON) return } } diff --git a/internal/app/service/execution/handler/http_test.go b/internal/app/service/execution/handler/http_test.go index 9a7e0c3e..b1b96a7b 100644 --- a/internal/app/service/execution/handler/http_test.go +++ b/internal/app/service/execution/handler/http_test.go @@ -261,7 +261,10 @@ func (suite *ExecutionHTTPHandlerTestSuite) TestSuccessfulJobExecutionPostHTTPHa userEmail := "mrproctor@example.com" job := parameter.Job{ Name: "sample-job-name", - Args: map[string]string{"argOne": "sample-arg"}, + Args: map[string]string{ + "argOne": "sample-arg", + "AUTHOR": userEmail, + }, } context := &model.ExecutionContext{ UserEmail: userEmail, @@ -321,7 +324,10 @@ func (suite *ExecutionHTTPHandlerTestSuite) TestGenericErrorJobExecutionPostHTTP userEmail := "mrproctor@example.com" job := parameter.Job{ Name: "sample-job-name", - Args: map[string]string{"argOne": "sample-arg"}, + Args: map[string]string{ + "argOne": "sample-arg", + "AUTHOR": userEmail, + }, } context := &model.ExecutionContext{ UserEmail: userEmail, diff --git a/internal/pkg/constant/constant.go b/internal/pkg/constant/constant.go index 14476b93..e4a190d3 100644 --- a/internal/pkg/constant/constant.go +++ b/internal/pkg/constant/constant.go @@ -49,3 +49,4 @@ const AccessTokenHeaderKey = "Access-Token" const ClientVersionHeaderKey = "Client-Version" const WorkerEmail = "worker@proctor" +const AuthorEmailKey = "AUTHOR" From 027871314c3806f5198909963588b0406356507f Mon Sep 17 00:00:00 2001 From: Hadrian Bayanulhaq S Date: Fri, 11 Oct 2019 17:02:49 +0700 Subject: [PATCH 2/3] [hadrian.siregar|bimo.horizon] Inject context args to execution args in serice instead doing it in controller --- internal/app/service/execution/handler/http.go | 3 --- .../app/service/execution/model/execution_context.go | 4 +++- internal/app/service/execution/service/execution.go | 10 ++++++++++ .../app/service/execution/service/execution_test.go | 4 ++-- internal/pkg/constant/constant.go | 1 - 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/app/service/execution/handler/http.go b/internal/app/service/execution/handler/http.go index 99ec9b31..e2fe4045 100644 --- a/internal/app/service/execution/handler/http.go +++ b/internal/app/service/execution/handler/http.go @@ -20,7 +20,6 @@ import ( "proctor/internal/app/service/infra/config" "proctor/internal/app/service/infra/logger" serviceNotification "proctor/internal/app/service/notification/service" - "proctor/internal/pkg/constant" "proctor/internal/pkg/model/execution" "proctor/pkg/notification/event" ) @@ -180,8 +179,6 @@ func (httpHandler *executionHTTPHandler) Post() http.HandlerFunc { _, _ = response.Write([]byte(status.MalformedRequest)) return } - job.Args[constant.AuthorEmailKey] = userEmail - context, executionName, err := httpHandler.service.Execute(job.Name, userEmail, job.Args) logger.LogErrors(err, "execute job: ", job) diff --git a/internal/app/service/execution/model/execution_context.go b/internal/app/service/execution/model/execution_context.go index 28d44ccc..43fd0224 100644 --- a/internal/app/service/execution/model/execution_context.go +++ b/internal/app/service/execution/model/execution_context.go @@ -1,10 +1,12 @@ package model import ( + "time" + sqlxTypes "github.com/jmoiron/sqlx/types" + "proctor/internal/app/service/execution/status" dbTypes "proctor/internal/app/service/infra/db/types" - "time" ) type ExecutionContext struct { diff --git a/internal/app/service/execution/service/execution.go b/internal/app/service/execution/service/execution.go index 2bfb4ac6..be8c78b8 100644 --- a/internal/app/service/execution/service/execution.go +++ b/internal/app/service/execution/service/execution.go @@ -104,6 +104,16 @@ func (service *executionService) ExecuteWithCommand(jobName string, userEmail st executionArgs := mergeArgs(args, secret) + contextArgsMap := map[string]string{ + "EXECUTION_ID": fmt.Sprint(context.ExecutionID), + "JOB_NAME": context.JobName, + "EXECUTION_NAME": context.Name, + "USER_EMAIL": context.UserEmail, + "IMAGE_TAG": context.ImageTag, + } + + executionArgs = mergeArgs(executionArgs, contextArgsMap) + context.Status = status.Created executionName, err := service.kubernetesClient.ExecuteJobWithCommand(metadata.ImageName, executionArgs, commands) logger.Info("Executed Job on Kubernetes got ", executionName, " execution jobName and ", err, "errors") diff --git a/internal/app/service/execution/service/execution_test.go b/internal/app/service/execution/service/execution_test.go index cf86b72a..bedc6f28 100644 --- a/internal/app/service/execution/service/execution_test.go +++ b/internal/app/service/execution/service/execution_test.go @@ -105,7 +105,7 @@ func (suite *TestExecutionServiceSuite) TestExecuteJobFailed() { suite.mockMetadataRepository.On("GetByName", jobName).Return(fakeMetadata, nil).Once() suite.mockSecretRepository.On("GetByJobName", jobName).Return(map[string]string{}, nil).Once() suite.mockRepository.On("Insert", mock.Anything).Return(0, nil).Once() - suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, jobArgs, []string{}).Return("", errors.New("Execution Failed")) + suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, mock.Anything, []string{}).Return("", errors.New("Execution Failed")) context, _, err := suite.service.Execute(jobName, userEmail, jobArgs) assert.Error(t, err, "error when executing image") @@ -143,7 +143,7 @@ func (suite *TestExecutionServiceSuite) TestExecuteJobSuccess() { suite.mockRepository.On("GetById", mock.Anything).Return(0, nil).Times(3) executionName := "execution-name" - suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, jobArgs, []string{}).Return(executionName, nil) + suite.mockKubernetesClient.On("ExecuteJobWithCommand", imageName, mock.Anything, []string{}).Return(executionName, nil) suite.mockKubernetesClient.On("WaitForReadyJob", executionName, mock.Anything).Return(nil) podDetail := &v1.Pod{} suite.mockKubernetesClient.On("WaitForReadyPod", executionName, mock.Anything).Return(podDetail, nil) diff --git a/internal/pkg/constant/constant.go b/internal/pkg/constant/constant.go index e4a190d3..14476b93 100644 --- a/internal/pkg/constant/constant.go +++ b/internal/pkg/constant/constant.go @@ -49,4 +49,3 @@ const AccessTokenHeaderKey = "Access-Token" const ClientVersionHeaderKey = "Client-Version" const WorkerEmail = "worker@proctor" -const AuthorEmailKey = "AUTHOR" From e827d703c2d833039f77903b1e81e5b478f785ef Mon Sep 17 00:00:00 2001 From: Hadrian Bayanulhaq S Date: Fri, 11 Oct 2019 17:08:30 +0700 Subject: [PATCH 3/3] remove unnecessary test code --- internal/app/service/execution/handler/http_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/app/service/execution/handler/http_test.go b/internal/app/service/execution/handler/http_test.go index b1b96a7b..c53a4d53 100644 --- a/internal/app/service/execution/handler/http_test.go +++ b/internal/app/service/execution/handler/http_test.go @@ -263,7 +263,6 @@ func (suite *ExecutionHTTPHandlerTestSuite) TestSuccessfulJobExecutionPostHTTPHa Name: "sample-job-name", Args: map[string]string{ "argOne": "sample-arg", - "AUTHOR": userEmail, }, } context := &model.ExecutionContext{ @@ -326,7 +325,6 @@ func (suite *ExecutionHTTPHandlerTestSuite) TestGenericErrorJobExecutionPostHTTP Name: "sample-job-name", Args: map[string]string{ "argOne": "sample-arg", - "AUTHOR": userEmail, }, } context := &model.ExecutionContext{