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 <[email protected]>
  • Loading branch information
joelanford committed Jul 16, 2024
1 parent 957fc1b commit b59ac9e
Show file tree
Hide file tree
Showing 30 changed files with 1,843 additions and 3,184 deletions.
58 changes: 36 additions & 22 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ 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"
crdupgradesafety "github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
"github.com/operator-framework/operator-controller/internal/resolve"
"github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
"github.com/operator-framework/operator-controller/internal/rukpak/source"
"github.com/operator-framework/operator-controller/internal/version"
"github.com/operator-framework/operator-controller/pkg/features"
Expand Down Expand Up @@ -153,25 +154,6 @@ func main() {
os.Exit(1)
}

certPool, err := httputil.NewCertPool(caCertDir)
if err != nil {
setupLog.Error(err, "unable to create CA certificate pool")
os.Exit(1)
}

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

cl := mgr.GetClient()
catalogsCachePath := filepath.Join(cachePath, "catalogs")
if err := os.MkdirAll(catalogsCachePath, 0700); err != nil {
setupLog.Error(err, "unable to create catalogs cache directory")
os.Exit(1)
}
catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(catalogsCachePath, httpClient))

installNamespaceMapper := helmclient.ObjectToStringMapper(func(obj client.Object) (string, error) {
ext := obj.(*ocv1alpha1.ClusterExtension)
return ext.Spec.InstallNamespace, nil
Expand All @@ -194,14 +176,19 @@ func main() {
os.Exit(1)
}

clusterExtensionFinalizers := crfinalizer.NewFinalizers()
certPool, err := httputil.NewCertPool(caCertDir)
if err != nil {
setupLog.Error(err, "unable to create CA certificate pool")
os.Exit(1)
}
unpacker := &source.ImageRegistry{
BaseCachePath: filepath.Join(cachePath, "unpack"),
// TODO: This needs to be derived per extension via ext.Spec.InstallNamespace
AuthNamespace: systemNamespace,
CaCertPool: certPool,
}

clusterExtensionFinalizers := crfinalizer.NewFinalizers()
domain := ocv1alpha1.GroupVersion.Group
cleanupUnpackCacheKey := fmt.Sprintf("%s/cleanup-unpack-cache", domain)
if err := clusterExtensionFinalizers.Register(cleanupUnpackCacheKey, finalizerFunc(func(ctx context.Context, obj client.Object) (crfinalizer.Result, error) {
Expand All @@ -222,9 +209,36 @@ func main() {
crdupgradesafety.NewPreflight(aeClient.CustomResourceDefinitions()),
}

cl := mgr.GetClient()
httpClient, err := httputil.BuildHTTPClient(certPool)
if err != nil {
setupLog.Error(err, "unable to create catalogd http client")
os.Exit(1)
}

catalogsCachePath := filepath.Join(cachePath, "catalogs")
if err := os.MkdirAll(catalogsCachePath, 0700); err != nil {
setupLog.Error(err, "unable to create catalogs cache directory")
os.Exit(1)
}
catalogClient := catalogclient.New(cache.NewFilesystemCache(catalogsCachePath, 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,
Resolver: resolver,
ActionClientGetter: acg,
Unpacker: unpacker,
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
Expand Down
38 changes: 38 additions & 0 deletions internal/bundleutil/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 name and version.
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 b59ac9e

Please sign in to comment.