Skip to content

Commit

Permalink
Merge pull request #316 from kthcloud/fix-update-when-max-replicas
Browse files Browse the repository at this point in the history
fix bug where deployments could not update if current replicas=max re…
  • Loading branch information
saffronjam authored Dec 11, 2023
2 parents b14820c + 4c714e5 commit f2013d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion routers/api/v1/v1_deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func Update(c *gin.Context) {
}
}

err = deployment_service.CheckQuotaUpdate(&requestBody, auth.UserID, &auth.GetEffectiveRole().Quotas, auth)
err = deployment_service.CheckQuotaUpdate(requestURI.DeploymentID, &requestBody, auth.UserID, &auth.GetEffectiveRole().Quotas, auth)
if err != nil {
var quotaExceededErr service.QuotaExceededError
if errors.As(err, &quotaExceededErr) {
Expand Down
19 changes: 13 additions & 6 deletions service/deployment_service/deployment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func CheckQuotaCreate(requestBody *body.DeploymentCreate, userID string, quota *
return nil
}

func CheckQuotaUpdate(requestBody *body.DeploymentUpdate, userID string, quota *roleModel.Quotas, auth *service.AuthInfo) error {
func CheckQuotaUpdate(id string, requestBody *body.DeploymentUpdate, userID string, quota *roleModel.Quotas, auth *service.AuthInfo) error {
makeError := func(err error) error {
return fmt.Errorf("failed to check quota. details: %w", err)
}
Expand All @@ -708,15 +708,22 @@ func CheckQuotaUpdate(requestBody *body.DeploymentUpdate, userID string, quota *
return makeError(err)
}

add := 1
deployment, err := deploymentModel.New().GetByID(id)
if err != nil {
return makeError(err)
}

totalBefore := usage.Count

add := 0
if requestBody.Replicas != nil {
add = *requestBody.Replicas
add = *requestBody.Replicas - deployment.GetMainApp().Replicas
}

totalCount := usage.Count + add
totalAfter := totalBefore + add

if totalCount > quota.Deployments {
return service.NewQuotaExceededError(fmt.Sprintf("Deployment quota exceeded. Current: %d, Quota: %d", totalCount, quota.Deployments))
if totalAfter > quota.Deployments {
return service.NewQuotaExceededError(fmt.Sprintf("Deployment quota exceeded. Current: %d, Quota: %d", totalAfter, quota.Deployments))
}

return nil
Expand Down

0 comments on commit f2013d0

Please sign in to comment.