Skip to content

Commit 6ec440c

Browse files
Merge pull request #503 from njhale/fix-bulk-add
Bug 1885425: fix(indexing): order bulk add by version field
2 parents bc3d300 + 8096517 commit 6ec440c

File tree

25 files changed

+1382
-303
lines changed

25 files changed

+1382
-303
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

cmd/opm/index/add.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var (
1717
Add operator bundles to an index.
1818
1919
This command will add the given set of bundle images (specified by the --bundles option) to an index image (provided by the --from-index option).
20+
21+
If multiple bundles are given with '--mode=replaces' (the default), bundles are added to the index by order of ascending (semver) version unless the update graph specified by replaces requires a different input order; e.g. 1.0.0 replaces 1.0.1 would result in [1.0.1, 1.0.0] instead of the [1.0.0, 1.0.1] normally expected of semver. However, for most cases (e.g. 1.0.1 replaces 1.0.0) the bundle with the highest version is used to set the default channel of the related package.
2022
`)
2123

2224
addExample = templates.Examples(`

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ require (
5757
k8s.io/api v0.19.3
5858
k8s.io/apiextensions-apiserver v0.19.3
5959
k8s.io/apimachinery v0.19.3
60+
k8s.io/apiserver v0.19.3
6061
k8s.io/client-go v0.19.3
6162
k8s.io/klog v1.0.0
6263
k8s.io/kubectl v0.18.0

pkg/containertools/containertoolsfakes/fake_command_runner.go

Lines changed: 0 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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/indexer/indexerfakes/fake_index_exporter.go

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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) {

0 commit comments

Comments
 (0)