Skip to content

Commit 93e0010

Browse files
author
Mikalai Radchuk
committed
Resolution POC: offline catalogs
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 99f1a1b commit 93e0010

File tree

6 files changed

+1166
-20
lines changed

6 files changed

+1166
-20
lines changed

cmd/resolutioncli/entity_source.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
"encoding/json"
22+
"fmt"
23+
24+
"github.com/operator-framework/deppy/pkg/deppy"
25+
"github.com/operator-framework/deppy/pkg/deppy/input"
26+
"github.com/operator-framework/operator-registry/alpha/action"
27+
"github.com/operator-framework/operator-registry/alpha/declcfg"
28+
"github.com/operator-framework/operator-registry/alpha/model"
29+
"github.com/operator-framework/operator-registry/alpha/property"
30+
)
31+
32+
type indexRefEntitySource struct {
33+
renderer action.Render
34+
entitiesCache input.EntityList
35+
}
36+
37+
func NewIndexRefEntitySourceEntitySource(indexRef string) *indexRefEntitySource {
38+
return &indexRefEntitySource{
39+
renderer: action.Render{
40+
Refs: []string{indexRef},
41+
AllowedRefMask: action.RefDCImage | action.RefDCDir,
42+
},
43+
}
44+
}
45+
46+
func (es *indexRefEntitySource) Get(ctx context.Context, id deppy.Identifier) (*input.Entity, error) {
47+
panic("not implemented")
48+
}
49+
50+
func (es *indexRefEntitySource) Filter(ctx context.Context, filter input.Predicate) (input.EntityList, error) {
51+
entities, err := es.entities(ctx)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
resultSet := input.EntityList{}
57+
for _, entity := range entities {
58+
if filter(&entity) {
59+
resultSet = append(resultSet, entity)
60+
}
61+
}
62+
return resultSet, nil
63+
}
64+
65+
func (es *indexRefEntitySource) GroupBy(ctx context.Context, fn input.GroupByFunction) (input.EntityListMap, error) {
66+
entities, err := es.entities(ctx)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
resultSet := input.EntityListMap{}
72+
for _, entity := range entities {
73+
keys := fn(&entity)
74+
for _, key := range keys {
75+
resultSet[key] = append(resultSet[key], entity)
76+
}
77+
}
78+
return resultSet, nil
79+
}
80+
81+
func (es *indexRefEntitySource) Iterate(ctx context.Context, fn input.IteratorFunction) error {
82+
entities, err := es.entities(ctx)
83+
if err != nil {
84+
return err
85+
}
86+
87+
for _, entity := range entities {
88+
if err := fn(&entity); err != nil {
89+
return err
90+
}
91+
}
92+
return nil
93+
}
94+
95+
func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList, error) {
96+
if es.entitiesCache == nil {
97+
cfg, err := es.renderer.Run(ctx)
98+
if err != nil {
99+
return nil, err
100+
}
101+
102+
model, err := declcfg.ConvertToModel(*cfg)
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
entities, err := modelToEntities(model)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
es.entitiesCache = entities
113+
}
114+
115+
return es.entitiesCache, nil
116+
}
117+
118+
func modelToEntities(model model.Model) (input.EntityList, error) {
119+
entities := input.EntityList{}
120+
121+
for _, pkg := range model {
122+
for _, ch := range pkg.Channels {
123+
for _, bundle := range ch.Bundles {
124+
props := map[string]string{}
125+
126+
for _, prop := range bundle.Properties {
127+
switch prop.Type {
128+
case property.TypePackage:
129+
// this is already a json marshalled object, so it doesn't need to be marshalled
130+
// like the other ones
131+
props[property.TypePackage] = string(prop.Value)
132+
}
133+
}
134+
135+
imgValue, err := json.Marshal(bundle.Image)
136+
if err != nil {
137+
return nil, err
138+
}
139+
props["olm.bundle.path"] = string(imgValue)
140+
141+
channelValue, _ := json.Marshal(property.Channel{ChannelName: ch.Name, Priority: 0})
142+
props[property.TypeChannel] = string(channelValue)
143+
entity := input.Entity{
144+
ID: deppy.IdentifierFromString(fmt.Sprintf("%s%s%s", bundle.Name, bundle.Package.Name, ch.Name)),
145+
Properties: props,
146+
}
147+
entities = append(entities, entity)
148+
}
149+
}
150+
}
151+
152+
return entities, nil
153+
}

cmd/resolutioncli/main.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"os"
2525

26+
"github.com/operator-framework/deppy/pkg/deppy/input"
2627
"github.com/operator-framework/deppy/pkg/deppy/solver"
2728
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
2829
"k8s.io/apimachinery/pkg/runtime"
@@ -34,8 +35,8 @@ import (
3435

3536
catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1"
3637
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
37-
"github.com/operator-framework/operator-controller/internal/resolution/entitysources"
3838
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies"
39+
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/crd_constraints"
3940
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity"
4041
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm"
4142
)
@@ -52,46 +53,64 @@ func init() {
5253
}
5354

