|
| 1 | +package resolve |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "reflect" |
| 9 | + |
| 10 | + bsemver "github.com/blang/semver/v4" |
| 11 | + |
| 12 | + "github.com/operator-framework/operator-registry/alpha/action" |
| 13 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 14 | + |
| 15 | + ocv1 "github.com/operator-framework/operator-controller/api/v1" |
| 16 | + "github.com/operator-framework/operator-controller/internal/operator-controller/bundleutil" |
| 17 | + "github.com/operator-framework/operator-controller/internal/shared/util/image" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + directBundleInstallImageAnnotation = "olm.operatorframework.io/bundle-image" |
| 22 | +) |
| 23 | + |
| 24 | +type BundleResolver struct { |
| 25 | + ImagePuller image.Puller |
| 26 | + ImageCache image.Cache |
| 27 | +} |
| 28 | + |
| 29 | +func (r *BundleResolver) Resolve(ctx context.Context, ext *ocv1.ClusterExtension, _ *ocv1.BundleMetadata) (*declcfg.Bundle, *bsemver.Version, *declcfg.Deprecation, error) { |
| 30 | + if ext.Annotations == nil || ext.Annotations[directBundleInstallImageAnnotation] == "" { |
| 31 | + return nil, nil, nil, fmt.Errorf("ClusterExtension is missing required annotation %s", directBundleInstallImageAnnotation) |
| 32 | + } |
| 33 | + bundleFS, canonicalRef, _, err := r.ImagePuller.Pull(ctx, ext.Name, ext.Annotations[directBundleInstallImageAnnotation], r.ImageCache) |
| 34 | + if err != nil { |
| 35 | + return nil, nil, nil, err |
| 36 | + } |
| 37 | + |
| 38 | + // TODO: This is a temporary workaround to get the bundle from the filesystem |
| 39 | + // until the operator-registry library is updated to support reading from a |
| 40 | + // fs.FS. This will be removed once the library is updated. |
| 41 | + bundlePath, err := getDirFSPath(bundleFS) |
| 42 | + if err != nil { |
| 43 | + panic(fmt.Errorf("expected to be able to recover bundle path from bundleFS: %v", err)) |
| 44 | + } |
| 45 | + |
| 46 | + // Render the bundle |
| 47 | + render := action.Render{ |
| 48 | + Refs: []string{bundlePath}, |
| 49 | + AllowedRefMask: action.RefBundleDir, |
| 50 | + } |
| 51 | + fbc, err := render.Run(ctx) |
| 52 | + if err != nil { |
| 53 | + return nil, nil, nil, err |
| 54 | + } |
| 55 | + if len(fbc.Bundles) != 1 { |
| 56 | + return nil, nil, nil, errors.New("expected exactly one bundle") |
| 57 | + } |
| 58 | + bundle := fbc.Bundles[0] |
| 59 | + bundle.Image = canonicalRef.String() |
| 60 | + v, err := bundleutil.GetVersion(bundle) |
| 61 | + if err != nil { |
| 62 | + return nil, nil, nil, err |
| 63 | + } |
| 64 | + return &bundle, v, nil, nil |
| 65 | +} |
| 66 | + |
| 67 | +// A function to recover the underlying path string from os.DirFS |
| 68 | +func getDirFSPath(f fs.FS) (string, error) { |
| 69 | + v := reflect.ValueOf(f) |
| 70 | + |
| 71 | + // Check if the underlying type is a string (its kind) |
| 72 | + if v.Kind() != reflect.String { |
| 73 | + return "", fmt.Errorf("underlying type is not a string, it is %s", v.Kind()) |
| 74 | + } |
| 75 | + |
| 76 | + // The type itself (os.dirFS) is unexported, but its Kind is a string. |
| 77 | + // We can convert the reflect.Value back to a regular string using .Interface() |
| 78 | + // after converting it to a basic string type. |
| 79 | + path, ok := v.Convert(reflect.TypeOf("")).Interface().(string) |
| 80 | + if !ok { |
| 81 | + return "", fmt.Errorf("could not convert reflected value to string") |
| 82 | + } |
| 83 | + |
| 84 | + return path, nil |
| 85 | +} |
0 commit comments