Skip to content

Commit b4029e8

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

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

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 & 9 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,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 := readManifestFiles(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

0 commit comments

Comments
 (0)