Skip to content

Commit 19998e5

Browse files
Merge pull request #408 from openshift-cherrypick-robot/cherry-pick-407-to-cert-manager-1.19
[cert-manager-1.19] CM-786: Updates controller override arg list to allow certificate-request-minimum-backoff-duration and fmt changes
2 parents f391037 + 56d95bd commit 19998e5

7 files changed

Lines changed: 456 additions & 596 deletions

File tree

go.work.sum

Lines changed: 4 additions & 316 deletions
Large diffs are not rendered by default.

pkg/controller/certmanager/deployment_helper_test.go

Lines changed: 95 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,108 @@
11
package certmanager
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

7-
"github.com/openshift/cert-manager-operator/api/operator/v1alpha1"
8-
"github.com/openshift/cert-manager-operator/pkg/operator/clientset/versioned/fake"
9-
certmanoperatorinformer "github.com/openshift/cert-manager-operator/pkg/operator/informers/externalversions"
10-
"github.com/stretchr/testify/assert"
11-
"github.com/stretchr/testify/require"
12-
138
corev1 "k8s.io/api/core/v1"
9+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1410
k8sresource "k8s.io/apimachinery/pkg/api/resource"
1511
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1612
"k8s.io/apimachinery/pkg/util/wait"
1713
"k8s.io/apimachinery/pkg/watch"
1814
clienttesting "k8s.io/client-go/testing"
1915
"k8s.io/client-go/tools/cache"
2016
"k8s.io/utils/ptr"
17+
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
20+
21+
"github.com/openshift/cert-manager-operator/api/operator/v1alpha1"
22+
"github.com/openshift/cert-manager-operator/pkg/operator/clientset/versioned/fake"
23+
certmanoperatorinformer "github.com/openshift/cert-manager-operator/pkg/operator/informers/externalversions"
24+
certmanagerinformerv1alpha1 "github.com/openshift/cert-manager-operator/pkg/operator/informers/externalversions/operator/v1alpha1"
2125
)
2226

