Skip to content

Commit

Permalink
Merge pull request #503 from njhale/fix-bulk-add
Browse files Browse the repository at this point in the history
Bug 1885425: fix(indexing): order bulk add by version field
  • Loading branch information
openshift-merge-robot committed Nov 17, 2020
2 parents bc3d300 + 8096517 commit 6ec440c
Show file tree
Hide file tree
Showing 25 changed files with 1,382 additions and 303 deletions.
Expand Up @@ -154,7 +154,7 @@ spec:
cpu: 100m
memory: 50Mi
maturity: alpha
version: 0.14.0
version: 0.14.0-beta
customresourcedefinitions:
owned:
- name: prometheuses.monitoring.coreos.com
Expand Down
2 changes: 2 additions & 0 deletions cmd/opm/index/add.go
Expand Up @@ -17,6 +17,8 @@ var (
Add operator bundles to an index.
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).
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.
`)

addExample = templates.Examples(`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -57,6 +57,7 @@ require (
k8s.io/api v0.19.3
k8s.io/apiextensions-apiserver v0.19.3
k8s.io/apimachinery v0.19.3
k8s.io/apiserver v0.19.3
k8s.io/client-go v0.19.3
k8s.io/klog v1.0.0
k8s.io/kubectl v0.18.0
Expand Down
75 changes: 0 additions & 75 deletions pkg/containertools/containertoolsfakes/fake_command_runner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/lib/bundle/validate.go
Expand Up @@ -365,7 +365,7 @@ func (i imageValidator) ValidateBundleContent(manifestDir string) error {

// Validate the bundle object
if len(unstObjs) > 0 {
bundle := registry.NewBundle(csvName, "", nil, unstObjs...)
bundle := registry.NewBundle(csvName, &registry.Annotations{}, unstObjs...)
bundleValidator := validation.BundleValidator
results := bundleValidator.Validate(bundle)
if len(results) > 0 {
Expand Down
110 changes: 110 additions & 0 deletions pkg/lib/indexer/indexerfakes/fake_index_exporter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/lib/validation/bundle_test.go
Expand Up @@ -60,7 +60,7 @@ func TestValidateBundle(t *testing.T) {
}

// Validate the bundle object
bundle := registry.NewBundle("test", "", nil, unstObjs...)
bundle := registry.NewBundle("test", &registry.Annotations{}, unstObjs...)
results := BundleValidator.Validate(bundle)

if len(results) > 0 {
Expand Down
65 changes: 53 additions & 12 deletions pkg/registry/bundle.go
@@ -1,6 +1,7 @@
package registry

import (
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -41,33 +42,59 @@ type Bundle struct {
Package string
Channels []string
BundleImage string
version string
csv *ClusterServiceVersion
v1beta1crds []*apiextensionsv1beta1.CustomResourceDefinition
v1crds []*apiextensionsv1.CustomResourceDefinition
Dependencies []*Dependency
Properties []*Property
Annotations *Annotations
cacheStale bool
}

func NewBundle(name, pkgName string, channels []string, objs ...*unstructured.Unstructured) *Bundle {
bundle := &Bundle{Name: name, Package: pkgName, Channels: channels, cacheStale: false}
func NewBundle(name string, annotations *Annotations, objs ...*unstructured.Unstructured) *Bundle {
bundle := &Bundle{
Name: name,
Package: annotations.PackageName,
Annotations: annotations,
}
for _, o := range objs {
bundle.Add(o)
}

if annotations == nil {
return bundle
}
bundle.Channels = strings.Split(annotations.Channels, ",")

return bundle
}

func NewBundleFromStrings(name, pkgName string, channels []string, objs []string) (*Bundle, error) {
func NewBundleFromStrings(name, version, pkg, defaultChannel, channels, objs string) (*Bundle, error) {
objStrs, err := BundleStringToObjectStrings(objs)
if err != nil {
return nil, err
}

unstObjs := []*unstructured.Unstructured{}
for _, o := range objs {
for _, o := range objStrs {
dec := yaml.NewYAMLOrJSONDecoder(strings.NewReader(o), 10)
unst := &unstructured.Unstructured{}
if err := dec.Decode(unst); err != nil {
return nil, err
}
unstObjs = append(unstObjs, unst)
}
return NewBundle(name, pkgName, channels, unstObjs...), nil

annotations := &Annotations{
PackageName: pkg,
Channels: channels,
DefaultChannelName: defaultChannel,
}
bundle := NewBundle(name, annotations, unstObjs...)
bundle.version = version

return bundle, nil
}

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

func (b *Bundle) Version() (string, error) {
if err := b.cache(); err != nil {
if b.version != "" {
return b.version, nil
}

var err error
if err = b.cache(); err != nil {
return "", err
}
return b.csv.GetVersion()

if b.csv != nil {
b.version, err = b.csv.GetVersion()
}

return b.version, err
}

func (b *Bundle) SkipRange() (string, error) {
Expand Down Expand Up @@ -226,29 +263,33 @@ func (b *Bundle) AllProvidedAPIsInBundle() error {
return nil
}

func (b *Bundle) Serialize() (csvName, bundleImage string, csvBytes []byte, bundleBytes []byte, err error) {
func (b *Bundle) Serialize() (csvName, bundleImage string, csvBytes []byte, bundleBytes []byte, annotationBytes []byte, err error) {
csvCount := 0
for _, obj := range b.Objects {
objBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
if err != nil {
return "", "", nil, nil, err
return "", "", nil, nil, nil, err
}
bundleBytes = append(bundleBytes, objBytes...)

if obj.GroupVersionKind().Kind == "ClusterServiceVersion" {
csvName = obj.GetName()
csvBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
if err != nil {
return "", "", nil, nil, err
return "", "", nil, nil, nil, err
}
csvCount += 1
if csvCount > 1 {
return "", "", nil, nil, fmt.Errorf("two csvs found in one bundle")
return "", "", nil, nil, nil, fmt.Errorf("two csvs found in one bundle")
}
}
}

return csvName, b.BundleImage, csvBytes, bundleBytes, nil
if b.Annotations != nil {
annotationBytes, err = json.Marshal(b.Annotations)
}

return csvName, b.BundleImage, csvBytes, bundleBytes, annotationBytes, nil
}

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

0 comments on commit 6ec440c

Please sign in to comment.