Skip to content
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
2 changes: 1 addition & 1 deletion api/helm-app/HelmAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (handler *HelmAppRestHandlerImpl) UpdateApplication(w http.ResponseWriter,
//RBAC enforcer Ends

// update application externally
res, err := handler.helmAppService.UpdateApplication(r.Context(), appIdentifier, request)
res, err := handler.helmAppService.UpdateApplication(r.Context(), appIdentifier, request, API_CALLER_EXTERNAL_HELM_APP)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
Expand Down
69 changes: 55 additions & 14 deletions api/helm-app/HelmAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/caarlos0/env/v6"
"github.com/devtron-labs/devtron/api/connector"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
openapi2 "github.com/devtron-labs/devtron/api/openapi/openapiClient"
Expand Down Expand Up @@ -35,8 +36,16 @@ import (
"time"
)

const DEFAULT_CLUSTER = "default_cluster"
const DEFAULT_CLUSTER_ID = 1
const (
DEFAULT_CLUSTER = "default_cluster"
DEFAULT_CLUSTER_ID = 1
API_CALLER_DEVTRON_APP ApiCallerAppType = "devtron-app"
API_CALLER_HELM_APP ApiCallerAppType = "helm-app"
API_CALLER_EXTERNAL_HELM_APP ApiCallerAppType = "external-helm-app"
API_CALLER_UNKNOWN ApiCallerAppType = "unknown"
)

type ApiCallerAppType string

type HelmAppService interface {
ListHelmApplications(ctx context.Context, clusterIds []int, w http.ResponseWriter, token string, helmAuth func(token string, object string) bool)
Expand All @@ -51,17 +60,18 @@ type HelmAppService interface {
GetValuesYaml(ctx context.Context, app *AppIdentifier) (*ReleaseInfo, error)
GetDesiredManifest(ctx context.Context, app *AppIdentifier, resource *openapi.ResourceIdentifier) (*openapi.DesiredManifestResponse, error)
DeleteApplication(ctx context.Context, app *AppIdentifier) (*openapi.UninstallReleaseResponse, error)
UpdateApplication(ctx context.Context, app *AppIdentifier, request *openapi.UpdateReleaseRequest) (*openapi.UpdateReleaseResponse, error)
UpdateApplication(ctx context.Context, app *AppIdentifier, request *openapi.UpdateReleaseRequest, apiCallerAppType ApiCallerAppType) (*openapi.UpdateReleaseResponse, error)
GetDeploymentDetail(ctx context.Context, app *AppIdentifier, version int32) (*openapi.HelmAppDeploymentManifestDetail, error)
InstallRelease(ctx context.Context, clusterId int, installReleaseRequest *InstallReleaseRequest) (*InstallReleaseResponse, error)
UpdateApplicationWithChartInfo(ctx context.Context, clusterId int, updateReleaseRequest *InstallReleaseRequest) (*openapi.UpdateReleaseResponse, error)
UpdateApplicationWithChartInfo(ctx context.Context, clusterId int, updateReleaseRequest *InstallReleaseRequest, apiCallerAppType ApiCallerAppType) (*openapi.UpdateReleaseResponse, error)
IsReleaseInstalled(ctx context.Context, app *AppIdentifier) (bool, error)
RollbackRelease(ctx context.Context, app *AppIdentifier, version int32) (bool, error)
GetClusterConf(clusterId int) (*ClusterConfig, error)
GetDevtronHelmAppIdentifier() *AppIdentifier
UpdateApplicationWithChartInfoWithExtraValues(ctx context.Context, appIdentifier *AppIdentifier, chartRepository *ChartRepository, extraValues map[string]interface{}, extraValuesYamlUrl string, useLatestChartVersion bool) (*openapi.UpdateReleaseResponse, error)
TemplateChart(ctx context.Context, templateChartRequest *openapi2.TemplateChartRequest) (*openapi2.TemplateChartResponse, error)
GetNotes(ctx context.Context, request *InstallReleaseRequest) (string, error)
GetRevisionHistoryMaxValue(appType ApiCallerAppType) int32
}

type HelmAppServiceImpl struct {
Expand All @@ -79,14 +89,17 @@ type HelmAppServiceImpl struct {
appRepository app.AppRepository
clusterRepository clusterRepository.ClusterRepository
K8sUtil *util.K8sUtil
helmReleaseConfig *HelmReleaseConfig
}

func NewHelmAppServiceImpl(Logger *zap.SugaredLogger,
clusterService cluster.ClusterService,
helmAppClient HelmAppClient,
pump connector.Pump, enforcerUtil rbac.EnforcerUtilHelm, serverDataStore *serverDataStore.ServerDataStore,
serverEnvConfig *serverEnvConfig.ServerEnvConfig, appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository,
environmentService cluster.EnvironmentService, pipelineRepository pipelineConfig.PipelineRepository, installedAppRepository repository.InstalledAppRepository, appRepository app.AppRepository, clusterRepository clusterRepository.ClusterRepository, K8sUtil *util.K8sUtil) *HelmAppServiceImpl {
func NewHelmAppServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.ClusterService,
helmAppClient HelmAppClient, pump connector.Pump, enforcerUtil rbac.EnforcerUtilHelm,
serverDataStore *serverDataStore.ServerDataStore, serverEnvConfig *serverEnvConfig.ServerEnvConfig,
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository,
environmentService cluster.EnvironmentService, pipelineRepository pipelineConfig.PipelineRepository,
installedAppRepository repository.InstalledAppRepository, appRepository app.AppRepository,
clusterRepository clusterRepository.ClusterRepository, K8sUtil *util.K8sUtil,
helmReleaseConfig *HelmReleaseConfig) *HelmAppServiceImpl {
return &HelmAppServiceImpl{
logger: Logger,
clusterService: clusterService,
Expand All @@ -102,9 +115,22 @@ func NewHelmAppServiceImpl(Logger *zap.SugaredLogger,
appRepository: appRepository,
clusterRepository: clusterRepository,
K8sUtil: K8sUtil,
helmReleaseConfig: helmReleaseConfig,
}
}

type HelmReleaseConfig struct {
RevisionHistoryLimitDevtronApp int `env:"REVISION_HISTORY_LIMIT_DEVTRON_APP" envDefault:"0"`
RevisionHistoryLimitHelmApp int `env:"REVISION_HISTORY_LIMIT_HELM_APP" envDefault:"0"`
RevisionHistoryLimitExternalHelmApp int `env:"REVISION_HISTORY_LIMIT_EXTERNAL_HELM_APP" envDefault:"0"`
}

func GetHelmReleaseConfig() (*HelmReleaseConfig, error) {
cfg := &HelmReleaseConfig{}
err := env.Parse(cfg)
return cfg, err
}

type ResourceRequestBean struct {
AppId string `json:"appId"`
K8sRequest application.K8sRequestBean `json:"k8sRequest"`
Expand Down Expand Up @@ -413,7 +439,7 @@ func (impl *HelmAppServiceImpl) DeleteApplication(ctx context.Context, app *AppI
return response, nil
}

func (impl *HelmAppServiceImpl) UpdateApplication(ctx context.Context, app *AppIdentifier, request *openapi.UpdateReleaseRequest) (*openapi.UpdateReleaseResponse, error) {
func (impl *HelmAppServiceImpl) UpdateApplication(ctx context.Context, app *AppIdentifier, request *openapi.UpdateReleaseRequest, apiCallerAppType ApiCallerAppType) (*openapi.UpdateReleaseResponse, error) {
config, err := impl.GetClusterConf(app.ClusterId)
if err != nil {
impl.logger.Errorw("error in fetching cluster detail", "clusterId", app.ClusterId, "err", err)
Expand All @@ -427,6 +453,7 @@ func (impl *HelmAppServiceImpl) UpdateApplication(ctx context.Context, app *AppI
ReleaseNamespace: app.Namespace,
},
ValuesYaml: request.GetValuesYaml(),
HistoryMax: impl.GetRevisionHistoryMaxValue(apiCallerAppType),
}

updateApplicationResponse, err := impl.helmAppClient.UpdateApplication(ctx, req)
Expand Down Expand Up @@ -489,13 +516,14 @@ func (impl *HelmAppServiceImpl) InstallRelease(ctx context.Context, clusterId in
return installReleaseResponse, nil
}

func (impl *HelmAppServiceImpl) UpdateApplicationWithChartInfo(ctx context.Context, clusterId int, updateReleaseRequest *InstallReleaseRequest) (*openapi.UpdateReleaseResponse, error) {
func (impl *HelmAppServiceImpl) UpdateApplicationWithChartInfo(ctx context.Context, clusterId int,
updateReleaseRequest *InstallReleaseRequest, apiCallerAppType ApiCallerAppType) (*openapi.UpdateReleaseResponse, error) {
config, err := impl.GetClusterConf(clusterId)
if err != nil {
impl.logger.Errorw("error in fetching cluster detail", "clusterId", clusterId, "err", err)
return nil, err
}

updateReleaseRequest.HistoryMax = impl.GetRevisionHistoryMaxValue(apiCallerAppType)
updateReleaseRequest.ReleaseIdentifier.ClusterConfig = config

updateReleaseResponse, err := impl.helmAppClient.UpdateApplicationWithChartInfo(ctx, updateReleaseRequest)
Expand Down Expand Up @@ -653,7 +681,7 @@ func (impl *HelmAppServiceImpl) UpdateApplicationWithChartInfoWithExtraValues(ct
updateReleaseRequest.ChartVersion = releaseInfo.DeployedAppDetail.ChartVersion
}

updateResponse, err := impl.UpdateApplicationWithChartInfo(ctx, appIdentifier.ClusterId, updateReleaseRequest)
updateResponse, err := impl.UpdateApplicationWithChartInfo(ctx, appIdentifier.ClusterId, updateReleaseRequest, API_CALLER_UNKNOWN)
if err != nil {
impl.logger.Errorw("error in upgrading release", "err", err)
return nil, err
Expand Down Expand Up @@ -856,3 +884,16 @@ func (impl *HelmAppServiceImpl) appListRespProtoTransformer(deployedApps *Deploy
}
return appList
}

func (impl *HelmAppServiceImpl) GetRevisionHistoryMaxValue(appType ApiCallerAppType) int32 {
switch appType {
case API_CALLER_DEVTRON_APP:
return int32(impl.helmReleaseConfig.RevisionHistoryLimitDevtronApp)
case API_CALLER_HELM_APP:
return int32(impl.helmReleaseConfig.RevisionHistoryLimitHelmApp)
case API_CALLER_EXTERNAL_HELM_APP:
return int32(impl.helmReleaseConfig.RevisionHistoryLimitExternalHelmApp)
default:
return 0
}
}
Loading