Skip to content

Commit 1a4391b

Browse files
Mikalai Radchukm1kola
authored andcommitted
Resolution CLI POC: offline catalogs
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent b7a737a commit 1a4391b

File tree

14 files changed

+1624
-120
lines changed

14 files changed

+1624
-120
lines changed

cmd/manager/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
3838
"github.com/operator-framework/operator-controller/internal/controllers"
3939
"github.com/operator-framework/operator-controller/internal/resolution/entitysources"
40-
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
4140
"github.com/operator-framework/operator-controller/pkg/features"
4241
)
4342

@@ -105,7 +104,7 @@ func main() {
105104
Scheme: mgr.GetScheme(),
106105
Resolver: solver.NewDeppySolver(
107106
entitysources.NewCatalogdEntitySource(mgr.GetClient()),
108-
variablesources.NewOperatorVariableSource(mgr.GetClient()),
107+
controllers.NewVariableSource(mgr.GetClient()),
109108
),
110109
}).SetupWithManager(mgr); err != nil {
111110
setupLog.Error(err, "unable to create controller", "controller", "Operator")

cmd/resolutioncli/entity_source.go

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

cmd/resolutioncli/main.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
Copyright 2023.
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+
"flag"
22+
"fmt"
23+
"os"
24+
25+
"github.com/operator-framework/deppy/pkg/deppy/solver"
26+
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
29+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
30+
_ "k8s.io/client-go/plugin/pkg/client/auth"
31+
"sigs.k8s.io/controller-runtime/pkg/client"
32+
"sigs.k8s.io/controller-runtime/pkg/client/config"
33+
34+
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
35+
36+
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
37+
"github.com/operator-framework/operator-controller/internal/controllers"
38+
olmentity "github.com/operator-framework/operator-controller/internal/resolution/entities"
39+
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
40+
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
41+
)
42+
43+
const pocMessage = `This command is a proof of concept for off-cluster resolution and is not intended for production use!
44+
45+
Please provide your feedback and ideas via https://github.com/operator-framework/operator-controller/discussions/262`
46+
47+
const (
48+
flagNamePackageName = "package-name"
49+
flagNamePackageVersion = "package-version"
50+
flagNamePackageChannel = "package-channel"
51+
flagNameIndexRef = "index-ref"
52+
)
53+
54+
var (
55+
scheme = runtime.NewScheme()
56+
)
57+
58+
func init() {
59+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
60+
utilruntime.Must(operatorsv1alpha1.AddToScheme(scheme))
61+
utilruntime.Must(rukpakv1alpha1.AddToScheme(scheme))
62+
utilruntime.Must(catalogd.AddToScheme(scheme))
63+
}
64+
65+
func main() {
66+
fmt.Fprintf(os.Stderr, "\033[0;31m%s\033[0m\n", pocMessage)
67+
68+
ctx := context.Background()
69+
70+
var packageName string
71+
var packageVersion string
72+
var packageChannel string
73+
var indexRef string
74+
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
75+
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
76+
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
77+
// TODO: Consider adding support of multiple refs
78+
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
79+
flag.Parse()
80+
81+
if err := validateFlags(packageName, indexRef); err != nil {
82+
fmt.Fprintln(os.Stderr, err)
83+
flag.Usage()
84+
os.Exit(1)
85+
}
86+
87+
err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
88+
if err != nil {
89+
fmt.Fprintln(os.Stderr, err)
90+
os.Exit(1)
91+
}
92+
}
93+
94+
func validateFlags(packageName, indexRef string) error {
95+
if packageName == "" {
96+
return fmt.Errorf("missing required -%s flag", flagNamePackageName)
97+
}
98+
99+
if indexRef == "" {
100+
return fmt.Errorf("missing required -%s flag", flagNameIndexRef)
101+
}
102+
103+
return nil
104+
}
105+
106+
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error {
107+
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
108+
if err != nil {
109+
return fmt.Errorf("failed to create client: %w", err)
110+
}
111+
112+
resolver := solver.NewDeppySolver(
113+
newIndexRefEntitySourceEntitySource(catalogRef),
114+
append(
115+
variablesources.NestedVariableSource{newPackageVariableSource(packageName, packageVersion, packageChannel)},
116+
controllers.NewVariableSource(client)...,
117+
),
118+
)
119+
120+
bundleImage, err := resolve(ctx, resolver, packageName)
121+
if err != nil {
122+
return err
123+
}
124+
125+
fmt.Println(bundleImage)
126+
return nil
127+
}
128+
129+
func resolve(ctx context.Context, resolver *solver.DeppySolver, packageName string) (string, error) {
130+
solution, err := resolver.Solve(ctx)
131+
if err != nil {
132+
return "", err
133+
}
134+
135+
bundleEntity, err := getBundleEntityFromSolution(solution, packageName)
136+
if err != nil {
137+
return "", err
138+
}
139+
140+
// Get the bundle image reference for the bundle
141+
bundleImage, err := bundleEntity.BundlePath()
142+
if err != nil {
143+
return "", err
144+
}
145+
146+
return bundleImage, nil
147+
}
148+
149+
func getBundleEntityFromSolution(solution *solver.Solution, packageName string) (*olmentity.BundleEntity, error) {
150+
for _, variable := range solution.SelectedVariables() {
151+
switch v := variable.(type) {
152+
case *olmvariables.BundleVariable:
153+
entityPkgName, err := v.BundleEntity().PackageName()
154+
if err != nil {
155+
return nil, err
156+
}
157+
if packageName == entityPkgName {
158+
return v.BundleEntity(), nil
159+
}
160+
}
161+
}
162+
return nil, fmt.Errorf("entity for package %q not found in solution", packageName)
163+
}

cmd/resolutioncli/variable_source.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2023.
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+
"github.com/operator-framework/deppy/pkg/deppy/input"
21+
22+
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
23+
)
24+
25+
func newPackageVariableSource(packageName, packageVersion, packageChannel string) func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
26+
return func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
27+
pkgSource, err := variablesources.NewRequiredPackageVariableSource(
28+
packageName,
29+
variablesources.InVersionRange(packageVersion),
30+
variablesources.InChannel(packageChannel),
31+
)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
sliceSource := variablesources.SliceVariableSource{pkgSource}
37+
if inputVariableSource != nil {
38+
sliceSource = append(sliceSource, inputVariableSource)
39+
}
40+
41+
return sliceSource, nil
42+
}
43+
}

0 commit comments

Comments
 (0)