Skip to content

Commit 41eeb55

Browse files
committed
Use TransformFunc
Unit tests not updated Signed-off-by: Todd Short <[email protected]>
1 parent 2ad0272 commit 41eeb55

File tree

6 files changed

+122
-165
lines changed

6 files changed

+122
-165
lines changed

pkg/controller/operators/catalog/operator.go

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -230,38 +230,53 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
230230

231231
// Fields are pruned from local copies of the objects managed
232232
// by this informer in order to reduce cached size.
233-
prunedCSVInformer := cache.NewSharedIndexInformer(
234-
pruning.NewListerWatcher(op.client, metav1.NamespaceAll,
233+
prunedCSVInformer := cache.NewSharedIndexInformerWithOptions(
234+
pruning.NewListerWatcher(
235+
op.client,
236+
metav1.NamespaceAll,
235237
func(options *metav1.ListOptions) {
236238
options.LabelSelector = fmt.Sprintf("!%s", v1alpha1.CopiedLabelKey)
237239
},
238-
pruning.PrunerFunc(func(csv *v1alpha1.ClusterServiceVersion) {
239-
*csv = v1alpha1.ClusterServiceVersion{
240-
TypeMeta: csv.TypeMeta,
241-
ObjectMeta: metav1.ObjectMeta{
242-
Name: csv.Name,
243-
Namespace: csv.Namespace,
244-
Labels: csv.Labels,
245-
Annotations: csv.Annotations,
246-
},
247-
Spec: v1alpha1.ClusterServiceVersionSpec{
248-
CustomResourceDefinitions: csv.Spec.CustomResourceDefinitions,
249-
APIServiceDefinitions: csv.Spec.APIServiceDefinitions,
250-
Replaces: csv.Spec.Replaces,
251-
Version: csv.Spec.Version,
252-
},
253-
Status: v1alpha1.ClusterServiceVersionStatus{
254-
Phase: csv.Status.Phase,
255-
Reason: csv.Status.Reason,
256-
},
257-
}
258-
})),
240+
),
259241
&v1alpha1.ClusterServiceVersion{},
260-
resyncPeriod(),
261-
cache.Indexers{
262-
cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
242+
cache.SharedIndexInformerOptions{
243+
ResyncPeriod: resyncPeriod(),
244+
Indexers: cache.Indexers{
245+
cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
246+
},
263247
},
264248
)
249+
250+
// Transformed the CSV to be just the necessary data
251+
prunedCSVTransformFunc := func(i interface{}) (interface{}, error) {
252+
if csv, ok := i.(*v1alpha1.ClusterServiceVersion); ok {
253+
*csv = v1alpha1.ClusterServiceVersion{
254+
TypeMeta: csv.TypeMeta,
255+
ObjectMeta: metav1.ObjectMeta{
256+
Name: csv.Name,
257+
Namespace: csv.Namespace,
258+
Labels: csv.Labels,
259+
Annotations: csv.Annotations,
260+
},
261+
Spec: v1alpha1.ClusterServiceVersionSpec{
262+
CustomResourceDefinitions: csv.Spec.CustomResourceDefinitions,
263+
APIServiceDefinitions: csv.Spec.APIServiceDefinitions,
264+
Replaces: csv.Spec.Replaces,
265+
Version: csv.Spec.Version,
266+
},
267+
Status: v1alpha1.ClusterServiceVersionStatus{
268+
Phase: csv.Status.Phase,
269+
Reason: csv.Status.Reason,
270+
},
271+
}
272+
return csv, nil
273+
}
274+
return nil, fmt.Errorf("Unable to convert input to CSV")
275+
}
276+
277+
if err := prunedCSVInformer.SetTransform(prunedCSVTransformFunc); err != nil {
278+
return nil, err
279+
}
265280
csvLister := operatorsv1alpha1listers.NewClusterServiceVersionLister(prunedCSVInformer.GetIndexer())
266281
op.lister.OperatorsV1alpha1().RegisterClusterServiceVersionLister(metav1.NamespaceAll, csvLister)
267282
if err := op.RegisterInformer(prunedCSVInformer); err != nil {

pkg/controller/operators/internal/pruning/listerwatcher.go

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,18 @@ import (
88
"k8s.io/apimachinery/pkg/watch"
99
"k8s.io/client-go/tools/cache"
1010

11-
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1211
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
1312
)
1413

15-
type Pruner interface {
16-
Prune(*v1alpha1.ClusterServiceVersion)
17-
}
18-
19-
type PrunerFunc func(*v1alpha1.ClusterServiceVersion)
20-
21-
func (f PrunerFunc) Prune(csv *v1alpha1.ClusterServiceVersion) {
22-
f(csv)
23-
}
24-
25-
func NewListerWatcher(client versioned.Interface, namespace string, override func(*metav1.ListOptions), p Pruner) cache.ListerWatcher {
14+
func NewListerWatcher(client versioned.Interface, namespace string, override func(*metav1.ListOptions)) cache.ListerWatcher {
2615
return &cache.ListWatch{
2716
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
2817
override(&options)
29-
list, err := client.OperatorsV1alpha1().ClusterServiceVersions(namespace).List(context.TODO(), options)
30-
if err != nil {
31-
return list, err
32-
}
33-
for i := range list.Items {
34-
p.Prune(&list.Items[i])
35-
}
36-
return list, nil
18+
return client.OperatorsV1alpha1().ClusterServiceVersions(namespace).List(context.TODO(), options)
3719
},
3820
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
3921
override(&options)
40-
w, err := client.OperatorsV1alpha1().ClusterServiceVersions(namespace).Watch(context.TODO(), options)
41-
if err != nil {
42-
return w, err
43-
}
44-
return watch.Filter(w, watch.FilterFunc(func(e watch.Event) (watch.Event, bool) {
45-
if csv, ok := e.Object.(*v1alpha1.ClusterServiceVersion); ok {
46-
p.Prune(csv)
47-
}
48-
return e, true
49-
})), nil
22+
return client.OperatorsV1alpha1().ClusterServiceVersions(namespace).Watch(context.TODO(), options)
5023
},
5124
}
5225
}

pkg/controller/operators/olm/operator.go

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ import (
4343

4444
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
4545
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/informers/externalversions"
46+
operatorsv1alpha1listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
4647
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/certs"
4748
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
49+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/internal/pruning"
4850
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/labeller"
4951
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides"
5052
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/plugins"
@@ -63,6 +65,14 @@ import (
6365
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
6466
)
6567

68+
const (
69+
// These annotation keys are intentionally invalid -- all writes
70+
// to copied CSVs are regenerated from the corresponding non-copied CSV,
71+
// so it should never be transmitted back to the API server.
72+
copyCSVStatusHash = "$copyhash-status"
73+
copyCSVSpecHash = "$copyhash-spec"
74+
)
75+
6676
var (
6777
ErrRequirementsNotMet = errors.New("requirements were not met")
6878
ErrCRDOwnerConflict = errors.New("conflicting CRD owner in namespace")
@@ -82,7 +92,7 @@ type Operator struct {
8292
client versioned.Interface
8393
lister operatorlister.OperatorLister
8494
protectedCopiedCSVNamespaces map[string]struct{}
85-
copiedCSVLister metadatalister.Lister
95+
copiedCSVLister operatorsv1alpha1listers.ClusterServiceVersionLister
8696
ogQueueSet *queueinformer.ResourceQueueSet
8797
csvQueueSet *queueinformer.ResourceQueueSet
8898
olmConfigQueue workqueue.TypedRateLimitingInterface[types.NamespacedName]
@@ -294,20 +304,54 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
294304
return nil, err
295305
}
296306

297-
// A separate informer solely for CSV copies. Object metadata requests are used
307+
// A separate informer solely for CSV copies. Fields
308+
// are pruned from local copies of the objects managed
298309
// by this informer in order to reduce cached size.
299-
gvr := v1alpha1.SchemeGroupVersion.WithResource("clusterserviceversions")
300-
copiedCSVInformer := metadatainformer.NewFilteredMetadataInformer(
301-
config.metadataClient,
302-
gvr,
303-
namespace,
304-
config.resyncPeriod(),
305-
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
306-
func(options *metav1.ListOptions) {
307-
options.LabelSelector = v1alpha1.CopiedLabelKey
310+
copiedCSVInformer := cache.NewSharedIndexInformerWithOptions(
311+
pruning.NewListerWatcher(
312+
op.client,
313+
namespace,
314+
func(opts *metav1.ListOptions) {
315+
opts.LabelSelector = v1alpha1.CopiedLabelKey
316+
},
317+
),
318+
&v1alpha1.ClusterServiceVersion{},
319+
cache.SharedIndexInformerOptions{
320+
ResyncPeriod: config.resyncPeriod(),
321+
Indexers: cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
308322
},
309-
).Informer()
310-
op.copiedCSVLister = metadatalister.New(copiedCSVInformer.GetIndexer(), gvr)
323+
)
324+
325+
// Transform the copied CSV to be just the Metadata
326+
// However, because we have the full copied CSV, we can calculate
327+
// a hash over the full CSV to store as an annotation
328+
copiedCSVTransformFunc := func(i interface{}) (interface{}, error) {
329+
if csv, ok := i.(*v1alpha1.ClusterServiceVersion); ok {
330+
specHash, statusHash, err := copyableCSVHash(csv)
331+
if err != nil {
332+
return nil, err
333+
}
334+
*csv = v1alpha1.ClusterServiceVersion{
335+
TypeMeta: csv.TypeMeta,
336+
ObjectMeta: csv.ObjectMeta,
337+
Spec: v1alpha1.ClusterServiceVersionSpec{},
338+
Status: v1alpha1.ClusterServiceVersionStatus{},
339+
}
340+
if csv.Annotations == nil {
341+
csv.Annotations = make(map[string]string, 2)
342+
}
343+
// fake CSV hashes for tracking purposes only
344+
csv.Annotations[copyCSVSpecHash] = specHash
345+
csv.Annotations[copyCSVStatusHash] = statusHash
346+
return csv, nil
347+
}
348+
return nil, fmt.Errorf("Unable to convert input to CSV")
349+
}
350+
351+
if err := copiedCSVInformer.SetTransform(copiedCSVTransformFunc); err != nil {
352+
return nil, err
353+
}
354+
op.copiedCSVLister = operatorsv1alpha1listers.NewClusterServiceVersionLister(copiedCSVInformer.GetIndexer())
311355
informersByNamespace[namespace].CopiedCSVInformer = copiedCSVInformer
312356
informersByNamespace[namespace].CopiedCSVLister = op.copiedCSVLister
313357

0 commit comments

Comments
 (0)