@@ -18,25 +18,30 @@ package controllers
1818
1919import (
2020 "context"
21+ "fmt"
22+ "io/fs"
2123
2224 apierrors "k8s.io/apimachinery/pkg/api/errors"
2325 ctrl "sigs.k8s.io/controller-runtime"
24- "sigs.k8s.io/controller-runtime/pkg/builder"
2526 "sigs.k8s.io/controller-runtime/pkg/client"
26- "sigs.k8s.io/controller-runtime/pkg/event"
27- "sigs.k8s.io/controller-runtime/pkg/predicate"
2827
2928 catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
3029)
3130
32- type CatalogCacheRemover interface {
31+ type CatalogCache interface {
32+ Get (catalogName , resolvedRef string ) (fs.FS , error )
3333 Remove (catalogName string ) error
3434}
3535
36+ type CatalogCachePopulator interface {
37+ PopulateCache (ctx context.Context , catalog * catalogd.ClusterCatalog ) (fs.FS , error )
38+ }
39+
3640// ClusterCatalogReconciler reconciles a ClusterCatalog object
3741type ClusterCatalogReconciler struct {
3842 client.Client
39- Cache CatalogCacheRemover
43+ CatalogCache CatalogCache
44+ CatalogCachePopulator CatalogCachePopulator
4045}
4146
4247//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clustercatalogs,verbs=get;list;watch
@@ -45,31 +50,44 @@ func (r *ClusterCatalogReconciler) Reconcile(ctx context.Context, req ctrl.Reque
4550 existingCatalog := & catalogd.ClusterCatalog {}
4651 err := r .Client .Get (ctx , req .NamespacedName , existingCatalog )
4752 if apierrors .IsNotFound (err ) {
48- return ctrl.Result {}, r .Cache .Remove (req .Name )
53+ if err := r .CatalogCache .Remove (req .Name ); err != nil {
54+ return ctrl.Result {}, fmt .Errorf ("error removing cache for catalog %q: %v" , req .Name , err )
55+ }
56+ return ctrl.Result {}, nil
4957 }
5058 if err != nil {
5159 return ctrl.Result {}, err
5260 }
61+
62+ if existingCatalog .Status .ResolvedSource == nil ||
63+ existingCatalog .Status .ResolvedSource .Image == nil ||
64+ existingCatalog .Status .ResolvedSource .Image .Ref == "" {
65+ // Reference is not known yet - skip cache population with no error.
66+ // Once the reference is resolved another reconcile cycle
67+ // will be triggered and we will progress further.
68+ return ctrl.Result {}, nil
69+ }
70+
71+ catalogFsys , err := r .CatalogCache .Get (existingCatalog .Name , existingCatalog .Status .ResolvedSource .Image .Ref )
72+ if err != nil {
73+ return ctrl.Result {}, fmt .Errorf ("error retrieving cache for catalog %q: %v" , existingCatalog .Name , err )
74+ }
75+ if catalogFsys != nil {
76+ // Cache already exists so we do not need to populate it
77+ return ctrl.Result {}, nil
78+ }
79+
80+ if _ , err = r .CatalogCachePopulator .PopulateCache (ctx , existingCatalog ); err != nil {
81+ return ctrl.Result {}, fmt .Errorf ("error populating cache for catalog %q: %v" , existingCatalog .Name , err )
82+ }
83+
5384 return ctrl.Result {}, nil
5485}
5586
5687// SetupWithManager sets up the controller with the Manager.
5788func (r * ClusterCatalogReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
5889 _ , err := ctrl .NewControllerManagedBy (mgr ).
59- For (& catalogd.ClusterCatalog {}, builder .WithPredicates (predicate.Funcs {
60- CreateFunc : func (e event.CreateEvent ) bool {
61- return false
62- },
63- UpdateFunc : func (e event.UpdateEvent ) bool {
64- return false
65- },
66- DeleteFunc : func (e event.DeleteEvent ) bool {
67- return true
68- },
69- GenericFunc : func (e event.GenericEvent ) bool {
70- return false
71- },
72- })).
90+ For (& catalogd.ClusterCatalog {}).
7391 Build (r )
7492
7593 return err
0 commit comments