Skip to content

Commit 16ca05d

Browse files
committed
Merge branch 'main' into v2-up-orch
2 parents e19b8d6 + a32692d commit 16ca05d

File tree

9 files changed

+147
-657
lines changed

9 files changed

+147
-657
lines changed

api/restHandler/AppListingRestHandler.go

Lines changed: 35 additions & 409 deletions
Large diffs are not rendered by default.

api/restHandler/app/BuildPipelineRestHandler.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/devtron-labs/devtron/pkg/pipeline/types"
2626
resourceGroup "github.com/devtron-labs/devtron/pkg/resourceGroup"
2727
util2 "github.com/devtron-labs/devtron/util"
28+
"github.com/devtron-labs/devtron/util/response"
2829
"github.com/go-pg/pg"
2930
"github.com/gorilla/mux"
3031
"go.opentelemetry.io/otel"
@@ -51,6 +52,7 @@ type DevtronAppBuildRestHandler interface {
5152
TriggerCiPipeline(w http.ResponseWriter, r *http.Request)
5253
GetCiPipelineMin(w http.ResponseWriter, r *http.Request)
5354
GetCIPipelineById(w http.ResponseWriter, r *http.Request)
55+
GetCIPipelineByPipelineId(w http.ResponseWriter, r *http.Request)
5456
HandleWorkflowWebhook(w http.ResponseWriter, r *http.Request)
5557
GetBuildLogs(w http.ResponseWriter, r *http.Request)
5658
FetchWorkflowDetails(w http.ResponseWriter, r *http.Request)
@@ -1170,6 +1172,69 @@ func (handler PipelineConfigRestHandlerImpl) GetCIPipelineById(w http.ResponseWr
11701172
common.WriteJsonResp(w, err, ciPipeline, http.StatusOK)
11711173
}
11721174

1175+
func (handler PipelineConfigRestHandlerImpl) GetCIPipelineByPipelineId(w http.ResponseWriter, r *http.Request) {
1176+
token := r.Header.Get("token")
1177+
var ciPipelineId int
1178+
var err error
1179+
v := r.URL.Query()
1180+
pipelineId := v.Get("pipelineId")
1181+
if len(pipelineId) != 0 {
1182+
ciPipelineId, err = strconv.Atoi(pipelineId)
1183+
if err != nil {
1184+
handler.Logger.Errorw("request err, GetCIPipelineByPipelineId", "err", err, "pipelineIdParam", pipelineId)
1185+
response.WriteResponse(http.StatusBadRequest, "please send valid pipelineId", w, errors.New("pipelineId id invalid"))
1186+
return
1187+
}
1188+
} else {
1189+
response.WriteResponse(http.StatusBadRequest, "please send valid pipelineId", w, errors.New("pipelineId id invalid"))
1190+
return
1191+
}
1192+
1193+
handler.Logger.Infow("request payload, GetCIPipelineByPipelineId", "pipelineId", pipelineId)
1194+
1195+
ciPipeline, err := handler.pipelineBuilder.GetCiPipelineById(ciPipelineId)
1196+
if err != nil {
1197+
handler.Logger.Infow("service error, GetCIPipelineById", "err", err, "pipelineId", pipelineId)
1198+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
1199+
return
1200+
}
1201+
1202+
app, err := handler.pipelineBuilder.GetApp(ciPipeline.AppId)
1203+
if err != nil {
1204+
handler.Logger.Infow("service error, GetCIPipelineByPipelineId", "err", err, "appId", ciPipeline.AppId, "pipelineId", pipelineId)
1205+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
1206+
return
1207+
}
1208+
ciPipeline.AppName = app.AppName
1209+
ciPipeline.AppType = app.AppType
1210+
1211+
resourceName := handler.enforcerUtil.GetAppRBACNameByAppId(app.Id)
1212+
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, resourceName); !ok {
1213+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
1214+
return
1215+
}
1216+
1217+
pipelineData, err := handler.pipelineRepository.FindActiveByAppIdAndPipelineId(ciPipeline.AppId, ciPipelineId)
1218+
if err != nil {
1219+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
1220+
return
1221+
}
1222+
var environmentIds []int
1223+
for _, pipeline := range pipelineData {
1224+
environmentIds = append(environmentIds, pipeline.EnvironmentId)
1225+
}
1226+
if handler.appWorkflowService.CheckCdPipelineByCiPipelineId(ciPipelineId) {
1227+
for _, envId := range environmentIds {
1228+
envObject := handler.enforcerUtil.GetEnvRBACNameByCiPipelineIdAndEnvId(ciPipelineId, envId)
1229+
if ok := handler.enforcer.Enforce(token, casbin.ResourceEnvironment, casbin.ActionUpdate, envObject); !ok {
1230+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
1231+
return
1232+
}
1233+
}
1234+
}
1235+
common.WriteJsonResp(w, err, ciPipeline, http.StatusOK)
1236+
}
1237+
11731238
func (handler PipelineConfigRestHandlerImpl) CreateMaterial(w http.ResponseWriter, r *http.Request) {
11741239
token := r.Header.Get("token")
11751240
decoder := json.NewDecoder(r.Body)

api/router/AppListingRouter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func (router AppListingRouterImpl) initAppListingRouter(appListingRouter *mux.Ro
4545
HandlerFunc(router.appListingRestHandler.GetHostUrlsByBatch).Methods("GET")
4646

4747
appListingRouter.Path("/list").
48-
HandlerFunc(router.appListingRestHandler.FetchAppsByEnvironment).
48+
HandlerFunc(router.appListingRestHandler.FetchAppsByEnvironmentV2).
4949
Methods("POST")
5050

51-
appListingRouter.Path("/list/{version}").
52-
HandlerFunc(router.appListingRestHandler.FetchAppsByEnvironmentVersioned).
51+
appListingRouter.Path("/list/v2").
52+
HandlerFunc(router.appListingRestHandler.FetchAppsByEnvironmentV2).
5353
Methods("POST")
5454

5555
appListingRouter.Path("/list/group/{env-id}").
@@ -60,7 +60,7 @@ func (router AppListingRouterImpl) initAppListingRouter(appListingRouter *mux.Ro
6060
Queries("size", "{size}", "offset", "{offset}").
6161
HandlerFunc(router.appListingRestHandler.FetchOverviewAppsByEnvironment).
6262
Methods("GET")
63-
//This API used for fetch app details, not deployment details
63+
// This API used for fetch app details, not deployment details
6464
appListingRouter.Path("/detail").Queries("app-id", "{app-id}").Queries("env-id", "{env-id}").
6565
HandlerFunc(router.appListingRestHandler.FetchAppDetails).
6666
Methods("GET")

api/router/PipelineConfigRouter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (router PipelineConfigRouterImpl) initPipelineConfigRouter(configRouter *mu
8181
configRouter.Path("/env").HandlerFunc(router.restHandler.EnvConfigOverrideUpdate).Methods("PUT")
8282
configRouter.Path("/env/{appId}/{environmentId}/{chartRefId}").HandlerFunc(router.restHandler.GetEnvConfigOverride).Methods("GET")
8383

84+
configRouter.Path("/ci-pipeline").HandlerFunc(router.restHandler.GetCIPipelineByPipelineId).Methods("GET")
8485
configRouter.Path("/ci-pipeline").HandlerFunc(router.restHandler.CreateCiConfig).Methods("POST")
8586
configRouter.Path("/ci-pipeline/{appId}").HandlerFunc(router.restHandler.GetCiPipeline).Methods("GET")
8687
configRouter.Path("/external-ci/{appId}").HandlerFunc(router.restHandler.GetExternalCi).Methods("GET")

client/argocdServer/application/Application.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
641641
}
642642
}
643643
}
644-
if node.Kind == "Pod" {
644+
if node.Kind == "Pod" && node.NetworkingInfo != nil && node.NetworkingInfo.Labels != nil {
645645
relevantCR[prefix+"-"+node.NetworkingInfo.Labels["controller-revision-hash"]] = true
646646
}
647647
}

internal/sql/repository/AppListingRepository.go

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/devtron-labs/devtron/internal/middleware"
2828
repository2 "github.com/devtron-labs/devtron/pkg/cluster/repository"
2929
"go.opentelemetry.io/otel"
30-
"strconv"
3130
"strings"
3231
"time"
3332

@@ -38,7 +37,6 @@ import (
3837
)
3938

4039
type AppListingRepository interface {
41-
FetchAppsByEnvironment(appListingFilter helper.AppListingFilter) ([]*bean.AppEnvironmentContainer, error)
4240
FetchJobs(appIds []int, statuses []string, environmentIds []int, sortOrder string) ([]*bean.JobListingContainer, error)
4341
FetchOverviewCiPipelines(jobId int) ([]*bean.JobListingContainer, error)
4442
FetchJobsLastSucceededOn(ciPipelineIDs []int) ([]*bean.CiPipelineLastSucceededTime, error)
@@ -47,7 +45,7 @@ type AppListingRepository interface {
4745
FetchAppTriggerView(appId int) ([]bean.TriggerView, error)
4846
FetchAppStageStatus(appId int, appType int) ([]bean.AppStageStatus, error)
4947

50-
//Not in used
48+
// Not in used
5149
PrometheusApiByEnvId(id int) (*string, error)
5250

5351
FetchOtherEnvironment(appId int) ([]*bean.Environment, error)
@@ -193,81 +191,6 @@ func getRequiredAppIdsInSequence(appIds []int) []int {
193191
return resIDs
194192
}
195193

196-
func (impl AppListingRepositoryImpl) FetchAppsByEnvironment(appListingFilter helper.AppListingFilter) ([]*bean.AppEnvironmentContainer, error) {
197-
impl.Logger.Debug("reached at FetchAppsByEnvironment:")
198-
var appEnvArr []*bean.AppEnvironmentContainer
199-
200-
query := impl.appListingRepositoryQueryBuilder.BuildAppListingQueryLastDeploymentTime()
201-
impl.Logger.Debugw("basic app detail query: ", query)
202-
var lastDeployedTimeDTO []*bean.AppEnvironmentContainer
203-
lastDeployedTimeMap := map[int]*bean.AppEnvironmentContainer{}
204-
start := time.Now()
205-
_, err := impl.dbConnection.Query(&lastDeployedTimeDTO, query)
206-
middleware.AppListingDuration.WithLabelValues("buildAppListingQueryLastDeploymentTime", "devtron").Observe(time.Since(start).Seconds())
207-
if err != nil {
208-
impl.Logger.Error(err)
209-
return appEnvArr, err
210-
}
211-
for _, item := range lastDeployedTimeDTO {
212-
if _, ok := lastDeployedTimeMap[item.PipelineId]; ok {
213-
continue
214-
}
215-
lastDeployedTimeMap[item.PipelineId] = &bean.AppEnvironmentContainer{
216-
LastDeployedTime: item.LastDeployedTime,
217-
DataSource: item.DataSource,
218-
MaterialInfoJson: item.MaterialInfoJson,
219-
CiArtifactId: item.CiArtifactId,
220-
}
221-
}
222-
223-
var appEnvContainer []*bean.AppEnvironmentContainer
224-
appsEnvquery := impl.appListingRepositoryQueryBuilder.BuildAppListingQuery(appListingFilter)
225-
impl.Logger.Debugw("basic app detail query: ", appsEnvquery)
226-
start = time.Now()
227-
_, appsErr := impl.dbConnection.Query(&appEnvContainer, appsEnvquery)
228-
middleware.AppListingDuration.WithLabelValues("buildAppListingQuery", "devtron").Observe(time.Since(start).Seconds())
229-
if appsErr != nil {
230-
impl.Logger.Error(appsErr)
231-
return appEnvContainer, appsErr
232-
}
233-
latestDeploymentStatusMap := map[string]*bean.AppEnvironmentContainer{}
234-
for _, item := range appEnvContainer {
235-
if item.EnvironmentId > 0 && item.PipelineId > 0 && item.Active == false {
236-
// skip adding apps which have linked with cd pipeline and that environment has marked as deleted.
237-
continue
238-
}
239-
// include only apps which are not linked with any cd pipeline + those linked with cd pipeline and env has active.
240-
key := strconv.Itoa(item.AppId) + "_" + strconv.Itoa(item.EnvironmentId)
241-
if _, ok := latestDeploymentStatusMap[key]; ok {
242-
continue
243-
}
244-
245-
if lastDeployedTime, ok := lastDeployedTimeMap[item.PipelineId]; ok {
246-
item.LastDeployedTime = lastDeployedTime.LastDeployedTime
247-
item.DataSource = lastDeployedTime.DataSource
248-
item.MaterialInfoJson = lastDeployedTime.MaterialInfoJson
249-
item.CiArtifactId = lastDeployedTime.CiArtifactId
250-
}
251-
252-
if len(item.DataSource) > 0 {
253-
mInfo, err := parseMaterialInfo(item.MaterialInfoJson, item.DataSource)
254-
if err == nil && len(mInfo) > 0 {
255-
item.MaterialInfo = mInfo
256-
} else {
257-
item.MaterialInfo = []byte("[]")
258-
}
259-
item.MaterialInfoJson = ""
260-
} else {
261-
item.MaterialInfo = []byte("[]")
262-
item.MaterialInfoJson = ""
263-
}
264-
appEnvArr = append(appEnvArr, item)
265-
latestDeploymentStatusMap[key] = item
266-
}
267-
268-
return appEnvArr, nil
269-
}
270-
271194
func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter helper.AppListingFilter) ([]*bean.AppEnvironmentContainer, int, error) {
272195
impl.Logger.Debugw("reached at FetchAppsByEnvironment ", "appListingFilter", appListingFilter)
273196
var appEnvArr []*bean.AppEnvironmentContainer
@@ -306,7 +229,7 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h
306229

307230
} else {
308231

309-
//to get all the appIds in appEnvs allowed for user and filtered by the appListing filter and sorted by name
232+
// to get all the appIds in appEnvs allowed for user and filtered by the appListing filter and sorted by name
310233
appIdCountDtos := make([]*bean.AppEnvironmentContainer, 0)
311234
appIdCountQuery := impl.appListingRepositoryQueryBuilder.GetAppIdsQueryWithPaginationForAppNameSearch(appListingFilter)
312235
impl.Logger.Debug("GetAppIdsQueryWithPaginationForAppNameSearch query ", appIdCountQuery)
@@ -327,7 +250,7 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h
327250
uniqueAppIds[i] = obj.AppId
328251
}
329252
appListingFilter.AppIds = uniqueAppIds
330-
//set appids required for this page in the filter and get the appEnv containers of these apps
253+
// set appids required for this page in the filter and get the appEnv containers of these apps
331254
appListingFilter.AppIds = uniqueAppIds
332255
appsEnvquery := impl.appListingRepositoryQueryBuilder.GetQueryForAppEnvContainerss(appListingFilter)
333256
impl.Logger.Debug("GetQueryForAppEnvContainerss query: ", appsEnvquery)
@@ -341,8 +264,8 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h
341264

342265
}
343266

344-
//filter out unique pipelineIds from the above result and get the deployment times for them
345-
//some items don't have pipelineId if no pipeline is configured for the app in the appEnv container
267+
// filter out unique pipelineIds from the above result and get the deployment times for them
268+
// some items don't have pipelineId if no pipeline is configured for the app in the appEnv container
346269
pipelineIdsSet := make(map[int]bool)
347270
pipelineIds := make([]int, 0)
348271
for _, item := range appEnvContainer {
@@ -353,7 +276,7 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h
353276
}
354277
}
355278

