|
| 1 | +package installedpackage |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/blang/semver/v4" |
| 9 | + "github.com/operator-framework/deppy/pkg/deppy" |
| 10 | + "github.com/operator-framework/deppy/pkg/deppy/constraint" |
| 11 | + "github.com/operator-framework/deppy/pkg/deppy/input" |
| 12 | + |
| 13 | + olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" |
| 14 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/predicates" |
| 15 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/sort" |
| 16 | + rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1" |
| 17 | +) |
| 18 | + |
| 19 | +type InstalledPackageVariable struct { |
| 20 | + *input.SimpleVariable |
| 21 | + bundleEntities []*olmentity.BundleEntity |
| 22 | +} |
| 23 | + |
| 24 | +func (r *InstalledPackageVariable) BundleEntities() []*olmentity.BundleEntity { |
| 25 | + return r.bundleEntities |
| 26 | +} |
| 27 | + |
| 28 | +func NewInstalledPackageVariable(packageName string, bundleEntities []*olmentity.BundleEntity) *InstalledPackageVariable { |
| 29 | + id := deppy.IdentifierFromString(fmt.Sprintf("installed package %s", packageName)) |
| 30 | + var entityIDs []deppy.Identifier |
| 31 | + for _, bundle := range bundleEntities { |
| 32 | + entityIDs = append(entityIDs, bundle.ID) |
| 33 | + } |
| 34 | + return &InstalledPackageVariable{ |
| 35 | + SimpleVariable: input.NewSimpleVariable(id, constraint.Mandatory(), constraint.Dependency(entityIDs...)), |
| 36 | + bundleEntities: bundleEntities, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +var _ input.VariableSource = &InstalledPackageVariableSource{} |
| 41 | + |
| 42 | +type InstalledPackageVariableSource struct { |
| 43 | + packageName string |
| 44 | + version semver.Version |
| 45 | + channelName string |
| 46 | +} |
| 47 | + |
| 48 | +func NewInstalledPackageVariableSource(packageName, version, channel string) (*InstalledPackageVariableSource, error) { |
| 49 | + if packageName == "" { |
| 50 | + return nil, fmt.Errorf("package name must not be empty") |
| 51 | + } |
| 52 | + |
| 53 | + semverVersion, err := semver.Parse(version) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + //TODO(jmprusi): check version and channel |
| 59 | + return &InstalledPackageVariableSource{ |
| 60 | + packageName: packageName, |
| 61 | + version: semverVersion, |
| 62 | + channelName: channel, |
| 63 | + }, nil |
| 64 | +} |
| 65 | + |
| 66 | +// TODO(jmprusi): move this somewhere else? |
| 67 | +type replacesProperty struct { |
| 68 | + Replaces string `json:"replaces"` |
| 69 | +} |
| 70 | + |
| 71 | +// TODO(jmprusi): move this somewhere else? |
| 72 | +type packageProperty struct { |
| 73 | + Package string `json:"packageName"` |
| 74 | + Version string `json:"version"` |
| 75 | +} |
| 76 | + |
| 77 | +func (r *InstalledPackageVariableSource) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) { |
| 78 | + validRange, err := semver.ParseRange(">=" + r.version.String()) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + resultSet, err := entitySource.Filter(ctx, input.And(predicates.WithPackageName(r.packageName), predicates.InChannel(r.channelName), predicates.InSemverRange(validRange))) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + if len(resultSet) == 0 { |
| 87 | + return nil, r.notFoundError() |
| 88 | + } |
| 89 | + resultSet = resultSet.Sort(sort.ByChannelAndVersion) |
| 90 | + var bundleEntities []*olmentity.BundleEntity |
| 91 | + for i := 0; i < len(resultSet); i++ { |
| 92 | + |
| 93 | + replacesJSON := resultSet[i].Properties["olm.replaces"] |
| 94 | + packageJSON := resultSet[i].Properties["olm.package"] |
| 95 | + |
| 96 | + if replacesJSON == "" || packageJSON == "" { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + // unmarshal replaces and packages |
| 101 | + var replaces replacesProperty |
| 102 | + var packages packageProperty |
| 103 | + if err := json.Unmarshal([]byte(replacesJSON), &replaces); err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + if err := json.Unmarshal([]byte(packageJSON), &packages); err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + version, err := semver.Parse(packages.Version) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + expectedReplace := fmt.Sprintf("%s.v%s", r.packageName, r.version.String()) |
| 116 | + if r.version.Equals(version) || replaces.Replaces == expectedReplace { |
| 117 | + bundleEntities = append(bundleEntities, olmentity.NewBundleEntity(&resultSet[i])) |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + return []deppy.Variable{ |
| 122 | + NewInstalledPackageVariable(r.packageName, bundleEntities), |
| 123 | + }, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (r *InstalledPackageVariableSource) notFoundError() error { |
| 127 | + return fmt.Errorf("package '%s' not installed", r.packageName) |
| 128 | +} |
| 129 | + |
| 130 | +func NewInstalledPackage(bundleDeployment *rukpakv1alpha1.BundleDeployment) (*InstalledPackageVariableSource, error) { |
| 131 | + |
| 132 | + // TODO(jmprusi): proper if ... validation |
| 133 | + version := bundleDeployment.Annotations["operators.operatorframework.io/version"] |
| 134 | + channel := bundleDeployment.Annotations["operators.operatorframework.io/channel"] |
| 135 | + pkg := bundleDeployment.Annotations["operators.operatorframework.io/package"] |
| 136 | + |
| 137 | + return NewInstalledPackageVariableSource(pkg, version, channel) |
| 138 | +} |
0 commit comments