|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "flag" |
| 23 | + "fmt" |
| 24 | + "os" |
| 25 | + |
| 26 | + "github.com/operator-framework/deppy/pkg/deppy/solver" |
| 27 | + rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 30 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 31 | + _ "k8s.io/client-go/plugin/pkg/client/auth" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 34 | + |
| 35 | + catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1" |
| 36 | + operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" |
| 37 | + "github.com/operator-framework/operator-controller/internal/resolution/entitysources" |
| 38 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies" |
| 39 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" |
| 40 | + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm" |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + scheme = runtime.NewScheme() |
| 45 | +) |
| 46 | + |
| 47 | +func init() { |
| 48 | + utilruntime.Must(clientgoscheme.AddToScheme(scheme)) |
| 49 | + utilruntime.Must(operatorsv1alpha1.AddToScheme(scheme)) |
| 50 | + utilruntime.Must(rukpakv1alpha1.AddToScheme(scheme)) |
| 51 | + utilruntime.Must(catalogd.AddToScheme(scheme)) |
| 52 | +} |
| 53 | + |
| 54 | +func main() { |
| 55 | + var packageName string |
| 56 | + var packageVersion string |
| 57 | + var packageChannel string |
| 58 | + flag.StringVar(&packageName, "package-name", "", "Name of the package to resolve") |
| 59 | + flag.StringVar(&packageVersion, "package-version", "", "Version of the package") |
| 60 | + flag.StringVar(&packageChannel, "package-channel", "", "Channel of the package") |
| 61 | + flag.Parse() |
| 62 | + |
| 63 | + if err := validateFlags(packageName); err != nil { |
| 64 | + fmt.Println(err) |
| 65 | + flag.Usage() |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + |
| 69 | + err := run(packageName, packageVersion, packageChannel) |
| 70 | + if err != nil { |
| 71 | + fmt.Println(err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func validateFlags(packageName string) error { |
| 77 | + if packageName == "" { |
| 78 | + return errors.New("missing required -package-name flag") |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func run(packageName, packageVersion, packageChannel string) error { |
| 85 | + ctx := context.Background() |
| 86 | + client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme}) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to create client: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + packageVariableSource := NewPackageVariableSource(packageName, packageVersion, packageChannel) |
| 92 | + resolver := solver.NewDeppySolver( |
| 93 | + entitysources.NewCatalogdEntitySource(client), |
| 94 | + append(olm.NestedVariableSource{packageVariableSource}, olm.NewOLMVariableSource(client)...), |
| 95 | + ) |
| 96 | + |
| 97 | + bundleImage, err := resolve(ctx, resolver, packageName) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + fmt.Println(bundleImage) |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func resolve(ctx context.Context, resolver *solver.DeppySolver, packageName string) (string, error) { |
| 107 | + solution, err := resolver.Solve(ctx) |
| 108 | + if err != nil { |
| 109 | + return "", err |
| 110 | + } |
| 111 | + |
| 112 | + bundleEntity, err := getBundleEntityFromSolution(solution, packageName) |
| 113 | + if err != nil { |
| 114 | + return "", err |
| 115 | + } |
| 116 | + |
| 117 | + // Get the bundle image reference for the bundle |
| 118 | + bundleImage, err := bundleEntity.BundlePath() |
| 119 | + if err != nil { |
| 120 | + return "", err |
| 121 | + } |
| 122 | + |
| 123 | + return bundleImage, nil |
| 124 | +} |
| 125 | + |
| 126 | +func getBundleEntityFromSolution(solution *solver.Solution, packageName string) (*entity.BundleEntity, error) { |
| 127 | + for _, variable := range solution.SelectedVariables() { |
| 128 | + switch v := variable.(type) { |
| 129 | + case *bundles_and_dependencies.BundleVariable: |
| 130 | + entityPkgName, err := v.BundleEntity().PackageName() |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + if packageName == entityPkgName { |
| 135 | + return v.BundleEntity(), nil |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + return nil, fmt.Errorf("entity for package %q not found in solution", packageName) |
| 140 | +} |
0 commit comments