-
Notifications
You must be signed in to change notification settings - Fork 50
/
catalogsource_controller.go
203 lines (177 loc) · 6.63 KB
/
catalogsource_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package controllers
import (
"context"
"fmt"
"reflect"
"sync"
"time"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/operator-framework/operator-controller/internal/resolution/entity_sources/catalogsource"
)
const (
defaultCatalogSourceSyncInterval = 5 * time.Minute
defaultRegistryGRPCConnectionTimeout = 10 * time.Second
eventTypeNormal = "Normal"
eventTypeWarning = "Warning"
eventReasonCacheUpdated = "BundleCacheUpdated"
eventReasonCacheUpdateFailed = "BundleCacheUpdateFailed"
)
type CatalogSourceReconcilerOption func(reconciler *CatalogSourceReconciler)
func WithRegistryClient(registry catalogsource.RegistryClient) CatalogSourceReconcilerOption {
return func(reconciler *CatalogSourceReconciler) {
reconciler.registry = registry
}
}
func WithUnmanagedCatalogSourceSyncInterval(interval time.Duration) CatalogSourceReconcilerOption {
return func(reconciler *CatalogSourceReconciler) {
reconciler.unmanagedCatalogSourceSyncInterval = interval
}
}
// applyDefaults applies default values to empty CatalogSourceReconciler fields _after_ options have been applied
func applyDefaults() CatalogSourceReconcilerOption {
return func(reconciler *CatalogSourceReconciler) {
if reconciler.registry == nil {
reconciler.registry = catalogsource.NewRegistryGRPCClient(defaultRegistryGRPCConnectionTimeout)
}
if reconciler.unmanagedCatalogSourceSyncInterval == 0 {
reconciler.unmanagedCatalogSourceSyncInterval = defaultCatalogSourceSyncInterval
}
}
}
type CatalogSourceReconciler struct {
sync.RWMutex
client.Client
scheme *runtime.Scheme
registry catalogsource.RegistryClient
recorder record.EventRecorder
unmanagedCatalogSourceSyncInterval time.Duration
cache map[string]map[deppy.Identifier]*input.Entity
}
func NewCatalogSourceReconciler(client client.Client, scheme *runtime.Scheme, recorder record.EventRecorder, options ...CatalogSourceReconcilerOption) *CatalogSourceReconciler {
reconciler := &CatalogSourceReconciler{
RWMutex: sync.RWMutex{},
Client: client,
scheme: scheme,
recorder: recorder,
unmanagedCatalogSourceSyncInterval: 0,
cache: map[string]map[deppy.Identifier]*input.Entity{},
}
// apply options
options = append(options, applyDefaults())
for _, option := range options {
option(reconciler)
}
return reconciler
}
// +kubebuilder:rbac:groups=operators.coreos.com,resources=catalogsources,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
func (r *CatalogSourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx).WithName("catalogsource-controller")
l.V(1).Info("starting")
defer l.V(1).Info("ending")
var catalogSource = &v1alpha1.CatalogSource{}
if err := r.Client.Get(ctx, req.NamespacedName, catalogSource); err != nil {
if errors.IsNotFound(err) {
r.dropSource(req.String())
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
entities, err := r.registry.ListEntities(ctx, catalogSource)
// TODO: invalidate stale cache for failed updates
if err != nil {
r.recorder.Event(catalogSource, eventTypeWarning, eventReasonCacheUpdateFailed, fmt.Sprintf("Failed to update bundle cache from %s/%s: %v", catalogSource.GetNamespace(), catalogSource.GetName(), err))
return ctrl.Result{Requeue: !isManagedCatalogSource(*catalogSource)}, err
}
if updated := r.updateCache(req.String(), entities); updated {
r.recorder.Event(catalogSource, eventTypeNormal, eventReasonCacheUpdated, fmt.Sprintf("Successfully updated bundle cache from %s/%s", catalogSource.GetNamespace(), catalogSource.GetName()))
}
if isManagedCatalogSource(*catalogSource) {
return ctrl.Result{}, nil
}
return ctrl.Result{RequeueAfter: r.unmanagedCatalogSourceSyncInterval}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *CatalogSourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.CatalogSource{}).
Complete(r)
}
// TODO: find better way to identify catalogSources unmanaged by olm
func isManagedCatalogSource(catalogSource v1alpha1.CatalogSource) bool {
return len(catalogSource.Spec.Address) == 0
}
func (r *CatalogSourceReconciler) updateCache(sourceID string, entities []*input.Entity) bool {
newSourceCache := make(map[deppy.Identifier]*input.Entity)
for _, entity := range entities {
newSourceCache[entity.Identifier()] = entity
}
if _, ok := r.cache[sourceID]; ok && reflect.DeepEqual(r.cache[sourceID], newSourceCache) {
return false
}
r.RWMutex.Lock()
defer r.RWMutex.Unlock()
r.cache[sourceID] = newSourceCache
// return whether cache had updates
return true
}
func (r *CatalogSourceReconciler) dropSource(sourceID string) {
r.RWMutex.Lock()
defer r.RWMutex.Unlock()
delete(r.cache, sourceID)
}
func (r *CatalogSourceReconciler) Get(ctx context.Context, id deppy.Identifier) *input.Entity {
r.RWMutex.RLock()
defer r.RWMutex.RUnlock()
// don't count on deppy ID to reflect its catalogsource
for _, source := range r.cache {
if entity, ok := source[id]; ok {
return entity
}
}
return nil
}
func (r *CatalogSourceReconciler) Filter(ctx context.Context, filter input.Predicate) (input.EntityList, error) {
resultSet := input.EntityList{}
if err := r.Iterate(ctx, func(entity *input.Entity) error {
if filter(entity) {
resultSet = append(resultSet, *entity)
}
return nil
}); err != nil {
return nil, err
}
return resultSet, nil
}
func (r *CatalogSourceReconciler) GroupBy(ctx context.Context, fn input.GroupByFunction) (input.EntityListMap, error) {
resultSet := input.EntityListMap{}
if err := r.Iterate(ctx, func(entity *input.Entity) error {
keys := fn(entity)
for _, key := range keys {
resultSet[key] = append(resultSet[key], *entity)
}
return nil
}); err != nil {
return nil, err
}
return resultSet, nil
}
func (r *CatalogSourceReconciler) Iterate(ctx context.Context, fn input.IteratorFunction) error {
r.RWMutex.RLock()
defer r.RWMutex.RUnlock()
for _, source := range r.cache {
for _, entity := range source {
if err := fn(entity); err != nil {
return err
}
}
}
return nil
}