Skip to content

Commit e086b91

Browse files
author
Mikalai Radchuk
committed
Resolution POC: offline catalogs
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 601c229 commit e086b91

File tree

6 files changed

+1177
-25
lines changed

6 files changed

+1177
-25
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: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package main
1818

1919
import (
2020
"context"
21-
"errors"
2221
"flag"
2322
"fmt"
2423
"os"
2524

25+
"github.com/operator-framework/deppy/pkg/deppy/input"
2626
"github.com/operator-framework/deppy/pkg/deppy/solver"
2727
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
2828
"k8s.io/apimachinery/pkg/runtime"
@@ -34,12 +34,19 @@ import (
3434

3535
catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1"
3636
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
37-
"github.com/operator-framework/operator-controller/internal/resolution/entitysources"
3837
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies"
38+
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/crd_constraints"
3939
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity"
4040
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm"
4141
)
4242

43+
const (
44+
flagNamePackageName = "package-name"
45+
flagNamePackageVersion = "package-version"
46+
flagNamePackageChannel = "package-channel"
47+
flagNameIndexRef = "index-ref"
48+
)
49+
4350
var (
4451
scheme = runtime.NewScheme()
4552
)
@@ -52,46 +59,64 @@ func init() {
5259
}
5360

5461
func main() {
62+
ctx := context.Background()
63+
5564
var packageName string
5665
var packageVersion string
5766
var packageChannel string
58-
flag.StringVar(&packageName, "package-name", "", "Name of the package to resolve")
59-
flag.StringVar(&packageVersion, "package-version", "", "Version of the package")
60-
flag.StringVar(&packageChannel, "package-channel", "", "Channel of the package")
67+
var indexRef string
68+
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
69+
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
70+
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
71+
// TODO: Consider adding support of multiple refs
72+
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
6173
flag.Parse()
6274

63-
if err := validateFlags(packageName); err != nil {
75+
if err := validateFlags(packageName, indexRef); err != nil {
6476
fmt.Println(err)
6577
flag.Usage()
6678
os.Exit(1)
6779
}
6880

69-
err := run(packageName, packageVersion, packageChannel)
81+
err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
7082
if err != nil {
7183
fmt.Println(err)
7284
os.Exit(1)
7385
}
7486
}
7587

76-
func validateFlags(packageName string) error {
88+
func validateFlags(packageName, indexRef string) error {
7789
if packageName == "" {
78-
return errors.New("missing required -package-name flag")
90+
return fmt.Errorf("missing required -%s flag", flagNamePackageName)
91+
}
92+
93+
if indexRef == "" {
94+
return fmt.Errorf("missing required -%s flag", flagNameIndexRef)
7995
}
8096

8197
return nil
8298
}
8399

84-
func run(packageName, packageVersion, packageChannel string) error {
85-
ctx := context.Background()
100+
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error {
86101
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
87102
if err != nil {
88103
return fmt.Errorf("failed to create client: %w", err)
89104
}
90105

91-
packageVariableSource := NewPackageVariableSource(packageName, packageVersion, packageChannel)
92106
resolver := solver.NewDeppySolver(
93-
entitysources.NewCatalogdEntitySource(client),
94-
append(olm.NestedVariableSource{packageVariableSource}, olm.NewOLMVariableSource(client)...),
107+
NewIndexRefEntitySourceEntitySource(catalogRef),
108+
olm.NestedVariableSource{
109+
NewPackageVariableSource(packageName, packageVersion, packageChannel),
110+
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
111+
return olm.NewOperatorVariableSource(client, inputVariableSource), nil
112+
},
113+
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
114+
return bundles_and_dependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil
115+
},
116+
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
117+
return crd_constraints.NewCRDUniquenessConstraintsVariableSource(inputVariableSource), nil
118+
},
119+
},
95120
)
96121

97122
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 (

0 commit comments

Comments
 (0)