-
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.
Turn required package variable source into a func
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
- Loading branch information
Showing
3 changed files
with
187 additions
and
178 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
109 changes: 37 additions & 72 deletions
109
internal/resolution/variablesources/required_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 |
---|---|---|
@@ -1,99 +1,64 @@ | ||
package variablesources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sort" | ||
|
||
mmsemver "github.com/Masterminds/semver/v3" | ||
"github.com/operator-framework/deppy/pkg/deppy" | ||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
|
||
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" | ||
"github.com/operator-framework/operator-controller/internal/catalogmetadata" | ||
catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" | ||
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" | ||
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables" | ||
) | ||
|
||
var _ input.VariableSource = &RequiredPackageVariableSource{} | ||
// MakeRequiredPackageVariables returns a variable which represent | ||
// explicit requirement for a package from an user. | ||
// This is when an user explicitly asks "install this" via Operator API. | ||
func MakeRequiredPackageVariables(allBundles []*catalogmetadata.Bundle, operators []operatorsv1alpha1.Operator) ([]*olmvariables.RequiredPackageVariable, error) { | ||
result := make([]*olmvariables.RequiredPackageVariable, 0, len(operators)) | ||
|
||
type RequiredPackageVariableSourceOption func(*RequiredPackageVariableSource) error | ||
for _, operator := range operators { | ||
packageName := operator.Spec.PackageName | ||
channelName := operator.Spec.Channel | ||
versionRange := operator.Spec.Version | ||
|
||
func InVersionRange(versionRange string) RequiredPackageVariableSourceOption { | ||
return func(r *RequiredPackageVariableSource) error { | ||
if versionRange != "" { | ||
vr, err := mmsemver.NewConstraint(versionRange) | ||
if err == nil { | ||
r.versionRange = versionRange | ||
r.predicates = append(r.predicates, catalogfilter.InMastermindsSemverRange(vr)) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("invalid version range '%s': %w", versionRange, err) | ||
predicates := []catalogfilter.Predicate[catalogmetadata.Bundle]{ | ||
catalogfilter.WithPackageName(packageName), | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func InChannel(channelName string) RequiredPackageVariableSourceOption { | ||
return func(r *RequiredPackageVariableSource) error { | ||
if channelName != "" { | ||
r.channelName = channelName | ||
r.predicates = append(r.predicates, catalogfilter.InChannel(channelName)) | ||
predicates = append(predicates, catalogfilter.InChannel(channelName)) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
type RequiredPackageVariableSource struct { | ||
allBundles []*catalogmetadata.Bundle | ||
|
||
packageName string | ||
versionRange string | ||
channelName string | ||
predicates []catalogfilter.Predicate[catalogmetadata.Bundle] | ||
} | ||
|
||
func NewRequiredPackageVariableSource(allBundles []*catalogmetadata.Bundle, packageName string, options ...RequiredPackageVariableSourceOption) (*RequiredPackageVariableSource, error) { | ||
if packageName == "" { | ||
return nil, fmt.Errorf("package name must not be empty") | ||
} | ||
r := &RequiredPackageVariableSource{ | ||
allBundles: allBundles, | ||
if versionRange != "" { | ||
vr, err := mmsemver.NewConstraint(versionRange) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid version range '%s': %w", versionRange, err) | ||
} | ||
predicates = append(predicates, catalogfilter.InMastermindsSemverRange(vr)) | ||
} | ||
|
||
packageName: packageName, | ||
predicates: []catalogfilter.Predicate[catalogmetadata.Bundle]{catalogfilter.WithPackageName(packageName)}, | ||
} | ||
for _, option := range options { | ||
if err := option(r); err != nil { | ||
return nil, err | ||
resultSet := catalogfilter.Filter(allBundles, catalogfilter.And(predicates...)) | ||
if len(resultSet) == 0 { | ||
if versionRange != "" && channelName != "" { | ||
return nil, fmt.Errorf("no package '%s' matching version '%s' found in channel '%s'", packageName, versionRange, channelName) | ||
} | ||
if versionRange != "" { | ||
return nil, fmt.Errorf("no package '%s' matching version '%s' found", packageName, versionRange) | ||
} | ||
if channelName != "" { | ||
return nil, fmt.Errorf("no package '%s' found in channel '%s'", packageName, channelName) | ||
} | ||
return nil, fmt.Errorf("no package '%s' found", packageName) | ||
} | ||
} | ||
return r, nil | ||
} | ||
sort.SliceStable(resultSet, func(i, j int) bool { | ||
return catalogsort.ByVersion(resultSet[i], resultSet[j]) | ||
}) | ||
|
||
func (r *RequiredPackageVariableSource) GetVariables(_ context.Context) ([]deppy.Variable, error) { | ||
resultSet := catalogfilter.Filter(r.allBundles, catalogfilter.And(r.predicates...)) | ||
if len(resultSet) == 0 { | ||
return nil, r.notFoundError() | ||
result = append(result, olmvariables.NewRequiredPackageVariable(packageName, resultSet)) | ||
} | ||
sort.SliceStable(resultSet, func(i, j int) bool { | ||
return catalogsort.ByVersion(resultSet[i], resultSet[j]) | ||
}) | ||
return []deppy.Variable{ | ||
olmvariables.NewRequiredPackageVariable(r.packageName, resultSet), | ||
}, nil | ||
} | ||
|
||
func (r *RequiredPackageVariableSource) notFoundError() error { | ||
if r.versionRange != "" && r.channelName != "" { | ||
return fmt.Errorf("no package '%s' matching version '%s' found in channel '%s'", r.packageName, r.versionRange, r.channelName) | ||
} | ||
if r.versionRange != "" { | ||
return fmt.Errorf("no package '%s' matching version '%s' found", r.packageName, r.versionRange) | ||
} | ||
if r.channelName != "" { | ||
return fmt.Errorf("no package '%s' found in channel '%s'", r.packageName, r.channelName) | ||
} | ||
return fmt.Errorf("no package '%s' found", r.packageName) | ||
return result, nil | ||
} |
Oops, something went wrong.