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 all commits
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
7 changes: 6 additions & 1 deletion api/chartRepo/ChartRepositoryRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,12 @@ func (handler *ChartRepositoryRestHandlerImpl) UpdateChartRepo(w http.ResponseWr
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

err = handler.chartRepositoryService.ValidateDeploymentCount(request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can put this validation after RBAC

if err != nil {
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
14 changes: 14 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)
ValidateDeploymentCount(request *ChartRepoDto) 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,19 @@ func (impl *ChartRepositoryServiceImpl) CreateChartRepo(request *ChartRepoDto) (
return chartRepo, nil
}

func (impl *ChartRepositoryServiceImpl) ValidateDeploymentCount(request *ChartRepoDto) error {
activeDeploymentCount, err := impl.repoRepository.FindDeploymentCountByChartRepoId(request.Id)
if err != nil {
impl.logger.Errorw("error in getting deployment count, CheckDeploymentCount", "err", err, "payload", request)
return err
}
if activeDeploymentCount > 0 {
err = &util.ApiError{Code: "400", HttpStatusCode: 400, UserMessage: "cannot update, found charts deployed using this repo"}
return err
}
return err
}

func (impl *ChartRepositoryServiceImpl) UpdateData(request *ChartRepoDto) (*chartRepoRepository.ChartRepo, error) {
dbConnection := impl.repoRepository.GetConnection()
tx, err := dbConnection.Begin()
Expand Down
14 changes: 14 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,18 @@ func (impl ChartRepoRepositoryImpl) FindAllWithDeploymentCount() ([]*ChartRepoWi
return repo, err
}

func (impl ChartRepoRepositoryImpl) FindDeploymentCountByChartRepoId(chartId int) (int, error) {
var activeDeploymentCount int
query := "select count(aps.chart_repo_id) as deployment_count " +
" from installed_app_versions iav" +
" 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=? and iav.active=? and aps.chart_repo_id=?;"
_, err := impl.dbConnection.Query(&activeDeploymentCount, query, true, true, strconv.Itoa(chartId))
return activeDeploymentCount, err
}

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