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

feat: config strategy #6115

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
18 changes: 16 additions & 2 deletions api/restHandler/CoreAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1871,8 +1871,22 @@ func (handler CoreAppRestHandlerImpl) createEnvDeploymentTemplate(appId int, use
appMetrics = *envConfigProperties.AppMetrics
}
chartEntry.GlobalOverride = string(envConfigProperties.EnvOverrideValues)
_, updatedAppMetrics, err := handler.propertiesConfigService.CreateIfRequired(chartEntry, envId, userId, envConfigProperties.ManualReviewed, models.CHARTSTATUS_SUCCESS,
true, appMetrics, envConfigProperties.Namespace, envConfigProperties.IsBasicViewLocked, envConfigProperties.CurrentViewEditor, nil)

overrideCreateRequest := &bean2.EnvironmentOverrideCreateInternalDTO{
Chart: chartEntry,
EnvironmentId: envConfigProperties.EnvironmentId,
UserId: envConfigProperties.UserId,
ManualReviewed: envConfigProperties.ManualReviewed,
ChartStatus: models.CHARTSTATUS_SUCCESS,
IsOverride: true,
IsAppMetricsEnabled: appMetrics,
IsBasicViewLocked: envConfigProperties.IsBasicViewLocked,
Namespace: envConfigProperties.Namespace,
CurrentViewEditor: envConfigProperties.CurrentViewEditor,
MergeStrategy: envConfigProperties.MergeStrategy,
}

