Skip to content

Commit

Permalink
WIP: Resolution POC: offline input
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Jun 9, 2023
1 parent 19185b5 commit d8f30a9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
29 changes: 20 additions & 9 deletions cmd/resolutioncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
"github.com/operator-framework/deppy/pkg/deppy/solver"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
Expand All @@ -45,10 +45,13 @@ const (
flagNamePackageVersion = "package-version"
flagNamePackageChannel = "package-channel"
flagNameIndexRef = "index-ref"
flagNameInputDir = "input-dir"
)

var (
scheme = runtime.NewScheme()

codecs = serializer.NewCodecFactory(scheme)
)

func init() {
Expand All @@ -65,27 +68,29 @@ func main() {
var packageVersion string
var packageChannel string
var indexRef string
var inputDir string
flag.StringVar(&packageName, flagNamePackageName, "", "Name of the package to resolve")
flag.StringVar(&packageVersion, flagNamePackageVersion, "", "Version of the package")
flag.StringVar(&packageChannel, flagNamePackageChannel, "", "Channel of the package")
// TODO: Consider adding support of multiple refs
flag.StringVar(&indexRef, flagNameIndexRef, "", "Index reference (FBC image or dir)")
flag.StringVar(&inputDir, flagNameInputDir, "", "Directory containing yaml files with Kubernetes objects (such as Operator)")
flag.Parse()

if err := validateFlags(packageName, indexRef); err != nil {
if err := validateFlags(packageName, indexRef, inputDir); err != nil {
fmt.Println(err)
flag.Usage()
os.Exit(1)
}

err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
err := run(ctx, packageName, packageVersion, packageChannel, indexRef, inputDir)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func validateFlags(packageName, indexRef string) error {
func validateFlags(packageName, indexRef, inputDir string) error {
if packageName == "" {
return fmt.Errorf("missing required -%s flag", flagNamePackageName)
}
Expand All @@ -94,21 +99,27 @@ func validateFlags(packageName, indexRef string) error {
return fmt.Errorf("missing required -%s flag", flagNameIndexRef)
}

if inputDir == "" {
return fmt.Errorf("missing required -%s flag", flagNameInputDir)
}

return nil
}

func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef string) error {
client, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme})
func run(ctx context.Context, packageName, packageVersion, packageChannel, catalogRef, inputDir string) error {
objects, err := readYAMLFiles(inputDir)
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
return err
}

cl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objects...).Build()

resolver := solver.NewDeppySolver(
NewIndexRefEntitySourceEntitySource(catalogRef),
olm.NestedVariableSource{
NewPackageVariableSource(packageName, packageVersion, packageChannel),
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
return olm.NewOperatorVariableSource(client, inputVariableSource), nil
return olm.NewOperatorVariableSource(cl, inputVariableSource), nil
},
func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
return bundles_and_dependencies.NewBundlesAndDepsVariableSource(inputVariableSource), nil
Expand Down
61 changes: 61 additions & 0 deletions cmd/resolutioncli/yamls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"k8s.io/apimachinery/pkg/runtime"
)

func readYAMLFiles(directory string) ([]runtime.Object, error) {
var objects []runtime.Object

err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

fileContent, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", path, err)
}

decoder := codecs.UniversalDecoder(scheme.PrioritizedVersionsAllGroups()...)
object, _, err := decoder.Decode(fileContent, nil, nil)
if err != nil {
return fmt.Errorf("failed to decode YAML file %s: %w", path, err)
}

objects = append(objects, object)

return nil
})

if err != nil {
return nil, fmt.Errorf("failed to read YAML files: %w", err)
}

return objects, nil
}

0 comments on commit d8f30a9

Please sign in to comment.