356-
//if any pipeline found get the latest deployment time
279+
// if any pipeline found get the latest deployment time
357280
if len(pipelineIds) > 0 {
358281
query := impl.appListingRepositoryQueryBuilder.BuildAppListingQueryLastDeploymentTimeV2(pipelineIds)
359282
impl.Logger.Debugw("basic app detail query: ", query)
@@ -366,7 +289,7 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h
366289
}
367290
}
368291

369-
//get the last deployment time for all the items
292+
// get the last deployment time for all the items
370293
for _, item := range lastDeployedTimeDTO {
371294
if _, ok := lastDeployedTimeMap[item.PipelineId]; ok {
372295
continue
@@ -375,7 +298,7 @@ func (impl AppListingRepositoryImpl) FetchAppsByEnvironmentV2(appListingFilter h
375298

376299
}
377300

378-
//set the time for corresponding appEnv container
301+
// set the time for corresponding appEnv container
379302
for _, item := range appEnvContainer {
380303
if lastDeployedTime, ok := lastDeployedTimeMap[item.PipelineId]; ok {
381304
item.LastDeployedTime = lastDeployedTime
@@ -483,7 +406,7 @@ func (impl AppListingRepositoryImpl) FetchAppDetail(ctx context.Context, appId i
483406
var appDetailContainer bean.AppDetailContainer
484407
newCtx, span := otel.Tracer("orchestrator").Start(ctx, "DeploymentDetailsByAppIdAndEnvId")
485408
defer span.End()
486-
//Fetch deployment detail of cd pipeline latest triggered within env of any App.
409+
// Fetch deployment detail of cd pipeline latest triggered within env of any App.
487410
deploymentDetail, err := impl.deploymentDetailsByAppIdAndEnvId(newCtx, appId, envId)
488411
if err != nil {
489412
impl.Logger.Warn("unable to fetch deployment detail for app")
@@ -511,7 +434,7 @@ func (impl AppListingRepositoryImpl) PrometheusApiByEnvId(id int) (*string, erro
511434
query := "SELECT env.prometheus_endpoint from environment env" +
512435
" WHERE env.id = ? AND env.active = TRUE"
513436
impl.Logger.Debugw("query", query)
514-
//environments := []string{"QA"}
437+
// environments := []string{"QA"}
515438
_, err := impl.dbConnection.Query(&prometheusEndpoint, query, id)
516439
if err != nil {
517440
impl.Logger.Error("Exception caught:", err)

internal/sql/repository/helper/AppListingRepositoryQueryBuilder.go

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type AppListingFilter struct {
5454
Offset int `json:"offset"`
5555
Size int `json:"size"`
5656
DeploymentGroupId int `json:"deploymentGroupId"`
57-
AppIds []int `json:"-"` //internal use only
57+
AppIds []int `json:"-"` // internal use only
5858
}
5959

6060
type SortBy string
@@ -128,17 +128,6 @@ func getAppListingCommonQueryString() string {
128128
" LEFT JOIN app_status aps on aps.app_id = a.id and p.environment_id = aps.env_id "
129129
}
130130

131-
func (impl AppListingRepositoryQueryBuilder) BuildAppListingQueryForAppIds(appListingFilter AppListingFilter) string {
132-
whereCondition := impl.buildAppListingWhereCondition(appListingFilter)
133-
orderByClause := impl.buildAppListingSortBy(appListingFilter)
134-
query := "SELECT a.id as app_id " + getAppListingCommonQueryString()
135-
if appListingFilter.DeploymentGroupId != 0 {
136-
query = query + " INNER JOIN deployment_group_app dga ON a.id = dga.app_id "
137-
}
138-
query = query + whereCondition + orderByClause
139-
return query
140-
}
141-
142131
func (impl AppListingRepositoryQueryBuilder) BuildAppListingQuery(appListingFilter AppListingFilter) string {
143132
whereCondition := impl.buildAppListingWhereCondition(appListingFilter)
144133
orderByClause := impl.buildAppListingSortBy(appListingFilter)
@@ -152,15 +141,6 @@ func (impl AppListingRepositoryQueryBuilder) BuildAppListingQuery(appListingFilt
152141
return query
153142
}
154143

155-
func (impl AppListingRepositoryQueryBuilder) BuildAppListingQueryLastDeploymentTime() string {
156-
query := "select DISTINCT ON( pco.pipeline_id) pco.pipeline_id, pco.pipeline_release_counter, pco.created_on as last_deployed_time," +
157-
" cia.data_source, cia.material_info as material_info_json, cia.id as ci_artifact_id" +
158-
" from pipeline_config_override pco" +
159-
" inner join ci_artifact cia on cia.id=pco.ci_artifact_id" +
160-
" order by pco.pipeline_id,pco.pipeline_release_counter desc;"
161-
return query
162-
}
163-
164144
func (impl AppListingRepositoryQueryBuilder) GetQueryForAppEnvContainerss(appListingFilter AppListingFilter) string {
165145

166146
query := "SELECT p.environment_id , a.id AS app_id, a.app_name,p.id as pipeline_id, a.team_id ,aps.status as app_status "
@@ -242,31 +222,6 @@ func (impl AppListingRepositoryQueryBuilder) buildAppListingSortBy(appListingFil
242222
return orderByCondition
243223
}
244224

245-
func (impl AppListingRepositoryQueryBuilder) buildJobListingSortBy(appListingFilter AppListingFilter) string {
246-
orderByCondition := " ORDER BY a.name"
247-
return orderByCondition
248-
}
249-
250-
func (impl AppListingRepositoryQueryBuilder) buildJobListingWhereCondition(jobListingFilter AppListingFilter) string {
251-
whereCondition := "WHERE a.active = true and a.app_type = 2 "
252-
253-
if len(jobListingFilter.Teams) > 0 {
254-
teamIds := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(jobListingFilter.Teams)), ","), "[]")
255-
whereCondition = whereCondition + "and a.team_id IN (" + teamIds + ") "
256-
}
257-
258-
if jobListingFilter.AppNameSearch != "" {
259-
likeClause := "'%" + jobListingFilter.AppNameSearch + "%'"
260-
whereCondition = whereCondition + "and a.display_name like " + likeClause + " "
261-
}
262-
// add job stats filter here
263-
if len(jobListingFilter.AppStatuses) > 0 {
264-
appStatuses := util.ProcessAppStatuses(jobListingFilter.AppStatuses)
265-
whereCondition = whereCondition + "and aps.status IN (" + appStatuses + ") "
266-
}
267-
return whereCondition
268-
}
269-
270225
func (impl AppListingRepositoryQueryBuilder) buildAppListingWhereCondition(appListingFilter AppListingFilter) string {
271226
whereCondition := "WHERE a.active = true and a.app_type = 0 "
272227
if len(appListingFilter.Environments) > 0 {
@@ -287,7 +242,7 @@ func (impl AppListingRepositoryQueryBuilder) buildAppListingWhereCondition(appLi
287242
if appListingFilter.DeploymentGroupId > 0 {
288243
whereCondition = whereCondition + "and dga.deployment_group_id = " + strconv.Itoa(appListingFilter.DeploymentGroupId) + " "
289244
}
290-
//add app-status filter here
245+
// add app-status filter here
291246
var appStatusExcludingNotDeployed []string
292247
var isNotDeployedFilterApplied bool
293248
if len(appListingFilter.AppStatuses) > 0 {

0 commit comments

Comments
 (0)