Skip to content

Commit b006b34

Browse files
authored
feat: cd pipeline deployment history refactoring (#5200)
* searchableKey service move * migrated cd deployment history code
1 parent 5e2c91f commit b006b34

26 files changed

+634
-28
lines changed

Wire.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build wireinject
2+
// +build wireinject
3+
14
/*
25
* Copyright (c) 2024. Devtron Inc.
36
*
@@ -14,9 +17,6 @@
1417
* limitations under the License.
1518
*/
1619

17-
//go:build wireinject
18-
// +build wireinject
19-
2020
package main
2121

2222
import (
@@ -39,6 +39,7 @@ import (
3939
"github.com/devtron-labs/devtron/api/connector"
4040
"github.com/devtron-labs/devtron/api/dashboardEvent"
4141
"github.com/devtron-labs/devtron/api/deployment"
42+
"github.com/devtron-labs/devtron/api/devtronResource"
4243
"github.com/devtron-labs/devtron/api/externalLink"
4344
client "github.com/devtron-labs/devtron/api/helm-app"
4445
"github.com/devtron-labs/devtron/api/infraConfig"
@@ -121,8 +122,6 @@ import (
121122
deployment2 "github.com/devtron-labs/devtron/pkg/deployment"
122123
git2 "github.com/devtron-labs/devtron/pkg/deployment/gitOps/git"
123124
"github.com/devtron-labs/devtron/pkg/deploymentGroup"
124-
"github.com/devtron-labs/devtron/pkg/devtronResource"
125-
repository9 "github.com/devtron-labs/devtron/pkg/devtronResource/repository"
126125
"github.com/devtron-labs/devtron/pkg/dockerRegistry"
127126
"github.com/devtron-labs/devtron/pkg/eventProcessor"
128127
"github.com/devtron-labs/devtron/pkg/generateManifest"
@@ -195,6 +194,9 @@ func InitializeApp() (*App, error) {
195194

196195
eventProcessor.EventProcessorWireSet,
197196
workflow3.WorkflowWireSet,
197+
198+
devtronResource.DevtronResourceWireSet,
199+
198200
// -------wireset end ----------
199201
// -------
200202
gitSensor.GetConfig,
@@ -950,12 +952,6 @@ func InitializeApp() (*App, error) {
950952
resourceQualifiers.NewQualifierMappingServiceImpl,
951953
wire.Bind(new(resourceQualifiers.QualifierMappingService), new(*resourceQualifiers.QualifierMappingServiceImpl)),
952954

953-
repository9.NewDevtronResourceSearchableKeyRepositoryImpl,
954-
wire.Bind(new(repository9.DevtronResourceSearchableKeyRepository), new(*repository9.DevtronResourceSearchableKeyRepositoryImpl)),
955-
956-
devtronResource.NewDevtronResourceSearchableKeyServiceImpl,
957-
wire.Bind(new(devtronResource.DevtronResourceSearchableKeyService), new(*devtronResource.DevtronResourceSearchableKeyServiceImpl)),
958-
959955
argocdServer.NewArgoClientWrapperServiceImpl,
960956
wire.Bind(new(argocdServer.ArgoClientWrapperService), new(*argocdServer.ArgoClientWrapperServiceImpl)),
961957

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package devtronResource
2+
3+
import (
4+
"fmt"
5+
apiBean "github.com/devtron-labs/devtron/api/devtronResource/bean"
6+
"github.com/devtron-labs/devtron/api/restHandler/common"
7+
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
8+
"github.com/devtron-labs/devtron/pkg/devtronResource"
9+
"github.com/devtron-labs/devtron/pkg/devtronResource/adapter"
10+
"github.com/devtron-labs/devtron/pkg/devtronResource/bean"
11+
"github.com/devtron-labs/devtron/pkg/devtronResource/history/deployment/cdPipeline"
12+
"github.com/devtron-labs/devtron/util/rbac"
13+
"github.com/gorilla/schema"
14+
"go.uber.org/zap"
15+
"net/http"
16+
)
17+
18+
type HistoryRestHandler interface {
19+
GetDeploymentHistory(w http.ResponseWriter, r *http.Request)
20+
GetDeploymentHistoryConfigList(w http.ResponseWriter, r *http.Request)
21+
}
22+
23+
type HistoryRestHandlerImpl struct {
24+
logger *zap.SugaredLogger
25+
enforcer casbin.Enforcer
26+
deploymentHistoryService cdPipeline.DeploymentHistoryService
27+
apiReqDecoderService devtronResource.APIReqDecoderService
28+
enforcerUtil rbac.EnforcerUtil
29+
}
30+
31+
func NewHistoryRestHandlerImpl(logger *zap.SugaredLogger,
32+
enforcer casbin.Enforcer,
33+
deploymentHistoryService cdPipeline.DeploymentHistoryService,
34+
apiReqDecoderService devtronResource.APIReqDecoderService,
35+
enforcerUtil rbac.EnforcerUtil) *HistoryRestHandlerImpl {
36+
return &HistoryRestHandlerImpl{
37+
logger: logger,
38+
enforcer: enforcer,
39+
deploymentHistoryService: deploymentHistoryService,
40+
apiReqDecoderService: apiReqDecoderService,
41+
enforcerUtil: enforcerUtil,
42+
}
43+
}
44+
45+
func (handler *HistoryRestHandlerImpl) GetDeploymentHistory(w http.ResponseWriter, r *http.Request) {
46+
kind, _, _, caughtError := getKindSubKindVersion(w, r)
47+
if caughtError || kind != bean.DevtronResourceCdPipeline.ToString() {
48+
common.WriteJsonResp(w, fmt.Errorf(apiBean.RequestInvalidKindVersionErrMessage), nil, http.StatusBadRequest)
49+
return
50+
}
51+
v := r.URL.Query()
52+
var decoder = schema.NewDecoder()
53+
decoder.IgnoreUnknownKeys(true)
54+
queryParams := apiBean.GetHistoryQueryParams{}
55+
err := decoder.Decode(&queryParams, v)
56+
if err != nil {
57+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
58+
return
59+
}
60+
decodedReqBean, err := handler.apiReqDecoderService.GetFilterCriteriaParamsForDeploymentHistory(queryParams.FilterCriteria)
61+
if err != nil {
62+
handler.logger.Errorw("error in getting filter criteria params", "err", err, "filterCriteria", queryParams.FilterCriteria)
63+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
64+
return
65+
}
66+
67+
// RBAC START
68+
token := r.Header.Get("token")
69+
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(decodedReqBean.AppId)
70+
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !ok {
71+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
72+
return
73+
}
74+
// RBAC END
75+
76+
resp, err := handler.deploymentHistoryService.
77+
GetCdPipelineDeploymentHistory(adapter.GetCDDeploymentHistoryListReq(&queryParams, decodedReqBean))
78+
if err != nil {
79+
handler.logger.Errorw("service error, GetCdPipelineDeploymentHistory", "err", err)
80+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
81+
return
82+
}
83+
common.WriteJsonResp(w, err, resp, http.StatusOK)
84+
return
85+
}
86+
87+
func (handler *HistoryRestHandlerImpl) GetDeploymentHistoryConfigList(w http.ResponseWriter, r *http.Request) {
88+
kind, _, _, caughtError := getKindSubKindVersion(w, r)
89+
if caughtError || kind != bean.DevtronResourceCdPipeline.ToString() {
90+
common.WriteJsonResp(w, fmt.Errorf(apiBean.RequestInvalidKindVersionErrMessage), nil, http.StatusBadRequest)
91+
return
92+
}
93+
v := r.URL.Query()
94+
var decoder = schema.NewDecoder()
95+
decoder.IgnoreUnknownKeys(true)
96+
queryParams := apiBean.GetHistoryConfigQueryParams{}
97+
err := decoder.Decode(&queryParams, v)
98+
if err != nil {
99+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
100+
return
101+
}
102+
decodedReqBean, err := handler.apiReqDecoderService.GetFilterCriteriaParamsForDeploymentHistory(queryParams.FilterCriteria)
103+
if err != nil {
104+
handler.logger.Errorw("error in getting filter criteria params", "err", err, "filterCriteria", queryParams.FilterCriteria)
105+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
106+
return
107+
}
108+
//RBAC START
109+
token := r.Header.Get("token")
110+
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(decodedReqBean.AppId)
111+
if isValidated := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !isValidated {
112+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
113+
return
114+
}
115+
//RBAC END
116+
resp, err := handler.deploymentHistoryService.
117+
GetCdPipelineDeploymentHistoryConfigList(adapter.GetCDDeploymentHistoryConfigListReq(&queryParams, decodedReqBean))
118+
if err != nil {
119+
handler.logger.Errorw("service error, GetCdPipelineDeploymentHistoryConfigList", "err", err)
120+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
121+
return
122+
}
123+
common.WriteJsonResp(w, err, resp, http.StatusOK)
124+
return
125+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package devtronResource
2+
3+
import "github.com/gorilla/mux"
4+
5+
type HistoryRouter interface {
6+
InitDtResourceHistoryRouter(devtronResourceRouter *mux.Router)
7+
}
8+
9+
type HistoryRouterImpl struct {
10+
dtResourceHistoryRestHandler HistoryRestHandler
11+
}
12+
13+
func NewHistoryRouterImpl(dtResourceHistoryRestHandler HistoryRestHandler) *HistoryRouterImpl {
14+
return &HistoryRouterImpl{dtResourceHistoryRestHandler: dtResourceHistoryRestHandler}
15+
}
16+
17+
func (router *HistoryRouterImpl) InitDtResourceHistoryRouter(historyRouter *mux.Router) {
18+
historyRouter.Path("/deployment/config/{kind:[a-zA-Z0-9/-]+}/{version:[a-zA-Z0-9]+}").
19+
HandlerFunc(router.dtResourceHistoryRestHandler.GetDeploymentHistoryConfigList).Methods("GET")
20+
21+
historyRouter.Path("/deployment/{kind:[a-zA-Z0-9/-]+}/{version:[a-zA-Z0-9]+}").
22+
HandlerFunc(router.dtResourceHistoryRestHandler.GetDeploymentHistory).Methods("GET")
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package devtronResource
2+
3+
import (
4+
"fmt"
5+
apiBean "github.com/devtron-labs/devtron/api/devtronResource/bean"
6+
"github.com/devtron-labs/devtron/api/restHandler/common"
7+
"github.com/devtron-labs/devtron/pkg/devtronResource/helper"
8+
"github.com/gorilla/mux"
9+
"net/http"
10+
)
11+
12+
func getKindSubKindVersion(w http.ResponseWriter, r *http.Request) (kind string, subKind string, version string, caughtError bool) {
13+
vars := mux.Vars(r)
14+
kindVar := vars[apiBean.PathParamKind]
15+
versionVar := vars[apiBean.PathParamVersion]
16+
kind, subKind, statusCode, err := resolveKindSubKindValues(kindVar)
17+
if err != nil {
18+
common.WriteJsonResp(w, err, nil, statusCode)
19+
caughtError = true
20+
}
21+
return kind, subKind, versionVar, caughtError
22+
}
23+
24+
func resolveKindSubKindValues(kindVar string) (kind, subKind string, statusCode int, err error) {
25+
kind, subKind, err = helper.GetKindAndSubKindFrom(kindVar)
26+
if err != nil {
27+
err = fmt.Errorf("invalid parameter: kind")
28+
statusCode = http.StatusBadRequest
29+
}
30+
return kind, subKind, statusCode, err
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package devtronResource
2+
3+
import "github.com/gorilla/mux"
4+
5+
type DevtronResourceRouter interface {
6+
InitDevtronResourceRouter(devtronResourceRouter *mux.Router)
7+
}
8+
9+
type DevtronResourceRouterImpl struct {
10+
historyRouter HistoryRouter
11+
}
12+
13+
func NewDevtronResourceRouterImpl(historyRouter HistoryRouter) *DevtronResourceRouterImpl {
14+
return &DevtronResourceRouterImpl{
15+
historyRouter: historyRouter,
16+
}
17+
}
18+
19+
func (router *DevtronResourceRouterImpl) InitDevtronResourceRouter(devtronResourceRouter *mux.Router) {
20+
historyRouter := devtronResourceRouter.PathPrefix("/history").Subrouter()
21+
router.historyRouter.InitDtResourceHistoryRouter(historyRouter)
22+
}

api/devtronResource/bean/bean.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package bean
2+
3+
type GetHistoryQueryParams struct {
4+
FilterCriteria []string `schema:"filterCriteria"`
5+
OffSet int `schema:"offSet"`
6+
Limit int `schema:"limit"`
7+
}
8+
9+
type GetHistoryConfigQueryParams struct {
10+
BaseConfigurationId int `schema:"baseConfigurationId"`
11+
HistoryComponent string `schema:"historyComponent"`
12+
HistoryComponentName string `schema:"historyComponentName"`
13+
FilterCriteria []string `schema:"filterCriteria"`
14+
}
15+
16+
const (
17+
RequestInvalidKindVersionErrMessage = "Invalid kind and version! Implementation not supported."
18+
PathParamKind = "kind"
19+
PathParamVersion = "version"
20+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package devtronResource
2+
3+
import (
4+
"github.com/devtron-labs/devtron/pkg/devtronResource"
5+
"github.com/devtron-labs/devtron/pkg/devtronResource/history/deployment/cdPipeline"
6+
"github.com/devtron-labs/devtron/pkg/devtronResource/read"
7+
"github.com/devtron-labs/devtron/pkg/devtronResource/repository"
8+
"github.com/google/wire"
9+
)
10+
11+
var DevtronResourceWireSet = wire.NewSet(
12+
//old bindings, migrated from wire.go
13+
read.NewDevtronResourceSearchableKeyServiceImpl,
14+
wire.Bind(new(read.DevtronResourceSearchableKeyService), new(*read.DevtronResourceSearchableKeyServiceImpl)),
15+
repository.NewDevtronResourceSearchableKeyRepositoryImpl,
16+
wire.Bind(new(repository.DevtronResourceSearchableKeyRepository), new(*repository.DevtronResourceSearchableKeyRepositoryImpl)),
17+
18+
NewDevtronResourceRouterImpl,
19+
wire.Bind(new(DevtronResourceRouter), new(*DevtronResourceRouterImpl)),
20+
21+
NewHistoryRouterImpl,
22+
wire.Bind(new(HistoryRouter), new(*HistoryRouterImpl)),
23+
NewHistoryRestHandlerImpl,
24+
wire.Bind(new(HistoryRestHandler), new(*HistoryRestHandlerImpl)),
25+
cdPipeline.NewDeploymentHistoryServiceImpl,
26+
wire.Bind(new(cdPipeline.DeploymentHistoryService), new(*cdPipeline.DeploymentHistoryServiceImpl)),
27+
28+
devtronResource.NewAPIReqDecoderServiceImpl,
29+
wire.Bind(new(devtronResource.APIReqDecoderService), new(*devtronResource.APIReqDecoderServiceImpl)),
30+
)

api/router/router.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/devtron-labs/devtron/api/cluster"
3030
"github.com/devtron-labs/devtron/api/dashboardEvent"
3131
"github.com/devtron-labs/devtron/api/deployment"
32+
"github.com/devtron-labs/devtron/api/devtronResource"
3233
"github.com/devtron-labs/devtron/api/externalLink"
3334
client "github.com/devtron-labs/devtron/api/helm-app"
3435
"github.com/devtron-labs/devtron/api/infraConfig"
@@ -114,6 +115,7 @@ type MuxRouter struct {
114115
ciTriggerCron cron.CiTriggerCron
115116
infraConfigRouter infraConfig.InfraConfigRouter
116117
argoApplicationRouter argoApplication.ArgoApplicationRouter
118+
devtronResourceRouter devtronResource.DevtronResourceRouter
117119
}
118120

119121
func NewMuxRouter(logger *zap.SugaredLogger,
@@ -143,7 +145,8 @@ func NewMuxRouter(logger *zap.SugaredLogger,
143145
ciTriggerCron cron.CiTriggerCron,
144146
proxyRouter proxy.ProxyRouter,
145147
infraConfigRouter infraConfig.InfraConfigRouter,
146-
argoApplicationRouter argoApplication.ArgoApplicationRouter) *MuxRouter {
148+
argoApplicationRouter argoApplication.ArgoApplicationRouter,
149+
devtronResourceRouter devtronResource.DevtronResourceRouter) *MuxRouter {
147150
r := &MuxRouter{
148151
Router: mux.NewRouter(),
149152
EnvironmentClusterMappingsRouter: EnvironmentClusterMappingsRouter,
@@ -205,6 +208,7 @@ func NewMuxRouter(logger *zap.SugaredLogger,
205208
ciTriggerCron: ciTriggerCron,
206209
infraConfigRouter: infraConfigRouter,
207210
argoApplicationRouter: argoApplicationRouter,
211+
devtronResourceRouter: devtronResourceRouter,
208212
}
209213
return r
210214
}
@@ -404,6 +408,9 @@ func (r MuxRouter) Init() {
404408
rbacRoleRouter := r.Router.PathPrefix("/orchestrator/rbac/role").Subrouter()
405409
r.rbacRoleRouter.InitRbacRoleRouter(rbacRoleRouter)
406410

411+
devtronResourceRouter := r.Router.PathPrefix("/orchestrator/resource").Subrouter()
412+
r.devtronResourceRouter.InitDevtronResourceRouter(devtronResourceRouter)
413+
407414
infraConfigRouter := r.Router.PathPrefix("/orchestrator/infra-config").Subrouter()
408415
r.infraConfigRouter.InitInfraConfigRouter(infraConfigRouter)
409416

cmd/external-app/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/sql/repository/pipelineConfig/PipelineRepository.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type PipelineRepository interface {
101101
FindActiveByNotFilter(envId int, appIdExcludes []int) (pipelines []*Pipeline, err error)
102102
FindAllPipelinesByChartsOverrideAndAppIdAndChartId(chartOverridden bool, appId int, chartId int) (pipelines []*Pipeline, err error)
103103
FindActiveByAppIdAndPipelineId(appId int, pipelineId int) ([]*Pipeline, error)
104+
FindActiveByAppIdAndEnvId(appId int, envId int) (*Pipeline, error)
104105
SetDeploymentAppCreatedInPipeline(deploymentAppCreated bool, pipelineId int, userId int32) error
105106
UpdateCdPipelineDeploymentAppInFilter(deploymentAppType string, cdPipelineIdIncludes []int, userId int32, deploymentAppCreated bool, delete bool) error
106107
UpdateCdPipelineAfterDeployment(deploymentAppType string, cdPipelineIdIncludes []int, userId int32, delete bool) error
@@ -527,6 +528,16 @@ func (impl PipelineRepositoryImpl) FindActiveByAppIdAndPipelineId(appId int, pip
527528
return pipelines, err
528529
}
529530

531+
func (impl PipelineRepositoryImpl) FindActiveByAppIdAndEnvId(appId int, envId int) (*Pipeline, error) {
532+
var pipeline Pipeline
533+
err := impl.dbConnection.Model(&pipeline).
534+
Where("app_id = ?", appId).
535+
Where("environment_id = ?", envId).
536+
Where("deleted = ?", false).
537+
Select()
538+
return &pipeline, err
539+
}
540+
530541
func (impl PipelineRepositoryImpl) SetDeploymentAppCreatedInPipeline(deploymentAppCreated bool, pipelineId int, userId int32) error {
531542
query := "update pipeline set deployment_app_created=?, updated_on=?, updated_by=? where id=?;"
532543
var pipeline *Pipeline

0 commit comments

Comments
 (0)