Skip to content

Commit 588af97

Browse files
author
Mikalai Radchuk
committed
Resolution CLI POC: offline catalogs
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 211d3e4 commit 588af97

File tree

6 files changed

+1178
-26
lines changed

6 files changed

+1178
-26
lines changed

cmd/resolutioncli/entity_source.go

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

cmd/resolutioncli/main.go

Lines changed: 40 additions & 15 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"
@@ -32,14 +32,21 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/client"
3333
"sigs.k8s.io/controller-runtime/pkg/client/config"
3434

35-
catalogd "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1"
35+
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
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)