27+
// setupSyncedFakeCertManagerInformer builds a fake CertManagers client and a running, synced
28+
// CertManagerInformer. It registers a watch reactor so creates after List are not missed (see
29+
// client-go fake limitations). events receives each CertManager on add and delete; buffer size 1
30+
// matches the tests that wait for a single add then a single delete per case.
31+
func setupSyncedFakeCertManagerInformer(t *testing.T, ctx context.Context) (
32+
fakeClient *fake.Clientset,
33+
informer certmanagerinformerv1alpha1.CertManagerInformer,
34+
events <-chan *v1alpha1.CertManager,
35+
) {
36+
t.Helper()
37+
38+
watcherStarted := make(chan struct{})
39+
fakeClient = fake.NewClientset()
40+
fakeClient.PrependWatchReactor("certmanagers", func(action clienttesting.Action) (handled bool, ret watch.Interface, err error) {
41+
gvr := action.GetResource()
42+
ns := action.GetNamespace()
43+
w, err := fakeClient.Tracker().Watch(gvr, ns)
44+
if err != nil {
45+
return false, nil, err
46+
}
47+
close(watcherStarted)
48+
return true, w, nil
49+
})
50+
51+
informer = certmanoperatorinformer.NewSharedInformerFactory(fakeClient, 0).Operator().V1alpha1().CertManagers()
52+
ch := make(chan *v1alpha1.CertManager, 1)
53+
_, err := informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
54+
AddFunc: func(obj any) {
55+
ch <- obj.(*v1alpha1.CertManager)
56+
},
57+
DeleteFunc: func(obj any) {
58+
switch o := obj.(type) {
59+
case *v1alpha1.CertManager:
60+
ch <- o
61+
case cache.DeletedFinalStateUnknown:
62+
if cm, ok := o.Obj.(*v1alpha1.CertManager); ok {
63+
ch <- cm
64+
}
65+
}
66+
},
67+
})
68+
require.NoError(t, err)
69+
70+
go informer.Informer().Run(ctx.Done())
71+
require.True(t, cache.WaitForCacheSync(ctx.Done(), informer.Informer().HasSynced), "failed to sync CertManager informer cache")
72+
select {
73+
case <-watcherStarted:
74+
case <-time.After(wait.ForeverTestTimeout):
75+
t.Fatal("watch reactor did not start")
76+
}
77+
78+
return fakeClient, informer, ch
79+
}
80+
81+
// withFakeCertManagerForTest creates obj in the fake API, waits for the informer add event, and
82+
// registers t.Cleanup to delete obj and wait for the delete event.
83+
func withFakeCertManagerForTest(t *testing.T, ctx context.Context, fakeClient *fake.Clientset, events <-chan *v1alpha1.CertManager, obj *v1alpha1.CertManager) {
84+
t.Helper()
85+
_, err := fakeClient.OperatorV1alpha1().CertManagers().Create(ctx, obj, metav1.CreateOptions{})
86+
require.NoError(t, err)
87+
t.Cleanup(func() {
88+
err := fakeClient.OperatorV1alpha1().CertManagers().Delete(ctx, obj.Name, metav1.DeleteOptions{})
89+
if err != nil && !apierrors.IsNotFound(err) {
90+
t.Errorf("cert-manager delete failed: %v", err)
91+
return
92+
}
93+
select {
94+
case <-events:
95+
case <-time.After(wait.ForeverTestTimeout):
96+
t.Errorf("Informer did not get the deleted cert manager object during cleanup")
97+
}
98+
})
99+
select {
100+
case <-events:
101+
case <-time.After(wait.ForeverTestTimeout):
102+
t.Fatal("Informer did not get the added cert manager object")
103+
}
104+
}
105+
23106
func TestMergeContainerResources(t *testing.T) {
24107
tests := []struct {
25108
name string
@@ -335,89 +418,15 @@ func TestGetOverrideResourcesFor(t *testing.T) {
335418
}
336419

337420
ctx := t.Context()
338-
339-
// Create channel to know when the watch has started.
340-
watcherStarted := make(chan struct{})
341-
// Create the fake client.
342-
fakeClient := fake.NewSimpleClientset()
343-
// A watch reactor for cert manager objects that allows the injection of the watcherStarted channel.
344-
fakeClient.PrependWatchReactor("certmanagers", func(action clienttesting.Action) (handled bool, ret watch.Interface, err error) {
345-
gvr := action.GetResource()
346-
ns := action.GetNamespace()
347-
watch, err := fakeClient.Tracker().Watch(gvr, ns)
348-
if err != nil {
349-
return false, nil, err
350-
}
351-
close(watcherStarted)
352-
return true, watch, nil
353-
})
354-
355-
// Create cert manager informers using the fake client.
356-
certManagerInformers := certmanoperatorinformer.NewSharedInformerFactory(fakeClient, 0).Operator().V1alpha1().CertManagers()
357-
358-
// Create a channel to receive the cert manager objects from the informer.
359-
certManagerChan := make(chan *v1alpha1.CertManager, 1)
360-
361-
// Add event handlers to the informer to write the cert manager objects to
362-
// the channel received during the add and the delete events.
363-
certManagerInformers.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
364-
AddFunc: func(obj any) {
365-
certManagerObj := obj.(*v1alpha1.CertManager)
366-
t.Logf("cert manager obj added: %s", certManagerObj.Name)
367-
certManagerChan <- certManagerObj
368-
},
369-
DeleteFunc: func(obj any) {
370-
certManagerObj := obj.(*v1alpha1.CertManager)
371-
t.Logf("cert manager obj deleted: %s", certManagerObj.Name)
372-
certManagerChan <- certManagerObj
373-
},
374-
})
375-
376-
// Make sure informer is running.
377-
go certManagerInformers.Informer().Run(ctx.Done())
378-
379-
// This is not required in tests, but it serves as a proof-of-concept by
380-
// ensuring that the informer goroutine have warmed up and called List before
381-
// we send any events to it.
382-
cache.WaitForCacheSync(ctx.Done(), certManagerInformers.Informer().HasSynced)
383-
384-
// The fake client doesn't support resource version. Any writes to the client
385-
// after the informer's initial LIST and before the informer establishing the
386-
// watcher will be missed by the informer. Therefore we wait until the watcher
387-
// starts.
388-
<-watcherStarted
421+
fakeClient, certManagerInformers, certManagerChan := setupSyncedFakeCertManagerInformer(t, ctx)
389422

390423
for _, tc := range tests {
391424
t.Run(tc.name, func(t *testing.T) {
392-
// Create the cert manager object using the fake client.
393-
_, err := fakeClient.OperatorV1alpha1().CertManagers().Create(ctx, &tc.certManagerObj, metav1.CreateOptions{})
394-
if err != nil {
395-
t.Fatalf("error injecting cert manager add: %v", err)
396-
}
397-
398-
// Wait for the informer to get the event.
399-
select {
400-
case <-certManagerChan:
401-
case <-time.After(wait.ForeverTestTimeout):
402-
t.Fatal("Informer did not get the added cert manager object")
403-
}
425+
withFakeCertManagerForTest(t, ctx, fakeClient, certManagerChan, &tc.certManagerObj)
404426

405427
actualOverrideResources, err := getOverrideResourcesFor(certManagerInformers, tc.deploymentName)
406428
assert.NoError(t, err)
407429
require.Equal(t, tc.expectedOverrideResources, actualOverrideResources)
408-
409-
// Delete the cert manager object using the fake client.
410-
err = fakeClient.OperatorV1alpha1().CertManagers().Delete(ctx, tc.certManagerObj.Name, metav1.DeleteOptions{})
411-
if err != nil {
412-
t.Fatalf("error deleting cert manager add: %v", err)
413-
}
414-
415-
// Wait for the informer to get the event.
416-
select {
417-
case <-certManagerChan:
418-
case <-time.After(wait.ForeverTestTimeout):
419-
t.Fatal("Informer did not get the deleted cert manager")
420-
}
421430
})
422431
}
423432
}
@@ -856,89 +865,15 @@ func TestGetOverrideSchedulingFor(t *testing.T) {
856865
}
857866

