Skip to content

Commit

Permalink
Resolution CLI POC: offline catalogs
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Jun 21, 2023
1 parent c61abe5 commit bfbc24f
Show file tree
Hide file tree
Showing 7 changed files with 1,389 additions and 15 deletions.
154 changes: 154 additions & 0 deletions cmd/resolutioncli/entity_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
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-controller/internal/resolution/variable_sources/entity"
"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"
)

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(ctx context.Context, id 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 _, entity := range entities {
if filter(&entity) {
resultSet = append(resultSet, entity)
}
}
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 _, entity := range entities {
keys := fn(&entity)
for _, key := range keys {
resultSet[key] = append(resultSet[key], entity)
}
}
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 _, entity := range entities {
if err := fn(&entity); 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
}
171 changes: 171 additions & 0 deletions cmd/resolutioncli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
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/bundles_and_dependencies"
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/crd_constraints"
"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 bundles_and_dependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil
},
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
return crd_constraints.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 *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)
}
43 changes: 43 additions & 0 deletions cmd/resolutioncli/variable_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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/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
}
}
Loading

0 comments on commit bfbc24f

Please sign in to comment.