Skip to content
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

fix: prevented chart updation when its in use #3755

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion api/chartRepo/ChartRepositoryRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,17 @@ func (handler *ChartRepositoryRestHandlerImpl) UpdateChartRepo(w http.ResponseWr
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

activeDeployment, err := handler.chartRepositoryService.CheckDeploymentCount(request)
if err != nil {
if activeDeployment > 0 {
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
err = &util.ApiError{Code: "400", HttpStatusCode: 400, UserMessage: "cannot update, found charts deployed using this repo", InternalMessage: err.Error()}
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
handler.Logger.Errorw("error updating, UpdateChartRepo", "err", err, "payload", request)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
token := r.Header.Get("token")
if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionUpdate, "*"); !ok {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
Expand Down
13 changes: 13 additions & 0 deletions pkg/chartRepo/ChartRepositoryService.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type ChartRepositoryService interface {
GetChartRepoByName(name string) (*ChartRepoDto, error)
GetChartRepoList() ([]*ChartRepoWithIsEditableDto, error)
GetChartRepoListMin() ([]*ChartRepoDto, error)
CheckDeploymentCount(request *ChartRepoDto) (int, error)
ValidateChartRepo(request *ChartRepoDto) *DetailedErrorHelmRepoValidation
ValidateAndCreateChartRepo(request *ChartRepoDto) (*chartRepoRepository.ChartRepo, error, *DetailedErrorHelmRepoValidation)
ValidateAndUpdateChartRepo(request *ChartRepoDto) (*chartRepoRepository.ChartRepo, error, *DetailedErrorHelmRepoValidation)
Expand Down Expand Up @@ -211,6 +212,18 @@ func (impl *ChartRepositoryServiceImpl) CreateChartRepo(request *ChartRepoDto) (
return chartRepo, nil
}

func (impl *ChartRepositoryServiceImpl) CheckDeploymentCount(request *ChartRepoDto) (int, error) {
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
activeDeploymentCount, err := impl.repoRepository.FindDeploymentCountByChartRepoId(request.Id)
if err != nil {
impl.logger.Errorw("error in getting deployment count, CheckDeploymentCount", "err", err, "payload", request)
return activeDeploymentCount, err
}
if activeDeploymentCount > 0 {
return activeDeploymentCount, errors.New("chart already in use")
}
return activeDeploymentCount, err
}

func (impl *ChartRepositoryServiceImpl) UpdateData(request *ChartRepoDto) (*chartRepoRepository.ChartRepo, error) {
dbConnection := impl.repoRepository.GetConnection()
tx, err := dbConnection.Begin()
Expand Down
18 changes: 18 additions & 0 deletions pkg/chartRepo/repository/ChartRepoRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"strconv"
)

type ChartRepoFields struct {
Expand Down Expand Up @@ -56,6 +57,7 @@ type ChartRepoRepository interface {
FindById(id int) (*ChartRepo, error)
FindAll() ([]*ChartRepo, error)
FindAllWithDeploymentCount() ([]*ChartRepoWithDeploymentCount, error)
FindDeploymentCountByChartRepoId(chartId int) (int, error)
GetConnection() *pg.DB
MarkChartRepoDeleted(chartRepo *ChartRepo, tx *pg.Tx) error
FindByName(name string) (*ChartRepo, error)
Expand Down Expand Up @@ -123,6 +125,22 @@ func (impl ChartRepoRepositoryImpl) FindAllWithDeploymentCount() ([]*ChartRepoWi
return repo, err
}

func (impl ChartRepoRepositoryImpl) FindDeploymentCountByChartRepoId(chartId int) (int, error) {
var activeDeploymentCount int
query := "select count(jq.ia_id ) as deployment_count" +
" from chart_repo left join" +
" (select aps.chart_repo_id as cr_id ,ia.id as ia_id from installed_app_versions iav" +
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
" inner join installed_apps ia on iav.installed_app_id = ia.id" +
" inner join app_store_application_version asav on iav.app_store_application_version_id = asav.id" +
" inner join app_store aps on asav.app_store_id = aps.id" +
" where ia.active=true and iav.active=true) jq" +
" on jq.cr_id = chart_repo.id" +
" where chart_repo.deleted = false and chart_repo.id = " + strconv.Itoa(chartId) +
" Group by chart_repo.id;"
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
_, err := impl.dbConnection.Query(&activeDeploymentCount, query)
return activeDeploymentCount, err
}

func (impl ChartRepoRepositoryImpl) MarkChartRepoDeleted(chartRepo *ChartRepo, tx *pg.Tx) error {
chartRepo.Deleted = true
return tx.Update(chartRepo)
Expand Down
Loading