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
22 changes: 20 additions & 2 deletions pkg/chartRepo/repository/ChartRefRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ type GlobalStrategyMetadata struct {
tableName struct{} `sql:"global_strategy_metadata" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
Name DeploymentStrategy `sql:"name"`
Key string `sql:"key"`
Description string `sql:"description"`
Deleted bool `sql:"deleted,notnull"`
sql.AuditLog
Expand Down Expand Up @@ -195,13 +196,16 @@ func (impl *GlobalStrategyMetadataRepositoryImpl) GetByChartRefId(chartRefId int
type GlobalStrategyMetadataChartRefMapping struct {
tableName struct{} `sql:"global_strategy_metadata_chart_ref_mapping" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
GlobalStrategyMetadataId string `sql:"global_strategy_metadata_id"`
ChartRefId string `sql:"chart_ref_id"`
GlobalStrategyMetadataId int `sql:"global_strategy_metadata_id"`
ChartRefId int `sql:"chart_ref_id"`
Active bool `sql:"active,notnull"`
Default bool `sql:"default,notnull"`
GlobalStrategyMetadata *GlobalStrategyMetadata
sql.AuditLog
}

type GlobalStrategyMetadataChartRefMappingRepository interface {
GetByChartRefId(chartRefId int) ([]*GlobalStrategyMetadataChartRefMapping, error)
}
type GlobalStrategyMetadataChartRefMappingRepositoryImpl struct {
dbConnection *pg.DB
Expand All @@ -215,3 +219,17 @@ func NewGlobalStrategyMetadataChartRefMappingRepositoryImpl(dbConnection *pg.DB,
logger: logger,
}
}

func (impl *GlobalStrategyMetadataChartRefMappingRepositoryImpl) GetByChartRefId(chartRefId int) ([]*GlobalStrategyMetadataChartRefMapping, error) {
var globalStrategies []*GlobalStrategyMetadataChartRefMapping
err := impl.dbConnection.Model(&globalStrategies).
Column("global_strategy_metadata_chart_ref_mapping.*", "GlobalStrategyMetadata").
Where("global_strategy_metadata_chart_ref_mapping.chart_ref_id = ?", chartRefId).
Where("global_strategy_metadata_chart_ref_mapping.active = ?", true).
Select()
if err != nil {
impl.logger.Errorw("error in getting global strategies metadata mapping by chartRefId", "err", err, "chartRefId", chartRefId)
return nil, err
}
return globalStrategies, err
}
139 changes: 26 additions & 113 deletions pkg/pipeline/PipelineBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2447,49 +2447,7 @@ type DeploymentType struct {
}

type Deployment struct {
Strategy Strategy `json:"strategy"`
}

type Strategy struct {
BlueGreen *BlueGreen `json:"blueGreen,omitempty"`
Rolling *Rolling `json:"rolling,omitempty"`
Canary *Canary `json:"canary,omitempty"`
Recreate *Recreate `json:"recreate,omitempty"`
}

type BlueGreen struct {
AutoPromotionSeconds int `json:"autoPromotionSeconds"`
ScaleDownDelaySeconds int `json:"scaleDownDelaySeconds"`
PreviewReplicaCount int `json:"previewReplicaCount"`
AutoPromotionEnabled bool `json:"autoPromotionEnabled"`
}

type Canary struct {
MaxSurge string `json:"maxSurge,omitempty"`
MaxUnavailable int `json:"maxUnavailable,omitempty"`
Steps []CanaryStep `json:"steps,omitempty"`
}

type CanaryStep struct {
// SetWeight sets what percentage of the newRS should receive
SetWeight *int32 `json:"setWeight,omitempty"`
// Pause freezes the rollout by setting spec.Paused to true.
// A Rollout will resume when spec.Paused is reset to false.
// +optional
Pause *RolloutPause `json:"pause,omitempty"`
}

type RolloutPause struct {
// Duration the amount of time to wait before moving to the next step.
// +optional
Duration *int32 `json:"duration,omitempty"`
}
type Recreate struct {
}

type Rolling struct {
MaxSurge string `json:"maxSurge"`
MaxUnavailable int `json:"maxUnavailable"`
Strategy map[string]interface{} `json:"strategy"`
}

