@@ -18,25 +18,30 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
22
+ "io/fs"
21
23
22
24
apierrors "k8s.io/apimachinery/pkg/api/errors"
23
25
ctrl "sigs.k8s.io/controller-runtime"
24
- "sigs.k8s.io/controller-runtime/pkg/builder"
25
26
"sigs.k8s.io/controller-runtime/pkg/client"
26
- "sigs.k8s.io/controller-runtime/pkg/event"
27
- "sigs.k8s.io/controller-runtime/pkg/predicate"
28
27
29
28
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
30
29
)
31
30
32
- type CatalogCacheRemover interface {
31
+ type CatalogCache interface {
32
+ Get (catalogName , resolvedRef string ) (fs.FS , error )
33
33
Remove (catalogName string ) error
34
34
}
35
35
36
+ type CatalogCachePopulator interface {
37
+ PopulateCache (ctx context.Context , catalog * catalogd.ClusterCatalog ) (fs.FS , error )
38
+ }
39
+
36
40
// ClusterCatalogReconciler reconciles a ClusterCatalog object
37
41
type ClusterCatalogReconciler struct {
38
42
client.Client
39
- Cache CatalogCacheRemover
43
+ CatalogCache CatalogCache
44
+ CatalogCachePopulator CatalogCachePopulator
40
45
}
41
46
42
47
//+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
45
50
existingCatalog := & catalogd.ClusterCatalog {}
46
51
err := r .Client .Get (ctx , req .NamespacedName , existingCatalog )
47
52
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
49
57
}
50
58
if err != nil {
51
59
return ctrl.Result {}, err
52
60
}
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
+
53
84
return ctrl.Result {}, nil
54
85
}
55
86
56
87
// SetupWithManager sets up the controller with the Manager.
57
88
func (r * ClusterCatalogReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
58
89
_ , 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 {}).
73
91
Build (r )
74
92
75
93
return err
0 commit comments