Skip to content

Commit

Permalink
Merge pull request #465 from dinhxuanvu/semver-default
Browse files Browse the repository at this point in the history
Bug 1883377: Enable default channel inference for the first bundle of the package
  • Loading branch information
openshift-merge-robot committed Oct 1, 2020
2 parents 9ba9df8 + 454dfed commit f6e5d92
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
13 changes: 12 additions & 1 deletion pkg/registry/bundlegraphloader.go
Expand Up @@ -13,7 +13,7 @@ type BundleGraphLoader struct {

// AddBundleToGraph takes a bundle and an existing graph and updates the graph to insert the new bundle
// into each channel it is included in
func (g *BundleGraphLoader) AddBundleToGraph(bundle *Bundle, graph *Package, newDefaultChannel string, skippatch bool) (*Package, error) {
func (g *BundleGraphLoader) AddBundleToGraph(bundle *Bundle, graph *Package, annotations *AnnotationsFile, skippatch bool) (*Package, error) {
bundleVersion, err := bundle.Version()
if err != nil {
return nil, fmt.Errorf("Unable to extract bundle version from bundle %s, can't insert in semver mode", bundle.BundleImage)
Expand All @@ -34,10 +34,21 @@ func (g *BundleGraphLoader) AddBundleToGraph(bundle *Bundle, graph *Package, new
if graph.Name == "" {
graph.Name = bundle.Package
}

newDefaultChannel := annotations.Annotations.DefaultChannelName
if newDefaultChannel != "" {
graph.DefaultChannel = newDefaultChannel
}

if graph.DefaultChannel == "" {
// Infer default channel from channel list
if annotations.SelectDefaultChannel() != "" {
graph.DefaultChannel = annotations.SelectDefaultChannel()
} else {
return nil, fmt.Errorf("Default channel is missing and can't be inferred")
}
}

// generate the DAG for each channel the new bundle is being insert into
for _, channel := range bundle.Channels {
replaces := make(map[BundleKey]struct{}, 0)
Expand Down
40 changes: 22 additions & 18 deletions pkg/registry/bundlegraphloader_test.go
Expand Up @@ -9,14 +9,18 @@ import (
)

func TestBundleGraphLoader(t *testing.T) {
empty := &AnnotationsFile{}
alpha := &AnnotationsFile{}
alpha.Annotations.DefaultChannelName = "alpha"

tests := []struct {
name string
fail bool
graph Package
bundle Bundle
newDefaultChannel string
expectedGraph *Package
skipPatch bool
name string
fail bool
graph Package
bundle Bundle
annotations *AnnotationsFile
expectedGraph *Package
skipPatch bool
}{
{
name: "Add bundle to head of channels",
Expand Down Expand Up @@ -88,7 +92,7 @@ func TestBundleGraphLoader(t *testing.T) {
}},
},
},
newDefaultChannel: "",
annotations: empty,
},
{
name: "Add a bundle already in the graph, expect an error",
Expand All @@ -108,13 +112,13 @@ func TestBundleGraphLoader(t *testing.T) {
Package: "etcd",
csv: &ClusterServiceVersion{
Spec: json.RawMessage(`
{
{
"version": "0.6.1"
}`),
},
Channels: []string{"beta"},
},
newDefaultChannel: "",
annotations: empty,
},
{
name: "Add a bundle behind the head of a channel",
Expand All @@ -134,7 +138,7 @@ func TestBundleGraphLoader(t *testing.T) {
Package: "etcd",
csv: &ClusterServiceVersion{
Spec: json.RawMessage(`
{
{
"version": "0.6.1"
}`),
},
Expand All @@ -151,7 +155,7 @@ func TestBundleGraphLoader(t *testing.T) {
}},
},
},
newDefaultChannel: "",
annotations: empty,
},
{
name: "Add a bundle to a new channel",
Expand All @@ -171,7 +175,7 @@ func TestBundleGraphLoader(t *testing.T) {
Package: "etcd",
csv: &ClusterServiceVersion{
Spec: json.RawMessage(`
{
{
"version": "0.9.3"
}`),
},
Expand All @@ -191,7 +195,7 @@ func TestBundleGraphLoader(t *testing.T) {
}},
},
},
newDefaultChannel: "alpha",
annotations: alpha,
},
{
name: "Add a bundle to an empty graph",
Expand All @@ -202,7 +206,7 @@ func TestBundleGraphLoader(t *testing.T) {
Package: "etcd",
csv: &ClusterServiceVersion{
Spec: json.RawMessage(`
{
{
"version": "0.9.3"
}`),
},
Expand All @@ -218,7 +222,7 @@ func TestBundleGraphLoader(t *testing.T) {
}},
},
},
newDefaultChannel: "alpha",
annotations: alpha,
},
{
name: "Add a bundle in skippatch mode",
Expand Down Expand Up @@ -289,15 +293,15 @@ func TestBundleGraphLoader(t *testing.T) {
}},
},
},
newDefaultChannel: "",
annotations: empty,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
graphLoader := BundleGraphLoader{}

newGraph, err := graphLoader.AddBundleToGraph(&tt.bundle, &tt.graph, tt.newDefaultChannel, tt.skipPatch)
newGraph, err := graphLoader.AddBundleToGraph(&tt.bundle, &tt.graph, tt.annotations, tt.skipPatch)
if tt.fail {
assert.Error(t, err)
return
Expand Down
8 changes: 4 additions & 4 deletions pkg/registry/populator.go
Expand Up @@ -307,7 +307,7 @@ func (i *DirectoryPopulator) loadManifestsSemver(bundle *Bundle, annotations *An

// add to the graph
bundleLoader := BundleGraphLoader{}
updatedGraph, err := bundleLoader.AddBundleToGraph(bundle, graph, annotations.Annotations.DefaultChannelName, skippatch)
updatedGraph, err := bundleLoader.AddBundleToGraph(bundle, graph, annotations, skippatch)
if err != nil {
return err
}
Expand Down Expand Up @@ -461,9 +461,9 @@ func (i *DirectoryPopulator) translateAnnotationsIntoPackage(annotations *Annota
if pkgm != nil {
manifest.DefaultChannelName = pkgm.GetDefaultChannel()
} else {
// Infer default channel if only one channel is provided
if len(annotations.GetChannels()) == 1 {
manifest.DefaultChannelName = annotations.GetChannels()[0]
// Infer default channel from channel list
if annotations.SelectDefaultChannel() != "" {
manifest.DefaultChannelName = annotations.SelectDefaultChannel()
} else {
return manifest, fmt.Errorf("Default channel is missing and can't be inferred")
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/registry/types.go
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"

"github.com/blang/semver"
Expand Down Expand Up @@ -344,3 +345,15 @@ func (a *AnnotationsFile) GetChannels() []string {
func (a *AnnotationsFile) GetDefaultChannelName() string {
return a.Annotations.DefaultChannelName
}

// SelectDefaultChannel returns the first item in channel list that is sorted
// in lexicographic order.
func (a *AnnotationsFile) SelectDefaultChannel() string {
if a.Annotations.Channels != "" {
channels := strings.Split(a.Annotations.Channels, ",")
sort.Strings(channels)
return channels[0]
}

return ""
}

0 comments on commit f6e5d92

Please sign in to comment.