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 <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Jun 8, 2023
1 parent 99f1a1b commit 93e0010
Show file tree
Hide file tree
Showing 6 changed files with 1,166 additions and 20 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
}
37 changes: 28 additions & 9 deletions cmd/resolutioncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"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,8 +35,8 @@ 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"
)
Expand All @@ -52,46 +53,64 @@ func init() {
}

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

var packageName string
var packageVersion string
var packageChannel string
var indexRef 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")
// TODO: Consider adding support of multiple refs
flag.StringVar(&indexRef, "index-ref", "", "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")
}

if indexRef == "" {
return errors.New("missing required -index-ref flag")
}

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
67 changes: 67 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,128 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.9.6 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/adrg/xdg v0.4.0 // indirect
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/containerd v1.6.15 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/containerd/ttrpc v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.21+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.21+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-air/gini v1.0.4 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-migrate/migrate/v4 v4.6.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/cel-go v0.12.6 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/h2non/filetype v1.1.1 // indirect
github.com/h2non/go-is-svg v0.0.0-20160927212452-35e8c4b0612c // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/joelanford/ignore v0.0.0-20210607151042-0d25dc18b62d // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-sqlite3 v1.14.10 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/sys/mountinfo v0.5.0 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/operator-framework/api v0.17.3 // indirect
github.com/otiai10/copy v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.35.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
google.golang.org/grpc v1.49.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.26.1 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/apiserver v0.26.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
rsc.io/letsencrypt v0.0.3 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.35 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading

0 comments on commit 93e0010

Please sign in to comment.