Skip to content

Commit

Permalink
remove catalogmetadata types, introduce to resolver interface
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford committed Jul 12, 2024
1 parent 60d26ed commit 7b0983c
Show file tree
Hide file tree
Showing 30 changed files with 1,847 additions and 3,175 deletions.
35 changes: 24 additions & 11 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ import (
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/internal/httputil"
"github.com/operator-framework/operator-controller/internal/labels"
"github.com/operator-framework/operator-controller/internal/resolve"
registryv1handler "github.com/operator-framework/operator-controller/internal/rukpak/handler"
crdupgradesafety "github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
"github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
"github.com/operator-framework/operator-controller/internal/rukpak/provisioner/registry"
"github.com/operator-framework/operator-controller/internal/rukpak/source"
"github.com/operator-framework/operator-controller/internal/rukpak/storage"
Expand Down Expand Up @@ -155,14 +156,6 @@ func main() {
os.Exit(1)
}

httpClient, err := httputil.BuildHTTPClient(caCertDir)
if err != nil {
setupLog.Error(err, "unable to create catalogd http client")
}

cl := mgr.GetClient()
catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(cachePath, httpClient))

installNamespaceMapper := helmclient.ObjectToStringMapper(func(obj client.Object) (string, error) {
ext := obj.(*ocv1alpha1.ClusterExtension)
return ext.Spec.InstallNamespace, nil
Expand Down Expand Up @@ -229,13 +222,33 @@ func main() {
crdupgradesafety.NewPreflight(aeClient.CustomResourceDefinitions()),
}

cl := mgr.GetClient()
httpClient, err := httputil.BuildHTTPClient(caCertDir)
if err != nil {
setupLog.Error(err, "unable to create catalogd http client")
}
catalogClient := catalogclient.New(cache.NewFilesystemCache(cachePath, httpClient))

resolver := &resolve.CatalogResolver{
WalkCatalogsFunc: resolve.CatalogWalker(
func(ctx context.Context, option ...client.ListOption) ([]catalogd.ClusterCatalog, error) {
var catalogs catalogd.ClusterCatalogList
if err := cl.List(ctx, &catalogs, option...); err != nil {
return nil, err
}
return catalogs.Items, nil
},
catalogClient.GetPackage,
),
}

if err = (&controllers.ClusterExtensionReconciler{
Client: cl,
BundleProvider: catalogClient,
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
Resolver: resolver,
ActionClientGetter: acg,
Unpacker: unpacker,
Storage: localStorage,
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
Finalizers: clusterExtensionFinalizers,
CaCertDir: caCertDir,
Expand Down
39 changes: 39 additions & 0 deletions internal/bundleutil/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package bundleutil

import (
"encoding/json"
"fmt"

bsemver "github.com/blang/semver/v4"

"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"

ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
)

func GetVersion(b declcfg.Bundle) (*bsemver.Version, error) {
for _, p := range b.Properties {
if p.Type == property.TypePackage {
var pkg property.Package
if err := json.Unmarshal(p.Value, &pkg); err != nil {
return nil, fmt.Errorf("error unmarshalling package property: %w", err)
}
vers, err := bsemver.Parse(pkg.Version)
if err != nil {
return nil, err
}
return &vers, nil
}
}
return nil, fmt.Errorf("no package property found in bundle %q", b.Name)
}

// MetadataFor returns a BundleMetadata for the given bundle. If the provided bundle is nil,
// this function panics. It is up to the caller to ensure that the bundle is non-nil.
func MetadataFor(bundleName string, bundleVersion *bsemver.Version) *ocv1alpha1.BundleMetadata {
return &ocv1alpha1.BundleMetadata{
Name: bundleName,
Version: bundleVersion.String(),
}
}
24 changes: 12 additions & 12 deletions internal/catalogmetadata/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestFilesystemCache(t *testing.T) {
catalog *catalogd.ClusterCatalog
contents fstest.MapFS
wantErr bool
tripper *MockTripper
tripper *mockTripper
testCaching bool
shouldHitCache bool
}
Expand All @@ -89,7 +89,7 @@ func TestFilesystemCache(t *testing.T) {
},
},
contents: defaultFS,
tripper: &MockTripper{},
tripper: &mockTripper{},
},
{
name: "valid cached fetch",
Expand All @@ -107,7 +107,7 @@ func TestFilesystemCache(t *testing.T) {
},
},
contents: defaultFS,
tripper: &MockTripper{},
tripper: &mockTripper{},
testCaching: true,
shouldHitCache: true,
},
Expand All @@ -127,7 +127,7 @@ func TestFilesystemCache(t *testing.T) {
},
},
contents: defaultFS,
tripper: &MockTripper{},
tripper: &mockTripper{},
testCaching: true,
shouldHitCache: false,
},
Expand All @@ -147,7 +147,7 @@ func TestFilesystemCache(t *testing.T) {
},
},
contents: defaultFS,
tripper: &MockTripper{shouldError: true},
tripper: &mockTripper{shouldError: true},
wantErr: true,
},
{
Expand All @@ -166,14 +166,14 @@ func TestFilesystemCache(t *testing.T) {
},
},
contents: defaultFS,
tripper: &MockTripper{serverError: true},
tripper: &mockTripper{serverError: true},
wantErr: true,
},
{
name: "nil catalog",
catalog: nil,
contents: defaultFS,
tripper: &MockTripper{serverError: true},
tripper: &mockTripper{serverError: true},
wantErr: true,
},
{
Expand All @@ -187,7 +187,7 @@ func TestFilesystemCache(t *testing.T) {
},
},
contents: defaultFS,
tripper: &MockTripper{serverError: true},
tripper: &mockTripper{serverError: true},
wantErr: true,
},
{
Expand All @@ -203,7 +203,7 @@ func TestFilesystemCache(t *testing.T) {
},
},
contents: defaultFS,
tripper: &MockTripper{serverError: true},
tripper: &mockTripper{serverError: true},
wantErr: true,
},
} {
Expand Down Expand Up @@ -242,15 +242,15 @@ func TestFilesystemCache(t *testing.T) {
}
}

var _ http.RoundTripper = &MockTripper{}
var _ http.RoundTripper = &mockTripper{}

type MockTripper struct {
type mockTripper struct {
content fstest.MapFS
shouldError bool
serverError bool
}

func (mt *MockTripper) RoundTrip(_ *http.Request) (*http.Response, error) {
func (mt *mockTripper) RoundTrip(_ *http.Request) (*http.Response, error) {
if mt.shouldError {
return nil, errors.New("mock tripper error")
}
Expand Down
Loading

0 comments on commit 7b0983c

Please sign in to comment.