Skip to content

Commit

Permalink
fix helm deploy job vars when only deploy image in somecase
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <[email protected]>
  • Loading branch information
PetrusZ committed Dec 24, 2024
1 parent ae85763 commit db8f811
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
9 changes: 6 additions & 3 deletions pkg/microservice/aslan/core/common/service/kube/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,9 @@ func PrepareHelmServiceData(applyParam *ResourceApplyParam) (*commonmodels.Produ
OverrideYaml: &template.CustomYaml{},
}
productService.Render = targetChart
} else {
productService.GetServiceRender().ValuesYaml = svcTemplate.HelmChart.ValuesYaml
productService.GetServiceRender().ChartVersion = svcTemplate.HelmChart.Version
}

if applyParam.UpdateServiceRevision && productService.Revision != svcTemplate.Revision {
Expand All @@ -805,18 +808,18 @@ func PrepareHelmServiceData(applyParam *ResourceApplyParam) (*commonmodels.Produ
}
}

replaceValuesMaps := make([]map[string]interface{}, 0)
imageValuesMaps := make([]map[string]interface{}, 0)
for _, targetContainer := range productService.Containers {
// prepare image replace info
replaceValuesMap, err := commonutil.AssignImageData(targetContainer.Image, commonutil.GetValidMatchData(targetContainer.ImagePath))
if err != nil {
return nil, nil, fmt.Errorf("failed to pase image uri %s/%s, err %s", productInfo.ProductName, applyParam.ServiceName, err.Error())
}
replaceValuesMaps = append(replaceValuesMaps, replaceValuesMap)
imageValuesMaps = append(imageValuesMaps, replaceValuesMap)
}

