Skip to content

Commit 49d6110

Browse files
committed
fix: migrate proxy chart dependencies and refactor related functions
1 parent 1000af5 commit 49d6110

File tree

4 files changed

+200
-97
lines changed

4 files changed

+200
-97
lines changed

pkg/appStore/bean/bean.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,6 @@ const (
305305
)
306306

307307
var CHART_PROXY_TEMPLATE = "reference-chart-proxy"
308-
var REQUIREMENTS_YAML_FILE = "requirements.yaml"
309-
var VALUES_YAML_FILE = "values.yaml"
310308

311309
type InstalledAppsResponse struct {
312310
AppStoreApplicationName string `json:"appStoreApplicationName"`

pkg/appStore/installedApp/service/FullMode/deployment/InstalledAppGitOpsService.go

Lines changed: 7 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@ import (
2828
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/bean"
2929
commonBean "github.com/devtron-labs/devtron/pkg/deployment/gitOps/common/bean"
3030
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/git"
31-
gitBean "github.com/devtron-labs/devtron/pkg/deployment/gitOps/git/bean"
3231
validationBean "github.com/devtron-labs/devtron/pkg/deployment/gitOps/validation/bean"
3332
chartRefBean "github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/chartRef/bean"
3433
globalUtil "github.com/devtron-labs/devtron/util"
35-
"github.com/devtron-labs/devtron/util/sliceUtil"
3634
"github.com/google/go-github/github"
3735
"github.com/microsoft/azure-devops-go-api/azuredevops"
3836
"github.com/xanzy/go-gitlab"
3937
"helm.sh/helm/v3/pkg/chart"
4038
"net/http"
41-
"os"
42-
"path/filepath"
4339
"sigs.k8s.io/yaml"
4440
"strconv"
4541
"strings"
@@ -137,20 +133,17 @@ func (impl *FullModeDeploymentServiceImpl) UpdateAppGitOpsOperations(manifest *b
137133

138134
gitOpsResponse := &bean.AppStoreGitOpsResponse{}
139135
ctx := context.Background()
136+
cloneChartToGitRequest := adapter.ParseChartGitPushRequest(installAppVersionRequest, "")
137+
err := impl.gitOperationService.MigrateProxyChartDependenciesIfRequired(ctx, updateDependencies, cloneChartToGitRequest, manifest.ChartMetaDataConfig.FileContent)
138+
if err != nil {
139+
impl.Logger.Errorw("error in checking if proxy chart dependencies should be migrated", "err", err)
140+
return nil, err
141+
}
140142
if updateDependencies {
141143
// update dependency if chart or chart version is changed
142144
_, _, requirementsCommitErr = impl.gitOperationService.CommitValues(ctx, manifest.ChartMetaDataConfig)
143145
gitHash, _, valuesCommitErr = impl.gitOperationService.CommitValues(ctx, manifest.ValuesConfig)
144146
} else {
145-
cloneChartToGitRequest := adapter.ParseChartGitPushRequest(installAppVersionRequest, "")
146-
migrateDependencies, err := impl.shouldMigrateProxyChartDependencies(cloneChartToGitRequest, manifest.ChartMetaDataConfig.FileContent)
147-
if err != nil {
148-
impl.Logger.Errorw("error in checking if proxy chart dependencies should be migrated", "err", err)
149-
return nil, err
150-
}
151-
if migrateDependencies {
152-
_, _, requirementsCommitErr = impl.gitOperationService.CommitValues(ctx, manifest.ChartMetaDataConfig)
153-
}
154147
// only values are changed in update, so commit values config
155148
gitHash, _, valuesCommitErr = impl.gitOperationService.CommitValues(ctx, manifest.ValuesConfig)
156149
}
@@ -372,7 +365,7 @@ func (impl *FullModeDeploymentServiceImpl) getValuesAndChartMetaDataForGitConfig
372365
impl.Logger.Errorw("error in marshalling values content", "err", err)
373366
return nil, nil, err
374367
}
375-
valuesConfig, err := impl.getGitCommitConfig(installAppVersionRequest, string(valuesContent), appStoreBean.VALUES_YAML_FILE)
368+
valuesConfig, err := impl.getGitCommitConfig(installAppVersionRequest, string(valuesContent), chartRefBean.VALUES_YAML_FILE)
376369
if err != nil {
377370
impl.Logger.Errorw("error in creating values config for git", "err", err)
378371
return nil, nil, err
@@ -426,83 +419,3 @@ func (impl *FullModeDeploymentServiceImpl) CreateArgoRepoSecretIfNeeded(appStore
426419
}
427420
return nil
428421
}
429-
430-
func (impl *FullModeDeploymentServiceImpl) shouldMigrateProxyChartDependencies(pushChartToGitRequest *gitBean.PushChartToGitRequestDTO, expectedChartYamlContent string) (bool, error) {
431-
clonedDir, err := impl.gitOperationService.CloneChartForHelmApp(pushChartToGitRequest.AppName, pushChartToGitRequest.RepoURL, pushChartToGitRequest.TargetRevision)
432-
if err != nil {
433-
impl.Logger.Errorw("error in cloning chart for helm app", "appName", pushChartToGitRequest.AppName, "repoUrl", pushChartToGitRequest.RepoURL, "err", err)
434-
return false, err
435-
}
436-
defer impl.chartTemplateService.CleanDir(clonedDir)
437-
gitOpsChartLocation := fmt.Sprintf("%s-%s", pushChartToGitRequest.AppName, pushChartToGitRequest.EnvName)
438-
dir := filepath.Join(clonedDir, gitOpsChartLocation)
439-
chartYamlPath := filepath.Join(dir, chartRefBean.CHART_YAML_FILE)
440-
if _, err := os.Stat(chartYamlPath); os.IsNotExist(err) {
441-
impl.Logger.Debugw("chart.yaml not found in cloned repo from git-ops, no migrations required", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName)
442-
return false, nil
443-
} else if err != nil {
444-
impl.Logger.Errorw("error in checking chart.yaml file", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "err", err)
445-
return false, err
446-
}
447-
expectedChartMetaData := &chart.Metadata{}
448-
expectedChartJsonContent, err := yaml.YAMLToJSON([]byte(expectedChartYamlContent))
449-
if err != nil {
450-
impl.Logger.Errorw("error in converting requirements.yaml to json", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "err", err)
451-
return false, err
452-
}
453-
err = json.Unmarshal(expectedChartJsonContent, &expectedChartMetaData)
454-
if err != nil {
455-
impl.Logger.Errorw("error in unmarshalling requirements.yaml file", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "err", err)
456-
return false, err
457-
}
458-
if len(expectedChartMetaData.Dependencies) == 0 {
459-
impl.Logger.Debugw("no dependencies found in requirements.yaml file", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName)
460-
return false, nil
461-
}
462-
impl.Logger.Debugw("dependencies found in requirements.yaml file", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "dependencies", expectedChartMetaData.Dependencies)
463-
// check if chart.yaml file has dependencies
464-
chartYamlContent, err := os.ReadFile(chartYamlPath)
465-
if err != nil {
466-
impl.Logger.Errorw("error in reading chart.yaml file", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "err", err)
467-
return false, err
468-
}
469-
chartMetadata := &chart.Metadata{}
470-
chartJsonContent, err := yaml.YAMLToJSON(chartYamlContent)
471-
if err != nil {
472-
impl.Logger.Errorw("error in converting chart.yaml to json", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "err", err)
473-
return false, err
474-
}
475-
err = json.Unmarshal(chartJsonContent, chartMetadata)
476-
if err != nil {
477-
impl.Logger.Errorw("error in unmarshalling chart.yaml file", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "err", err)
478-
return false, err
479-
}
480-
if len(chartMetadata.Dependencies) == 0 {
481-
impl.Logger.Debugw("no dependencies found in chart.yaml file, need to migrate proxy chart dependencies", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName)
482-
return true, nil
483-
}
484-
impl.Logger.Debugw("dependencies found in chart.yaml file, validating against requirements.yaml", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "chartDependencies", chartMetadata.Dependencies)
485-
// validate if chart.yaml dependencies are present in requirements.yaml
486-
latestDependencies := sliceUtil.NewMapFromFuncExec(chartMetadata.Dependencies, func(dependency *chart.Dependency) string {
487-
return getUniqueKeyFromDependency(dependency)
488-
})
489-
previousDependencies := sliceUtil.NewMapFromFuncExec(expectedChartMetaData.Dependencies, func(dependency *chart.Dependency) string {
490-
return getUniqueKeyFromDependency(dependency)
491-
})
492-
for key := range latestDependencies {
493-
if _, ok := previousDependencies[key]; !ok {
494-
impl.Logger.Debugw("dependency found in chart.yaml but not in requirements.yaml, need to migrate proxy chart dependencies", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName, "dependency", key)
495-
return true, nil
496-
}
497-
}
498-
impl.Logger.Debugw("all dependencies found in chart.yaml and requirements.yaml, no migration required", "appName", pushChartToGitRequest.AppName, "envName", pushChartToGitRequest.EnvName)
499-
return false, nil
500-
}
501-
502-
func getUniqueKeyFromDependency(dependency *chart.Dependency) string {
503-
// return unique key for dependency
504-
return fmt.Sprintf("%s-%s-%s",
505-
strings.ToLower(strings.TrimSpace(dependency.Name)),
506-
strings.ToLower(strings.TrimSpace(dependency.Version)),
507-
strings.ToLower(strings.TrimSpace(dependency.Repository)))
508-
}

0 commit comments

Comments
 (0)