Skip to content

Commit

Permalink
Include mixin version when sorting mxins for the bundle stamp
Browse files Browse the repository at this point in the history
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 <me@carolynvanslyck.com>
  • Loading branch information
carolynvs committed Apr 12, 2023
1 parent c59a360 commit 41cc316
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
21 changes: 19 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,22 @@ 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 by name only
iVersion, iErr := semver.NewVersion(iRecord.Version)
jVersion, jErr := semver.NewVersion(jRecord.Version)
if iErr == nil && jErr == nil {
return iVersion.LessThan(jVersion)
}
}

return iRecord.Name < jRecord.Name
}

func (m MixinRecords) Swap(i, j int) {
Expand Down Expand Up @@ -173,7 +189,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
26 changes: 22 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,32 @@ 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"},
}

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: "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)
}

0 comments on commit 41cc316

Please sign in to comment.