Skip to content

Commit d6e03e5

Browse files
authored
Merge pull request #133 from nicholasSUSE/fix-make-validade-lifecycle-assets
Fix make validade lifecycle assets
2 parents f2ca872 + b039377 commit d6e03e5

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ func checkRCTagsAndVersions(c *cli.Context) {
559559
func lifecycleAssetsClean(c *cli.Context) {
560560
// Initialize dependencies with branch-version, current chart and debug mode
561561
repoRoot := getRepoRoot()
562-
lifeCycleDep, err := lifecycle.InitDependencies(repoRoot, c.String("branch-version"), CurrentChart, DebugMode)
562+
rootFs := filesystem.GetFilesystem(repoRoot)
563+
lifeCycleDep, err := lifecycle.InitDependencies(rootFs, c.String("branch-version"), CurrentChart, DebugMode)
563564
if err != nil {
564565
logrus.Fatalf("encountered error while initializing dependencies for lifecycle-assets-clean: %s", err)
565566
}

pkg/lifecycle/lifecycle.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Asset struct {
2222
type Dependencies struct {
2323
rootFs billy.Filesystem
2424
assetsVersionsMap map[string][]Asset
25-
vr *VersionRules
25+
VR *VersionRules
2626
// These wrappers are used to mock the filesystem and git status in the tests
2727
walkDirWrapper WalkDirFunc
2828
makeRemoveWrapper MakeRemoveFunc
@@ -58,7 +58,7 @@ func cycleLog(debugMode bool, msg string, data interface{}) {
5858
// InitDependencies will check the filesystem, branch version,
5959
// git status, initialize the Dependencies struct and populate it.
6060
// If anything fails the operation will be aborted.
61-
func InitDependencies(repoRoot, branchVersion string, currentChart string, debug bool) (*Dependencies, error) {
61+
func InitDependencies(rootFs billy.Filesystem, branchVersion string, currentChart string, debug bool) (*Dependencies, error) {
6262
logrus.SetFormatter(&logrus.TextFormatter{
6363
DisableQuote: true,
6464
})
@@ -82,13 +82,13 @@ func InitDependencies(repoRoot, branchVersion string, currentChart string, debug
8282

8383
cycleLog(debug, "Getting branch version rules for: ", branchVersion)
8484
// Initialize and check version rules for the current branch
85-
dep.vr, err = GetVersionRules(branchVersion, debug)
85+
dep.VR, err = GetVersionRules(branchVersion, debug)
8686
if err != nil {
8787
return nil, fmt.Errorf("encountered error while getting current branch version: %s", err)
8888
}
8989

9090
// Get the filesystem and index.yaml path for the repository
91-
dep.rootFs = filesystem.GetFilesystem(repoRoot)
91+
dep.rootFs = rootFs
9292

9393
// Check if the assets folder and Helm index file exists in the repository
9494
exists, err := filesystem.PathExists(dep.rootFs, path.RepositoryAssetsDir)
@@ -172,7 +172,7 @@ func (ld *Dependencies) removeAssetsVersions(debug bool) (map[string][]Asset, er
172172

173173
// Loop through the versions of the asset and remove the ones that are not in the lifecycle
174174
for _, asset := range assetsVersionsMap {
175-
isVersionInLifecycle := ld.vr.checkChartVersionForLifecycle(asset.version)
175+
isVersionInLifecycle := ld.VR.CheckChartVersionForLifecycle(asset.version)
176176
if isVersionInLifecycle {
177177
logrus.Debugf("Version %s is in lifecycle for %s", asset.version, chartName)
178178
continue // Skipping version in lifecycle

pkg/lifecycle/lifecycle_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func Test_removeAssetsVersions(t *testing.T) {
1818
checkIfGitIsCleanWrapper: func(debug bool) (bool, error) { return false, nil },
1919
gitAddAndCommitWrapper: func(message string) error { return nil },
2020
assetsVersionsMap: map[string][]Asset{"chart1": {{version: "999.0.0"}}},
21-
vr: vr,
21+
VR: vr,
2222
}
2323

2424
// Execute
@@ -41,7 +41,7 @@ func Test_removeAssetsVersions(t *testing.T) {
4141
},
4242
gitAddAndCommitWrapper: func(message string) error { return nil },
4343
assetsVersionsMap: map[string][]Asset{"chart1": {{version: "999.0.0"}}},
44-
vr: vr,
44+
VR: vr,
4545
}
4646

4747
// Execute
@@ -64,7 +64,7 @@ func Test_removeAssetsVersions(t *testing.T) {
6464
return fmt.Errorf("Some error at gitAddAndCommitWrapper")
6565
},
6666
assetsVersionsMap: map[string][]Asset{"chart1": {{version: "999.0.0"}}},
67-
vr: vr,
67+
VR: vr,
6868
}
6969

7070
// Execute
@@ -98,7 +98,7 @@ func Test_removeAssetsVersions(t *testing.T) {
9898
{version: "0.1.0"},
9999
},
100100
},
101-
vr: vr,
101+
VR: vr,
102102
}
103103

104104
// Execute
@@ -148,7 +148,7 @@ func Test_removeAssetsVersions(t *testing.T) {
148148
{version: "101.0.0"},
149149
},
150150
},
151-
vr: vr,
151+
VR: vr,
152152
}
153153

154154
// Execute
@@ -178,7 +178,7 @@ func Test_removeAssetsVersions(t *testing.T) {
178178
assetsVersionsMap: map[string][]Asset{
179179
"chart1": {},
180180
},
181-
vr: vr,
181+
VR: vr,
182182
}
183183

184184
// Execute

pkg/lifecycle/versions.rules.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func GetVersionRules(branchVersion string, debug bool) (*VersionRules, error) {
7676
// Branch can only hold until 2 previous versions of the current branch version.
7777
// Branch cannot hold versions from newer branches, only older ones.
7878
//
79-
// See checkChartVersionForLifecycle() for more details.
79+
// See CheckChartVersionForLifecycle() for more details.
8080
func (vr *VersionRules) getMinMaxVersionInts() {
8181
// e.g: 2.9 - 0.2 = 2.7
8282
minVersionStr := vr.rules[(vr.branchVersion - 0.2)].min
@@ -100,11 +100,17 @@ func convertBranchVersion(branchVersion string) (float32, error) {
100100
return float32(floatVersion), nil
101101
}
102102

103-
// checkChartVersionForLifecycle will
103+
// ExtractBranchVersion will extract the branch version from the branch name
104+
func ExtractBranchVersion(branch string) string {
105+
parts := strings.Split(branch, "-v")
106+
return parts[len(parts)-1]
107+
}
108+
109+
// CheckChartVersionForLifecycle will
104110
// Check if the chart version is within the range of the current version:
105111
//
106112
// If the chart version is within the range, return true, otherwise return false
107-
func (vr *VersionRules) checkChartVersionForLifecycle(chartVersion string) bool {
113+
func (vr *VersionRules) CheckChartVersionForLifecycle(chartVersion string) bool {
108114
chartVersionInt, _ := strconv.Atoi(strings.Split(chartVersion, ".")[0])
109115
/**
110116
Rule Example:

pkg/validate/validate.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/go-git/go-billy/v5"
99
"github.com/rancher/charts-build-scripts/pkg/filesystem"
10+
"github.com/rancher/charts-build-scripts/pkg/lifecycle"
1011
"github.com/rancher/charts-build-scripts/pkg/options"
1112
"github.com/rancher/charts-build-scripts/pkg/path"
1213
"github.com/rancher/charts-build-scripts/pkg/puller"
@@ -69,6 +70,13 @@ func CompareGeneratedAssets(repoFs billy.Filesystem, u options.UpstreamOptions,
6970
ModifiedPostRelease: options.ReleaseOptions{},
7071
RemovedPostRelease: options.ReleaseOptions{},
7172
}
73+
74+
// Initialize lifecycle package for validating with assets lifecycle rules
75+
lifeCycleDep, err := lifecycle.InitDependencies(repoFs, lifecycle.ExtractBranchVersion(branch), "", false)
76+
if err != nil {
77+
logrus.Fatalf("encountered error while initializing dependencies for lifecycle-assets-clean: %s", err)
78+
}
79+
7280
// Pull repository
7381
logrus.Infof("Pulling upstream repository %s at branch %s", u.URL, branch)
7482
releasedChartsRepoBranch, err := puller.GetGithubRepository(u, &branch)
@@ -88,6 +96,7 @@ func CompareGeneratedAssets(repoFs billy.Filesystem, u options.UpstreamOptions,
8896
if err := standardize.RestructureChartsAndAssets(releaseFs); err != nil {
8997
return response, fmt.Errorf("failed to standardize upstream: %s", err)
9098
}
99+
91100
// Walk through directories and execute release logic
92101
localOnly := func(fs billy.Filesystem, localPath string, isDir bool) error {
93102
if isDir {
@@ -111,7 +120,12 @@ func CompareGeneratedAssets(repoFs billy.Filesystem, u options.UpstreamOptions,
111120
}
112121
// Chart exists in local and is not tracked by release.yaml
113122
logrus.Infof("%s/%s is untracked", chart.Metadata.Name, chart.Metadata.Version)
114-
response.UntrackedInRelease = response.UntrackedInRelease.Append(chart.Metadata.Name, chart.Metadata.Version)
123+
// If the chart exists in local and not on the upstream it may have been removed by the lifecycle rules
124+
isVersionInLifecycle := lifeCycleDep.VR.CheckChartVersionForLifecycle(chart.Metadata.Version)
125+
if isVersionInLifecycle {
126+
// this chart should not be removed
127+
response.UntrackedInRelease = response.UntrackedInRelease.Append(chart.Metadata.Name, chart.Metadata.Version)
128+
}
115129
return nil
116130
}
117131

0 commit comments

Comments
 (0)