-
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.
- Loading branch information
Showing
8 changed files
with
295 additions
and
13 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
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
127 changes: 127 additions & 0 deletions
127
internal/resolution/variable_sources/installed_package/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,127 @@ | ||
package installed_package | ||
|
||
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" | ||
) | ||
|
||
type InstalledPackageVariable struct { | ||
*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 { | ||
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) | ||
} |
58 changes: 58 additions & 0 deletions
58
internal/resolution/variable_sources/installed_package/installed_package_test.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,58 @@ | ||
package installed_package_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/operator-framework/deppy/pkg/deppy" | ||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
"github.com/operator-framework/operator-registry/alpha/property" | ||
|
||
olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/installed_package" | ||
) | ||
|
||
func TestInstalledPackage(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "InstalledPackageVariableSource Suite") | ||
} | ||
|
||
var _ = Describe("installedPackageVariable", func() { | ||
var ( | ||
rpv *installed_package.InstalledPackageVariable | ||
packageName string | ||
bundleEntities []*olmentity.BundleEntity | ||
) | ||
|
||
BeforeEach(func() { | ||
packageName = "test-package" | ||
bundleEntities = []*olmentity.BundleEntity{ | ||
olmentity.NewBundleEntity(input.NewEntity("bundle-1", map[string]string{ | ||
property.TypePackage: `{"packageName": "test-package", "version": "1.0.0"}`, | ||
property.TypeChannel: `{"channelName":"stable","priority":0}`, | ||
"olm.replaces": `{"packageName": "test-package", "version": "0.0.0"}`, | ||
})), | ||
olmentity.NewBundleEntity(input.NewEntity("bundle-2", map[string]string{ | ||
property.TypePackage: `{"packageName": "test-package", "version": "2.0.0"}`, | ||
property.TypeChannel: `{"channelName":"stable","priority":0}`, | ||
"olm.replaces": `{"packageName": "test-package", "version": "1.0.0"}`, | ||
})), | ||
olmentity.NewBundleEntity(input.NewEntity("bundle-3", map[string]string{ | ||
property.TypePackage: `{"packageName": "test-package", "version": "3.0.0"}`, | ||
property.TypeChannel: `{"channelName":"stable","priority":0}`, | ||
"olm.replaces": `{"packageName": "test-package", "version": "2.0.0"}`, | ||
})), | ||
} | ||
rpv = installed_package.NewInstalledPackageVariable(packageName, bundleEntities) | ||
}) | ||
|
||
It("should return the correct package name", func() { | ||
Expect(rpv.Identifier()).To(Equal(deppy.IdentifierFromString(fmt.Sprintf("installed package %s", packageName)))) | ||
}) | ||
|
||
It("should return the correct bundle entities", func() { | ||
Expect(rpv.BundleEntities()).To(Equal(bundleEntities)) | ||
}) | ||
}) |
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
Oops, something went wrong.