-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joaquim Moreno Prusi <joaquim@redhat.com>
- Loading branch information
Showing
5 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
internal/resolution/variable_sources/installedpackage/installed_package.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package installedpackage | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/blang/semver/v4" | ||
"github.com/operator-framework/deppy/pkg/deppy" | ||
"github.com/operator-framework/deppy/pkg/deppy/constraint" | ||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
|
||
olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/predicates" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/sort" | ||
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1" | ||
) | ||
|
||
type InstalledPackageVariable struct { | ||
Check warning on line 19 in internal/resolution/variable_sources/installedpackage/installed_package.go GitHub Actions / lint
|
||
*input.SimpleVariable | ||
bundleEntities []*olmentity.BundleEntity | ||
} | ||
|
||
func (r *InstalledPackageVariable) BundleEntities() []*olmentity.BundleEntity { | ||
return r.bundleEntities | ||
} | ||
|
||
func NewInstalledPackageVariable(packageName string, bundleEntities []*olmentity.BundleEntity) *InstalledPackageVariable { | ||
id := deppy.IdentifierFromString(fmt.Sprintf("installed package %s", packageName)) | ||
var entityIDs []deppy.Identifier | ||
for _, bundle := range bundleEntities { | ||
entityIDs = append(entityIDs, bundle.ID) | ||
} | ||
return &InstalledPackageVariable{ | ||
SimpleVariable: input.NewSimpleVariable(id, constraint.Mandatory(), constraint.Dependency(entityIDs...)), | ||
bundleEntities: bundleEntities, | ||
} | ||
} | ||
|
||
var _ input.VariableSource = &InstalledPackageVariableSource{} | ||
|
||
type InstalledPackageVariableSource struct { | ||
Check warning on line 42 in internal/resolution/variable_sources/installedpackage/installed_package.go GitHub Actions / lint
|
||
packageName string | ||
version semver.Version | ||
channelName string | ||
} | ||
|
||
func NewInstalledPackageVariableSource(packageName, version, channel string) (*InstalledPackageVariableSource, error) { | ||
if packageName == "" { | ||
return nil, fmt.Errorf("package name must not be empty") | ||
} | ||
|
||
semverVersion, err := semver.Parse(version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//TODO(jmprusi): check version and channel | ||
return &InstalledPackageVariableSource{ | ||
packageName: packageName, | ||
version: semverVersion, | ||
channelName: channel, | ||
}, nil | ||
} | ||
|
||
// TODO(jmprusi): move this somewhere else? | ||
type replacesProperty struct { | ||
Replaces string `json:"replaces"` | ||
} | ||
|
||
// TODO(jmprusi): move this somewhere else? | ||
type packageProperty struct { | ||
Package string `json:"packageName"` | ||
Version string `json:"version"` | ||
} | ||
|
||
func (r *InstalledPackageVariableSource) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) { | ||
validRange, err := semver.ParseRange(">=" + r.version.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resultSet, err := entitySource.Filter(ctx, input.And(predicates.WithPackageName(r.packageName), predicates.InChannel(r.channelName), predicates.InSemverRange(validRange))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(resultSet) == 0 { | ||
return nil, r.notFoundError() | ||
} | ||
resultSet = resultSet.Sort(sort.ByChannelAndVersion) | ||
var bundleEntities []*olmentity.BundleEntity | ||
for i := 0; i < len(resultSet); i++ { | ||
|
||
replacesJSON := resultSet[i].Properties["olm.replaces"] | ||
packageJSON := resultSet[i].Properties["olm.package"] | ||
|
||
if replacesJSON == "" || packageJSON == "" { | ||
continue | ||
} | ||
|
||
// unmarshal replaces and packages | ||
var replaces replacesProperty | ||
var packages packageProperty | ||
if err := json.Unmarshal([]byte(replacesJSON), &replaces); err != nil { | ||
return nil, err | ||
} | ||
if err := json.Unmarshal([]byte(packageJSON), &packages); err != nil { | ||
return nil, err | ||
} | ||
|
||
version, err := semver.Parse(packages.Version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
expectedReplace := fmt.Sprintf("%s.v%s", r.packageName, r.version.String()) | ||
if r.version.Equals(version) || replaces.Replaces == expectedReplace { | ||
bundleEntities = append(bundleEntities, olmentity.NewBundleEntity(&resultSet[i])) | ||
} | ||
|
||
} | ||
return []deppy.Variable{ | ||
NewInstalledPackageVariable(r.packageName, bundleEntities), | ||
}, nil | ||
} | ||
|
||
func (r *InstalledPackageVariableSource) notFoundError() error { | ||
return fmt.Errorf("package '%s' not installed", r.packageName) | ||
} | ||
|
||
func NewInstalledPackage(bundleDeployment *rukpakv1alpha1.BundleDeployment) (*InstalledPackageVariableSource, error) { | ||
|
||
// TODO(jmprusi): proper if ... validation | ||
version := bundleDeployment.Annotations["operators.operatorframework.io/version"] | ||
channel := bundleDeployment.Annotations["operators.operatorframework.io/channel"] | ||
pkg := bundleDeployment.Annotations["operators.operatorframework.io/package"] | ||
|
||
return NewInstalledPackageVariableSource(pkg, version, channel) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters