-
Notifications
You must be signed in to change notification settings - Fork 54
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: Mikalai Radchuk <mradchuk@redhat.com>
- Loading branch information
Showing
4 changed files
with
237 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
Copyright 2022. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/operator-framework/deppy/pkg/deppy/solver" | ||
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
|
||
catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1" | ||
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" | ||
"github.com/operator-framework/operator-controller/internal/resolution/entitysources" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm" | ||
) | ||
|
||
var ( | ||
scheme = runtime.NewScheme() | ||
) | ||
|
||
func init() { | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
utilruntime.Must(operatorsv1alpha1.AddToScheme(scheme)) | ||
utilruntime.Must(rukpakv1alpha1.AddToScheme(scheme)) | ||
utilruntime.Must(catalogd.AddToScheme(scheme)) | ||
} | ||
|
||
func main() { | ||
var packageName string | ||
var packageVersion string | ||
var packageChannel string | ||
flag.StringVar(&packageName, "package-name", "", "Name of the package to resolve") | ||
flag.StringVar(&packageVersion, "package-version", "", "Version of the package") | ||
flag.StringVar(&packageChannel, "package-channel", "", "Channel of the package") | ||
flag.Parse() | ||
|
||
if err := validateFlags(packageName); err != nil { | ||
fmt.Println(err) | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
err := run(packageName, packageVersion, packageChannel) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func validateFlags(packageName string) error { | ||
if packageName == "" { | ||
return errors.New("missing required -package-name flag") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func run(packageName, packageVersion, packageChannel string) error { | ||
ctx := context.Background() | ||
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create client: %w", err) | ||
} | ||
|
||
packageVariableSource := NewPackageVariableSource(packageName, packageVersion, packageChannel) | ||
resolver := solver.NewDeppySolver( | ||
entitysources.NewCatalogdEntitySource(client), | ||
append(olm.NestedVariableSource{packageVariableSource}, olm.NewOLMVariableSource(client)...), | ||
) | ||
|
||
bundleImage, err := resolve(ctx, resolver, packageName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(bundleImage) | ||
return nil | ||
} | ||
|
||
func resolve(ctx context.Context, resolver *solver.DeppySolver, packageName string) (string, error) { | ||
solution, err := resolver.Solve(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
bundleEntity, err := getBundleEntityFromSolution(solution, packageName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Get the bundle image reference for the bundle | ||
bundleImage, err := bundleEntity.BundlePath() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return bundleImage, nil | ||
} | ||
|
||
func getBundleEntityFromSolution(solution *solver.Solution, packageName string) (*entity.BundleEntity, error) { | ||
for _, variable := range solution.SelectedVariables() { | ||
switch v := variable.(type) { | ||
case *bundles_and_dependencies.BundleVariable: | ||
entityPkgName, err := v.BundleEntity().PackageName() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if packageName == entityPkgName { | ||
return v.BundleEntity(), nil | ||
} | ||
} | ||
} | ||
return nil, fmt.Errorf("entity for package %q not found in solution", packageName) | ||
} |
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,27 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/required_package" | ||
) | ||
|
||
func NewPackageVariableSource(packageName, packageVersion, packageChannel string) func(inputVariableSource input.VariableSource) (input.VariableSource, error) { | ||
return func(inputVariableSource input.VariableSource) (input.VariableSource, error) { | ||
pkgSource, err := required_package.NewRequiredPackage( | ||
packageName, | ||
required_package.InVersionRange(packageVersion), | ||
required_package.InChannel(packageChannel), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sliceSource := olm.SliceVariableSource{pkgSource} | ||
if inputVariableSource != nil { | ||
sliceSource = append(sliceSource, inputVariableSource) | ||
} | ||
|
||
return sliceSource, nil | ||
} | ||
} |
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,46 @@ | ||
package olm | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/operator-framework/deppy/pkg/deppy" | ||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
) | ||
|
||
var _ input.VariableSource = &SliceVariableSource{} | ||
var _ input.VariableSource = &NestedVariableSource{} | ||
|
||
type NestedVariableSource []func(inputVariableSource input.VariableSource) (input.VariableSource, error) | ||
|
||
func (s NestedVariableSource) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) { | ||
if len(s) == 0 { | ||
return nil, errors.New("empty nested variable sources") | ||
} | ||
|
||
var variableSource input.VariableSource | ||
var err error | ||
for _, constructor := range s { | ||
variableSource, err = constructor(variableSource) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return variableSource.GetVariables(ctx, entitySource) | ||
} | ||
|
||
type SliceVariableSource []input.VariableSource | ||
|
||
func (s SliceVariableSource) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) { | ||
var variables []deppy.Variable | ||
for _, variableSource := range s { | ||
inputVariables, err := variableSource.GetVariables(ctx, entitySource) | ||
if err != nil { | ||
return nil, err | ||
} | ||
variables = append(variables, inputVariables...) | ||
} | ||
|
||
return variables, nil | ||
} |
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