|
| 1 | +package resolve |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + bsemver "github.com/blang/semver/v4" |
| 10 | + "github.com/containers/image/v5/docker/reference" |
| 11 | + |
| 12 | + "github.com/operator-framework/operator-registry/alpha/action" |
| 13 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 14 | + |
| 15 | + ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" |
| 16 | + "github.com/operator-framework/operator-controller/internal/bundleutil" |
| 17 | + "github.com/operator-framework/operator-controller/internal/rukpak/source" |
| 18 | +) |
| 19 | + |
| 20 | +type BundleResolver struct { |
| 21 | + Unpacker source.Unpacker |
| 22 | + BrittleUnpackerCacheDir string |
| 23 | +} |
| 24 | + |
| 25 | +func (r *BundleResolver) Resolve(ctx context.Context, ext *ocv1alpha1.ClusterExtension, _ *ocv1alpha1.BundleMetadata) (*declcfg.Bundle, *bsemver.Version, *declcfg.Deprecation, error) { |
| 26 | + res, err := r.Unpacker.Unpack(ctx, &source.BundleSource{ |
| 27 | + Name: ext.Name, |
| 28 | + Type: source.SourceTypeImage, |
| 29 | + Image: &source.ImageSource{ |
| 30 | + Ref: ext.Spec.Source.Bundle.Ref, |
| 31 | + }, |
| 32 | + }) |
| 33 | + if err != nil { |
| 34 | + return nil, nil, nil, err |
| 35 | + } |
| 36 | + if res.State != source.StateUnpacked { |
| 37 | + return nil, nil, nil, fmt.Errorf("bundle not unpacked: %v", res.Message) |
| 38 | + } |
| 39 | + |
| 40 | + ref, err := reference.ParseNamed(res.ResolvedSource.Image.Ref) |
| 41 | + if err != nil { |
| 42 | + return nil, nil, nil, err |
| 43 | + } |
| 44 | + canonicalRef, ok := ref.(reference.Canonical) |
| 45 | + if !ok { |
| 46 | + return nil, nil, nil, errors.New("expected canonical reference") |
| 47 | + } |
| 48 | + bundlePath := filepath.Join(r.BrittleUnpackerCacheDir, ext.Name, canonicalRef.Digest().String()) |
| 49 | + |
| 50 | + // TODO: This is a temporary workaround to get the bundle from the filesystem |
| 51 | + // until the operator-registry library is updated to support reading from a |
| 52 | + // filesystem. This will be removed once the library is updated. |
| 53 | + |
| 54 | + render := action.Render{ |
| 55 | + Refs: []string{bundlePath}, |
| 56 | + AllowedRefMask: action.RefBundleDir, |
| 57 | + } |
| 58 | + fbc, err := render.Run(ctx) |
| 59 | + if err != nil { |
| 60 | + return nil, nil, nil, err |
| 61 | + } |
| 62 | + if len(fbc.Bundles) != 1 { |
| 63 | + return nil, nil, nil, errors.New("expected exactly one bundle") |
| 64 | + } |
| 65 | + bundle := fbc.Bundles[0] |
| 66 | + bundle.Image = canonicalRef.String() |
| 67 | + v, err := bundleutil.GetVersion(bundle) |
| 68 | + if err != nil { |
| 69 | + return nil, nil, nil, err |
| 70 | + } |
| 71 | + return &bundle, v, nil, nil |
| 72 | +} |
0 commit comments