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

Fix inconsistent rebuild #2719

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Include mixin version when sorting mxins for the bundle stamp
When sorting mixins for the bundle stamp, sort by the mixin name and also include the mixin version. Currently bundles can only have a single version of a mixin, so the version comparison is for the future, after we have mixins as bundles, where it could be possible perhaps to use two different versions of a mixin in the same bundle, because they will be referenced images instead of embedded binaries.

Make sure to sort the versions by semantic version and not string, so that the leading v prefix is ignored and stuff like pre-releases and 0.0.10 and 0.0.1 sort correctly.

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs committed Apr 12, 2023
commit d749dde64de7f1baf2f923514ed7439662b15438
23 changes: 21 additions & 2 deletions pkg/cnab/config-adapter/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/portercontext"
"get.porter.sh/porter/pkg/tracing"
"github.com/Masterminds/semver/v3"
)

// Stamp contains Porter specific metadata about a bundle that we can place
Expand Down Expand Up @@ -86,7 +87,24 @@ func (m MixinRecords) Len() int {
}

func (m MixinRecords) Less(i, j int) bool {
return m[i].Name < m[j].Name
// Currently there can only be a single version of a mixin used in a bundle
// I'm considering version as well for sorting in case that changes in the future once mixins are bundles
// referenced by a bundle, and not embedded binaries
iRecord := m[i]
jRecord := m[j]
if iRecord.Name == jRecord.Name {
// Try to sort by the mixin's semantic version
// If it doesn't parse, just fall through and sort as a string instead
iVersion, iErr := semver.NewVersion(iRecord.Version)
jVersion, jErr := semver.NewVersion(jRecord.Version)
if iErr == nil && jErr == nil {
return iVersion.LessThan(jVersion)
} else {
return iRecord.Version < jRecord.Version
}
}

return iRecord.Name < jRecord.Name
}

func (m MixinRecords) Swap(i, j int) {
Expand Down Expand Up @@ -173,7 +191,8 @@ func LoadStamp(bun cnab.ExtendedBundle) (Stamp, error) {
return stamp, nil
}

// getUsedMixinRecords compare the mixins defined in the manifest and the ones installed and then retrieve the mixin's version info
// getUsedMixinRecords returns a list of the mixins used by the bundle, including
// information about the installed mixin, such as its version.
func (c *ManifestConverter) getUsedMixinRecords() MixinRecords {
usedMixins := make(MixinRecords, 0)

Expand Down
43 changes: 39 additions & 4 deletions pkg/cnab/config-adapter/stamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,49 @@ func TestConfig_GenerateStamp_IncludeVersion(t *testing.T) {

func TestMixinRecord_Sort(t *testing.T) {
records := MixinRecords{
{Name: "helm", Version: "0.1.2"},
{Name: "helm", Version: "0.1.13"},
{Name: "helm", Version: "v0.1.2"},
{Name: "testmixin", Version: "1.2.3"},
{Name: "exec", Version: "2.1.0"},
// These won't parse as valid semver, so just sort them by the string representation instead
{
Name: "az",
Version: "invalid-version2",
},
{
Name: "az",
Version: "invalid-version1",
},
}

sort.Sort(records)

assert.Equal(t, "exec", records[0].Name)
assert.Equal(t, "helm", records[1].Name)
assert.Equal(t, "testmixin", records[2].Name)
wantRecords := MixinRecords{
{
Name: "az",
Version: "invalid-version1",
},
{
Name: "az",
Version: "invalid-version2",
},
{
Name: "exec",
Version: "2.1.0",
},
{
Name: "helm",
Version: "v0.1.2",
},
{
Name: "helm",
Version: "0.1.13",
},
{
Name: "testmixin",
Version: "1.2.3",
},
}

assert.Equal(t, wantRecords, records)
}