Skip to content

Commit 943145e

Browse files
committed
feat(registryreconciler): add now function
1 parent 3d6f069 commit 943145e

File tree

6 files changed

+54
-34
lines changed

6 files changed

+54
-34
lines changed

pkg/controller/registry/reconciler/configmap.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package reconciler
33

44
import (
55
"fmt"
6-
"time"
76

87
"github.com/pkg/errors"
98
"github.com/sirupsen/logrus"
@@ -19,8 +18,6 @@ import (
1918
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
2019
)
2120

22-
var timeNow = func() metav1.Time { return metav1.NewTime(time.Now().UTC()) }
23-
2421
// configMapCatalogSourceDecorator wraps CatalogSource to add additional methods
2522
type configMapCatalogSourceDecorator struct {
2623
*v1alpha1.CatalogSource
@@ -194,6 +191,7 @@ func (s *configMapCatalogSourceDecorator) RoleBinding() *rbacv1.RoleBinding {
194191
}
195192

196193
type ConfigMapRegistryReconciler struct {
194+
now nowFunc
197195
Lister operatorlister.OperatorLister
198196
OpClient operatorclient.ClientInterface
199197
Image string
@@ -328,14 +326,15 @@ func (c *ConfigMapRegistryReconciler) EnsureRegistryServer(catalogSource *v1alph
328326
}
329327

330328
if overwritePod {
329+
now := c.now()
331330
catalogSource.Status.RegistryServiceStatus = &v1alpha1.RegistryServiceStatus{
332-
CreatedAt: timeNow(),
331+
CreatedAt: now,
333332
Protocol: "grpc",
334333
ServiceName: source.Service().GetName(),
335334
ServiceNamespace: source.GetNamespace(),
336335
Port: fmt.Sprintf("%d", source.Service().Spec.Ports[0].Port),
337336
}
338-
catalogSource.Status.LastSync = timeNow()
337+
catalogSource.Status.LastSync = now
339338
}
340339
return nil
341340
}

pkg/controller/registry/reconciler/configmap_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ const (
3333
)
3434

3535
type fakeReconcilerConfig struct {
36+
now nowFunc
3637
k8sObjs []runtime.Object
3738
k8sClientOptions []clientfake.Option
3839
configMapServerImage string
3940
}
4041

4142
type fakeReconcilerOption func(*fakeReconcilerConfig)
4243

44+
func withNow(now nowFunc) fakeReconcilerOption {
45+
return func(config *fakeReconcilerConfig) {
46+
config.now = now
47+
}
48+
}
49+
4350
func withK8sObjs(k8sObjs ...runtime.Object) fakeReconcilerOption {
4451
return func(config *fakeReconcilerConfig) {
4552
config.k8sObjs = k8sObjs
@@ -60,6 +67,7 @@ func withConfigMapServerImage(configMapServerImage string) fakeReconcilerOption
6067

6168
func fakeReconcilerFactory(t *testing.T, stopc <-chan struct{}, options ...fakeReconcilerOption) (RegistryReconcilerFactory, operatorclient.ClientInterface) {
6269
config := &fakeReconcilerConfig{
70+
now: metav1.Now,
6371
configMapServerImage: registryImageName,
6472
}
6573

@@ -97,6 +105,7 @@ func fakeReconcilerFactory(t *testing.T, stopc <-chan struct{}, options ...fakeR
97105
lister.CoreV1().RegisterConfigMapLister(testNamespace, configMapInformer.Lister())
98106

99107
rec := &registryReconcilerFactory{
108+
now: config.now,
100109
OpClient: opClientFake,
101110
Lister: lister,
102111
ConfigMapServerImage: config.configMapServerImage,
@@ -246,8 +255,7 @@ func setLabel(objs []runtime.Object, kind runtime.Object, label, value string) [
246255
}
247256

248257
func TestConfigMapRegistryReconciler(t *testing.T) {
249-
nowTime := metav1.Date(2018, time.January, 26, 20, 40, 0, 0, time.UTC)
250-
timeNow = func() metav1.Time { return nowTime }
258+
now := func() metav1.Time { return metav1.Date(2018, time.January, 26, 20, 40, 0, 0, time.UTC) }
251259

252260
validConfigMap := validConfigMap()
253261
validCatalogSource := validConfigMapCatalogSource(validConfigMap)
@@ -293,7 +301,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
293301
},
294302
out: out{
295303
status: &v1alpha1.RegistryServiceStatus{
296-
CreatedAt: timeNow(),
304+
CreatedAt: now(),
297305
Protocol: "grpc",
298306
ServiceName: "cool-catalog",
299307
ServiceNamespace: testNamespace,
@@ -311,7 +319,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
311319
},
312320
out: out{
313321
status: &v1alpha1.RegistryServiceStatus{
314-
CreatedAt: timeNow(),
322+
CreatedAt: now(),
315323
Protocol: "grpc",
316324
ServiceName: "cool-catalog",
317325
ServiceNamespace: testNamespace,
@@ -329,7 +337,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
329337
},
330338
out: out{
331339
status: &v1alpha1.RegistryServiceStatus{
332-
CreatedAt: timeNow(),
340+
CreatedAt: now(),
333341
Protocol: "grpc",
334342
ServiceName: "cool-catalog",
335343
ServiceNamespace: testNamespace,
@@ -347,7 +355,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
347355
},
348356
out: out{
349357
status: &v1alpha1.RegistryServiceStatus{
350-
CreatedAt: timeNow(),
358+
CreatedAt: now(),
351359
Protocol: "grpc",
352360
ServiceName: "cool-catalog",
353361
ServiceNamespace: testNamespace,
@@ -365,7 +373,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
365373
},
366374
out: out{
367375
status: &v1alpha1.RegistryServiceStatus{
368-
CreatedAt: timeNow(),
376+
CreatedAt: now(),
369377
Protocol: "grpc",
370378
ServiceName: "cool-catalog",
371379
ServiceNamespace: testNamespace,
@@ -383,7 +391,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
383391
},
384392
out: out{
385393
status: &v1alpha1.RegistryServiceStatus{
386-
CreatedAt: timeNow(),
394+
CreatedAt: now(),
387395
Protocol: "grpc",
388396
ServiceName: "cool-catalog",
389397
ServiceNamespace: testNamespace,
@@ -401,7 +409,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
401409
},
402410
out: out{
403411
status: &v1alpha1.RegistryServiceStatus{
404-
CreatedAt: timeNow(),
412+
CreatedAt: now(),
405413
Protocol: "grpc",
406414
ServiceName: "cool-catalog",
407415
ServiceNamespace: testNamespace,
@@ -415,7 +423,7 @@ func TestConfigMapRegistryReconciler(t *testing.T) {
415423
stopc := make(chan struct{})
416424
defer close(stopc)
417425

418-
factory, client := fakeReconcilerFactory(t, stopc, withK8sObjs(tt.in.cluster.k8sObjs...), withK8sClientOptions(clientfake.WithNameGeneration(t)))
426+
factory, client := fakeReconcilerFactory(t, stopc, withNow(now), withK8sObjs(tt.in.cluster.k8sObjs...), withK8sClientOptions(clientfake.WithNameGeneration(t)))
419427
rec := factory.ReconcilerForSource(tt.in.catsrc)
420428

421429
err := rec.EnsureRegistryServer(tt.in.catsrc)

pkg/controller/registry/reconciler/grpc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (s *grpcCatalogSourceDecorator) Pod() *v1.Pod {
102102
}
103103

104104
type GrpcRegistryReconciler struct {
105+
now nowFunc
105106
Lister operatorlister.OperatorLister
106107
OpClient operatorclient.ClientInterface
107108
}
@@ -163,14 +164,15 @@ func (c *GrpcRegistryReconciler) EnsureRegistryServer(catalogSource *v1alpha1.Ca
163164
}
164165

165166
if overwritePod {
167+
now := c.now()
166168
catalogSource.Status.RegistryServiceStatus = &v1alpha1.RegistryServiceStatus{
167-
CreatedAt: timeNow(),
169+
CreatedAt: now,
168170
Protocol: "grpc",
169171
ServiceName: source.Service().GetName(),
170172
ServiceNamespace: source.GetNamespace(),
171173
Port: fmt.Sprintf("%d", source.Service().Spec.Ports[0].Port),
172174
}
173-
catalogSource.Status.LastSync = timeNow()
175+
catalogSource.Status.LastSync = now
174176
}
175177
return nil
176178
}

pkg/controller/registry/reconciler/grpc_address.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ import (
44
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
55
)
66

7-
type GrpcAddressRegistryReconciler struct{}
7+
type GrpcAddressRegistryReconciler struct {
8+
now nowFunc
9+
}
810

911
var _ RegistryEnsurer = &GrpcAddressRegistryReconciler{}
1012
var _ RegistryChecker = &GrpcAddressRegistryReconciler{}
1113
var _ RegistryReconciler = &GrpcAddressRegistryReconciler{}
1214

1315
// EnsureRegistryServer ensures a registry server exists for the given CatalogSource.
1416
func (g *GrpcAddressRegistryReconciler) EnsureRegistryServer(catalogSource *v1alpha1.CatalogSource) error {
17+
now := g.now()
1518
catalogSource.Status.RegistryServiceStatus = &v1alpha1.RegistryServiceStatus{
16-
CreatedAt: timeNow(),
19+
CreatedAt: now,
1720
Protocol: "grpc",
1821
}
22+
catalogSource.Status.LastSync = now
1923

2024
return nil
2125
}

pkg/controller/registry/reconciler/grpc_test.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func validGrpcCatalogSource(image, address string) *v1alpha1.CatalogSource {
3232
}
3333

3434
func TestGrpcRegistryReconciler(t *testing.T) {
35-
nowTime := metav1.Date(2018, time.January, 26, 20, 40, 0, 0, time.UTC)
36-
timeNow = func() metav1.Time { return nowTime }
35+
now := func() metav1.Time { return metav1.Date(2018, time.January, 26, 20, 40, 0, 0, time.UTC) }
3736

3837
type cluster struct {
3938
k8sObjs []runtime.Object
@@ -58,7 +57,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
5857
},
5958
out: out{
6059
status: &v1alpha1.RegistryServiceStatus{
61-
CreatedAt: timeNow(),
60+
CreatedAt: now(),
6261
Protocol: "grpc",
6362
ServiceName: "img-catalog",
6463
ServiceNamespace: testNamespace,
@@ -76,7 +75,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
7675
},
7776
out: out{
7877
status: &v1alpha1.RegistryServiceStatus{
79-
CreatedAt: timeNow(),
78+
CreatedAt: now(),
8079
Protocol: "grpc",
8180
ServiceName: "img-catalog",
8281
ServiceNamespace: testNamespace,
@@ -92,7 +91,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
9291
},
9392
out: out{
9493
status: &v1alpha1.RegistryServiceStatus{
95-
CreatedAt: timeNow(),
94+
CreatedAt: now(),
9695
Protocol: "grpc",
9796
},
9897
},
@@ -105,7 +104,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
105104
},
106105
out: out{
107106
status: &v1alpha1.RegistryServiceStatus{
108-
CreatedAt: timeNow(),
107+
CreatedAt: now(),
109108
Protocol: "grpc",
110109
ServiceName: "img-catalog",
111110
ServiceNamespace: testNamespace,
@@ -123,7 +122,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
123122
},
124123
out: out{
125124
status: &v1alpha1.RegistryServiceStatus{
126-
CreatedAt: timeNow(),
125+
CreatedAt: now(),
127126
Protocol: "grpc",
128127
ServiceName: "img-catalog",
129128
ServiceNamespace: testNamespace,
@@ -141,7 +140,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
141140
},
142141
out: out{
143142
status: &v1alpha1.RegistryServiceStatus{
144-
CreatedAt: timeNow(),
143+
CreatedAt: now(),
145144
Protocol: "grpc",
146145
ServiceName: "img-catalog",
147146
ServiceNamespace: testNamespace,
@@ -159,7 +158,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
159158
},
160159
out: out{
161160
status: &v1alpha1.RegistryServiceStatus{
162-
CreatedAt: timeNow(),
161+
CreatedAt: now(),
163162
Protocol: "grpc",
164163
ServiceName: "img-catalog",
165164
ServiceNamespace: testNamespace,
@@ -173,7 +172,7 @@ func TestGrpcRegistryReconciler(t *testing.T) {
173172
stopc := make(chan struct{})
174173
defer close(stopc)
175174

176-
factory, client := fakeReconcilerFactory(t, stopc, withK8sObjs(tt.in.cluster.k8sObjs...), withK8sClientOptions(clientfake.WithNameGeneration(t)))
175+
factory, client := fakeReconcilerFactory(t, stopc, withNow(now), withK8sObjs(tt.in.cluster.k8sObjs...), withK8sClientOptions(clientfake.WithNameGeneration(t)))
177176
rec := factory.ReconcilerForSource(tt.in.catsrc)
178177

179178
err := rec.EnsureRegistryServer(tt.in.catsrc)
@@ -217,9 +216,6 @@ func TestGrpcRegistryReconciler(t *testing.T) {
217216
}
218217

219218
func TestGrpcRegistryChecker(t *testing.T) {
220-
nowTime := metav1.Date(2018, time.January, 26, 20, 40, 0, 0, time.UTC)
221-
timeNow = func() metav1.Time { return nowTime }
222-
223219
type cluster struct {
224220
k8sObjs []runtime.Object
225221
}

pkg/controller/registry/reconciler/reconciler.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
package reconciler
33

44
import (
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
57
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
68
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
79
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
810
)
911

12+
type nowFunc func() metav1.Time
13+
1014
const (
1115
// CatalogSourceLabelKey is the key for a label containing a CatalogSource name.
1216
CatalogSourceLabelKey string = "olm.catalogSource"
@@ -37,36 +41,43 @@ type RegistryReconcilerFactory interface {
3741

3842
// RegistryReconcilerFactory is a factory for RegistryReconcilers.
3943
type registryReconcilerFactory struct {
44+
now nowFunc
4045
Lister operatorlister.OperatorLister
4146
OpClient operatorclient.ClientInterface
4247
ConfigMapServerImage string
4348
}
4449

4550
// ReconcilerForSource returns a RegistryReconciler based on the configuration of the given CatalogSource.
4651
func (r *registryReconcilerFactory) ReconcilerForSource(source *v1alpha1.CatalogSource) RegistryReconciler {
52+
// TODO: add memoization by source type
4753
switch source.Spec.SourceType {
4854
case v1alpha1.SourceTypeInternal, v1alpha1.SourceTypeConfigmap:
4955
return &ConfigMapRegistryReconciler{
56+
now: r.now,
5057
Lister: r.Lister,
5158
OpClient: r.OpClient,
5259
Image: r.ConfigMapServerImage,
5360
}
5461
case v1alpha1.SourceTypeGrpc:
5562
if source.Spec.Image != "" {
5663
return &GrpcRegistryReconciler{
64+
now: r.now,
5765
Lister: r.Lister,
5866
OpClient: r.OpClient,
5967
}
6068
} else if source.Spec.Address != "" {
61-
return &GrpcAddressRegistryReconciler{}
69+
return &GrpcAddressRegistryReconciler{
70+
now: r.now,
71+
}
6272
}
6373
}
6474
return nil
6575
}
6676

6777
// NewRegistryReconcilerFactory returns an initialized RegistryReconcilerFactory.
68-
func NewRegistryReconcilerFactory(lister operatorlister.OperatorLister, opClient operatorclient.ClientInterface, configMapServerImage string) RegistryReconcilerFactory {
78+
func NewRegistryReconcilerFactory(lister operatorlister.OperatorLister, opClient operatorclient.ClientInterface, configMapServerImage string, now nowFunc) RegistryReconcilerFactory {
6979
return &registryReconcilerFactory{
80+
now: now,
7081
Lister: lister,
7182
OpClient: opClient,
7283
ConfigMapServerImage: configMapServerImage,

0 commit comments

Comments
 (0)