func (impl PipelineBuilderImpl) createCdPipeline(ctx context.Context, app *app2.App, pipeline *bean.CDPipelineConfigObject, userId int32) (pipelineRes int, err error) {
Expand Down Expand Up @@ -2749,71 +2707,30 @@ func (impl PipelineBuilderImpl) updateCdPipeline(ctx context.Context, pipeline *
return nil
}

func (impl PipelineBuilderImpl) filterDeploymentTemplate(deploymentTemplate chartRepoRepository.DeploymentStrategy, pipelineOverride string) (string, error) {
var deploymentType DeploymentType
err := json.Unmarshal([]byte(pipelineOverride), &deploymentType)
func (impl PipelineBuilderImpl) filterDeploymentTemplate(strategyKey string, pipelineStrategiesJson string) (string, error) {
var pipelineStrategies DeploymentType
err := json.Unmarshal([]byte(pipelineStrategiesJson), &pipelineStrategies)
if err != nil {
impl.logger.Errorw("err", err)
impl.logger.Errorw("error while unmarshal strategies", "err", err)
return "", err
}
if chartRepoRepository.DEPLOYMENT_STRATEGY_BLUE_GREEN == deploymentTemplate {
newDeploymentType := DeploymentType{
Deployment: Deployment{
Strategy: Strategy{
BlueGreen: deploymentType.Deployment.Strategy.BlueGreen,
},
},
}
pipelineOverrideBytes, err := json.Marshal(newDeploymentType)
if err != nil {
impl.logger.Errorw("err", err)
return "", err
}
pipelineOverride = string(pipelineOverrideBytes)
} else if chartRepoRepository.DEPLOYMENT_STRATEGY_ROLLING == deploymentTemplate {
newDeploymentType := DeploymentType{
Deployment: Deployment{
Strategy: Strategy{
Rolling: deploymentType.Deployment.Strategy.Rolling,
},
},
}
pipelineOverrideBytes, err := json.Marshal(newDeploymentType)
if err != nil {
impl.logger.Errorw("err", err)
return "", err
}
pipelineOverride = string(pipelineOverrideBytes)
} else if chartRepoRepository.DEPLOYMENT_STRATEGY_CANARY == deploymentTemplate {
newDeploymentType := DeploymentType{
Deployment: Deployment{
Strategy: Strategy{
Canary: deploymentType.Deployment.Strategy.Canary,
},
},
}
pipelineOverrideBytes, err := json.Marshal(newDeploymentType)
if err != nil {
impl.logger.Errorw("err", err)
return "", err
}
pipelineOverride = string(pipelineOverrideBytes)
} else if chartRepoRepository.DEPLOYMENT_STRATEGY_RECREATE == deploymentTemplate {
newDeploymentType := DeploymentType{
Deployment: Deployment{
Strategy: Strategy{
Recreate: deploymentType.Deployment.Strategy.Recreate,
},
},
}
pipelineOverrideBytes, err := json.Marshal(newDeploymentType)
if err != nil {
impl.logger.Errorw("err", err)
return "", err
}
pipelineOverride = string(pipelineOverrideBytes)
if pipelineStrategies.Deployment.Strategy[strategyKey] == nil {
return "", fmt.Errorf("no deployment strategy found for %s", strategyKey)
}
return pipelineOverride, nil
strategy := make(map[string]interface{})
strategy[strategyKey] = pipelineStrategies.Deployment.Strategy[strategyKey].(map[string]interface{})
pipelineStrategy := DeploymentType{
Deployment: Deployment{
Strategy: strategy,
},
}
pipelineOverrideBytes, err := json.Marshal(pipelineStrategy)
if err != nil {
impl.logger.Errorw("error while marshal strategies", "err", err)
return "", err
}
pipelineStrategyJson := string(pipelineOverrideBytes)
return pipelineStrategyJson, nil
}

func (impl PipelineBuilderImpl) GetTriggerViewCdPipelinesForApp(appId int) (cdPipelines *bean.CdPipelines, err error) {
Expand Down Expand Up @@ -3368,7 +3285,7 @@ func (impl PipelineBuilderImpl) FetchCDPipelineStrategy(appId int) (PipelineStra
}

//get global strategy for this chart
globalStrategies, err := impl.globalStrategyMetadataRepository.GetByChartRefId(chart.ChartRefId)
globalStrategies, err := impl.globalStrategyMetadataChartRefMappingRepository.GetByChartRefId(chart.ChartRefId)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in getting global strategies", "err", err)
return pipelineStrategiesResponse, err
Expand All @@ -3378,19 +3295,15 @@ func (impl PipelineBuilderImpl) FetchCDPipelineStrategy(appId int) (PipelineStra
}
pipelineOverride := chart.PipelineOverride
for _, globalStrategy := range globalStrategies {
config, err := impl.filterDeploymentTemplate(globalStrategy.Name, pipelineOverride)
pipelineStrategyJson, err := impl.filterDeploymentTemplate(globalStrategy.GlobalStrategyMetadata.Key, pipelineOverride)
if err != nil {
return pipelineStrategiesResponse, err
}
pipelineStrategy := PipelineStrategy{
DeploymentTemplate: globalStrategy.Name,
Config: []byte(config),
}
if globalStrategy.Name == chartRepoRepository.DEPLOYMENT_STRATEGY_ROLLING {
pipelineStrategy.Default = true
} else {
pipelineStrategy.Default = false
DeploymentTemplate: globalStrategy.GlobalStrategyMetadata.Name,
Config: []byte(pipelineStrategyJson),
}
pipelineStrategy.Default = globalStrategy.Default
pipelineStrategiesResponse.PipelineStrategy = append(pipelineStrategiesResponse.PipelineStrategy, pipelineStrategy)
}
return pipelineStrategiesResponse, nil
Expand Down
2 changes: 2 additions & 0 deletions scripts/sql/132_alter_global_strategy_metadata.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "public"."global_strategy_metadata" DROP COLUMN "key";
ALTER TABLE "public"."global_strategy_metadata_chart_ref_mapping" DROP COLUMN "default";
9 changes: 9 additions & 0 deletions scripts/sql/132_alter_global_strategy_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ALTER TABLE "public"."global_strategy_metadata" ADD COLUMN "key" varchar(250);
COMMENT ON COLUMN "public"."global_strategy_metadata"."key" IS 'strategy json key';
UPDATE "public"."global_strategy_metadata" SET "key" = 'recreate' WHERE "name" = 'RECREATE';
UPDATE "public"."global_strategy_metadata" SET "key" = 'canary' WHERE "name" = 'CANARY';
UPDATE "public"."global_strategy_metadata" SET "key" = 'rolling' WHERE "name" = 'ROLLING';
UPDATE "public"."global_strategy_metadata" SET "key" = 'blueGreen' WHERE "name" = 'BLUE-GREEN';

ALTER TABLE "public"."global_strategy_metadata_chart_ref_mapping" ADD COLUMN "default" bool DEFAULT 'false';
UPDATE global_strategy_metadata_chart_ref_mapping set "default"=true WHERE global_strategy_metadata_id=(SELECT id from global_strategy_metadata WHERE name like 'ROLLING');