Skip to content

Commit

Permalink
Resolution POC: offline catalogs
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <[email protected]>
  • Loading branch information
Mikalai Radchuk committed Jun 8, 2023
1 parent 601c229 commit e086b91
Show file tree
Hide file tree
Showing 6 changed files with 1,177 additions and 25 deletions.
153 changes: 153 additions & 0 deletions cmd/resolutioncli/entity_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
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"
)

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["olm.bundle.path"] = 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
}
53 changes: 39 additions & 14 deletions cmd/resolutioncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package main

import (
"context"
"errors"
"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"
Expand All @@ -34,12 +34,19 @@ import (

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/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 (
flagNamePackageName = "package-name"
flagNamePackageVersion = "package-version"
flagNamePackageChannel = "package-channel"
flagNameIndexRef = "index-ref"
)

var (
scheme = runtime.NewScheme()
)
Expand All @@ -52,46 +59,64 @@ func init() {
}

func main() {
ctx := context.Background()

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")
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); err != nil {
if err := validateFlags(packageName, indexRef); err != nil {
fmt.Println(err)
flag.Usage()
os.Exit(1)
}

err := run(packageName, packageVersion, packageChannel)
err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func validateFlags(packageName string) error {
func validateFlags(packageName, indexRef string) error {
if packageName == "" {
return errors.New("missing required -package-name flag")
return fmt.Errorf("missing required -%s flag", flagNamePackageName)
}

if indexRef == "" {
return fmt.Errorf("missing required -%s flag", flagNameIndexRef)
}

return nil
}

func run(packageName, packageVersion, packageChannel string) error {
ctx := context.Background()
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)
}

packageVariableSource := NewPackageVariableSource(packageName, packageVersion, packageChannel)
resolver := solver.NewDeppySolver(
entitysources.NewCatalogdEntitySource(client),
append(olm.NestedVariableSource{packageVariableSource}, olm.NewOLMVariableSource(client)...),
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)
Expand Down
16 changes: 16 additions & 0 deletions cmd/resolutioncli/variable_source.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 (
Expand Down
Loading

0 comments on commit e086b91

Please sign in to comment.