_, updatedAppMetrics, err := handler.propertiesConfigService.CreateIfRequired(overrideCreateRequest, nil)
if err != nil {
handler.logger.Errorw("service err, CreateIfRequired", "err", err, "appId", appId, "envId", envId, "chartRefId", chartRefId)
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DeploymentConfigurationRestHandler interface {
ConfigAutoComplete(w http.ResponseWriter, r *http.Request)
GetConfigData(w http.ResponseWriter, r *http.Request)
CompareCategoryWiseConfigData(w http.ResponseWriter, r *http.Request)
GetManifest(w http.ResponseWriter, r *http.Request)
}
type DeploymentConfigurationRestHandlerImpl struct {
logger *zap.SugaredLogger
Expand Down Expand Up @@ -120,6 +121,48 @@ func (handler *DeploymentConfigurationRestHandlerImpl) GetConfigData(w http.Resp
common.WriteJsonResp(w, nil, res, http.StatusOK)
}

func (handler *DeploymentConfigurationRestHandlerImpl) GetManifest(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userAuthService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}

decoder := json.NewDecoder(r.Body)
var request bean.ManifestRequest
err = decoder.Decode(&request)
if err != nil {
handler.logger.Errorw("request err, HandleGitWebhook", "err", err, "payload", request)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

//RBAC START
token := r.Header.Get(common.TokenHeaderKey)
//RBAC START
object := handler.enforcerUtil.GetAppRBACNameByAppId(request.AppId)
ok := handler.enforcerUtil.CheckAppRbacForAppOrJob(token, object, casbin.ActionGet)
if !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
return
}
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*")

ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
ctx = util2.SetSuperAdminInContext(ctx, isSuperAdmin)

request.UserHasAdminAccess = handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionUpdate, object)

res, err := handler.deploymentConfigurationService.GetManifest(ctx, &request)
if err != nil {
handler.logger.Errorw("service err, GetMergedValues ", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, nil, res, http.StatusOK)
}

func (handler *DeploymentConfigurationRestHandlerImpl) enforceForAppAndEnv(appName, envName string, token string, action string) bool {
object := handler.enforcerUtil.GetAppRBACNameByAppName(appName)
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, action, object); !ok {
Expand Down
3 changes: 3 additions & 0 deletions api/router/app/configDiff/DeploymentConfigRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ func (router DeploymentConfigurationRouterImpl) InitDeploymentConfigurationRoute
HandlerFunc(router.deploymentGroupRestHandler.CompareCategoryWiseConfigData).
Methods("GET")

configRouter.Path("/manifest").
HandlerFunc(router.deploymentGroupRestHandler.GetManifest).
Methods("POST")
}
6 changes: 6 additions & 0 deletions internal/sql/models/Enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ const (
//default value
EDITOR_TYPE_UNDEFINED ChartsViewEditorType = "UNDEFINED"
)

type MergeStrategy string

const (
MERGE_STRATEGY_REPLACE MergeStrategy = "replace"
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type EnvConfigOverride struct {
IsOverride bool `sql:"is_override,notnull"`
IsBasicViewLocked bool `sql:"is_basic_view_locked,notnull"`
CurrentViewEditor models.ChartsViewEditorType `sql:"current_view_editor"`
MergeStrategy models.MergeStrategy `sql:"merge_strategy"`
sql.AuditLog
ResolvedEnvOverrideValues string `sql:"-"`
VariableSnapshot map[string]string `sql:"-"`
Expand Down Expand Up @@ -213,6 +214,7 @@ func (r EnvConfigOverrideRepositoryImpl) UpdateProperties(config *EnvConfigOverr
Set("latest =?", config.Latest).
Set("is_basic_view_locked = ?", config.IsBasicViewLocked).
Set("current_view_editor = ?", config.CurrentViewEditor).
Set("merge_strategy = ?", config.MergeStrategy).
//Set("app_metrics_override =?", config.AppMetricsOverride).
WherePK().
Update()
Expand Down
15 changes: 8 additions & 7 deletions pkg/app/AppListingService.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strconv"
"strings"
"time"

"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/read"
"github.com/devtron-labs/common-lib/utils/k8s/health"
"github.com/devtron-labs/devtron/internal/middleware"
"github.com/devtron-labs/devtron/internal/sql/repository/app"
Expand Down Expand Up @@ -137,24 +137,25 @@ type AppListingServiceImpl struct {
pipelineOverrideRepository chartConfig.PipelineOverrideRepository
environmentRepository repository2.EnvironmentRepository
argoUserService argo.ArgoUserService
envOverrideRepository chartConfig.EnvConfigOverrideRepository
chartRepository chartRepoRepository.ChartRepository
ciPipelineRepository pipelineConfig.CiPipelineRepository
dockerRegistryIpsConfigService dockerRegistry.DockerRegistryIpsConfigService
userRepository userrepository.UserRepository
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService
ciArtifactRepository repository.CiArtifactRepository
envConfigOverrideReadService read.EnvConfigOverrideService
}

func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository repository.AppListingRepository,
application application2.ServiceClient, appRepository app.AppRepository,
appListingViewBuilder AppListingViewBuilder, pipelineRepository pipelineConfig.PipelineRepository,
linkoutsRepository repository.LinkoutsRepository, cdWorkflowRepository pipelineConfig.CdWorkflowRepository,
pipelineOverrideRepository chartConfig.PipelineOverrideRepository, environmentRepository repository2.EnvironmentRepository,
argoUserService argo.ArgoUserService, envOverrideRepository chartConfig.EnvConfigOverrideRepository,
argoUserService argo.ArgoUserService,
chartRepository chartRepoRepository.ChartRepository, ciPipelineRepository pipelineConfig.CiPipelineRepository,
dockerRegistryIpsConfigService dockerRegistry.DockerRegistryIpsConfigService, userRepository userrepository.UserRepository,
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService, ciArtifactRepository repository.CiArtifactRepository) *AppListingServiceImpl {
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService, ciArtifactRepository repository.CiArtifactRepository,
envConfigOverrideReadService read.EnvConfigOverrideService) *AppListingServiceImpl {
serviceImpl := &AppListingServiceImpl{
Logger: Logger,
appListingRepository: appListingRepository,
Expand All @@ -167,13 +168,13 @@ func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository re
pipelineOverrideRepository: pipelineOverrideRepository,
environmentRepository: environmentRepository,
argoUserService: argoUserService,
envOverrideRepository: envOverrideRepository,
chartRepository: chartRepository,
ciPipelineRepository: ciPipelineRepository,
dockerRegistryIpsConfigService: dockerRegistryIpsConfigService,
userRepository: userRepository,
deployedAppMetricsService: deployedAppMetricsService,
ciArtifactRepository: ciArtifactRepository,
envConfigOverrideReadService: envConfigOverrideReadService,
}
return serviceImpl
}
Expand Down Expand Up @@ -887,7 +888,7 @@ func (impl AppListingServiceImpl) FetchOtherEnvironment(ctx context.Context, app
}
for _, env := range envs {
newCtx, span = otel.Tracer("envOverrideRepository").Start(newCtx, "FindLatestChartForAppByAppIdAndEnvId")
envOverride, err := impl.envOverrideRepository.FindLatestChartForAppByAppIdAndEnvId(appId, env.EnvironmentId)
envOverride, err := impl.envConfigOverrideReadService.FindLatestChartForAppByAppIdAndEnvId(appId, env.EnvironmentId)
span.End()
if err != nil && !errors2.IsNotFound(err) {
impl.Logger.Errorw("error in fetching latest chart by appId and envId", "err", err, "appId", appId, "envId", env.EnvironmentId)
Expand Down Expand Up @@ -938,7 +939,7 @@ func (impl AppListingServiceImpl) FetchMinDetailOtherEnvironment(appId int) ([]*
impl.Logger.Infow("No environments found for appId", "appId", appId)
return envs, nil
}
overrideChartRefIds, err := impl.envOverrideRepository.FindChartRefIdsForLatestChartForAppByAppIdAndEnvIds(appId, envIds)
overrideChartRefIds, err := impl.envConfigOverrideReadService.FindChartRefIdsForLatestChartForAppByAppIdAndEnvIds(appId, envIds)
if err != nil && !errors2.IsNotFound(err) {
impl.Logger.Errorw("error in fetching latest chartRefIds id by appId and envIds", "err", err, "appId", appId, "envId", envIds)
return envs, err
Expand Down
14 changes: 8 additions & 6 deletions pkg/app/AppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/config"
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/git"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate"
bean6 "github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/bean"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/read"
bean4 "github.com/devtron-labs/devtron/pkg/deployment/trigger/devtronApps/bean"
"io/ioutil"
"net/url"
Expand Down Expand Up @@ -95,7 +97,6 @@ func GetAppServiceConfig() (*AppServiceConfig, error) {
}

type AppServiceImpl struct {
environmentConfigRepository chartConfig.EnvConfigOverrideRepository
pipelineOverrideRepository chartConfig.PipelineOverrideRepository
mergeUtil *MergeUtil
logger *zap.SugaredLogger
Expand Down Expand Up @@ -125,6 +126,7 @@ type AppServiceImpl struct {
deploymentTemplateService deploymentTemplate.DeploymentTemplateService
appListingService AppListingService
deploymentConfigService common2.DeploymentConfigService
envConfigOverrideReadService read.EnvConfigOverrideService
}

type AppService interface {
Expand All @@ -143,7 +145,6 @@ type AppService interface {
}

func NewAppService(
environmentConfigRepository chartConfig.EnvConfigOverrideRepository,
pipelineOverrideRepository chartConfig.PipelineOverrideRepository,
mergeUtil *MergeUtil, logger *zap.SugaredLogger,
pipelineRepository pipelineConfig.PipelineRepository,
Expand All @@ -165,9 +166,9 @@ func NewAppService(
gitOpsConfigReadService config.GitOpsConfigReadService, gitOperationService git.GitOperationService,
deploymentTemplateService deploymentTemplate.DeploymentTemplateService,
appListingService AppListingService,
deploymentConfigService common2.DeploymentConfigService) *AppServiceImpl {
deploymentConfigService common2.DeploymentConfigService,
envConfigOverrideReadService read.EnvConfigOverrideService) *AppServiceImpl {
appServiceImpl := &AppServiceImpl{
environmentConfigRepository: environmentConfigRepository,
mergeUtil: mergeUtil,
pipelineOverrideRepository: pipelineOverrideRepository,
logger: logger,
Expand Down Expand Up @@ -197,6 +198,7 @@ func NewAppService(
deploymentTemplateService: deploymentTemplateService,
appListingService: appListingService,
deploymentConfigService: deploymentConfigService,
envConfigOverrideReadService: envConfigOverrideReadService,
}
return appServiceImpl
}
Expand Down Expand Up @@ -771,7 +773,7 @@ func (impl *AppServiceImpl) BuildCDSuccessPayload(appName string, environmentNam
type ValuesOverrideResponse struct {
MergedValues string
ReleaseOverrideJSON string
EnvOverride *chartConfig.EnvConfigOverride
EnvOverride *bean6.EnvConfigOverride
PipelineStrategy *chartConfig.PipelineStrategy
PipelineOverride *chartConfig.PipelineOverride
Artifact *repository.CiArtifact
Expand Down Expand Up @@ -807,7 +809,7 @@ func (impl *AppServiceImpl) GetDeployedManifestByPipelineIdAndCDWorkflowId(appId
return manifestByteArray, err
}

envConfigOverride, err := impl.environmentConfigRepository.GetByIdIncludingInactive(pipelineOverride.EnvConfigOverrideId)
envConfigOverride, err := impl.envConfigOverrideReadService.GetByIdIncludingInactive(pipelineOverride.EnvConfigOverrideId)
if err != nil {
impl.logger.Errorw("error in fetching env config repository by appId and envId", "appId", appId, "envId", envId, "err", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/appClone/AppCloneService.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ func (impl *AppCloneServiceImpl) configDataClone(cfData []*bean3.ConfigData) []*
FilePermission: refdata.FilePermission,
SubPath: refdata.SubPath,
ESOSubPath: refdata.ESOSubPath,
MergeStrategy: refdata.MergeStrategy,
}
copiedData = append(copiedData, data)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/bulkAction/BulkUpdateService.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
bean5 "github.com/devtron-labs/devtron/pkg/deployment/deployedApp/bean"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/configMapAndSecret"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deployedAppMetrics"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/adapter"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/chartRef"
bean3 "github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/chartRef/bean"
Expand Down Expand Up @@ -506,7 +507,8 @@ func (impl BulkUpdateServiceImpl) BulkUpdateDeploymentTemplate(bulkUpdatePayload
return nil
}
chartEnv.EnvOverrideValues = modified
err = impl.deploymentTemplateHistoryService.CreateDeploymentTemplateHistoryFromEnvOverrideTemplate(chartEnv, nil, isAppMetricsEnabled, 0)
chartEnvDTO := adapter.EnvOverrideDBToDTO(chartEnv)
err = impl.deploymentTemplateHistoryService.CreateDeploymentTemplateHistoryFromEnvOverrideTemplate(chartEnvDTO, nil, isAppMetricsEnabled, 0)
if err != nil {
impl.logger.Errorw("error in creating entry for env deployment template history", "err", err, "envOverride", chartEnv)
}
Expand Down
19 changes: 13 additions & 6 deletions pkg/chart/ChartService.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import (
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deployedAppMetrics"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deployedAppMetrics/bean"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/adapter"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/chartRef"
chartRefBean "github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/chartRef/bean"
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/read"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/devtron-labs/devtron/pkg/variables"
variablesRepository "github.com/devtron-labs/devtron/pkg/variables/repository"
Expand Down Expand Up @@ -88,6 +90,7 @@ type ChartServiceImpl struct {
chartRefService chartRef.ChartRefService
gitOpsConfigReadService config.GitOpsConfigReadService
deploymentConfigService common.DeploymentConfigService
envConfigOverrideReadService read.EnvConfigOverrideService
}

func NewChartServiceImpl(chartRepository chartRepoRepository.ChartRepository,
Expand All @@ -104,7 +107,8 @@ func NewChartServiceImpl(chartRepository chartRepoRepository.ChartRepository,
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService,
chartRefService chartRef.ChartRefService,
gitOpsConfigReadService config.GitOpsConfigReadService,
deploymentConfigService common.DeploymentConfigService) *ChartServiceImpl {
deploymentConfigService common.DeploymentConfigService,
envConfigOverrideReadService read.EnvConfigOverrideService) *ChartServiceImpl {
return &ChartServiceImpl{
chartRepository: chartRepository,
logger: logger,
Expand All @@ -121,6 +125,7 @@ func NewChartServiceImpl(chartRepository chartRepoRepository.ChartRepository,
chartRefService: chartRefService,
gitOpsConfigReadService: gitOpsConfigReadService,
deploymentConfigService: deploymentConfigService,
envConfigOverrideReadService: envConfigOverrideReadService,
}
}

Expand Down Expand Up @@ -777,7 +782,7 @@ type IsReady struct {

func (impl *ChartServiceImpl) IsReadyToTrigger(appId int, envId int, pipelineId int) (IsReady, error) {
isReady := IsReady{Flag: false}
envOverride, err := impl.envOverrideRepository.ActiveEnvConfigOverride(appId, envId)
envOverride, err := impl.envConfigOverrideReadService.ActiveEnvConfigOverride(appId, envId)
if err != nil {
impl.logger.Errorf("invalid state", "err", err, "envId", envId)
isReady.Message = "Something went wrong"
Expand Down Expand Up @@ -844,7 +849,7 @@ func (impl *ChartServiceImpl) ChartRefAutocompleteForAppOrEnv(appId int, envId i
}

if envId > 0 {
envOverride, err := impl.envOverrideRepository.FindLatestChartForAppByAppIdAndEnvId(appId, envId)
envOverride, err := impl.envConfigOverrideReadService.FindLatestChartForAppByAppIdAndEnvId(appId, envId)
if err != nil && !errors.IsNotFound(err) {
impl.logger.Errorw("error in fetching latest chart", "err", err)
return chartRefResponse, err
Expand Down Expand Up @@ -913,7 +918,7 @@ func (impl *ChartServiceImpl) UpgradeForApp(appId int, chartRefId int, newAppOve
//STEP 2 - env upgrade
impl.logger.Debugw("creating env and pipeline config for app", "appId", appId)
//step 1
envOverrides, err := impl.envOverrideRepository.GetEnvConfigByChartId(currentChart.Id)
envOverrides, err := impl.envConfigOverrideReadService.GetEnvConfigByChartId(currentChart.Id)
if err != nil && envOverrides == nil {
return false, err
}
Expand Down Expand Up @@ -949,12 +954,14 @@ func (impl *ChartServiceImpl) UpgradeForApp(appId int, chartRefId int, newAppOve
impl.logger.Errorw("error, GetMetricsFlagForAPipelineByAppIdAndEnvId", "err", err, "appId", appId, "envId", envOverrideNew.TargetEnvironment)
return false, err
}
err = impl.deploymentTemplateHistoryService.CreateDeploymentTemplateHistoryFromEnvOverrideTemplate(envOverrideNew, nil, isAppMetricsEnabled, 0)
envOverrideNewDTO := adapter.EnvOverrideDBToDTO(envOverrideNew)
err = impl.deploymentTemplateHistoryService.CreateDeploymentTemplateHistoryFromEnvOverrideTemplate(envOverrideNewDTO, nil, isAppMetricsEnabled, 0)
if err != nil {
impl.logger.Errorw("error in creating entry for env deployment template history", "err", err, "envOverride", envOverrideNew)
impl.logger.Errorw("error in creating entry for env deployment template history", "err", err, "envOverride", envOverrideNewDTO)
return false, err
}
//VARIABLE_MAPPING_UPDATE
//TODO ayush, check if this is needed
err = impl.scopedVariableManager.ExtractAndMapVariables(envOverrideNew.EnvOverrideValues, envOverrideNew.Id, variablesRepository.EntityTypeDeploymentTemplateEnvLevel, envOverrideNew.CreatedBy, nil)
if err != nil {
return false, err
Expand Down
Loading
Loading