858867
ctx := t.Context()
859-
860-
// Create channel to know when the watch has started.
861-
watcherStarted := make(chan struct{})
862-
// Create the fake client.
863-
fakeClient := fake.NewSimpleClientset()
864-
// A watch reactor for cert manager objects that allows the injection of the watcherStarted channel.
865-
fakeClient.PrependWatchReactor("certmanagers", func(action clienttesting.Action) (handled bool, ret watch.Interface, err error) {
866-
gvr := action.GetResource()
867-
ns := action.GetNamespace()
868-
watch, err := fakeClient.Tracker().Watch(gvr, ns)
869-
if err != nil {
870-
return false, nil, err
871-
}
872-
close(watcherStarted)
873-
return true, watch, nil
874-
})
875-
876-
// Create cert manager informers using the fake client.
877-
certManagerInformers := certmanoperatorinformer.NewSharedInformerFactory(fakeClient, 0).Operator().V1alpha1().CertManagers()
878-
879-
// Create a channel to receive the cert manager objects from the informer.
880-
certManagerChan := make(chan *v1alpha1.CertManager, 1)
881-
882-
// Add event handlers to the informer to write the cert manager objects to
883-
// the channel received during the add and the delete events.
884-
certManagerInformers.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
885-
AddFunc: func(obj any) {
886-
certManagerObj := obj.(*v1alpha1.CertManager)
887-
t.Logf("cert manager obj added: %s", certManagerObj.Name)
888-
certManagerChan <- certManagerObj
889-
},
890-
DeleteFunc: func(obj any) {
891-
certManagerObj := obj.(*v1alpha1.CertManager)
892-
t.Logf("cert manager obj deleted: %s", certManagerObj.Name)
893-
certManagerChan <- certManagerObj
894-
},
895-
})
896-
897-
// Make sure informer is running.
898-
go certManagerInformers.Informer().Run(ctx.Done())
899-
900-
// This is not required in tests, but it serves as a proof-of-concept by
901-
// ensuring that the informer goroutine have warmed up and called List before
902-
// we send any events to it.
903-
cache.WaitForCacheSync(ctx.Done(), certManagerInformers.Informer().HasSynced)
904-
905-
// The fake client doesn't support resource version. Any writes to the client
906-
// after the informer's initial LIST and before the informer establishing the
907-
// watcher will be missed by the informer. Therefore we wait until the watcher
908-
// starts.
909-
<-watcherStarted
868+
fakeClient, certManagerInformers, certManagerChan := setupSyncedFakeCertManagerInformer(t, ctx)
910869

911870
for _, tc := range tests {
912871
t.Run(tc.name, func(t *testing.T) {
913-
// Create the cert manager object using the fake client.
914-
_, err := fakeClient.OperatorV1alpha1().CertManagers().Create(ctx, &tc.certManagerObj, metav1.CreateOptions{})
915-
if err != nil {
916-
t.Fatalf("error injecting cert manager add: %v", err)
917-
}
918-
919-
// Wait for the informer to get the event.
920-
select {
921-
case <-certManagerChan:
922-
case <-time.After(wait.ForeverTestTimeout):
923-
t.Fatal("Informer did not get the added cert manager object")
924-
}
872+
withFakeCertManagerForTest(t, ctx, fakeClient, certManagerChan, &tc.certManagerObj)
925873

926874
actualOverrideScheduling, err := getOverrideSchedulingFor(certManagerInformers, tc.deploymentName)
927875
assert.NoError(t, err)
928876
require.Equal(t, tc.expectedOverrideScheduling, actualOverrideScheduling)
929-
930-
// Delete the cert manager object using the fake client.
931-
err = fakeClient.OperatorV1alpha1().CertManagers().Delete(ctx, tc.certManagerObj.Name, metav1.DeleteOptions{})
932-
if err != nil {
933-
t.Fatalf("error deleting cert manager add: %v", err)
934-
}
935-
936-
// Wait for the informer to get the event.
937-
select {
938-
case <-certManagerChan:
939-
case <-time.After(wait.ForeverTestTimeout):
940-
t.Fatal("Informer did not get the deleted cert manager")
941-
}
942877
})
943878
}
944879
}
@@ -1057,72 +992,11 @@ func TestGetOverrideReplicasFor(t *testing.T) {
1057992
}
1058993

