|
| 1 | +package manifests |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/blang/semver" |
| 7 | + operatorsv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1" |
| 8 | + "github.com/operator-framework/operator-registry/pkg/registry" |
| 9 | + "github.com/operator-framework/operator-registry/pkg/sqlite" |
| 10 | + "github.com/pkg/errors" |
| 11 | +) |
| 12 | + |
| 13 | +// TODO: use internal version of registry.Bundle/registry.PackageManifest so |
| 14 | +// operator-registry can use validation library. |
| 15 | + |
| 16 | +// manifestsLoad loads a manifests directory from disk. |
| 17 | +type manifestsLoad struct { |
| 18 | + dir string |
| 19 | + pkg registry.PackageManifest |
| 20 | + bundles map[string]*registry.Bundle |
| 21 | +} |
| 22 | + |
| 23 | +// Ensure manifestsLoad implements registry.Load. |
| 24 | +var _ registry.Load = &manifestsLoad{} |
| 25 | + |
| 26 | +// populate uses operator-registry's sqlite.NewSQLLoaderForDirectory to load |
| 27 | +// l.dir's manifests. Note that this method does not call any functions that |
| 28 | +// use SQL drivers. |
| 29 | +func (l *manifestsLoad) populate() error { |
| 30 | + loader := sqlite.NewSQLLoaderForDirectory(l, l.dir) |
| 31 | + if err := loader.Populate(); err != nil { |
| 32 | + return errors.Wrapf(err, "error getting bundles from manifests dir %q", l.dir) |
| 33 | + } |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +// AddOperatorBundle adds a bundle to l. |
| 38 | +func (l *manifestsLoad) AddOperatorBundle(bundle *registry.Bundle) error { |
| 39 | + csvRaw, err := bundle.ClusterServiceVersion() |
| 40 | + if err != nil { |
| 41 | + return errors.Wrap(err, "error getting bundle CSV") |
| 42 | + } |
| 43 | + csvSpec := operatorsv1alpha1.ClusterServiceVersionSpec{} |
| 44 | + if err := json.Unmarshal(csvRaw.Spec, &csvSpec); err != nil { |
| 45 | + return errors.Wrap(err, "error unmarshaling CSV spec") |
| 46 | + } |
| 47 | + bundle.Name = csvSpec.Version.String() |
| 48 | + l.bundles[csvSpec.Version.String()] = bundle |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +// AddOperatorBundle adds the package manifest to l. |
| 53 | +func (l *manifestsLoad) AddPackageChannels(pkg registry.PackageManifest) error { |
| 54 | + l.pkg = pkg |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// AddBundlePackageChannels is a no-op to implement the registry.Load interface. |
| 59 | +func (l *manifestsLoad) AddBundlePackageChannels(manifest registry.PackageManifest, bundle registry.Bundle) error { |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// RmPackageName is a no-op to implement the registry.Load interface. |
| 64 | +func (l *manifestsLoad) RmPackageName(packageName string) error { |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// ClearNonDefaultBundles is a no-op to implement the registry.Load interface. |
| 69 | +func (l *manifestsLoad) ClearNonDefaultBundles(packageName string) error { |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// ManifestsStore knows how to query for an operator's package manifest and |
| 74 | +// related bundles. |
| 75 | +type ManifestsStore interface { |
| 76 | + // GetPackageManifest returns the ManifestsStore's registry.PackageManifest. |
| 77 | + // The returned object is assumed to be valid. |
| 78 | + GetPackageManifest() registry.PackageManifest |
| 79 | + // GetBundles returns the ManifestsStore's set of registry.Bundle. These |
| 80 | + // bundles are unique by CSV version, since only one operator type should |
| 81 | + // exist in one manifests dir. |
| 82 | + // The returned objects are assumed to be valid. |
| 83 | + GetBundles() []*registry.Bundle |
| 84 | + // GetBundleForVersion returns the ManifestsStore's registry.Bundle for a |
| 85 | + // given version string. An error should be returned if the passed version |
| 86 | + // does not exist in the store. |
| 87 | + // The returned object is assumed to be valid. |
| 88 | + GetBundleForVersion(string) (*registry.Bundle, error) |
| 89 | +} |
| 90 | + |
| 91 | +// manifests implements ManifestsStore |
| 92 | +type manifests struct { |
| 93 | + pkg registry.PackageManifest |
| 94 | + bundles map[string]*registry.Bundle |
| 95 | +} |
| 96 | + |
| 97 | +// ManifestsStoreForDir populates a ManifestsStore from the metadata in dir. |
| 98 | +// Each bundle and the package manifest are statically validated, and will |
| 99 | +// return an error if any are not valid. |
| 100 | +func ManifestsStoreForDir(dir string) (ManifestsStore, error) { |
| 101 | + load := &manifestsLoad{ |
| 102 | + dir: dir, |
| 103 | + bundles: map[string]*registry.Bundle{}, |
| 104 | + } |
| 105 | + if err := load.populate(); err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + return &manifests{ |
| 109 | + pkg: load.pkg, |
| 110 | + bundles: load.bundles, |
| 111 | + }, nil |
| 112 | +} |
| 113 | + |
| 114 | +func (l manifests) GetPackageManifest() registry.PackageManifest { |
| 115 | + return l.pkg |
| 116 | +} |
| 117 | + |
| 118 | +func (l manifests) GetBundles() (bundles []*registry.Bundle) { |
| 119 | + for _, bundle := range l.bundles { |
| 120 | + bundles = append(bundles, bundle) |
| 121 | + } |
| 122 | + return bundles |
| 123 | +} |
| 124 | + |
| 125 | +func (l manifests) GetBundleForVersion(version string) (*registry.Bundle, error) { |
| 126 | + if _, err := semver.Parse(version); err != nil { |
| 127 | + return nil, errors.Wrapf(err, "error getting bundle for version %q", version) |
| 128 | + } |
| 129 | + bundle, ok := l.bundles[version] |
| 130 | + if !ok { |
| 131 | + return nil, errors.Errorf("bundle for version %q does not exist", version) |
| 132 | + } |
| 133 | + return bundle, nil |
| 134 | +} |
0 commit comments