Skip to content

Commit

Permalink
Resolution CLI POC: offline catalogs
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 b7a737a commit fe187f1
Show file tree
Hide file tree
Showing 11 changed files with 1,429 additions and 140 deletions.
3 changes: 1 addition & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/internal/resolution/entitysources"
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
"github.com/operator-framework/operator-controller/pkg/features"
)

Expand Down Expand Up @@ -105,7 +104,7 @@ func main() {
Scheme: mgr.GetScheme(),
Resolver: solver.NewDeppySolver(
entitysources.NewCatalogdEntitySource(mgr.GetClient()),
variablesources.NewOperatorVariableSource(mgr.GetClient()),
controllers.NewVariableSource(mgr.GetClient()),
),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Operator")
Expand Down
155 changes: 155 additions & 0 deletions cmd/resolutioncli/entity_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
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 (
"context"
"encoding/json"
"fmt"

"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/operator-registry/alpha/action"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/model"
"github.com/operator-framework/operator-registry/alpha/property"

olmentity "github.com/operator-framework/operator-controller/internal/resolution/entities"
)

type indexRefEntitySource struct {
renderer action.Render
entitiesCache input.EntityList
}

func newIndexRefEntitySourceEntitySource(indexRef string) *indexRefEntitySource {
return &indexRefEntitySource{
renderer: action.Render{
Refs: []string{indexRef},
AllowedRefMask: action.RefDCImage | action.RefDCDir,
},
}
}

func (es *indexRefEntitySource) Get(_ context.Context, _ deppy.Identifier) (*input.Entity, error) {
panic("not implemented")
}

func (es *indexRefEntitySource) Filter(ctx context.Context, filter input.Predicate) (input.EntityList, error) {
entities, err := es.entities(ctx)
if err != nil {
return nil, err
}

resultSet := input.EntityList{}
for i := range entities {
if filter(&entities[i]) {
resultSet = append(resultSet, entities[i])
}
}
return resultSet, nil
}

func (es *indexRefEntitySource) GroupBy(ctx context.Context, fn input.GroupByFunction) (input.EntityListMap, error) {
entities, err := es.entities(ctx)
if err != nil {
return nil, err
}

resultSet := input.EntityListMap{}
for i := range entities {
keys := fn(&entities[i])
for _, key := range keys {
resultSet[key] = append(resultSet[key], entities[i])
}
}
return resultSet, nil
}

func (es *indexRefEntitySource) Iterate(ctx context.Context, fn input.IteratorFunction) error {
entities, err := es.entities(ctx)
if err != nil {
return err
}

for i := range entities {
if err := fn(&entities[i]); err != nil {
return err
}
}
return nil
}

func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList, error) {
if es.entitiesCache == nil {
cfg, err := es.renderer.Run(ctx)
if err != nil {
return nil, err
}

model, err := declcfg.ConvertToModel(*cfg)
if err != nil {
return nil, err
}

entities, err := modelToEntities(model)
if err != nil {
return nil, err
}

es.entitiesCache = entities
}

return es.entitiesCache, nil
}

func modelToEntities(model model.Model) (input.EntityList, error) {
entities := input.EntityList{}

for _, pkg := range model {
for _, ch := range pkg.Channels {
for _, bundle := range ch.Bundles {
props := map[string]string{}

for _, prop := range bundle.Properties {
switch prop.Type {
case property.TypePackage:
// this is already a json marshalled object, so it doesn't need to be marshalled
// like the other ones
props[property.TypePackage] = string(prop.Value)
}
}

imgValue, err := json.Marshal(bundle.Image)
if err != nil {
return nil, err
}
props[olmentity.PropertyBundlePath] = string(imgValue)

channelValue, _ := json.Marshal(property.Channel{ChannelName: ch.Name, Priority: 0})
props[property.TypeChannel] = string(channelValue)
entity := input.Entity{
ID: deppy.IdentifierFromString(fmt.Sprintf("%s%s%s", bundle.Name, bundle.Package.Name, ch.Name)),
Properties: props,
}
entities = append(entities, entity)
}
}
}

return entities, nil
}
163 changes: 163 additions & 0 deletions cmd/resolutioncli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
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 (
"context"
"flag"
"fmt"
"os"

"github.com/operator-framework/deppy/pkg/deppy/solver"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
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"

catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"

operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/controllers"
olmentity "github.com/operator-framework/operator-controller/internal/resolution/entities"
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
)

const pocMessage = `This command is a proof of concept for off-cluster resolution and is not intended for production use!
Please provide your feedback and ideas via https://github.com/operator-framework/operator-controller/discussions/262`

const (
flagNamePackageName = "package-name"
flagNamePackageVersion = "package-version"
flagNamePackageChannel = "package-channel"
flagNameIndexRef = "index-ref"
)

var (
scheme = runtime.NewScheme()
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(operatorsv1alpha1.AddToScheme(scheme))
utilruntime.Must(rukpakv1alpha1.AddToScheme(scheme))
utilruntime.Must(catalogd.AddToScheme(scheme))
}

func main() {
fmt.Fprintf(os.Stderr, "\033[0;31m%s\033[0m\n", pocMessage)

ctx := context.Background()

var packageName string
var packageVersion string
var packageChannel string
var indexRef 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.Parse()

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

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

func validateFlags(packageName, indexRef string) error {
if packageName == "" {
return fmt.Errorf("missing required -%s flag", flagNamePackageName)
}

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

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)
}

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

bundleImage, err := resolve(ctx, resolver, packageName)
if err != nil {
return err
}

fmt.Println(bundleImage)
return nil
}

func resolve(ctx context.Context, resolver *solver.DeppySolver, packageName string) (string, error) {
solution, err := resolver.Solve(ctx)
if err != nil {
return "", err
}

bundleEntity, err := getBundleEntityFromSolution(solution, packageName)
if err != nil {
return "", err
}

// Get the bundle image reference for the bundle
bundleImage, err := bundleEntity.BundlePath()
if err != nil {
return "", err
}

return bundleImage, nil
}

func getBundleEntityFromSolution(solution *solver.Solution, packageName string) (*olmentity.BundleEntity, error) {
for _, variable := range solution.SelectedVariables() {
switch v := variable.(type) {
case *olmvariables.BundleVariable:
entityPkgName, err := v.BundleEntity().PackageName()
if err != nil {
return nil, err
}
if packageName == entityPkgName {
return v.BundleEntity(), nil
}
}
}
return nil, fmt.Errorf("entity for package %q not found in solution", packageName)
}
43 changes: 43 additions & 0 deletions cmd/resolutioncli/variable_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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 (
"github.com/operator-framework/deppy/pkg/deppy/input"

"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
)

func newPackageVariableSource(packageName, packageVersion, packageChannel string) func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
return func(inputVariableSource input.VariableSource) (input.VariableSource, error) {
pkgSource, err := variablesources.NewRequiredPackageVariableSource(
packageName,
variablesources.InVersionRange(packageVersion),
variablesources.InChannel(packageChannel),
)
if err != nil {
return nil, err
}

sliceSource := variablesources.SliceVariableSource{pkgSource}
if inputVariableSource != nil {
sliceSource = append(sliceSource, inputVariableSource)
}

return sliceSource, nil
}
}
Loading

0 comments on commit fe187f1

Please sign in to comment.