Skip to content

Commit

Permalink
Merge pull request #201 from Ajpantuso/apantuso/quarantine_opm
Browse files Browse the repository at this point in the history
fix: quarantine opm apis
  • Loading branch information
Ajpantuso authored Sep 29, 2022
2 parents 4fe327b + 186b820 commit 13f8b0a
Show file tree
Hide file tree
Showing 24 changed files with 358 additions and 398 deletions.
8 changes: 3 additions & 5 deletions cmd/mtcli/list/bundles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ func run(cmd *cobra.Command, args []string) error {

var operatorVersionedNames []string
for _, bundle := range allBundles {
csv, err := bundle.ClusterServiceVersion()
if err != nil {
return fmt.Errorf("extracting version info for bundle %q: %w", bundle.Name, err)
}
operatorVersionedNames = append(operatorVersionedNames, csv.GetName())
csv := bundle.ClusterServiceVersion

operatorVersionedNames = append(operatorVersionedNames, csv.Name)
}

fmt.Fprintln(os.Stdout, strings.Join(operatorVersionedNames, "\n"))
Expand Down
85 changes: 15 additions & 70 deletions pkg/extractor/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import (
"strings"
"time"

"github.com/mt-sre/addon-metadata-operator/pkg/operator"
"github.com/operator-framework/operator-registry/pkg/image"
"github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
opmbundle "github.com/operator-framework/operator-registry/pkg/lib/bundle"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)

// BundleCache provides a cache of OPM bundles which are referenced by
Expand All @@ -26,10 +23,10 @@ type BundleCache interface {
// GetBundle returns a bundle for the given image. An error is
// returned if the bundle cannot be retrieved or the data is
// corrupted.
GetBundle(img string) (*registry.Bundle, error)
GetBundle(img string) (*operator.Bundle, error)
// SetBundle caches a bundle for the given image. An error
// is returned if the bundle cannot be cached.
SetBundle(img string, bundle registry.Bundle) error
SetBundle(img string, bundle operator.Bundle) error
}

type DefaultBundleExtractor struct {
Expand Down Expand Up @@ -80,39 +77,40 @@ func WithBundleTimeout(timeout time.Duration) BundleExtractorOpt {
}
}

func (e *DefaultBundleExtractor) Extract(ctx context.Context, bundleImage string) (*registry.Bundle, error) {
bundle, err := e.Cache.GetBundle(bundleImage)
func (e *DefaultBundleExtractor) Extract(ctx context.Context, bundleImage string) (operator.Bundle, error) {
cachedBundle, err := e.Cache.GetBundle(bundleImage)
if err != nil {
e.Log.Warnf("retrieving bundle %q from cache: %w", bundleImage, err)
}

if bundle != nil {
e.Log.Debugf("cache hit for '%s'", bundleImage)
return bundle, nil
if cachedBundle != nil {
e.Log.Debugf("cache hit for %q", bundleImage)
return *cachedBundle, nil
}

e.Log.Debugf("cache miss for '%s'", bundleImage)
tmpDirs, err := createTempDirs()
if err != nil {
return nil, err
return operator.Bundle{}, err
}
defer func() {
if err := tmpDirs.CleanUp(); err != nil {
e.Log.Errorf("failed to cleanup tmpDirs: %w", err)
e.Log.Errorf("cleaning up tmpDirs: %w", err)
}
}()

if err := e.unpackAndValidateBundle(ctx, bundleImage, tmpDirs); err != nil {
return nil, fmt.Errorf("failed to unpack and validate bundle: %w", err)
return operator.Bundle{}, fmt.Errorf("unpacking and validating bundle: %w", err)
}

bundle, err = e.loadBundle(tmpDirs["bundle"])
bundle, err := operator.NewBundleFromDirectory(tmpDirs["bundle"])
if err != nil {
return nil, err
return operator.Bundle{}, err
}

bundle.BundleImage = bundleImage // not set by OPM

if err := e.Cache.SetBundle(bundleImage, *bundle); err != nil {
if err := e.Cache.SetBundle(bundleImage, bundle); err != nil {
e.Log.Warnf("caching bundle %q: %w", bundleImage, err)
}

Expand Down Expand Up @@ -173,59 +171,6 @@ func (e *DefaultBundleExtractor) ValidateBundle(registry *containerdregistry.Reg
return nil
}

func (e *DefaultBundleExtractor) loadBundle(tmpDir string) (*registry.Bundle, error) {
e.Log.Debugf("loading the bundle from tmpDir: %s", tmpDir)
unstObjs, err := readAllManifests(filepath.Join(tmpDir, opmbundle.ManifestsDir))
if err != nil {
return nil, err
}
annotations, err := readAnnotations(filepath.Join(tmpDir, opmbundle.MetadataDir))
if err != nil {
return nil, err
}
return registry.NewBundle(annotations.PackageName, annotations, unstObjs...), nil
}

func readAllManifests(manifestsDir string) ([]*unstructured.Unstructured, error) {
unstObjs := []*unstructured.Unstructured{}
items, err := ioutil.ReadDir(manifestsDir)
if err != nil {
return nil, err
}

for _, item := range items {
path := filepath.Join(manifestsDir, item.Name())
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("unable to read file %s, got %w", path, err)
}

dec := k8syaml.NewYAMLOrJSONDecoder(strings.NewReader(string(data)), 30)
k8sFile := &unstructured.Unstructured{}
if err = dec.Decode(k8sFile); err != nil {
return nil, fmt.Errorf("unable to decode file %s, got %w", path, err)
}

unstObjs = append(unstObjs, k8sFile)
}
return unstObjs, nil
}

func readAnnotations(metadataDir string) (*registry.Annotations, error) {
path := filepath.Join(metadataDir, opmbundle.AnnotationsFile)
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("unable to read file '%s': %w", path, err)
}

var annotationsFile registry.AnnotationsFile
if err = yaml.Unmarshal(content, &annotationsFile); err != nil {
return nil, fmt.Errorf("unable to unmarshal file '%s': %w", path, err)
}

return &annotationsFile.Annotations, nil
}

type tempDirs map[string]string

func createTempDirs() (tempDirs, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/extractor/bundle_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/mt-sre/addon-metadata-operator/pkg/operator"
)

// NewBundleCacheImpl returns an initialized BundleCacheImpl. A
Expand All @@ -27,21 +27,21 @@ type BundleCacheImpl struct {

var ErrInvalidBundleData = errors.New("invalid bundle data")

func (c *BundleCacheImpl) GetBundle(img string) (*registry.Bundle, error) {
func (c *BundleCacheImpl) GetBundle(img string) (*operator.Bundle, error) {
data, ok := c.cfg.Store.Read(img)
if !ok {
return nil, nil
}

bundle, ok := data.(registry.Bundle)
bundle, ok := data.(operator.Bundle)
if !ok {
return nil, ErrInvalidBundleData
}

return &bundle, nil
}

func (c *BundleCacheImpl) SetBundle(img string, bundle registry.Bundle) error {
func (c *BundleCacheImpl) SetBundle(img string, bundle operator.Bundle) error {
if err := c.cfg.Store.Write(img, bundle); err != nil {
return fmt.Errorf("writing bundle data: %w", err)
}
Expand Down
17 changes: 5 additions & 12 deletions pkg/extractor/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/mt-sre/addon-metadata-operator/pkg/operator"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestDefaultBundleExtractor(t *testing.T) {
cachedBundle, err := cache.GetBundle(tc.BundleImage)
require.NoError(t, err)

tc.AssertExpectations(t, cachedBundle)
tc.AssertExpectations(t, *cachedBundle)
})
}
}
Expand All @@ -67,22 +67,15 @@ type testCase struct {
ExpectedCSVVersion string
}

func (tc testCase) AssertExpectations(t *testing.T, b *registry.Bundle) {
func (tc testCase) AssertExpectations(t *testing.T, b operator.Bundle) {
t.Helper()

assert.Equal(t, b.Name, tc.ExpectedPackageName)
assert.Equal(t, b.BundleImage, tc.BundleImage)
assert.NotNil(t, b.Annotations)
assert.Equal(t, b.Annotations.PackageName, tc.ExpectedPackageName)

csv, err := b.ClusterServiceVersion()
require.NoError(t, err)
require.NotNil(t, csv)
assert.Equal(t, b.ClusterServiceVersion.Name, tc.ExpectedCSVName)

assert.Equal(t, csv.Name, tc.ExpectedCSVName)

version, err := csv.GetVersion()
require.NoError(t, err)

assert.Equal(t, version, tc.ExpectedCSVVersion)
assert.Equal(t, b.Version, tc.ExpectedCSVVersion)
}
90 changes: 0 additions & 90 deletions pkg/extractor/bundles_benchmark_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"strings"

"github.com/mt-sre/addon-metadata-operator/pkg/operator"
imageparser "github.com/novln/docker-parser"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -66,7 +66,7 @@ func WithLog(log logrus.FieldLogger) MainExtractorOpt {
}

// ExtractBundles - extract bundles from indexImage matching pkgName
func (e *MainExtractor) ExtractBundles(indexImage string, pkgName string) ([]*registry.Bundle, error) {
func (e *MainExtractor) ExtractBundles(indexImage string, pkgName string) ([]operator.Bundle, error) {
if err := validateIndexImage(indexImage); err != nil {
if errors.Is(err, ErrTaglessImage) {
e.Log.Info("skipping tagless image, nothing to extract")
Expand All @@ -92,7 +92,7 @@ func (e *MainExtractor) ExtractBundles(indexImage string, pkgName string) ([]*re
}

// ExtractAllBundles - extract bundles for all packages from indexImage
func (e *MainExtractor) ExtractAllBundles(indexImage string) ([]*registry.Bundle, error) {
func (e *MainExtractor) ExtractAllBundles(indexImage string) ([]operator.Bundle, error) {
if err := validateIndexImage(indexImage); err != nil {
if errors.Is(err, ErrTaglessImage) {
e.Log.Info("skipping tagless image, nothing to extract")
Expand All @@ -111,8 +111,8 @@ func (e *MainExtractor) ExtractAllBundles(indexImage string) ([]*registry.Bundle
return e.extractBundlesConcurrent(bundleImages)
}

func (e *MainExtractor) extractBundlesConcurrent(bundleImages []string) ([]*registry.Bundle, error) {
res := make([]*registry.Bundle, len(bundleImages))
func (e *MainExtractor) extractBundlesConcurrent(bundleImages []string) ([]operator.Bundle, error) {
res := make([]operator.Bundle, len(bundleImages))
g := new(errgroup.Group)

// we need the global context to be able to cancel all goroutines
Expand Down
4 changes: 2 additions & 2 deletions pkg/extractor/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package extractor
import (
"testing"

"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/mt-sre/addon-metadata-operator/pkg/operator"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func TestMainExtractorWithDefaultValues(t *testing.T) {
t.Run(tc.indexImage, func(t *testing.T) {
t.Parallel()

var bundles []*registry.Bundle
var bundles []operator.Bundle
var err error

if tc.pkgName == "" {
Expand Down
Loading

0 comments on commit 13f8b0a

Please sign in to comment.