1059994
ctx := t.Context()
1060-
1061-
// Create channel to know when the watch has started.
1062-
watcherStarted := make(chan struct{})
1063-
// Create the fake client.
1064-
fakeClient := fake.NewSimpleClientset()
1065-
// A watch reactor for cert manager objects that allows the injection of the watcherStarted channel.
1066-
fakeClient.PrependWatchReactor("certmanagers", func(action clienttesting.Action) (handled bool, ret watch.Interface, err error) {
1067-
gvr := action.GetResource()
1068-
ns := action.GetNamespace()
1069-
watch, err := fakeClient.Tracker().Watch(gvr, ns)
1070-
if err != nil {
1071-
return false, nil, err
1072-
}
1073-
close(watcherStarted)
1074-
return true, watch, nil
1075-
})
1076-
1077-
// Create cert manager informers using the fake client.
1078-
certManagerInformers := certmanoperatorinformer.NewSharedInformerFactory(fakeClient, 0).Operator().V1alpha1().CertManagers()
1079-
1080-
// Create a channel to receive the cert manager objects from the informer.
1081-
certManagerChan := make(chan *v1alpha1.CertManager, 1)
1082-
1083-
// Add event handlers to the informer to write the cert manager objects to
1084-
// the channel received during the add and the delete events.
1085-
certManagerInformers.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
1086-
AddFunc: func(obj any) {
1087-
certManagerObj := obj.(*v1alpha1.CertManager)
1088-
t.Logf("cert manager obj added: %s", certManagerObj.Name)
1089-
certManagerChan <- certManagerObj
1090-
},
1091-
DeleteFunc: func(obj any) {
1092-
certManagerObj := obj.(*v1alpha1.CertManager)
1093-
t.Logf("cert manager obj deleted: %s", certManagerObj.Name)
1094-
certManagerChan <- certManagerObj
1095-
},
1096-
})
1097-
1098-
// Make sure informer is running.
1099-
go certManagerInformers.Informer().Run(ctx.Done())
1100-
1101-
// This is not required in tests, but it serves as a proof-of-concept by
1102-
// ensuring that the informer goroutine have warmed up and called List before
1103-
// we send any events to it.
1104-
cache.WaitForCacheSync(ctx.Done(), certManagerInformers.Informer().HasSynced)
1105-
1106-
// The fake client doesn't support resource version. Any writes to the client
1107-
// after the informer's initial LIST and before the informer establishing the
1108-
// watcher will be missed by the informer. Therefore we wait until the watcher
1109-
// starts.
1110-
<-watcherStarted
995+
fakeClient, certManagerInformers, certManagerChan := setupSyncedFakeCertManagerInformer(t, ctx)
1111996

1112997
for _, tc := range tests {
1113998
t.Run(tc.name, func(t *testing.T) {
1114-
// Create the cert manager object using the fake client.
1115-
_, err := fakeClient.OperatorV1alpha1().CertManagers().Create(ctx, &tc.certManagerObj, metav1.CreateOptions{})
1116-
if err != nil {
1117-
t.Fatalf("error injecting cert manager add: %v", err)
1118-
}
1119-
1120-
// Wait for the informer to get the event.
1121-
select {
1122-
case <-certManagerChan:
1123-
case <-time.After(wait.ForeverTestTimeout):
1124-
t.Fatal("Informer did not get the added cert manager object")
1125-
}
999+
withFakeCertManagerForTest(t, ctx, fakeClient, certManagerChan, &tc.certManagerObj)
11261000

11271001
actualOverrideReplicas, err := getOverrideReplicasFor(certManagerInformers, tc.deploymentName)
11281002
assert.NoError(t, err)
@@ -1132,19 +1006,6 @@ func TestGetOverrideReplicasFor(t *testing.T) {
11321006
require.NotNil(t, actualOverrideReplicas)
11331007
assert.Equal(t, *tc.expectedOverrideReplicas, *actualOverrideReplicas)
11341008
}
1135-
1136-
// Delete the cert manager object using the fake client.
1137-
err = fakeClient.OperatorV1alpha1().CertManagers().Delete(ctx, tc.certManagerObj.Name, metav1.DeleteOptions{})
1138-
if err != nil {
1139-
t.Fatalf("error deleting cert manager add: %v", err)
1140-
}
1141-
1142-
// Wait for the informer to get the event.
1143-
select {
1144-
case <-certManagerChan:
1145-
case <-time.After(wait.ForeverTestTimeout):
1146-
t.Fatal("Informer did not get the deleted cert manager")
1147-
}
11481009
})
11491010
}
11501011
}

0 commit comments

Comments
 (0)