Skip to content

Commit f187e01

Browse files
vishal-dtkripanshdevtronkartik-579
authored
demo feat deployment app type change api (#3006)
* add api to force delete all cd pipelines of all apps of an environment from argo cd * update deployment_app_type to helm and toggle deployment_app_created to false in db when changing cd app type and refactored * remove code smell * add check on app health status before deleting app from argocd * resolve build issues * Revert "resolve build issues" This reverts commit 647fab2. * revert and resolve build issues: missing argocd assets * add support for changing deployment type from helm to argo_cd * resolve sonar issue: decrease cognitive complexity * resolve sonar issue: decrease cognitive complexity * resolve sonar issue: decrease cognitive complexity * resolve sonar issue: decrease cognitive complexity * resolve sonar issue: decrease cognitive complexity * resolve sonar issue: decrease cognitive complexity * add api spec * add exclusion list in request body and refactored code * fixed failedPipelines empty list issue and handled case for missing status rows in db * fixed pg query issue * handled argocd app progressing app status * add exclude apps property in api spec * fix: helm deploy hibernate fix (#2983) * finished on time update fix * hibernate handling for helm apps * HPA support for Helm deploy * bulk API hibernate and unhibernate in helm deployment * bulk deploy for helm type + hibernate support for custom chart * scaling bug fixes * refactoring done * autoscaling handling for deployment as well * removed dead code * dead code removed * fix: added deployment app type support for app create api, separated cron time for helm app status update (#2995) * wip * updated spec * register app in acd when migrating from helm to argocd * updated PipelineRepository mock * add bulk cd pipeline async trigger --------- Co-authored-by: kripanshdevtron <[email protected]> Co-authored-by: kartik-579 <[email protected]>
1 parent 3a2abb1 commit f187e01

File tree

9 files changed

+121
-72
lines changed

9 files changed

+121
-72
lines changed

api/restHandler/AppListingRestHandler.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/devtron-labs/devtron/api/restHandler/common"
3030
"github.com/devtron-labs/devtron/client/argocdServer/application"
3131
"github.com/devtron-labs/devtron/client/cron"
32-
"github.com/devtron-labs/devtron/internal/constants"
3332
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
3433
"github.com/devtron-labs/devtron/internal/util"
3534
"github.com/devtron-labs/devtron/pkg/app"
@@ -777,13 +776,11 @@ func (handler AppListingRestHandlerImpl) fetchResourceTree(w http.ResponseWriter
777776
resp, err := handler.application.ResourceTree(ctx, query)
778777
elapsed := time.Since(start)
779778
if err != nil {
780-
handler.logger.Errorw("service err, FetchAppDetails, resource tree", "err", err, "app", appId, "env", envId)
781-
err = &util.ApiError{
782-
Code: constants.AppDetailResourceTreeNotFound,
783-
InternalMessage: "app detail fetched, failed to get resource tree from acd",
784-
UserMessage: "Error fetching detail, if you have recently created this deployment pipeline please try after sometime.",
785-
}
786-
common.WriteJsonResp(w, err, "", http.StatusInternalServerError)
779+
handler.logger.Errorw("service err, FetchAppDetails, resource tree",
780+
"err", err,
781+
"app", appId,
782+
"env", envId)
783+
787784
return appDetail
788785
}
789786
if resp.Status == string(health.HealthStatusHealthy) {

api/restHandler/app/DeploymentPipelineRestHandler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ func (handler PipelineConfigRestHandlerImpl) HandleChangeDeploymentRequest(w htt
299299
decoder := json.NewDecoder(r.Body)
300300
var deploymentAppTypeChangeRequest *bean.DeploymentAppTypeChangeRequest
301301
err = decoder.Decode(&deploymentAppTypeChangeRequest)
302+
deploymentAppTypeChangeRequest.UserId = userId
302303
if err != nil {
303304
handler.Logger.Errorw("request err, HandleChangeDeploymentRequest", "err", err, "payload",
304305
deploymentAppTypeChangeRequest)

internal/sql/repository/pipelineConfig/PipelineRepository.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ type PipelineRepository interface {
9292
FindAllPipelinesByChartsOverrideAndAppIdAndChartId(chartOverridden bool, appId int, chartId int) (pipelines []*Pipeline, err error)
9393
FindActiveByAppIdAndPipelineId(appId int, pipelineId int) ([]*Pipeline, error)
9494
UpdateCdPipeline(pipeline *Pipeline) error
95-
UpdateCdPipelineDeploymentAppInFilter(deploymentAppType string, cdPipelineIdIncludes []int) error
95+
UpdateCdPipelineDeploymentAppInFilter(deploymentAppType string, cdPipelineIdIncludes []int, userId int32) error
9696
FindNumberOfAppsWithCdPipeline(appIds []int) (count int, err error)
9797
GetAppAndEnvDetailsForDeploymentAppTypePipeline(deploymentAppType string, clusterIds []int) ([]*Pipeline, error)
9898
GetArgoPipelinesHavingTriggersStuckInLastPossibleNonTerminalTimelines(pendingSinceSeconds int, timeForDegradation int) ([]*Pipeline, error)
@@ -453,15 +453,17 @@ func (impl PipelineRepositoryImpl) UpdateCdPipeline(pipeline *Pipeline) error {
453453
// UpdateCdPipelineDeploymentAppInFilter takes in deployment app type and list of cd pipeline ids and
454454
// updates the deployment_app_type and sets deployment_app_created to false in the table for given ids.
455455
func (impl PipelineRepositoryImpl) UpdateCdPipelineDeploymentAppInFilter(deploymentAppType string,
456-
cdPipelineIdIncludes []int) error {
456+
cdPipelineIdIncludes []int, userId int32) error {
457457

458458
query := "update pipeline set " +
459459
"deployment_app_created = false, " +
460-
"deployment_app_type = '" + deploymentAppType + "' " +
460+
"deployment_app_type = '" + deploymentAppType + "', " +
461+
"updated_by = ?, " +
462+
"updated_on = ? " +
461463
"where id in (?)"
462464

463465
var pipeline *Pipeline
464-
_, err := impl.dbConnection.Query(pipeline, query, pg.In(cdPipelineIdIncludes))
466+
_, err := impl.dbConnection.Query(pipeline, query, userId, time.Now(), pg.In(cdPipelineIdIncludes))
465467

466468
return err
467469
}

internal/sql/repository/pipelineConfig/mocks/PipelineRepository.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/app/AppService.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ import (
2727
pubsub "github.com/devtron-labs/common-lib/pubsub-lib"
2828
client2 "github.com/devtron-labs/devtron/api/helm-app"
2929
application3 "github.com/devtron-labs/devtron/client/k8s/application"
30+
bean2 "github.com/devtron-labs/devtron/pkg/bean"
3031
"github.com/devtron-labs/devtron/pkg/chart"
3132
"github.com/devtron-labs/devtron/pkg/dockerRegistry"
3233
repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
3334
"github.com/devtron-labs/devtron/util/argo"
3435
"github.com/devtron-labs/devtron/util/k8s"
36+
"github.com/tidwall/gjson"
37+
"github.com/tidwall/sjson"
3538
"go.opentelemetry.io/otel"
3639
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3740
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -1679,7 +1682,7 @@ func (impl *AppServiceImpl) mergeAndSave(envOverride *chartConfig.EnvConfigOverr
16791682
}
16801683

16811684
appName := fmt.Sprintf("%s-%s", pipeline.App.AppName, envOverride.Environment.Name)
1682-
merged = impl.hpaCheckBeforeTrigger(ctx, appName, envOverride.Namespace, merged, pipeline.AppId, pipeline.DeploymentAppType, pipeline.Environment.ClusterId)
1685+
merged = impl.autoscalingCheckBeforeTrigger(ctx, appName, envOverride.Namespace, merged, pipeline, overrideRequest)
16831686

16841687
_, span := otel.Tracer("orchestrator").Start(ctx, "dockerRegistryIpsConfigService.HandleImagePullSecretOnApplicationDeployment")
16851688
// handle image pull secret if access given
@@ -1888,7 +1891,12 @@ func (impl *AppServiceImpl) UpdateCdWorkflowRunnerByACDObject(app *v1alpha1.Appl
18881891
return nil
18891892
}
18901893

1891-
func (impl *AppServiceImpl) hpaCheckBeforeTrigger(ctx context.Context, appName string, namespace string, merged []byte, appId int, appDeploymentType string, clusterId int) []byte {
1894+
func (impl *AppServiceImpl) autoscalingCheckBeforeTrigger(ctx context.Context, appName string, namespace string, merged []byte, pipeline *pipelineConfig.Pipeline, overrideRequest *bean.ValuesOverrideRequest) []byte {
1895+
var appId = pipeline.AppId
1896+
pipelineId := pipeline.Id
1897+
var appDeploymentType = pipeline.DeploymentAppType
1898+
var clusterId = pipeline.Environment.ClusterId
1899+
deploymentType := overrideRequest.DeploymentType
18921900
templateMap := make(map[string]interface{})
18931901
err := json.Unmarshal(merged, &templateMap)
18941902
if err != nil {
@@ -1946,21 +1954,13 @@ func (impl *AppServiceImpl) hpaCheckBeforeTrigger(ctx context.Context, appName s
19461954
}
19471955
if len(resourceManifest) > 0 {
19481956
statusMap := resourceManifest["status"].(map[string]interface{})
1949-
currentReplicaVal := fmt.Sprintf("%v", statusMap["currentReplicas"])
1950-
currentReplicaCount, err := strconv.ParseFloat(currentReplicaVal, 64)
1957+
currentReplicaVal := statusMap["currentReplicas"]
1958+
currentReplicaCount, err := util2.ParseFloatNumber(currentReplicaVal)
19511959
if err != nil {
19521960
impl.logger.Errorw("error occurred while parsing replica count", "currentReplicas", currentReplicaVal, "err", err)
19531961
return merged
19541962
}
19551963

1956-
if currentReplicaCount <= reqMaxReplicas && currentReplicaCount >= reqMinReplicas {
1957-
reqReplicaCount = currentReplicaCount
1958-
} else if currentReplicaCount > reqMaxReplicas {
1959-
reqReplicaCount = reqMaxReplicas
1960-
} else if currentReplicaCount < reqMinReplicas {
1961-
reqReplicaCount = reqMinReplicas
1962-
}
1963-
19641964
reqReplicaCount = impl.fetchRequiredReplicaCount(currentReplicaCount, reqMaxReplicas, reqMinReplicas)
19651965
templateMap["replicaCount"] = reqReplicaCount
19661966
merged, err = json.Marshal(&templateMap)

pkg/bean/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ type DeploymentAppTypeChangeRequest struct {
527527
EnvId int `json:"envId,omitempty" validate:"required"`
528528
DesiredDeploymentType DeploymentType `json:"desiredDeploymentType,omitempty" validate:"required"`
529529
ExcludeApps []int `json:"excludeApps"`
530+
UserId int32 `json:"-"`
530531
}
531532

532533
type DeploymentChangeStatus struct {

pkg/pipeline/GitopsOrHelmOption_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ func TestGitopsOrHelmOption(t *testing.T) {
2424
nil, nil, nil,
2525
nil, nil, nil, nil,
2626
nil, nil, nil, nil,
27-
nil, nil, nil, nil, nil,
28-
nil, nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: false}, nil, nil)
27+
nil, nil, nil, nil, nil, nil, nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: false}, nil, nil)
2928

3029
pipelineCreateRequest := &bean.CdPipelines{
3130
Pipelines: []*bean.CDPipelineConfigObject{
@@ -78,9 +77,7 @@ func TestGitopsOrHelmOption(t *testing.T) {
7877
nil, nil, nil,
7978
nil, nil, nil, nil,
8079
nil, nil, nil, nil,
81-
nil, nil, nil, nil,
82-
nil, nil, nil, nil,
83-
&DeploymentServiceTypeConfig{IsInternalUse: false}, nil, nil)
80+
nil, nil, nil, nil, nil, nil, nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: false}, nil, nil)
8481

8582
pipelineCreateRequest := &bean.CdPipelines{
8683
Pipelines: []*bean.CDPipelineConfigObject{
@@ -133,8 +130,7 @@ func TestGitopsOrHelmOption(t *testing.T) {
133130
nil, nil, nil,
134131
nil, nil, nil, nil,
135132
nil, nil, nil, nil,
136-
nil, nil, nil, nil, nil, nil,
137-
nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: true}, nil, nil)
133+
nil, nil, nil, nil, nil, nil, nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: true}, nil, nil)
138134

139135
pipelineCreateRequestHelm := &bean.CdPipelines{
140136
Pipelines: []*bean.CDPipelineConfigObject{
@@ -225,8 +221,7 @@ func TestGitopsOrHelmOption(t *testing.T) {
225221
nil, nil, nil,
226222
nil, nil, nil, nil,
227223
nil, nil, nil, nil,
228-
nil, nil, nil, nil, nil, nil,
229-
nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: false}, nil, nil)
224+
nil, nil, nil, nil, nil, nil, nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: false}, nil, nil)
230225

231226
pipelineCreateRequest := &bean.CdPipelines{
232227
Pipelines: []*bean.CDPipelineConfigObject{
@@ -283,8 +278,7 @@ func TestGitopsOrHelmOption(t *testing.T) {
283278
nil, nil, nil,
284279
nil, nil, nil, nil,
285280
nil, nil, nil, nil,
286-
nil, nil, nil, nil, nil, nil,
287-
nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: true}, nil, nil)
281+
nil, nil, nil, nil, nil, nil, nil, nil, &DeploymentServiceTypeConfig{IsInternalUse: true}, nil, nil)
288282

289283
pipelineCreateRequest := &bean.CdPipelines{
290284
Pipelines: []*bean.CDPipelineConfigObject{

0 commit comments

Comments
 (0)