// replace image into service's values.yaml
replacedValuesYaml, err := commonutil.ReplaceImage(svcTemplate.HelmChart.ValuesYaml, replaceValuesMaps...)
replacedValuesYaml, err := commonutil.ReplaceImage(svcTemplate.HelmChart.ValuesYaml, imageValuesMaps...)
if err != nil {
return nil, nil, fmt.Errorf("failed to replace image uri %s/%s, err %s", productInfo.ProductName, applyParam.ServiceName, err.Error())

Expand Down
14 changes: 9 additions & 5 deletions pkg/microservice/aslan/core/common/service/kube/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ func InstallOrUpgradeHelmChartWithValues(param *ReleaseInstallParam, isRetry boo
}

// GeneMergedValues generate values.yaml used to install or upgrade helm chart, like param in after option -f
// If fullValues is set to true, full values yaml content will be returned, this case is used to preview values when running workflows
// productSvc: contains current images info
// svcRender: contains env values info, including service's values and env's override values
// defaultValues: global values yaml
// images: images to be replaced
// fullValues: If fullValues is set to true, full values yaml content will be returned, this case is used to preview values when running workflows
func GeneMergedValues(productSvc *commonmodels.ProductService, svcRender *templatemodels.ServiceRender, defaultValues string, images []string, fullValues bool) (string, error) {
serviceName := productSvc.ServiceName
var targetContainers []*commonmodels.Container
Expand All @@ -164,18 +168,18 @@ func GeneMergedValues(productSvc *commonmodels.ProductService, svcRender *templa

targetChart := svcRender

replaceValuesMaps := make([]map[string]interface{}, 0)
imageValuesMaps := make([]map[string]interface{}, 0)
for _, targetContainer := range targetContainers {
// prepare image replace info
replaceValuesMap, err := commonutil.AssignImageData(targetContainer.Image, commonutil.GetValidMatchData(targetContainer.ImagePath))
if err != nil {
return "", fmt.Errorf("failed to pase image uri %s/%s, err %s", productSvc.ProductName, serviceName, err.Error())
}
replaceValuesMaps = append(replaceValuesMaps, replaceValuesMap)
imageValuesMaps = append(imageValuesMaps, replaceValuesMap)
}

imageKVS := make([]*helmtool.KV, 0)
for _, imageSecs := range replaceValuesMaps {
for _, imageSecs := range imageValuesMaps {
for key, value := range imageSecs {
imageKVS = append(imageKVS, &helmtool.KV{
Key: key,
Expand All @@ -185,7 +189,7 @@ func GeneMergedValues(productSvc *commonmodels.ProductService, svcRender *templa
}

// replace image into service's values.yaml
replacedValuesYaml, err := commonutil.ReplaceImage(targetChart.ValuesYaml, replaceValuesMaps...)
replacedValuesYaml, err := commonutil.ReplaceImage(targetChart.ValuesYaml, imageValuesMaps...)
if err != nil {
return "", fmt.Errorf("failed to replace image uri %s/%s, err %s", productSvc.ProductName, serviceName, err.Error())

Expand Down
17 changes: 4 additions & 13 deletions pkg/microservice/aslan/core/environment/service/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1504,17 +1504,17 @@ func GetAffectedServices(productName, envName string, arg *K8sRendersetArg, log
func GeneEstimatedValues(productName, envName, serviceOrReleaseName, scene, format string, arg *EstimateValuesArg, isHelmChartDeploy bool, log *zap.SugaredLogger) (interface{}, error) {
var (
productSvc *commonmodels.ProductService
latestSvc *commonmodels.Service
tmplSvc *commonmodels.Service
productInfo *commonmodels.Product
err error
)

switch scene {
case usageScenarioCreateEnv:
productInfo = &commonmodels.Product{}
productSvc, latestSvc, err = prepareEstimateDataForEnvCreation(productName, serviceOrReleaseName, arg.Production, isHelmChartDeploy, log)
productSvc, tmplSvc, err = prepareEstimateDataForEnvCreation(productName, serviceOrReleaseName, arg.Production, isHelmChartDeploy, log)
default:
productSvc, latestSvc, productInfo, err = prepareEstimateDataForEnvUpdate(productName, envName, serviceOrReleaseName, scene, arg.Production, isHelmChartDeploy, log)
productSvc, tmplSvc, productInfo, err = prepareEstimateDataForEnvUpdate(productName, envName, serviceOrReleaseName, scene, arg.Production, isHelmChartDeploy, log)
}

if err != nil {
Expand All @@ -1532,15 +1532,6 @@ func GeneEstimatedValues(productName, envName, serviceOrReleaseName, scene, form

images := make([]string, 0)

curUsedSvc, err := repository.QueryTemplateService(&commonrepo.ServiceFindOption{
ServiceName: productSvc.ServiceName,
Revision: productSvc.Revision,
ProductName: productSvc.ProductName,
}, arg.Production)
if err != nil {
curUsedSvc = nil
}

mergedValues := ""
if isHelmChartDeploy {
chartRepo, err := commonrepo.NewHelmRepoColl().Find(&commonrepo.HelmRepoFindOption{RepoName: arg.ChartRepo})
Expand All @@ -1563,7 +1554,7 @@ func GeneEstimatedValues(productName, envName, serviceOrReleaseName, scene, form
return nil, e.ErrUpdateRenderSet.AddDesc(fmt.Sprintf("failed to merge override values, err %s", err))
}
} else {
containers := kube.CalculateContainer(productSvc, curUsedSvc, latestSvc.Containers, productInfo)
containers := kube.CalculateContainer(productSvc, tmplSvc, tmplSvc.Containers, productInfo)
for _, container := range containers {
images = append(images, container.Image)
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/microservice/aslan/core/service/service/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,6 @@ func CreateOrUpdateHelmServiceFromRepo(projectName string, args *HelmServiceCrea
filePaths = createFromRepo.Paths
base = path.Join(config.S3StoragePath(), createFromRepo.Repo)

helmRenderCharts := make([]*templatemodels.ServiceRender, 0, len(filePaths))
var wg wait.Group
var mux sync.RWMutex
var serviceList []*commonmodels.Service
Expand Down Expand Up @@ -962,12 +961,6 @@ func CreateOrUpdateHelmServiceFromRepo(projectName string, args *HelmServiceCrea
return
}
serviceList = append(serviceList, svc)

helmRenderCharts = append(helmRenderCharts, &templatemodels.ServiceRender{
ServiceName: serviceName,
ChartVersion: svc.HelmChart.Version,
ValuesYaml: svc.HelmChart.ValuesYaml,
})
})
}

Expand Down

0 comments on commit db8f811

Please sign in to comment.