Skip to content

Commit b785cc2

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

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

cmd/resolutioncli/main.go

+20-9
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/pkg/apis/core/v1beta1"
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,27 +68,29 @@ 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 yaml files with Kubernetes objects (such as Operator)")
7378
flag.Parse()
7479

75-
if err := validateFlags(packageName, indexRef); err != nil {
80+
if err := validateFlags(packageName, indexRef, inputDir); err != nil {
7681
fmt.Println(err)
7782
flag.Usage()
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)
8590
}
8691
}
8792

88-
func validateFlags(packageName, indexRef string) error {
93+
func validateFlags(packageName, indexRef, inputDir string) error {
8994
if packageName == "" {
9095
return fmt.Errorf("missing required -%s flag", flagNamePackageName)
9196
}
@@ -94,21 +99,27 @@ func validateFlags(packageName, indexRef string) error {
9499
return fmt.Errorf("missing required -%s flag", flagNameIndexRef)
95100
}
96101

102+
if inputDir == "" {
103+
return fmt.Errorf("missing required -%s flag", flagNameInputDir)
104+
}
105+
97106
return nil
98107
}
99108

100-
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error {
101-
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
109+
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef, inputDir string) error {
110+
objects, err := readYAMLFiles(inputDir)
102111
if err != nil {
103-
return fmt.Errorf("failed to create client: %w", err)
112+
return err
104113
}
105114

115+
cl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objects...).Build()
116+
106117
resolver := solver.NewDeppySolver(
107118
NewIndexRefEntitySourceEntitySource(catalogRef),
108119
olm.NestedVariableSource{
109120
NewPackageVariableSource(packageName, packageVersion, packageChannel),
110121
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
111-
return olm.NewOperatorVariableSource(client, inputVariableSource), nil
122+
return olm.NewOperatorVariableSource(cl, inputVariableSource), nil
112123
},
113124
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
114125
return bundles_and_dependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil

cmd/resolutioncli/yamls.go

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

0 commit comments

Comments
 (0)