Skip to content

Commit

Permalink
feat: cd pipeline deployment history refactoring (#5200)
Browse files Browse the repository at this point in the history
* searchableKey service move

* migrated cd deployment history code
  • Loading branch information
kartik-579 authored Jun 6, 2024
1 parent 5e2c91f commit b006b34
Show file tree
Hide file tree
Showing 26 changed files with 634 additions and 28 deletions.
18 changes: 7 additions & 11 deletions Wire.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build wireinject
// +build wireinject

/*
* Copyright (c) 2024. Devtron Inc.
*
Expand All @@ -14,9 +17,6 @@
* limitations under the License.
*/

//go:build wireinject
// +build wireinject

package main

import (
Expand All @@ -39,6 +39,7 @@ import (
"github.com/devtron-labs/devtron/api/connector"
"github.com/devtron-labs/devtron/api/dashboardEvent"
"github.com/devtron-labs/devtron/api/deployment"
"github.com/devtron-labs/devtron/api/devtronResource"
"github.com/devtron-labs/devtron/api/externalLink"
client "github.com/devtron-labs/devtron/api/helm-app"
"github.com/devtron-labs/devtron/api/infraConfig"
Expand Down Expand Up @@ -121,8 +122,6 @@ import (
deployment2 "github.com/devtron-labs/devtron/pkg/deployment"
git2 "github.com/devtron-labs/devtron/pkg/deployment/gitOps/git"
"github.com/devtron-labs/devtron/pkg/deploymentGroup"
"github.com/devtron-labs/devtron/pkg/devtronResource"
repository9 "github.com/devtron-labs/devtron/pkg/devtronResource/repository"
"github.com/devtron-labs/devtron/pkg/dockerRegistry"
"github.com/devtron-labs/devtron/pkg/eventProcessor"
"github.com/devtron-labs/devtron/pkg/generateManifest"
Expand Down Expand Up @@ -195,6 +194,9 @@ func InitializeApp() (*App, error) {

eventProcessor.EventProcessorWireSet,
workflow3.WorkflowWireSet,

devtronResource.DevtronResourceWireSet,

// -------wireset end ----------
// -------
gitSensor.GetConfig,
Expand Down Expand Up @@ -950,12 +952,6 @@ func InitializeApp() (*App, error) {
resourceQualifiers.NewQualifierMappingServiceImpl,
wire.Bind(new(resourceQualifiers.QualifierMappingService), new(*resourceQualifiers.QualifierMappingServiceImpl)),

repository9.NewDevtronResourceSearchableKeyRepositoryImpl,
wire.Bind(new(repository9.DevtronResourceSearchableKeyRepository), new(*repository9.DevtronResourceSearchableKeyRepositoryImpl)),

devtronResource.NewDevtronResourceSearchableKeyServiceImpl,
wire.Bind(new(devtronResource.DevtronResourceSearchableKeyService), new(*devtronResource.DevtronResourceSearchableKeyServiceImpl)),

argocdServer.NewArgoClientWrapperServiceImpl,
wire.Bind(new(argocdServer.ArgoClientWrapperService), new(*argocdServer.ArgoClientWrapperServiceImpl)),

Expand Down
125 changes: 125 additions & 0 deletions api/devtronResource/DevtronResourceHistoryRestHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package devtronResource

import (
"fmt"
apiBean "github.com/devtron-labs/devtron/api/devtronResource/bean"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/devtronResource"
"github.com/devtron-labs/devtron/pkg/devtronResource/adapter"
"github.com/devtron-labs/devtron/pkg/devtronResource/bean"
"github.com/devtron-labs/devtron/pkg/devtronResource/history/deployment/cdPipeline"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/schema"
"go.uber.org/zap"
"net/http"
)

type HistoryRestHandler interface {
GetDeploymentHistory(w http.ResponseWriter, r *http.Request)
GetDeploymentHistoryConfigList(w http.ResponseWriter, r *http.Request)
}

type HistoryRestHandlerImpl struct {
logger *zap.SugaredLogger
enforcer casbin.Enforcer
deploymentHistoryService cdPipeline.DeploymentHistoryService
apiReqDecoderService devtronResource.APIReqDecoderService
enforcerUtil rbac.EnforcerUtil
}

func NewHistoryRestHandlerImpl(logger *zap.SugaredLogger,
enforcer casbin.Enforcer,
deploymentHistoryService cdPipeline.DeploymentHistoryService,
apiReqDecoderService devtronResource.APIReqDecoderService,
enforcerUtil rbac.EnforcerUtil) *HistoryRestHandlerImpl {
return &HistoryRestHandlerImpl{
logger: logger,
enforcer: enforcer,
deploymentHistoryService: deploymentHistoryService,
apiReqDecoderService: apiReqDecoderService,
enforcerUtil: enforcerUtil,
}
}

func (handler *HistoryRestHandlerImpl) GetDeploymentHistory(w http.ResponseWriter, r *http.Request) {
kind, _, _, caughtError := getKindSubKindVersion(w, r)
if caughtError || kind != bean.DevtronResourceCdPipeline.ToString() {
common.WriteJsonResp(w, fmt.Errorf(apiBean.RequestInvalidKindVersionErrMessage), nil, http.StatusBadRequest)
return
}
v := r.URL.Query()
var decoder = schema.NewDecoder()
decoder.IgnoreUnknownKeys(true)
queryParams := apiBean.GetHistoryQueryParams{}
err := decoder.Decode(&queryParams, v)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
decodedReqBean, err := handler.apiReqDecoderService.GetFilterCriteriaParamsForDeploymentHistory(queryParams.FilterCriteria)
if err != nil {
handler.logger.Errorw("error in getting filter criteria params", "err", err, "filterCriteria", queryParams.FilterCriteria)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

// RBAC START
token := r.Header.Get("token")
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(decodedReqBean.AppId)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
return
}
// RBAC END

resp, err := handler.deploymentHistoryService.
GetCdPipelineDeploymentHistory(adapter.GetCDDeploymentHistoryListReq(&queryParams, decodedReqBean))
if err != nil {
handler.logger.Errorw("service error, GetCdPipelineDeploymentHistory", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, resp, http.StatusOK)
return
}

func (handler *HistoryRestHandlerImpl) GetDeploymentHistoryConfigList(w http.ResponseWriter, r *http.Request) {
kind, _, _, caughtError := getKindSubKindVersion(w, r)
if caughtError || kind != bean.DevtronResourceCdPipeline.ToString() {
common.WriteJsonResp(w, fmt.Errorf(apiBean.RequestInvalidKindVersionErrMessage), nil, http.StatusBadRequest)
return
}
v := r.URL.Query()
var decoder = schema.NewDecoder()
decoder.IgnoreUnknownKeys(true)
queryParams := apiBean.GetHistoryConfigQueryParams{}
err := decoder.Decode(&queryParams, v)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
decodedReqBean, err := handler.apiReqDecoderService.GetFilterCriteriaParamsForDeploymentHistory(queryParams.FilterCriteria)
if err != nil {
handler.logger.Errorw("error in getting filter criteria params", "err", err, "filterCriteria", queryParams.FilterCriteria)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
//RBAC START
token := r.Header.Get("token")
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(decodedReqBean.AppId)
if isValidated := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !isValidated {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
return
}
//RBAC END
resp, err := handler.deploymentHistoryService.
GetCdPipelineDeploymentHistoryConfigList(adapter.GetCDDeploymentHistoryConfigListReq(&queryParams, decodedReqBean))
if err != nil {
handler.logger.Errorw("service error, GetCdPipelineDeploymentHistoryConfigList", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, resp, http.StatusOK)
return
}
23 changes: 23 additions & 0 deletions api/devtronResource/DevtronResourceHistoryRouter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package devtronResource

import "github.com/gorilla/mux"

type HistoryRouter interface {
InitDtResourceHistoryRouter(devtronResourceRouter *mux.Router)
}

type HistoryRouterImpl struct {
dtResourceHistoryRestHandler HistoryRestHandler
}

func NewHistoryRouterImpl(dtResourceHistoryRestHandler HistoryRestHandler) *HistoryRouterImpl {
return &HistoryRouterImpl{dtResourceHistoryRestHandler: dtResourceHistoryRestHandler}
}

func (router *HistoryRouterImpl) InitDtResourceHistoryRouter(historyRouter *mux.Router) {
historyRouter.Path("/deployment/config/{kind:[a-zA-Z0-9/-]+}/{version:[a-zA-Z0-9]+}").
HandlerFunc(router.dtResourceHistoryRestHandler.GetDeploymentHistoryConfigList).Methods("GET")

historyRouter.Path("/deployment/{kind:[a-zA-Z0-9/-]+}/{version:[a-zA-Z0-9]+}").
HandlerFunc(router.dtResourceHistoryRestHandler.GetDeploymentHistory).Methods("GET")
}
31 changes: 31 additions & 0 deletions api/devtronResource/DevtronResourceRestHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package devtronResource

import (
"fmt"
apiBean "github.com/devtron-labs/devtron/api/devtronResource/bean"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/devtronResource/helper"
"github.com/gorilla/mux"
"net/http"
)

func getKindSubKindVersion(w http.ResponseWriter, r *http.Request) (kind string, subKind string, version string, caughtError bool) {
vars := mux.Vars(r)
kindVar := vars[apiBean.PathParamKind]
versionVar := vars[apiBean.PathParamVersion]
kind, subKind, statusCode, err := resolveKindSubKindValues(kindVar)
if err != nil {
common.WriteJsonResp(w, err, nil, statusCode)
caughtError = true
}
return kind, subKind, versionVar, caughtError
}

func resolveKindSubKindValues(kindVar string) (kind, subKind string, statusCode int, err error) {
kind, subKind, err = helper.GetKindAndSubKindFrom(kindVar)
if err != nil {
err = fmt.Errorf("invalid parameter: kind")
statusCode = http.StatusBadRequest
}
return kind, subKind, statusCode, err
}
22 changes: 22 additions & 0 deletions api/devtronResource/DevtronResourceRouter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package devtronResource

import "github.com/gorilla/mux"

type DevtronResourceRouter interface {
InitDevtronResourceRouter(devtronResourceRouter *mux.Router)
}

type DevtronResourceRouterImpl struct {
historyRouter HistoryRouter
}

func NewDevtronResourceRouterImpl(historyRouter HistoryRouter) *DevtronResourceRouterImpl {
return &DevtronResourceRouterImpl{
historyRouter: historyRouter,
}
}

func (router *DevtronResourceRouterImpl) InitDevtronResourceRouter(devtronResourceRouter *mux.Router) {
historyRouter := devtronResourceRouter.PathPrefix("/history").Subrouter()
router.historyRouter.InitDtResourceHistoryRouter(historyRouter)
}
20 changes: 20 additions & 0 deletions api/devtronResource/bean/bean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bean

type GetHistoryQueryParams struct {
FilterCriteria []string `schema:"filterCriteria"`
OffSet int `schema:"offSet"`
Limit int `schema:"limit"`
}

type GetHistoryConfigQueryParams struct {
BaseConfigurationId int `schema:"baseConfigurationId"`
HistoryComponent string `schema:"historyComponent"`
HistoryComponentName string `schema:"historyComponentName"`
FilterCriteria []string `schema:"filterCriteria"`
}

const (
RequestInvalidKindVersionErrMessage = "Invalid kind and version! Implementation not supported."
PathParamKind = "kind"
PathParamVersion = "version"
)
30 changes: 30 additions & 0 deletions api/devtronResource/wire_devtronResource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package devtronResource

import (
"github.com/devtron-labs/devtron/pkg/devtronResource"
"github.com/devtron-labs/devtron/pkg/devtronResource/history/deployment/cdPipeline"
"github.com/devtron-labs/devtron/pkg/devtronResource/read"
"github.com/devtron-labs/devtron/pkg/devtronResource/repository"
"github.com/google/wire"
)

var DevtronResourceWireSet = wire.NewSet(
//old bindings, migrated from wire.go
read.NewDevtronResourceSearchableKeyServiceImpl,
wire.Bind(new(read.DevtronResourceSearchableKeyService), new(*read.DevtronResourceSearchableKeyServiceImpl)),
repository.NewDevtronResourceSearchableKeyRepositoryImpl,
wire.Bind(new(repository.DevtronResourceSearchableKeyRepository), new(*repository.DevtronResourceSearchableKeyRepositoryImpl)),

NewDevtronResourceRouterImpl,
wire.Bind(new(DevtronResourceRouter), new(*DevtronResourceRouterImpl)),

NewHistoryRouterImpl,
wire.Bind(new(HistoryRouter), new(*HistoryRouterImpl)),
NewHistoryRestHandlerImpl,
wire.Bind(new(HistoryRestHandler), new(*HistoryRestHandlerImpl)),
cdPipeline.NewDeploymentHistoryServiceImpl,
wire.Bind(new(cdPipeline.DeploymentHistoryService), new(*cdPipeline.DeploymentHistoryServiceImpl)),

devtronResource.NewAPIReqDecoderServiceImpl,
wire.Bind(new(devtronResource.APIReqDecoderService), new(*devtronResource.APIReqDecoderServiceImpl)),
)
9 changes: 8 additions & 1 deletion api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/devtron-labs/devtron/api/cluster"
"github.com/devtron-labs/devtron/api/dashboardEvent"
"github.com/devtron-labs/devtron/api/deployment"
"github.com/devtron-labs/devtron/api/devtronResource"
"github.com/devtron-labs/devtron/api/externalLink"
client "github.com/devtron-labs/devtron/api/helm-app"
"github.com/devtron-labs/devtron/api/infraConfig"
Expand Down Expand Up @@ -114,6 +115,7 @@ type MuxRouter struct {
ciTriggerCron cron.CiTriggerCron
infraConfigRouter infraConfig.InfraConfigRouter
argoApplicationRouter argoApplication.ArgoApplicationRouter
devtronResourceRouter devtronResource.DevtronResourceRouter
}

func NewMuxRouter(logger *zap.SugaredLogger,
Expand Down Expand Up @@ -143,7 +145,8 @@ func NewMuxRouter(logger *zap.SugaredLogger,
ciTriggerCron cron.CiTriggerCron,
proxyRouter proxy.ProxyRouter,
infraConfigRouter infraConfig.InfraConfigRouter,
argoApplicationRouter argoApplication.ArgoApplicationRouter) *MuxRouter {
argoApplicationRouter argoApplication.ArgoApplicationRouter,
devtronResourceRouter devtronResource.DevtronResourceRouter) *MuxRouter {
r := &MuxRouter{
Router: mux.NewRouter(),
EnvironmentClusterMappingsRouter: EnvironmentClusterMappingsRouter,
Expand Down Expand Up @@ -205,6 +208,7 @@ func NewMuxRouter(logger *zap.SugaredLogger,
ciTriggerCron: ciTriggerCron,
infraConfigRouter: infraConfigRouter,
argoApplicationRouter: argoApplicationRouter,
devtronResourceRouter: devtronResourceRouter,
}
return r
}
Expand Down Expand Up @@ -404,6 +408,9 @@ func (r MuxRouter) Init() {
rbacRoleRouter := r.Router.PathPrefix("/orchestrator/rbac/role").Subrouter()
r.rbacRoleRouter.InitRbacRoleRouter(rbacRoleRouter)

devtronResourceRouter := r.Router.PathPrefix("/orchestrator/resource").Subrouter()
r.devtronResourceRouter.InitDevtronResourceRouter(devtronResourceRouter)

infraConfigRouter := r.Router.PathPrefix("/orchestrator/infra-config").Subrouter()
r.infraConfigRouter.InitInfraConfigRouter(infraConfigRouter)

Expand Down
2 changes: 1 addition & 1 deletion cmd/external-app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/sql/repository/pipelineConfig/PipelineRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ 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)
FindActiveByAppIdAndEnvId(appId int, envId int) (*Pipeline, 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
Expand Down Expand Up @@ -527,6 +528,16 @@ func (impl PipelineRepositoryImpl) FindActiveByAppIdAndPipelineId(appId int, pip
return pipelines, err
}

func (impl PipelineRepositoryImpl) FindActiveByAppIdAndEnvId(appId int, envId int) (*Pipeline, error) {
var pipeline Pipeline
err := impl.dbConnection.Model(&pipeline).
Where("app_id = ?", appId).
Where("environment_id = ?", envId).
Where("deleted = ?", false).
Select()
return &pipeline, err
}

func (impl PipelineRepositoryImpl) SetDeploymentAppCreatedInPipeline(deploymentAppCreated bool, pipelineId int, userId int32) error {
query := "update pipeline set deployment_app_created=?, updated_on=?, updated_by=? where id=?;"
var pipeline *Pipeline
Expand Down
Loading

0 comments on commit b006b34

Please sign in to comment.