Skip to content

Commit 63474c8

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

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
@@ -117,6 +117,12 @@ func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList,
117117
return es.entitiesCache, nil
118118
}
119119

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

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

@@ -50,10 +50,13 @@ const (
5050
flagNamePackageVersion = "package-version"
5151
flagNamePackageChannel = "package-channel"
5252
flagNameIndexRef = "index-ref"
53+
flagNameInputDir = "input-dir"
5354
)
5455

5556
var (
5657
scheme = runtime.NewScheme()
58+
59+
codecs = serializer.NewCodecFactory(scheme)
5760
)
5861

5962
func init() {
@@ -72,11 +75,13 @@ func main() {
7275
var packageVersion string
7376
var packageChannel string
7477
var indexRef string
78+
var inputDir string
7579
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
7680
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
7781
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
7882
// TODO: Consider adding support of multiple refs
7983
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
84+
flag.StringVar(&inputDir, flagNameInputDir, "", "Directory containing Kubernetes manifests (such as Operator) to be used as an input for resolution")
8085
flag.Parse()
8186

8287
if err := validateFlags(packageName, indexRef); err != nil {
@@ -85,7 +90,7 @@ func main() {
8590
os.Exit(1)
8691
}
8792

88-
err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
93+
err := run(ctx, packageName, packageVersion, packageChannel, indexRef, inputDir)
8994
if err != nil {
9095
fmt.Fprintln(os.Stderr, err)
9196
os.Exit(1)
@@ -104,18 +109,25 @@ func validateFlags(packageName, indexRef string) error {
104109
return nil
105110
}
106111

107-
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error {
108-
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
109-
if err != nil {
110-
return fmt.Errorf("failed to create client: %w", err)
112+
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef, inputDir string) error {
113+
clientBuilder := fake.NewClientBuilder().WithScheme(scheme)
114+
115+
if inputDir != "" {
116+
objects, err := readManifestFiles(inputDir)
117+
if err != nil {
118+
return err
119+
}
120+
121+
clientBuilder.WithRuntimeObjects(objects...)
111122
}
112123

124+
cl := clientBuilder.Build()
113125
resolver := solver.NewDeppySolver(
114126
newIndexRefEntitySourceEntitySource(catalogRef),
115127
olm.NestedVariableSource{
116128
newPackageVariableSource(packageName, packageVersion, packageChannel),
117129
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
118-
return olm.NewOperatorVariableSource(client, inputVariableSource), nil
130+
return olm.NewOperatorVariableSource(cl, inputVariableSource), nil
119131
},
120132
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
121133
return bundlesanddependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil

0 commit comments

Comments
 (0)