5455
func main() {
56+
ctx := context.Background()
57+
5558
var packageName string
5659
var packageVersion string
5760
var packageChannel string
61+
var indexRef string
5862
flag.StringVar(&packageName, "package-name", "", "Name of the package to resolve")
5963
flag.StringVar(&packageVersion, "package-version", "", "Version of the package")
6064
flag.StringVar(&packageChannel, "package-channel", "", "Channel of the package")
65+
// TODO: Consider adding support of multiple refs
66+
flag.StringVar(&indexRef, "index-ref", "", "Index reference (FBC image or dir)")
6167
flag.Parse()
6268

63-
if err := validateFlags(packageName); err != nil {
69+
if err := validateFlags(packageName, indexRef); err != nil {
6470
fmt.Println(err)
6571
flag.Usage()
6672
os.Exit(1)
6773
}
6874

69-
err := run(packageName, packageVersion, packageChannel)
75+
err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
7076
if err != nil {
7177
fmt.Println(err)
7278
os.Exit(1)
7379
}
7480
}
7581

76-
func validateFlags(packageName string) error {
82+
func validateFlags(packageName, indexRef string) error {
7783
if packageName == "" {
7884
return errors.New("missing required -package-name flag")
7985
}
8086

87+
if indexRef == "" {
88+
return errors.New("missing required -index-ref flag")
89+
}
90+
8191
return nil
8292
}
8393

84-
func run(packageName, packageVersion, packageChannel string) error {
85-
ctx := context.Background()
94+
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error {
8695
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
8796
if err != nil {
8897
return fmt.Errorf("failed to create client: %w", err)
8998
}
9099

91-
packageVariableSource := NewPackageVariableSource(packageName, packageVersion, packageChannel)
92100
resolver := solver.NewDeppySolver(
93-
entitysources.NewCatalogdEntitySource(client),
94-
append(olm.NestedVariableSource{packageVariableSource}, olm.NewOLMVariableSource(client)...),
101+
NewIndexRefEntitySourceEntitySource(catalogRef),
102+
olm.NestedVariableSource{
103+
NewPackageVariableSource(packageName, packageVersion, packageChannel),
104+
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
105+
return olm.NewOperatorVariableSource(client, inputVariableSource), nil
106+
},
107+
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
108+
return bundles_and_dependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil
109+
},
110+
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
111+
return crd_constraints.NewCRDUniquenessConstraintsVariableSource(inputVariableSource), nil
112+
},
113+
},
95114
)
96115

97116
bundleImage, err := resolve(ctx, resolver, packageName)

cmd/resolutioncli/variable_source.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package main
218

319
import (

go.mod

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,128 @@ require (
1919
)
2020

2121
require (
22+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
23+
github.com/Microsoft/go-winio v0.5.2 // indirect
24+
github.com/Microsoft/hcsshim v0.9.6 // indirect
25+
github.com/acomagu/bufpipe v1.0.3 // indirect
26+
github.com/adrg/xdg v0.4.0 // indirect
27+
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
28+
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
2229
github.com/beorn7/perks v1.0.1 // indirect
30+
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
2331
github.com/cespare/xxhash/v2 v2.1.2 // indirect
32+
github.com/containerd/cgroups v1.0.4 // indirect
33+
github.com/containerd/containerd v1.6.15 // indirect
34+
github.com/containerd/continuity v0.3.0 // indirect
35+
github.com/containerd/ttrpc v1.1.0 // indirect
2436
github.com/davecgh/go-spew v1.1.1 // indirect
37+
github.com/docker/cli v20.10.21+incompatible // indirect
38+
github.com/docker/distribution v2.8.1+incompatible // indirect
39+
github.com/docker/docker v20.10.21+incompatible // indirect
40+
github.com/docker/docker-credential-helpers v0.7.0 // indirect
41+
github.com/docker/go-connections v0.4.0 // indirect
42+
github.com/docker/go-metrics v0.0.1 // indirect
43+
github.com/docker/go-units v0.4.0 // indirect
2544
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
2645
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
2746
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
47+
github.com/felixge/httpsnoop v1.0.3 // indirect
2848
github.com/fsnotify/fsnotify v1.6.0 // indirect
49+
github.com/ghodss/yaml v1.0.0 // indirect
2950
github.com/go-air/gini v1.0.4 // indirect
51+
github.com/go-git/gcfg v1.5.0 // indirect
52+
github.com/go-git/go-billy/v5 v5.4.1 // indirect
53+
github.com/go-git/go-git/v5 v5.4.2 // indirect
54+
github.com/go-logr/stdr v1.2.2 // indirect
3055
github.com/go-logr/zapr v1.2.3 // indirect
3156
github.com/go-openapi/jsonpointer v0.19.5 // indirect
3257
github.com/go-openapi/jsonreference v0.20.0 // indirect
3358
github.com/go-openapi/swag v0.19.14 // indirect
3459
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
3560
github.com/gogo/protobuf v1.3.2 // indirect
61+
github.com/golang-migrate/migrate/v4 v4.6.2 // indirect
3662
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3763
github.com/golang/protobuf v1.5.2 // indirect
64+
github.com/golang/snappy v0.0.3 // indirect
65+
github.com/google/cel-go v0.12.6 // indirect
3866
github.com/google/gnostic v0.5.7-v3refs // indirect
3967
github.com/google/go-cmp v0.5.9 // indirect
4068
github.com/google/gofuzz v1.2.0 // indirect
4169
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
4270
github.com/google/uuid v1.3.0 // indirect
71+
github.com/gorilla/mux v1.8.0 // indirect
72+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
73+
github.com/h2non/filetype v1.1.1 // indirect
74+
github.com/h2non/go-is-svg v0.0.0-20160927212452-35e8c4b0612c // indirect
4375
github.com/imdario/mergo v0.3.13 // indirect
76+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
77+
github.com/joelanford/ignore v0.0.0-20210607151042-0d25dc18b62d // indirect
4478
github.com/josharian/intern v1.0.0 // indirect
4579
github.com/json-iterator/go v1.1.12 // indirect
80+
github.com/klauspost/compress v1.12.3 // indirect
4681
github.com/mailru/easyjson v0.7.6 // indirect
82+
github.com/mattn/go-sqlite3 v1.14.10 // indirect
4783
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
84+
github.com/mitchellh/mapstructure v1.4.3 // indirect
85+
github.com/moby/locker v1.0.1 // indirect
86+
github.com/moby/sys/mountinfo v0.5.0 // indirect
87+
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
4888
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4989
github.com/modern-go/reflect2 v1.0.2 // indirect
90+
github.com/morikuni/aec v1.0.0 // indirect
5091
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
92+
github.com/opencontainers/go-digest v1.0.0 // indirect
93+
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
94+
github.com/operator-framework/api v0.17.3 // indirect
95+
github.com/otiai10/copy v1.2.0 // indirect
5196
github.com/pkg/errors v0.9.1 // indirect
97+
github.com/pmezard/go-difflib v1.0.0 // indirect
5298
github.com/prometheus/client_golang v1.14.0 // indirect
5399
github.com/prometheus/client_model v0.3.0 // indirect
54100
github.com/prometheus/common v0.37.0 // indirect
55101
github.com/prometheus/procfs v0.8.0 // indirect
102+
github.com/sirupsen/logrus v1.9.0 // indirect
56103
github.com/spf13/pflag v1.0.5 // indirect
104+
github.com/stoewer/go-strcase v1.2.0 // indirect
105+
github.com/stretchr/testify v1.8.1 // indirect
106+
go.etcd.io/bbolt v1.3.6 // indirect
107+
go.opencensus.io v0.23.0 // indirect
108+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.35.0 // indirect
109+
go.opentelemetry.io/otel v1.10.0 // indirect
110+
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0 // indirect
111+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.10.0 // indirect
112+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0 // indirect
113+
go.opentelemetry.io/otel/metric v0.31.0 // indirect
114+
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
115+
go.opentelemetry.io/otel/trace v1.10.0 // indirect
116+
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
57117
go.uber.org/atomic v1.7.0 // indirect
58118
go.uber.org/multierr v1.6.0 // indirect
59119
golang.org/x/net v0.7.0 // indirect
60120
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
121+
golang.org/x/sync v0.1.0 // indirect
61122
golang.org/x/sys v0.5.0 // indirect
62123
golang.org/x/term v0.5.0 // indirect
63124
golang.org/x/text v0.7.0 // indirect
64125
golang.org/x/time v0.3.0 // indirect
65126
golang.org/x/tools v0.6.0 // indirect
66127
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
67128
google.golang.org/appengine v1.6.7 // indirect
129+
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
130+
google.golang.org/grpc v1.49.0 // indirect
68131
google.golang.org/protobuf v1.28.1 // indirect
69132
gopkg.in/inf.v0 v0.9.1 // indirect
133+
gopkg.in/warnings.v0 v0.1.2 // indirect
70134
gopkg.in/yaml.v2 v2.4.0 // indirect
71135
gopkg.in/yaml.v3 v3.0.1 // indirect
72136
k8s.io/api v0.26.1 // indirect
73137
k8s.io/apiextensions-apiserver v0.26.1 // indirect
138+
k8s.io/apiserver v0.26.1 // indirect
74139
k8s.io/component-base v0.26.1 // indirect
75140
k8s.io/klog/v2 v2.80.1 // indirect
76141
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
142+
rsc.io/letsencrypt v0.0.3 // indirect
143+
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.35 // indirect
77144
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
78145
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
79146
sigs.k8s.io/yaml v1.3.0 // indirect

0 commit comments

Comments
 (0)