-
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.
Resolution CLI POC: offline catalogs
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
- Loading branch information
Showing
7 changed files
with
1,408 additions
and
14 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,155 @@ | ||
/* | ||
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" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/operator-framework/deppy/pkg/deppy" | ||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
"github.com/operator-framework/operator-registry/alpha/action" | ||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||
"github.com/operator-framework/operator-registry/alpha/model" | ||
"github.com/operator-framework/operator-registry/alpha/property" | ||
|
||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" | ||
) | ||
|
||
type indexRefEntitySource struct { | ||
renderer action.Render | ||
entitiesCache input.EntityList | ||
} | ||
|
||
func newIndexRefEntitySourceEntitySource(indexRef string) *indexRefEntitySource { | ||
return &indexRefEntitySource{ | ||
renderer: action.Render{ | ||
Refs: []string{indexRef}, | ||
AllowedRefMask: action.RefDCImage | action.RefDCDir, | ||
}, | ||
} | ||
} | ||
|
||
func (es *indexRefEntitySource) Get(_ context.Context, _ deppy.Identifier) (*input.Entity, error) { | ||
panic("not implemented") | ||
} | ||
|
||
func (es *indexRefEntitySource) Filter(ctx context.Context, filter input.Predicate) (input.EntityList, error) { | ||
entities, err := es.entities(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resultSet := input.EntityList{} | ||
for i := range entities { | ||
if filter(&entities[i]) { | ||
resultSet = append(resultSet, entities[i]) | ||
} | ||
} | ||
return resultSet, nil | ||
} | ||
|
||
func (es *indexRefEntitySource) GroupBy(ctx context.Context, fn input.GroupByFunction) (input.EntityListMap, error) { | ||
entities, err := es.entities(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resultSet := input.EntityListMap{} | ||
for i := range entities { | ||
keys := fn(&entities[i]) | ||
for _, key := range keys { | ||
resultSet[key] = append(resultSet[key], entities[i]) | ||
} | ||
} | ||
return resultSet, nil | ||
} | ||
|
||
func (es *indexRefEntitySource) Iterate(ctx context.Context, fn input.IteratorFunction) error { | ||
entities, err := es.entities(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := range entities { | ||
if err := fn(&entities[i]); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList, error) { | ||
if es.entitiesCache == nil { | ||
cfg, err := es.renderer.Run(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
model, err := declcfg.ConvertToModel(*cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
entities, err := modelToEntities(model) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
es.entitiesCache = entities | ||
} | ||
|
||
return es.entitiesCache, nil | ||
} | ||
|
||
func modelToEntities(model model.Model) (input.EntityList, error) { | ||
entities := input.EntityList{} | ||
|
||
for _, pkg := range model { | ||
for _, ch := range pkg.Channels { | ||
for _, bundle := range ch.Bundles { | ||
props := map[string]string{} | ||
|
||
for _, prop := range bundle.Properties { | ||
switch prop.Type { | ||
case property.TypePackage: | ||
// this is already a json marshalled object, so it doesn't need to be marshalled | ||
// like the other ones | ||
props[property.TypePackage] = string(prop.Value) | ||
} | ||
} | ||
|
||
imgValue, err := json.Marshal(bundle.Image) | ||
if err != nil { | ||
return nil, err | ||
} | ||
props[entity.PropertyBundlePath] = string(imgValue) | ||
|
||
channelValue, _ := json.Marshal(property.Channel{ChannelName: ch.Name, Priority: 0}) | ||
props[property.TypeChannel] = string(channelValue) | ||
entity := input.Entity{ | ||
ID: deppy.IdentifierFromString(fmt.Sprintf("%s%s%s", bundle.Name, bundle.Package.Name, ch.Name)), | ||
Properties: props, | ||
} | ||
entities = append(entities, entity) | ||
} | ||
} | ||
} | ||
|
||
return entities, 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,172 @@ | ||
/* | ||
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" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/operator-framework/deppy/pkg/deppy/input" | ||
"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/api/core/v1alpha1" | ||
|
||
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundlesanddependencies" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/crdconstraints" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" | ||
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm" | ||
) | ||
|
||
const pocMessage = `This command is a proof of concept for off-cluster resolution and is not intended for production use! | ||
Please provide your feedback and ideas via https://github.com/operator-framework/operator-controller/discussions/262` | ||
|
||
const ( | ||
flagNamePackageName = "package-name" | ||
flagNamePackageVersion = "package-version" | ||
flagNamePackageChannel = "package-channel" | ||
flagNameIndexRef = "index-ref" | ||
) | ||
|
||
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() { | ||
fmt.Fprintf(os.Stderr, "\033[0;31m%s\033[0m\n", pocMessage) | ||
|
||
ctx := context.Background() | ||
|
||
var packageName string | ||
var packageVersion string | ||
var packageChannel string | ||
var indexRef string | ||
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve") | ||
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package") | ||
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package") | ||
// TODO: Consider adding support of multiple refs | ||
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)") | ||
flag.Parse() | ||
|
||
if err := validateFlags(packageName, indexRef); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
err := run(ctx, packageName, packageVersion, packageChannel, indexRef) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func validateFlags(packageName, indexRef string) error { | ||
if packageName == "" { | ||
return fmt.Errorf("missing required -%s flag", flagNamePackageName) | ||
} | ||
|
||
if indexRef == "" { | ||
return fmt.Errorf("missing required -%s flag", flagNameIndexRef) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error { | ||
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create client: %w", err) | ||
} | ||
|
||
resolver := solver.NewDeppySolver( | ||
newIndexRefEntitySourceEntitySource(catalogRef), | ||
olm.NestedVariableSource{ | ||
newPackageVariableSource(packageName, packageVersion, packageChannel), | ||
func(inputVariableSource input.VariableSource) (input.VariableSource, error) { | ||
return olm.NewOperatorVariableSource(client, inputVariableSource), nil | ||
}, | ||
func(inputVariableSource input.VariableSource) (input.VariableSource, error) { | ||
return bundlesanddependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil | ||
}, | ||
func(inputVariableSource input.VariableSource) (input.VariableSource, error) { | ||
return crdconstraints.NewCRDUniquenessConstraintsVariableSource(inputVariableSource), nil | ||
}, | ||
}, | ||
) | ||
|
||
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 *bundlesanddependencies.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,44 @@ | ||
/* | ||
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 ( | ||
"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/requiredpackage" | ||
) | ||
|
||
func newPackageVariableSource(packageName, packageVersion, packageChannel string) func(inputVariableSource input.VariableSource) (input.VariableSource, error) { | ||
return func(inputVariableSource input.VariableSource) (input.VariableSource, error) { | ||
pkgSource, err := requiredpackage.NewRequiredPackage( | ||
packageName, | ||
requiredpackage.InVersionRange(packageVersion), | ||
requiredpackage.InChannel(packageChannel), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sliceSource := olm.SliceVariableSource{pkgSource} | ||
if inputVariableSource != nil { | ||
sliceSource = append(sliceSource, inputVariableSource) | ||
} | ||
|
||
return sliceSource, nil | ||
} | ||
} |
Oops, something went wrong.