Skip to content

Commit 1c3a3ad

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

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

cmd/resolutioncli/entity_source.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList,
116116
return es.entitiesCache, nil
117117
}
118118

119+
// TODO: Reduce code duplication: share a function with catalogdEntitySource (see getEntities)
120+
// We don't want to maintain two functions performing conversion into input.EntityList.
121+
// For this we need some common format. So we need a package which will be able
122+
// to convert from declfcg structs into CRD structs directly or via model.Model.
123+
// One option would be to make this piece of code from catalogd reusable and exportable:
124+
// https://github.com/operator-framework/catalogd/blob/9fe45a628de2e74d9cd73c3650fa2582aaac5213/pkg/controllers/core/catalog_controller.go#L200-L360
119125
func modelToEntities(model model.Model) (input.EntityList, error) {
120126
entities := input.EntityList{}
121127

cmd/resolutioncli/input_manifests.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
"fmt"
21+
"os"
22+
"path/filepath"
23+
24+
"k8s.io/apimachinery/pkg/runtime"
25+
)
26+
27+
func readManifestFiles(directory string) ([]runtime.Object, error) {
28+
var objects []runtime.Object
29+
30+
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
31+
if err != nil {
32+
return err
33+
}
34+
35+
if info.IsDir() {
36+
return nil
37+
}
38+
39+
fileContent, err := os.ReadFile(path)
40+
if err != nil {
41+
return fmt.Errorf("failed to read file %s: %w", path, err)
42+
}
43+
44+
decoder := codecs.UniversalDecoder(scheme.PrioritizedVersionsAllGroups()...)
45+
object, _, err := decoder.Decode(fileContent, nil, nil)
46+
if err != nil {
47+
return fmt.Errorf("failed to decode file %s: %w", path, err)
48+
}
49+
50+
objects = append(objects, object)
51+
52+
return nil
53+
})
54+
55+
if err != nil {
56+
return nil, fmt.Errorf("failed to read files: %w", err)
57+
}
58+
59+
return objects, nil
60+
}

cmd/resolutioncli/main.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import (
2626
"github.com/operator-framework/deppy/pkg/deppy/solver"
2727
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
2828
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/apimachinery/pkg/runtime/serializer"
2930
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3031
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3132
_ "k8s.io/client-go/plugin/pkg/client/auth"
32-
"sigs.k8s.io/controller-runtime/pkg/client"
33-
"sigs.k8s.io/controller-runtime/pkg/client/config"
33+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3434

3535
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
3636
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
@@ -45,10 +45,13 @@ const (
4545
flagNamePackageVersion = "package-version"
4646
flagNamePackageChannel = "package-channel"
4747
flagNameIndexRef = "index-ref"
48+
flagNameInputDir = "input-dir"
4849
)
4950

5051
var (
5152
scheme = runtime.NewScheme()
53+
54+
codecs = serializer.NewCodecFactory(scheme)
5255
)
5356

5457
func init() {
@@ -65,11 +68,13 @@ func main() {
6568
var packageVersion string
6669
var packageChannel string
6770
var indexRef string
71+
var inputDir string
6872
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
6973
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
7074
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
7175
// TODO: Consider adding support of multiple refs
7276
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
77+
flag.StringVar(&inputDir, flagNameInputDir, "", "Directory containing Kubernetes manifests (such as Operator) to be used as an input for resolution")
7378
flag.Parse()
7479

7580
if err := validateFlags(packageName, indexRef); err != nil {
@@ -78,7 +83,7 @@ func main() {
7883
os.Exit(1)
7984
}
8085

81-
err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
86+
err := run(ctx, packageName, packageVersion, packageChannel, indexRef, inputDir)
8287
if err != nil {
8388
fmt.Println(err)
8489
os.Exit(1)
@@ -97,18 +102,25 @@ func validateFlags(packageName, indexRef string) error {
97102
return nil
98103
}
99104

100-
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error {
101-
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
102-
if err != nil {
103-
return fmt.Errorf("failed to create client: %w", err)
105+
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef, inputDir string) error {
106+
clientBuilder := fake.NewClientBuilder().WithScheme(scheme)
107+
108+
if inputDir != "" {
109+
objects, err := readManifestFiles(inputDir)
110+
if err != nil {
111+
return err
112+
}
113+
114+
clientBuilder.WithRuntimeObjects(objects...)
104115
}
105116

117+
cl := clientBuilder.Build()
106118
resolver := solver.NewDeppySolver(
107119
NewIndexRefEntitySourceEntitySource(catalogRef),
108120
olm.NestedVariableSource{
109121
NewPackageVariableSource(packageName, packageVersion, packageChannel),
110122
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
111-
return olm.NewOperatorVariableSource(client, inputVariableSource), nil
123+
return olm.NewOperatorVariableSource(cl, inputVariableSource), nil
112124
},
113125
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
114126
return bundles_and_dependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil

0 commit comments

Comments
 (0)