Skip to content

Commit

Permalink
works for all but 2-entry channels
Browse files Browse the repository at this point in the history
  • Loading branch information
grokspawn committed Nov 17, 2023
1 parent 79e93e1 commit 9073a6b
Showing 1 changed file with 51 additions and 64 deletions.
115 changes: 51 additions & 64 deletions alpha/template/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"fmt"
"io"
"slices"

Check failure on line 7 in alpha/template/semver/semver.go

View workflow job for this annotation

GitHub Actions / build

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.10/x64/src/slices)

Check failure on line 7 in alpha/template/semver/semver.go

View workflow job for this annotation

GitHub Actions / e2e

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.10/x64/src/slices)

Check failure on line 7 in alpha/template/semver/semver.go

View workflow job for this annotation

GitHub Actions / unit

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.10/x64/src/slices)

Check failure on line 7 in alpha/template/semver/semver.go

View workflow job for this annotation

GitHub Actions / build-linux

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.10/x64/src/slices)
"sort"

"github.com/blang/semver/v4"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/yaml"

"github.com/operator-framework/operator-registry/alpha/action"
Expand Down Expand Up @@ -288,77 +288,64 @@ func (sv *semverTemplate) generateChannels(semverChannels *bundleVersions) []dec
func (sv *semverTemplate) linkChannels(unlinkedChannels map[string]*declcfg.Channel, entries []entryTuple) []declcfg.Channel {
channels := []declcfg.Channel{}

// sort to force partitioning by archetype --> kind --> semver
sort.Slice(entries, func(i, j int) bool {
if channelPriorities[entries[i].arch] != channelPriorities[entries[j].arch] {
return channelPriorities[entries[i].arch] < channelPriorities[entries[j].arch]
parentChannels := []string{}
channelVersions := make(map[string][]semver.Version)

for e := range entries {
if _, ok := channelVersions[entries[e].parent]; !ok {
channelVersions[entries[e].parent] = []semver.Version{}
} else {
channelVersions[entries[e].parent] = append(channelVersions[entries[e].parent], entries[e].version)
}
if streamTypePriorities[entries[i].kind] != streamTypePriorities[entries[j].kind] {
return streamTypePriorities[entries[i].kind] < streamTypePriorities[entries[j].kind]
if !slices.Contains(parentChannels, entries[e].parent) {
parentChannels = append(parentChannels, entries[e].parent)
}
return entries[i].version.LT(entries[j].version)
})

prevZMax := ""
var curSkips sets.String = sets.NewString()

for index := 1; index < len(entries); index++ {
prevTuple := entries[index-1]
curTuple := entries[index]
prevX := getMajorVersion(prevTuple.version)
prevY := getMinorVersion(prevTuple.version)
curX := getMajorVersion(curTuple.version)
curY := getMinorVersion(curTuple.version)

archChange := curTuple.arch != prevTuple.arch
kindChange := curTuple.kind != prevTuple.kind
xChange := !prevX.EQ(curX)
yChange := !prevY.EQ(curY)

if archChange || kindChange || xChange || yChange {
// if we passed any kind of change besides Z, then we need to set skips/replaces for previous max-Z
prevChannel := unlinkedChannels[prevTuple.parent]
finalEntry := &prevChannel.Entries[prevTuple.index]
finalEntry.Replaces = prevZMax
// don't include replaces in skips list, but they are accumulated in discrete cycles (and maybe useful for later channels) so remove here
if curSkips.Has(finalEntry.Replaces) {
finalEntry.Skips = curSkips.Difference(sets.NewString(finalEntry.Replaces)).List()
} else {
finalEntry.Skips = curSkips.List()
}
sort.Slice(parentChannels, func(i, j int) bool { return parentChannels[i] < parentChannels[j] })
for _, p := range parentChannels {
versions := channelVersions[p]
slices.SortStableFunc(versions, func(a, b semver.Version) int {
if a.LT(b) {
return -1
}
}
if a.GT(b) {
return 1
}
return 0
})

if archChange || kindChange || xChange {
// we don't maintain skips/replaces over these transitions
curSkips = sets.NewString()
prevZMax = ""
} else {
if yChange {
prevZMax = prevTuple.name
// tracking 2 things:
// - the highest Z version before a Y-stream change
// - the beginning of new Y-stream
z, y := 0, 1

fmt.Printf(">>> examining channel %v, entries %v\n", p, len(versions))
for y < len(versions) {
for y < len(versions) {
if getMinorVersion(versions[y]).EQ(getMinorVersion(versions[z])) {
y += 1
} else {
break
}
}
if y < len(versions) {
// found a y step between z and y
// add replaces edge between y and y-1
fmt.Printf(">>> edge %v -- replaces --> %v\n", versions[y].String(), versions[y-1].String())
unlinkedChannels[p].Entries[y+1].Replaces = unlinkedChannels[p].Entries[y].Name
}
curSkips.Insert(prevTuple.name)
fmt.Printf(">>> accumulating skips from index %v (%v) to %v (%v)\n", z, unlinkedChannels[p].Entries[z].Name, y-1, unlinkedChannels[p].Entries[y-1].Name)
for z < y {
// add skips edges to y-1 from z < y
fmt.Printf(">>> edge %v -- skips --> %v\n", versions[y-1].String(), versions[z].String())
unlinkedChannels[p].Entries[y].Skips = append(unlinkedChannels[p].Entries[y].Skips, unlinkedChannels[p].Entries[z].Name)
z += 1
}
y += 2 // seek point for next y change
z += 1
}
}

// last entry accumulation
// for the last entry in
lastTuple := entries[len(entries)-1]
prevTuple := entries[len(entries)-2]
yChange := !getMinorVersion(lastTuple.version).EQ(getMinorVersion(prevTuple.version))
prevChannel := unlinkedChannels[lastTuple.parent]
finalEntry := &prevChannel.Entries[lastTuple.index]

finalEntry.Replaces = prevZMax
if yChange {
curSkips = sets.NewString()
}
// don't include replaces in skips list, but they are accumulated in discrete cycles (and maybe useful for later channels) so remove here
if curSkips.Has(finalEntry.Replaces) {
finalEntry.Skips = curSkips.Difference(sets.NewString(finalEntry.Replaces)).List()
} else {
finalEntry.Skips = curSkips.List()
}

for _, ch := range unlinkedChannels {
channels = append(channels, *ch)
}
Expand Down

0 comments on commit 9073a6b

Please sign in to comment.