Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ef9f9a3

Browse files
committedAug 3, 2023
operators/olm: use a partial object metadata watch for copied CSVs
All we ever ned to know about copied CSVs is their metadata. No need to prune objects in memory, it's better to never allocate the memory to deserilize them in the first place. Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent 58adbea commit ef9f9a3

File tree

16 files changed

+1283
-258
lines changed

16 files changed

+1283
-258
lines changed
 

‎cmd/olm/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/sirupsen/logrus"
1515
"github.com/spf13/pflag"
1616
corev1 "k8s.io/api/core/v1"
17+
"k8s.io/client-go/metadata"
1718
"k8s.io/klog"
1819
ctrl "sigs.k8s.io/controller-runtime"
1920

@@ -154,6 +155,10 @@ func main() {
154155
if err != nil {
155156
logger.WithError(err).Fatal("error configuring custom resource client")
156157
}
158+
metadataClient, err := metadata.NewForConfig(config)
159+
if err != nil {
160+
logger.WithError(err).Fatal("error configuring metadata client")
161+
}
157162

158163
// Create a new instance of the operator.
159164
op, err := olm.NewOperator(
@@ -162,6 +167,7 @@ func main() {
162167
olm.WithWatchedNamespaces(namespaces...),
163168
olm.WithResyncPeriod(queueinformer.ResyncWithJitter(*wakeupInterval, 0.2)),
164169
olm.WithExternalClient(crClient),
170+
olm.WithMetadataClient(metadataClient),
165171
olm.WithOperatorClient(opClient),
166172
olm.WithRestConfig(config),
167173
olm.WithConfigClient(versionedConfigClient),

‎deploy/chart/crds/0000_50_olm_00-clusterserviceversions.crd.yaml

+88-37
Large diffs are not rendered by default.

‎deploy/chart/crds/0000_50_olm_00-subscriptions.crd.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ spec:
644644
type: object
645645
properties:
646646
claims:
647-
description: "Claims lists the names of resources, defined in spec.resourceClaims, that are used by this container. \n This is an alpha field and requires enabling the DynamicResourceAllocation feature gate. \n This field is immutable."
647+
description: "Claims lists the names of resources, defined in spec.resourceClaims, that are used by this container. \n This is an alpha field and requires enabling the DynamicResourceAllocation feature gate. \n This field is immutable. It can only be set for containers."
648648
type: array
649649
items:
650650
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
@@ -668,7 +668,7 @@ spec:
668668
- type: string
669669
x-kubernetes-int-or-string: true
670670
requests:
671-
description: 'Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified, otherwise to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
671+
description: 'Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified, otherwise to an implementation-defined value. Requests cannot exceed Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
672672
type: object
673673
additionalProperties:
674674
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
@@ -1002,7 +1002,7 @@ spec:
10021002
description: 'medium represents what type of storage medium should back this directory. The default is "" which means to use the node''s default medium. Must be an empty string (default) or Memory. More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir'
10031003
type: string
10041004
sizeLimit:
1005-
description: 'sizeLimit is the total amount of local storage required for this EmptyDir volume. The size limit is also applicable for memory medium. The maximum usage on memory medium EmptyDir would be the minimum value between the SizeLimit specified here and the sum of memory limits of all containers in a pod. The default is nil which means that the limit is undefined. More info: http://kubernetes.io/docs/user-guide/volumes#emptydir'
1005+
description: 'sizeLimit is the total amount of local storage required for this EmptyDir volume. The size limit is also applicable for memory medium. The maximum usage on memory medium EmptyDir would be the minimum value between the SizeLimit specified here and the sum of memory limits of all containers in a pod. The default is nil which means that the limit is undefined. More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir'
10061006
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
10071007
anyOf:
10081008
- type: integer
@@ -1070,7 +1070,7 @@ spec:
10701070
type: object
10711071
properties:
10721072
claims:
1073-
description: "Claims lists the names of resources, defined in spec.resourceClaims, that are used by this container. \n This is an alpha field and requires enabling the DynamicResourceAllocation feature gate. \n This field is immutable."
1073+
description: "Claims lists the names of resources, defined in spec.resourceClaims, that are used by this container. \n This is an alpha field and requires enabling the DynamicResourceAllocation feature gate. \n This field is immutable. It can only be set for containers."
10741074
type: array
10751075
items:
10761076
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
@@ -1094,7 +1094,7 @@ spec:
10941094
- type: string
10951095
x-kubernetes-int-or-string: true
10961096
requests:
1097-
description: 'Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified, otherwise to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
1097+
description: 'Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified, otherwise to an implementation-defined value. Requests cannot exceed Limits. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
10981098
type: object
10991099
additionalProperties:
11001100
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$

‎pkg/controller/operators/catalog/operator.go

+25-23
Original file line numberDiff line numberDiff line change
@@ -204,29 +204,31 @@ func NewOperator(ctx context.Context, kubeconfigPath string, clock utilclock.Clo
204204
// Fields are pruned from local copies of the objects managed
205205
// by this informer in order to reduce cached size.
206206
prunedCSVInformer := cache.NewSharedIndexInformer(
207-
pruning.NewListerWatcher(op.client, metav1.NamespaceAll, func(options *metav1.ListOptions) {
208-
options.LabelSelector = fmt.Sprintf("!%s", v1alpha1.CopiedLabelKey)
209-
}, pruning.PrunerFunc(func(csv *v1alpha1.ClusterServiceVersion) {
210-
*csv = v1alpha1.ClusterServiceVersion{
211-
TypeMeta: csv.TypeMeta,
212-
ObjectMeta: metav1.ObjectMeta{
213-
Name: csv.Name,
214-
Namespace: csv.Namespace,
215-
Labels: csv.Labels,
216-
Annotations: csv.Annotations,
217-
},
218-
Spec: v1alpha1.ClusterServiceVersionSpec{
219-
CustomResourceDefinitions: csv.Spec.CustomResourceDefinitions,
220-
APIServiceDefinitions: csv.Spec.APIServiceDefinitions,
221-
Replaces: csv.Spec.Replaces,
222-
Version: csv.Spec.Version,
223-
},
224-
Status: v1alpha1.ClusterServiceVersionStatus{
225-
Phase: csv.Status.Phase,
226-
Reason: csv.Status.Reason,
227-
},
228-
}
229-
})),
207+
pruning.NewListerWatcher(op.client, metav1.NamespaceAll,
208+
func(options *metav1.ListOptions) {
209+
options.LabelSelector = fmt.Sprintf("!%s", v1alpha1.CopiedLabelKey)
210+
},
211+
pruning.PrunerFunc(func(csv *v1alpha1.ClusterServiceVersion) {
212+
*csv = v1alpha1.ClusterServiceVersion{
213+
TypeMeta: csv.TypeMeta,
214+
ObjectMeta: metav1.ObjectMeta{
215+
Name: csv.Name,
216+
Namespace: csv.Namespace,
217+
Labels: csv.Labels,
218+
Annotations: csv.Annotations,
219+
},
220+
Spec: v1alpha1.ClusterServiceVersionSpec{
221+
CustomResourceDefinitions: csv.Spec.CustomResourceDefinitions,
222+
APIServiceDefinitions: csv.Spec.APIServiceDefinitions,
223+
Replaces: csv.Spec.Replaces,
224+
Version: csv.Spec.Version,
225+
},
226+
Status: v1alpha1.ClusterServiceVersionStatus{
227+
Phase: csv.Status.Phase,
228+
Reason: csv.Status.Reason,
229+
},
230+
}
231+
})),
230232
&v1alpha1.ClusterServiceVersion{},
231233
resyncPeriod(),
232234
cache.Indexers{

‎pkg/controller/operators/olm/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
8+
"k8s.io/client-go/metadata"
89

910
"github.com/pkg/errors"
1011
"github.com/sirupsen/logrus"
@@ -29,6 +30,7 @@ type operatorConfig struct {
2930
clock utilclock.Clock
3031
logger *logrus.Logger
3132
operatorClient operatorclient.ClientInterface
33+
metadataClient metadata.Interface
3234
externalClient versioned.Interface
3335
strategyResolver install.StrategyResolverInterface
3436
apiReconciler APIIntersectionReconciler
@@ -159,6 +161,12 @@ func WithOperatorClient(operatorClient operatorclient.ClientInterface) OperatorO
159161
}
160162
}
161163

164+
func WithMetadataClient(metadataClient metadata.Interface) OperatorOption {
165+
return func(config *operatorConfig) {
166+
config.metadataClient = metadataClient
167+
}
168+
}
169+
162170
func WithExternalClient(externalClient versioned.Interface) OperatorOption {
163171
return func(config *operatorConfig) {
164172
config.externalClient = externalClient

‎pkg/controller/operators/olm/operator.go

+23-45
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2525
"k8s.io/client-go/informers"
2626
k8sscheme "k8s.io/client-go/kubernetes/scheme"
27+
"k8s.io/client-go/metadata/metadatainformer"
28+
"k8s.io/client-go/metadata/metadatalister"
2729
"k8s.io/client-go/tools/cache"
2830
"k8s.io/client-go/tools/record"
2931
"k8s.io/client-go/util/workqueue"
@@ -35,12 +37,10 @@ import (
3537
"github.com/operator-framework/api/pkg/operators/v1alpha1"
3638
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
3739
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/informers/externalversions"
38-
operatorsv1alpha1listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
3940
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/certs"
4041
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
41-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/internal/pruning"
4242
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides"
43-
resolver "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver"
43+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver"
4444
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/clients"
4545
csvutility "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/csv"
4646
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/event"
@@ -75,7 +75,7 @@ type Operator struct {
7575
client versioned.Interface
7676
lister operatorlister.OperatorLister
7777
protectedCopiedCSVNamespaces map[string]struct{}
78-
copiedCSVLister operatorsv1alpha1listers.ClusterServiceVersionLister
78+
copiedCSVLister metadatalister.Lister
7979
ogQueueSet *queueinformer.ResourceQueueSet
8080
csvQueueSet *queueinformer.ResourceQueueSet
8181
olmConfigQueue workqueue.RateLimitingInterface
@@ -127,6 +127,9 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
127127
if err := k8sscheme.AddToScheme(scheme); err != nil {
128128
return nil, err
129129
}
130+
if err := metav1.AddMetaToScheme(scheme); err != nil {
131+
return nil, err
132+
}
130133

131134
op := &Operator{
132135
Operator: queueOperator,
@@ -208,44 +211,20 @@ func newOperatorWithConfig(ctx context.Context, config *operatorConfig) (*Operat
208211
return nil, err
209212
}
210213

211-
// A separate informer solely for CSV copies. Fields
212-
// are pruned from local copies of the objects managed
214+
// A separate informer solely for CSV copies. Object metadata requests are used
213215
// by this informer in order to reduce cached size.
214-
copiedCSVInformer := cache.NewSharedIndexInformer(
215-
pruning.NewListerWatcher(
216-
op.client,
217-
namespace,
218-
func(opts *metav1.ListOptions) {
219-
opts.LabelSelector = v1alpha1.CopiedLabelKey
220-
},
221-
pruning.PrunerFunc(func(csv *v1alpha1.ClusterServiceVersion) {
222-
nonstatus, status := copyableCSVHash(csv)
223-
*csv = v1alpha1.ClusterServiceVersion{
224-
TypeMeta: csv.TypeMeta,
225-
ObjectMeta: csv.ObjectMeta,
226-
Status: v1alpha1.ClusterServiceVersionStatus{
227-
Phase: csv.Status.Phase,
228-
Reason: csv.Status.Reason,
229-
},
230-
}
231-
if csv.Annotations == nil {
232-
csv.Annotations = make(map[string]string, 2)
233-
}
234-
// These annotation keys are
235-
// intentionally invalid -- all writes
236-
// to copied CSVs are regenerated from
237-
// the corresponding non-copied CSV,
238-
// so it should never be transmitted
239-
// back to the API server.
240-
csv.Annotations["$copyhash-nonstatus"] = nonstatus
241-
csv.Annotations["$copyhash-status"] = status
242-
}),
243-
),
244-
&v1alpha1.ClusterServiceVersion{},
216+
gvr := v1alpha1.SchemeGroupVersion.WithResource("clusterserviceversions")
217+
copiedCSVInformer := metadatainformer.NewFilteredMetadataInformer(
218+
config.metadataClient,
219+
gvr,
220+
namespace,
245221
config.resyncPeriod(),
246222
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
247-
)
248-
op.copiedCSVLister = operatorsv1alpha1listers.NewClusterServiceVersionLister(copiedCSVInformer.GetIndexer())
223+
func(options *metav1.ListOptions) {
224+
options.LabelSelector = v1alpha1.CopiedLabelKey
225+
},
226+
).Informer()
227+
op.copiedCSVLister = metadatalister.New(copiedCSVInformer.GetIndexer(), gvr)
249228

250229
// Register separate queue for gcing copied csvs
251230
copiedCSVGCQueue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), fmt.Sprintf("%s/csv-gc", namespace))
@@ -1195,17 +1174,16 @@ func (a *Operator) handleClusterServiceVersionDeletion(obj interface{}) {
11951174
}
11961175
}
11971176

1198-
func (a *Operator) removeDanglingChildCSVs(csv *v1alpha1.ClusterServiceVersion) error {
1177+
func (a *Operator) removeDanglingChildCSVs(csv *metav1.PartialObjectMetadata) error {
11991178
logger := a.logger.WithFields(logrus.Fields{
12001179
"id": queueinformer.NewLoopID(),
12011180
"csv": csv.GetName(),
12021181
"namespace": csv.GetNamespace(),
1203-
"phase": csv.Status.Phase,
12041182
"labels": csv.GetLabels(),
12051183
"annotations": csv.GetAnnotations(),
12061184
})
12071185

1208-
if !csv.IsCopied() {
1186+
if !v1alpha1.IsCopied(csv) {
12091187
logger.Warning("removeDanglingChild called on a parent. this is a no-op but should be avoided.")
12101188
return nil
12111189
}
@@ -1244,7 +1222,7 @@ func (a *Operator) removeDanglingChildCSVs(csv *v1alpha1.ClusterServiceVersion)
12441222
return nil
12451223
}
12461224

1247-
func (a *Operator) deleteChild(csv *v1alpha1.ClusterServiceVersion, logger *logrus.Entry) error {
1225+
func (a *Operator) deleteChild(csv *metav1.PartialObjectMetadata, logger *logrus.Entry) error {
12481226
logger.Debug("gcing csv")
12491227
return a.client.OperatorsV1alpha1().ClusterServiceVersions(csv.GetNamespace()).Delete(context.TODO(), csv.GetName(), metav1.DeleteOptions{})
12501228
}
@@ -1683,12 +1661,12 @@ func (a *Operator) createCSVCopyingDisabledEvent(csv *v1alpha1.ClusterServiceVer
16831661
}
16841662

16851663
func (a *Operator) syncGcCsv(obj interface{}) (syncError error) {
1686-
clusterServiceVersion, ok := obj.(*v1alpha1.ClusterServiceVersion)
1664+
clusterServiceVersion, ok := obj.(*metav1.PartialObjectMetadata)
16871665
if !ok {
16881666
a.logger.Debugf("wrong type: %#v", obj)
16891667
return fmt.Errorf("casting ClusterServiceVersion failed")
16901668
}
1691-
if clusterServiceVersion.IsCopied() {
1669+
if v1alpha1.IsCopied(clusterServiceVersion) {
16921670
syncError = a.removeDanglingChildCSVs(clusterServiceVersion)
16931671
return
16941672
}

‎pkg/controller/operators/olm/operator_test.go

+222-108
Large diffs are not rendered by default.

‎pkg/controller/operators/olm/operatorgroup.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
corev1 "k8s.io/api/core/v1"
1313
rbacv1 "k8s.io/api/rbac/v1"
1414
apierrors "k8s.io/apimachinery/pkg/api/errors"
15-
meta "k8s.io/apimachinery/pkg/api/meta"
15+
"k8s.io/apimachinery/pkg/api/meta"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1717
"k8s.io/apimachinery/pkg/labels"
1818
"k8s.io/apimachinery/pkg/util/errors"
@@ -797,15 +797,15 @@ func (a *Operator) copyToNamespace(prototype *v1alpha1.ClusterServiceVersion, ns
797797
prototype.ResourceVersion = ""
798798
prototype.UID = ""
799799

800-
existing, err := a.copiedCSVLister.ClusterServiceVersions(nsTo).Get(prototype.GetName())
800+
existing, err := a.copiedCSVLister.Namespace(nsTo).Get(prototype.GetName())
801801
if apierrors.IsNotFound(err) {
802802
created, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(nsTo).Create(context.TODO(), prototype, metav1.CreateOptions{})
803803
if err != nil {
804-
return nil, err
804+
return nil, fmt.Errorf("failed to create new CSV: %w", err)
805805
}
806806
created.Status = prototype.Status
807807
if _, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(nsTo).UpdateStatus(context.TODO(), created, metav1.UpdateOptions{}); err != nil {
808-
return nil, err
808+
return nil, fmt.Errorf("failed to update status on new CSV: %w", err)
809809
}
810810
return &v1alpha1.ClusterServiceVersion{
811811
ObjectMeta: metav1.ObjectMeta{
@@ -824,38 +824,39 @@ func (a *Operator) copyToNamespace(prototype *v1alpha1.ClusterServiceVersion, ns
824824
existingNonStatus := existing.Annotations["$copyhash-nonstatus"]
825825
existingStatus := existing.Annotations["$copyhash-status"]
826826

827+
var updated *v1alpha1.ClusterServiceVersion
827828
if existingNonStatus != nonstatus {
828-
if existing, err = a.client.OperatorsV1alpha1().ClusterServiceVersions(nsTo).Update(context.TODO(), prototype, metav1.UpdateOptions{}); err != nil {
829-
return nil, err
829+
if updated, err = a.client.OperatorsV1alpha1().ClusterServiceVersions(nsTo).Update(context.TODO(), prototype, metav1.UpdateOptions{}); err != nil {
830+
return nil, fmt.Errorf("failed to update: %w", err)
830831
}
831832
} else {
832833
// Avoid mutating cached copied CSV.
833-
existing = prototype
834+
updated = prototype
834835
}
835836

836837
if existingStatus != status {
837-
existing.Status = prototype.Status
838-
if _, err = a.client.OperatorsV1alpha1().ClusterServiceVersions(nsTo).UpdateStatus(context.TODO(), existing, metav1.UpdateOptions{}); err != nil {
839-
return nil, err
838+
updated.Status = prototype.Status
839+
if _, err = a.client.OperatorsV1alpha1().ClusterServiceVersions(nsTo).UpdateStatus(context.TODO(), updated, metav1.UpdateOptions{}); err != nil {
840+
return nil, fmt.Errorf("failed to update status: %w", err)
840841
}
841842
}
842843
return &v1alpha1.ClusterServiceVersion{
843844
ObjectMeta: metav1.ObjectMeta{
844-
Name: existing.Name,
845-
Namespace: existing.Namespace,
846-
UID: existing.UID,
845+
Name: updated.Name,
846+
Namespace: updated.Namespace,
847+
UID: updated.UID,
847848
},
848849
}, nil
849850
}
850851

851852
func (a *Operator) pruneFromNamespace(operatorGroupName, namespace string) error {
852-
fetchedCSVs, err := a.copiedCSVLister.ClusterServiceVersions(namespace).List(labels.Everything())
853+
fetchedCSVs, err := a.copiedCSVLister.Namespace(namespace).List(labels.Everything())
853854
if err != nil {
854855
return err
855856
}
856857

857858
for _, csv := range fetchedCSVs {
858-
if csv.IsCopied() && csv.GetAnnotations()[operatorsv1.OperatorGroupAnnotationKey] == operatorGroupName {
859+
if v1alpha1.IsCopied(csv) && csv.GetAnnotations()[operatorsv1.OperatorGroupAnnotationKey] == operatorGroupName {
859860
a.logger.Debugf("Found CSV '%v' in namespace %v to delete", csv.GetName(), namespace)
860861
if err := a.copiedCSVGCQueueSet.Requeue(csv.GetNamespace(), csv.GetName()); err != nil {
861862
return err

‎pkg/controller/operators/olm/operatorgroup_test.go

+28-25
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/google/go-cmp/cmp"
78
"github.com/sirupsen/logrus/hooks/test"
89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
11+
"k8s.io/client-go/metadata/metadatalister"
1012

1113
"k8s.io/apimachinery/pkg/api/errors"
1214
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -15,8 +17,6 @@ import (
1517

1618
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1719
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
18-
listersv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
19-
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister/operatorlisterfakes"
2020
)
2121

2222
func TestCopyToNamespace(t *testing.T) {
@@ -29,7 +29,7 @@ func TestCopyToNamespace(t *testing.T) {
2929
Hash string
3030
StatusHash string
3131
Prototype v1alpha1.ClusterServiceVersion
32-
ExistingCopy *v1alpha1.ClusterServiceVersion
32+
ExistingCopy *metav1.PartialObjectMetadata
3333
ExpectedResult *v1alpha1.ClusterServiceVersion
3434
ExpectedError error
3535
ExpectedActions []ktesting.Action
@@ -105,7 +105,7 @@ func TestCopyToNamespace(t *testing.T) {
105105
Phase: "waxing gibbous",
106106
},
107107
},
108-
ExistingCopy: &v1alpha1.ClusterServiceVersion{
108+
ExistingCopy: &metav1.PartialObjectMetadata{
109109
ObjectMeta: metav1.ObjectMeta{
110110
Name: "name",
111111
Namespace: "to",
@@ -158,7 +158,7 @@ func TestCopyToNamespace(t *testing.T) {
158158
Phase: "waxing gibbous",
159159
},
160160
},
161-
ExistingCopy: &v1alpha1.ClusterServiceVersion{
161+
ExistingCopy: &metav1.PartialObjectMetadata{
162162
ObjectMeta: metav1.ObjectMeta{
163163
Name: "name",
164164
Namespace: "to",
@@ -211,7 +211,7 @@ func TestCopyToNamespace(t *testing.T) {
211211
Phase: "waxing gibbous",
212212
},
213213
},
214-
ExistingCopy: &v1alpha1.ClusterServiceVersion{
214+
ExistingCopy: &metav1.PartialObjectMetadata{
215215
ObjectMeta: metav1.ObjectMeta{
216216
Name: "name",
217217
Namespace: "to",
@@ -272,7 +272,7 @@ func TestCopyToNamespace(t *testing.T) {
272272
Name: "name",
273273
},
274274
},
275-
ExistingCopy: &v1alpha1.ClusterServiceVersion{
275+
ExistingCopy: &metav1.PartialObjectMetadata{
276276
ObjectMeta: metav1.ObjectMeta{
277277
Name: "name",
278278
Namespace: "to",
@@ -293,21 +293,20 @@ func TestCopyToNamespace(t *testing.T) {
293293
},
294294
} {
295295
t.Run(tc.Name, func(t *testing.T) {
296-
lister := &operatorlisterfakes.FakeOperatorLister{}
297-
v1alpha1lister := &operatorlisterfakes.FakeOperatorsV1alpha1Lister{}
298-
lister.OperatorsV1alpha1Returns(v1alpha1lister)
299-
300296
client := fake.NewSimpleClientset()
297+
var lister metadatalister.Lister
301298
if tc.ExistingCopy != nil {
302-
client = fake.NewSimpleClientset(tc.ExistingCopy)
303-
v1alpha1lister.ClusterServiceVersionListerReturns(FakeClusterServiceVersionLister{tc.ExistingCopy})
299+
client = fake.NewSimpleClientset(&v1alpha1.ClusterServiceVersion{
300+
ObjectMeta: tc.ExistingCopy.ObjectMeta,
301+
})
302+
lister = FakeClusterServiceVersionLister{tc.ExistingCopy}
304303
} else {
305-
v1alpha1lister.ClusterServiceVersionListerReturns(FakeClusterServiceVersionLister(nil))
304+
lister = FakeClusterServiceVersionLister{{}}
306305
}
307306

308307
logger, _ := test.NewNullLogger()
309308
o := &Operator{
310-
copiedCSVLister: v1alpha1lister.ClusterServiceVersionLister(),
309+
copiedCSVLister: lister,
311310
client: client,
312311
logger: logger,
313312
}
@@ -319,21 +318,25 @@ func TestCopyToNamespace(t *testing.T) {
319318
} else {
320319
require.EqualError(t, err, tc.ExpectedError.Error())
321320
}
322-
assert.Equal(t, tc.ExpectedResult, result)
321+
if diff := cmp.Diff(tc.ExpectedResult, result); diff != "" {
322+
t.Errorf("incorrect result: %v", diff)
323+
}
323324

324325
actions := client.Actions()
325326
if len(actions) == 0 {
326327
actions = nil
327328
}
328-
assert.Equal(t, tc.ExpectedActions, actions)
329+
if diff := cmp.Diff(tc.ExpectedActions, actions); diff != "" {
330+
t.Errorf("incorrect actions: %v", diff)
331+
}
329332
})
330333
}
331334
}
332335

333-
type FakeClusterServiceVersionLister []*v1alpha1.ClusterServiceVersion
336+
type FakeClusterServiceVersionLister []*metav1.PartialObjectMetadata
334337

335-
func (l FakeClusterServiceVersionLister) List(selector labels.Selector) ([]*v1alpha1.ClusterServiceVersion, error) {
336-
var result []*v1alpha1.ClusterServiceVersion
338+
func (l FakeClusterServiceVersionLister) List(selector labels.Selector) ([]*metav1.PartialObjectMetadata, error) {
339+
var result []*metav1.PartialObjectMetadata
337340
for _, csv := range l {
338341
if !selector.Matches(labels.Set(csv.GetLabels())) {
339342
continue
@@ -343,8 +346,8 @@ func (l FakeClusterServiceVersionLister) List(selector labels.Selector) ([]*v1al
343346
return result, nil
344347
}
345348

346-
func (l FakeClusterServiceVersionLister) ClusterServiceVersions(namespace string) listersv1alpha1.ClusterServiceVersionNamespaceLister {
347-
var filtered []*v1alpha1.ClusterServiceVersion
349+
func (l FakeClusterServiceVersionLister) Namespace(namespace string) metadatalister.NamespaceLister {
350+
var filtered []*metav1.PartialObjectMetadata
348351
for _, csv := range l {
349352
if csv.GetNamespace() != namespace {
350353
continue
@@ -354,7 +357,7 @@ func (l FakeClusterServiceVersionLister) ClusterServiceVersions(namespace string
354357
return FakeClusterServiceVersionLister(filtered)
355358
}
356359

357-
func (l FakeClusterServiceVersionLister) Get(name string) (*v1alpha1.ClusterServiceVersion, error) {
360+
func (l FakeClusterServiceVersionLister) Get(name string) (*metav1.PartialObjectMetadata, error) {
358361
for _, csv := range l {
359362
if csv.GetName() == name {
360363
return csv, nil
@@ -364,8 +367,8 @@ func (l FakeClusterServiceVersionLister) Get(name string) (*v1alpha1.ClusterServ
364367
}
365368

366369
var (
367-
_ listersv1alpha1.ClusterServiceVersionLister = FakeClusterServiceVersionLister{}
368-
_ listersv1alpha1.ClusterServiceVersionNamespaceLister = FakeClusterServiceVersionLister{}
370+
_ metadatalister.Lister = FakeClusterServiceVersionLister{}
371+
_ metadatalister.NamespaceLister = FakeClusterServiceVersionLister{}
369372
)
370373

371374
func TestCSVCopyPrototype(t *testing.T) {

‎vendor/k8s.io/client-go/metadata/fake/simple.go

+405
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/k8s.io/client-go/metadata/metadatainformer/informer.go

+183
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/k8s.io/client-go/metadata/metadatainformer/interface.go

+53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/k8s.io/client-go/metadata/metadatalister/interface.go

+40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/k8s.io/client-go/metadata/metadatalister/lister.go

+91
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/k8s.io/client-go/metadata/metadatalister/shim.go

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/modules.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,9 @@ k8s.io/client-go/listers/storage/v1
17601760
k8s.io/client-go/listers/storage/v1alpha1
17611761
k8s.io/client-go/listers/storage/v1beta1
17621762
k8s.io/client-go/metadata
1763+
k8s.io/client-go/metadata/fake
1764+
k8s.io/client-go/metadata/metadatainformer
1765+
k8s.io/client-go/metadata/metadatalister
17631766
k8s.io/client-go/openapi
17641767
k8s.io/client-go/openapi/cached
17651768
k8s.io/client-go/openapi3

0 commit comments

Comments
 (0)
Please sign in to comment.