Skip to content

Commit

Permalink
Resolution CLI 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 29, 2023
1 parent fe187f1 commit 7e3c73a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cmd/resolutioncli/entity_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList,
return es.entitiesCache, nil
}

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

Expand Down
60 changes: 60 additions & 0 deletions cmd/resolutioncli/input_manifests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2023.
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"
"os"
"path/filepath"

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

func readManifestFiles(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 := os.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 file %s: %w", path, err)
}

objects = append(objects, object)

return nil
})

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

return objects, nil
}
28 changes: 20 additions & 8 deletions cmd/resolutioncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,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"

Expand All @@ -49,10 +49,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 @@ -71,11 +74,13 @@ 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 Kubernetes manifests (such as Operator) to be used as an input for resolution")
flag.Parse()

if err := validateFlags(packageName, indexRef); err != nil {
Expand All @@ -84,7 +89,7 @@ func main() {
os.Exit(1)
}

err := run(ctx, packageName, packageVersion, packageChannel, indexRef)
err := run(ctx, packageName, packageVersion, packageChannel, indexRef, inputDir)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand All @@ -103,17 +108,24 @@ func validateFlags(packageName, indexRef string) error {
return nil
}

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

if inputDir != "" {
objects, err := readManifestFiles(inputDir)
if err != nil {
return err
}

clientBuilder.WithRuntimeObjects(objects...)
}

cl := clientBuilder.Build()
resolver := solver.NewDeppySolver(
newIndexRefEntitySourceEntitySource(catalogRef),
append(
variablesources.NestedVariableSource{newPackageVariableSource(packageName, packageVersion, packageChannel)},
controllers.NewVariableSource(client)...,
controllers.NewVariableSource(cl)...,
),
)

Expand Down

0 comments on commit 7e3c73a

Please sign in to comment.