Skip to content

Commit 8096517

Browse files
committed
fix(indexing): pick channel heads by semver
On index add in replaces mode, pick the highest bundle version in each channel as its head; take the default channel from the highest bundle version in the entire package.
1 parent 396ca5b commit 8096517

File tree

15 files changed

+577
-98
lines changed

15 files changed

+577
-98
lines changed

bundles/prometheus.0.14.0-beta/manifests/prometheusoperator.0.14.0.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ spec:
154154
cpu: 100m
155155
memory: 50Mi
156156
maturity: alpha
157-
version: 0.14.0
157+
version: 0.14.0-beta
158158
customresourcedefinitions:
159159
owned:
160160
- name: prometheuses.monitoring.coreos.com

pkg/lib/bundle/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func (i imageValidator) ValidateBundleContent(manifestDir string) error {
365365

366366
// Validate the bundle object
367367
if len(unstObjs) > 0 {
368-
bundle := registry.NewBundle(csvName, "", nil, unstObjs...)
368+
bundle := registry.NewBundle(csvName, &registry.Annotations{}, unstObjs...)
369369
bundleValidator := validation.BundleValidator
370370
results := bundleValidator.Validate(bundle)
371371
if len(results) > 0 {

pkg/lib/validation/bundle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestValidateBundle(t *testing.T) {
6060
}
6161

6262
// Validate the bundle object
63-
bundle := registry.NewBundle("test", "", nil, unstObjs...)
63+
bundle := registry.NewBundle("test", &registry.Annotations{}, unstObjs...)
6464
results := BundleValidator.Validate(bundle)
6565

6666
if len(results) > 0 {

pkg/registry/bundle.go

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package registry
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"strings"
67

@@ -41,33 +42,59 @@ type Bundle struct {
4142
Package string
4243
Channels []string
4344
BundleImage string
45+
version string
4446
csv *ClusterServiceVersion
4547
v1beta1crds []*apiextensionsv1beta1.CustomResourceDefinition
4648
v1crds []*apiextensionsv1.CustomResourceDefinition
4749
Dependencies []*Dependency
4850
Properties []*Property
51+
Annotations *Annotations
4952
cacheStale bool
5053
}
5154

52-
func NewBundle(name, pkgName string, channels []string, objs ...*unstructured.Unstructured) *Bundle {
53-
bundle := &Bundle{Name: name, Package: pkgName, Channels: channels, cacheStale: false}
55+
func NewBundle(name string, annotations *Annotations, objs ...*unstructured.Unstructured) *Bundle {
56+
bundle := &Bundle{
57+
Name: name,
58+
Package: annotations.PackageName,
59+
Annotations: annotations,
60+
}
5461
for _, o := range objs {
5562
bundle.Add(o)
5663
}
64+
65+
if annotations == nil {
66+
return bundle
67+
}
68+
bundle.Channels = strings.Split(annotations.Channels, ",")
69+
5770
return bundle
5871
}
5972

60-
func NewBundleFromStrings(name, pkgName string, channels []string, objs []string) (*Bundle, error) {
73+
func NewBundleFromStrings(name, version, pkg, defaultChannel, channels, objs string) (*Bundle, error) {
74+
objStrs, err := BundleStringToObjectStrings(objs)
75+
if err != nil {
76+
return nil, err
77+
}
78+
6179
unstObjs := []*unstructured.Unstructured{}
62-
for _, o := range objs {
80+
for _, o := range objStrs {
6381
dec := yaml.NewYAMLOrJSONDecoder(strings.NewReader(o), 10)
6482
unst := &unstructured.Unstructured{}
6583
if err := dec.Decode(unst); err != nil {
6684
return nil, err
6785
}
6886
unstObjs = append(unstObjs, unst)
6987
}
70-
return NewBundle(name, pkgName, channels, unstObjs...), nil
88+
89+
annotations := &Annotations{
90+
PackageName: pkg,
91+
Channels: channels,
92+
DefaultChannelName: defaultChannel,
93+
}
94+
bundle := NewBundle(name, annotations, unstObjs...)
95+
bundle.version = version
96+
97+
return bundle, nil
7198
}
7299

73100
func (b *Bundle) Size() int {
@@ -86,10 +113,20 @@ func (b *Bundle) ClusterServiceVersion() (*ClusterServiceVersion, error) {
86113
}
87114

88115
func (b *Bundle) Version() (string, error) {
89-
if err := b.cache(); err != nil {
116+
if b.version != "" {
117+
return b.version, nil
118+
}
119+
120+
var err error
121+
if err = b.cache(); err != nil {
90122
return "", err
91123
}
92-
return b.csv.GetVersion()
124+
125+
if b.csv != nil {
126+
b.version, err = b.csv.GetVersion()
127+
}
128+
129+
return b.version, err
93130
}
94131

95132
func (b *Bundle) SkipRange() (string, error) {
@@ -226,29 +263,33 @@ func (b *Bundle) AllProvidedAPIsInBundle() error {
226263
return nil
227264
}
228265

229-
func (b *Bundle) Serialize() (csvName, bundleImage string, csvBytes []byte, bundleBytes []byte, err error) {
266+
func (b *Bundle) Serialize() (csvName, bundleImage string, csvBytes []byte, bundleBytes []byte, annotationBytes []byte, err error) {
230267
csvCount := 0
231268
for _, obj := range b.Objects {
232269
objBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
233270
if err != nil {
234-
return "", "", nil, nil, err
271+
return "", "", nil, nil, nil, err
235272
}
236273
bundleBytes = append(bundleBytes, objBytes...)
237274

238275
if obj.GroupVersionKind().Kind == "ClusterServiceVersion" {
239276
csvName = obj.GetName()
240277
csvBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
241278
if err != nil {
242-
return "", "", nil, nil, err
279+
return "", "", nil, nil, nil, err
243280
}
244281
csvCount += 1
245282
if csvCount > 1 {
246-
return "", "", nil, nil, fmt.Errorf("two csvs found in one bundle")
283+
return "", "", nil, nil, nil, fmt.Errorf("two csvs found in one bundle")
247284
}
248285
}
249286
}
250287

251-
return csvName, b.BundleImage, csvBytes, bundleBytes, nil
288+
if b.Annotations != nil {
289+
annotationBytes, err = json.Marshal(b.Annotations)
290+
}
291+
292+
return csvName, b.BundleImage, csvBytes, bundleBytes, annotationBytes, nil
252293
}
253294

254295
func (b *Bundle) Images() (map[string]struct{}, error) {

pkg/registry/bundle_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package registry
22

33
import (
44
"io/ioutil"
5-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
6-
"k8s.io/apimachinery/pkg/runtime/serializer"
7-
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
85
"path/filepath"
96
"reflect"
107
"strings"
118
"testing"
129

10+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11+
"k8s.io/apimachinery/pkg/runtime/serializer"
12+
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
13+
1314
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1415
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
1516
)
@@ -22,7 +23,10 @@ const (
2223
// The provided APIs and CRD objects in the created bundle are compared to those in a test manifest directory.
2324
func TestV1CRDsInBundle(t *testing.T) {
2425
// create bundle from manifests that include a v1 CRD
25-
bundle := NewBundle("test", "lib-bucket-provisioner", []string{"alpha"})
26+
bundle := NewBundle("test", &Annotations{
27+
PackageName: "lib-bucket-provisioner",
28+
Channels: "alpha",
29+
})
2630

2731
// Read all files in manifests directory
2832
items, err := ioutil.ReadDir(manifestDir)

pkg/registry/decode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
78
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
89
"k8s.io/apimachinery/pkg/util/yaml"
910
)

pkg/registry/empty.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func (EmptyQuery) GetBundlePathIfExists(ctx context.Context, csvName string) (bu
108108
return "", errors.New("empty querier: cannot get bundle path for bundle")
109109
}
110110

111+
func (EmptyQuery) ListRegistryBundles(ctx context.Context) ([]*Bundle, error) {
112+
return nil, errors.New("empty querier: cannot list registry bundles")
113+
}
114+
111115
var _ Query = &EmptyQuery{}
112116

113117
func NewEmptyQuerier() *EmptyQuery {

pkg/registry/imageinput.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (i *ImageInput) getBundleFromManifests() error {
115115
bundle.Dependencies = i.dependenciesFile.GetDependencies()
116116

117117
bundle.Name = csvName
118+
bundle.Annotations = &i.AnnotationsFile.Annotations
118119
bundle.Package = i.AnnotationsFile.Annotations.PackageName
119120
bundle.Channels = strings.Split(i.AnnotationsFile.Annotations.Channels, ",")
120121

pkg/registry/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type Query interface {
5959
GetDependenciesForBundle(ctx context.Context, name, version, path string) (dependencies []*api.Dependency, err error)
6060
// Get the bundle path if it exists
6161
GetBundlePathIfExists(ctx context.Context, csvName string) (string, error)
62+
// ListRegistryBundles returns a set of registry bundles.
63+
ListRegistryBundles(ctx context.Context) ([]*Bundle, error)
6264
}
6365

6466
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . GraphLoader

0 commit comments

Comments
 (0)