-
Notifications
You must be signed in to change notification settings - Fork 553
feat: Change cd deployment type #3332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
eb8b37e
93700a1
688ddc9
049e637
8c8941e
4be35b5
8d3fcfc
e4f5745
b1e626f
70796ee
785263c
70abf7f
efa2c8b
47216f2
1542b9c
45f6b1e
03da8fc
5baca2e
d8af573
9b2b62b
454d78b
ed52712
c364591
a54ae3e
e04e935
8fa989c
d1e0545
d53fdc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,8 @@ type DevtronAppDeploymentRestHandler interface { | |
| GetCdPipelineById(w http.ResponseWriter, r *http.Request) | ||
| PatchCdPipeline(w http.ResponseWriter, r *http.Request) | ||
| HandleChangeDeploymentRequest(w http.ResponseWriter, r *http.Request) | ||
| HandleChangeDeploymentTypeRequest(w http.ResponseWriter, r *http.Request) | ||
| HandleTriggerDeploymentAfterTypeChange(w http.ResponseWriter, r *http.Request) | ||
| GetCdPipelines(w http.ResponseWriter, r *http.Request) | ||
| GetCdPipelinesForAppAndEnv(w http.ResponseWriter, r *http.Request) | ||
|
|
||
|
|
@@ -357,6 +359,135 @@ func (handler PipelineConfigRestHandlerImpl) HandleChangeDeploymentRequest(w htt | |
| return | ||
| } | ||
|
|
||
| func (handler PipelineConfigRestHandlerImpl) HandleChangeDeploymentTypeRequest(w http.ResponseWriter, r *http.Request) { | ||
|
|
||
| // Auth check | ||
| userId, err := handler.userAuthService.GetLoggedInUser(r) | ||
| if userId == 0 || err != nil { | ||
| common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) | ||
| return | ||
| } | ||
|
|
||
| // Retrieving and parsing request body | ||
| decoder := json.NewDecoder(r.Body) | ||
| var deploymentTypeChangeRequest *bean.DeploymentAppTypeChangeRequest | ||
| err = decoder.Decode(&deploymentTypeChangeRequest) | ||
| if err != nil { | ||
| handler.Logger.Errorw("request err, HandleChangeDeploymentTypeRequest", "err", err, "payload", | ||
| deploymentTypeChangeRequest) | ||
|
|
||
| common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
| return | ||
| } | ||
| deploymentTypeChangeRequest.UserId = userId | ||
|
|
||
| // Validate incoming request | ||
| err = handler.validator.Struct(deploymentTypeChangeRequest) | ||
| if err != nil { | ||
| handler.Logger.Errorw("validation err, HandleChangeDeploymentTypeRequest", "err", err, "payload", | ||
| deploymentTypeChangeRequest) | ||
|
|
||
| common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Only super-admin access | ||
| token := r.Header.Get("token") | ||
| if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionDelete, "*"); !ok { | ||
| common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) | ||
| return | ||
| } | ||
|
|
||
| // Retrieve argocd token | ||
| acdToken, err := handler.argoUserService.GetLatestDevtronArgoCdUserToken() | ||
| if err != nil { | ||
| handler.Logger.Errorw("error in getting acd token", "err", err) | ||
| common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) | ||
| return | ||
| } | ||
| ctx := context.WithValue(r.Context(), "token", acdToken) | ||
|
|
||
| resp, err := handler.pipelineBuilder.ChangePipelineDeploymentType(ctx, deploymentTypeChangeRequest) | ||
|
|
||
| if err != nil { | ||
| nErr := errors.New("failed to change deployment type with error msg: " + err.Error()) | ||
| handler.Logger.Errorw(err.Error(), | ||
| "payload", deploymentTypeChangeRequest, | ||
| "err", err) | ||
|
|
||
| common.WriteJsonResp(w, nErr, nil, http.StatusInternalServerError) | ||
| return | ||
| } | ||
| common.WriteJsonResp(w, nil, resp, http.StatusOK) | ||
| return | ||
| } | ||
|
|
||
| func (handler PipelineConfigRestHandlerImpl) HandleTriggerDeploymentAfterTypeChange(w http.ResponseWriter, r *http.Request) { | ||
|
|
||
| // Auth check | ||
|
||
| userId, err := handler.userAuthService.GetLoggedInUser(r) | ||
| if userId == 0 || err != nil { | ||
| common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) | ||
| return | ||
| } | ||
|
|
||
| // Retrieving and parsing request body | ||
| decoder := json.NewDecoder(r.Body) | ||
| var deploymentAppTriggerRequest *bean.DeploymentAppTypeChangeRequest | ||
| err = decoder.Decode(&deploymentAppTriggerRequest) | ||
| if err != nil { | ||
| handler.Logger.Errorw("request err, HandleChangeDeploymentTypeRequest", "err", err, "payload", | ||
| deploymentAppTriggerRequest) | ||
|
|
||
| common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
| return | ||
| } | ||
| deploymentAppTriggerRequest.UserId = userId | ||
|
|
||
| // Validate incoming request | ||
| err = handler.validator.Struct(deploymentAppTriggerRequest) | ||
| if err != nil { | ||
| handler.Logger.Errorw("validation err, HandleChangeDeploymentTypeRequest", "err", err, "payload", | ||
| deploymentAppTriggerRequest) | ||
|
|
||
| common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Only super-admin access | ||
| token := r.Header.Get("token") | ||
|
|
||
| if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionDelete, "*"); !ok { | ||
| common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) | ||
| return | ||
| } | ||
|
|
||
| // Retrieve argocd token | ||
| acdToken, err := handler.argoUserService.GetLatestDevtronArgoCdUserToken() | ||
|
|
||
| if err != nil { | ||
| handler.Logger.Errorw("error in getting acd token", "err", err) | ||
| common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| ctx := context.WithValue(r.Context(), "token", acdToken) | ||
|
|
||
| resp, err := handler.pipelineBuilder.TriggerDeploymentAfterTypeChange(ctx, deploymentAppTriggerRequest) | ||
|
|
||
| if err != nil { | ||
| nErr := errors.New("failed to change deployment type with error msg: " + err.Error()) | ||
| handler.Logger.Errorw(err.Error(), | ||
| "payload", deploymentAppTriggerRequest, | ||
| "err", err) | ||
|
|
||
| common.WriteJsonResp(w, nErr, nil, http.StatusInternalServerError) | ||
| return | ||
| } | ||
| common.WriteJsonResp(w, nil, resp, http.StatusOK) | ||
| return | ||
| } | ||
|
|
||
| func (handler PipelineConfigRestHandlerImpl) EnvConfigOverrideCreate(w http.ResponseWriter, r *http.Request) { | ||
| userId, err := handler.userAuthService.GetLoggedInUser(r) | ||
| if userId == 0 || err != nil { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,8 +95,9 @@ type PipelineRepository interface { | |
| FindActiveByNotFilter(envId int, appIdExcludes []int) (pipelines []*Pipeline, err error) | ||
| FindAllPipelinesByChartsOverrideAndAppIdAndChartId(chartOverridden bool, appId int, chartId int) (pipelines []*Pipeline, err error) | ||
| FindActiveByAppIdAndPipelineId(appId int, pipelineId int) ([]*Pipeline, error) | ||
| UpdateCdPipeline(pipeline *Pipeline) error | ||
| UpdateCdPipelineDeploymentAppInFilter(deploymentAppType string, cdPipelineIdIncludes []int, userId int32) error | ||
| SetDeploymentAppCreatedInPipeline(deploymentAppCreated bool, pipelineId int, userId int32) error | ||
| UpdateCdPipelineDeploymentAppInFilter(deploymentAppType string, cdPipelineIdIncludes []int, userId int32, deploymentAppCreated bool, delete bool) error | ||
| UpdateCdPipelineAfterDeployment(deploymentAppType string, cdPipelineIdIncludes []int, userId int32, delete bool) error | ||
| FindNumberOfAppsWithCdPipeline(appIds []int) (count int, err error) | ||
| GetAppAndEnvDetailsForDeploymentAppTypePipeline(deploymentAppType string, clusterIds []int) ([]*Pipeline, error) | ||
| GetArgoPipelinesHavingTriggersStuckInLastPossibleNonTerminalTimelines(pendingSinceSeconds int, timeForDegradation int) ([]*Pipeline, error) | ||
|
|
@@ -490,25 +491,31 @@ func (impl PipelineRepositoryImpl) FindActiveByAppIdAndPipelineId(appId int, pip | |
| return pipelines, err | ||
| } | ||
|
|
||
| func (impl PipelineRepositoryImpl) UpdateCdPipeline(pipeline *Pipeline) error { | ||
| err := impl.dbConnection.Update(pipeline) | ||
| func (impl PipelineRepositoryImpl) SetDeploymentAppCreatedInPipeline(deploymentAppCreated bool, pipelineId int, userId int32) error { | ||
| query := "update pipeline set deployment_app_created=?, updated_on=?, updated_by=? where id=?;" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid writing native queries |
||
| var pipeline *Pipeline | ||
| _, err := impl.dbConnection.Query(pipeline, query, deploymentAppCreated, time.Now(), userId, pipelineId) | ||
| return err | ||
| } | ||
|
|
||
| // UpdateCdPipelineDeploymentAppInFilter takes in deployment app type and list of cd pipeline ids and | ||
| // updates the deployment_app_type and sets deployment_app_created to false in the table for given ids. | ||
| func (impl PipelineRepositoryImpl) UpdateCdPipelineDeploymentAppInFilter(deploymentAppType string, | ||
| cdPipelineIdIncludes []int, userId int32) error { | ||
| cdPipelineIdIncludes []int, userId int32, deploymentAppCreated bool, delete bool) error { | ||
| query := "update pipeline set deployment_app_created = ?, deployment_app_type = ?, " + | ||
| "updated_by = ?, updated_on = ?, deployment_app_delete_request = ? where id in (?);" | ||
| var pipeline *Pipeline | ||
| _, err := impl.dbConnection.Query(pipeline, query, deploymentAppCreated, deploymentAppType, userId, time.Now(), delete, pg.In(cdPipelineIdIncludes)) | ||
|
||
|
|
||
| query := "update pipeline set " + | ||
| "deployment_app_created = false, " + | ||
| "deployment_app_type = ?, " + | ||
| "updated_by = ?, " + | ||
| "updated_on = ? " + | ||
| "where id in (?)" | ||
| return err | ||
| } | ||
|
|
||
| func (impl PipelineRepositoryImpl) UpdateCdPipelineAfterDeployment(deploymentAppType string, | ||
| cdPipelineIdIncludes []int, userId int32, delete bool) error { | ||
| query := "update pipeline set deployment_app_type = ?, " + | ||
| "updated_by = ?, updated_on = ?, deployment_app_delete_request = ? where id in (?);" | ||
| var pipeline *Pipeline | ||
| _, err := impl.dbConnection.Query(pipeline, query, deploymentAppType, userId, time.Now(), pg.In(cdPipelineIdIncludes)) | ||
| _, err := impl.dbConnection.Query(pipeline, query, deploymentAppType, userId, time.Now(), delete, pg.In(cdPipelineIdIncludes)) | ||
|
||
|
|
||
| return err | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use format printer