@@ -20,14 +20,20 @@ import (
2020 "context"
2121 "testing"
2222
23+ certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
24+ cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
2325 . "github.com/onsi/gomega" //revive:disable:dot-imports
2426
2527 operatorv1beta1 "github.com/openstack-k8s-operators/openstack-operator/api/operator/v1beta1"
2628 corev1 "k8s.io/api/core/v1"
29+ apierrors "k8s.io/apimachinery/pkg/api/errors"
2730 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2831 "k8s.io/apimachinery/pkg/runtime"
32+ "k8s.io/utils/ptr"
33+ ctrl "sigs.k8s.io/controller-runtime"
2934 "sigs.k8s.io/controller-runtime/pkg/client"
3035 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
36+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
3137 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3238)
3339
@@ -37,9 +43,150 @@ func newTestScheme(t *testing.T) *runtime.Scheme {
3743 g := NewWithT (t )
3844 g .Expect (corev1 .AddToScheme (s )).To (Succeed ())
3945 g .Expect (operatorv1beta1 .AddToScheme (s )).To (Succeed ())
46+ g .Expect (certmgrv1 .AddToScheme (s )).To (Succeed ())
4047 return s
4148}
4249
50+ func readyCertificate (name , namespace string ) * certmgrv1.Certificate {
51+ return & certmgrv1.Certificate {
52+ ObjectMeta : metav1.ObjectMeta {Name : name , Namespace : namespace , Generation : 1 },
53+ Status : certmgrv1.CertificateStatus {
54+ Conditions : []certmgrv1.CertificateCondition {
55+ {Type : certmgrv1 .CertificateConditionReady , Status : cmmeta .ConditionTrue , ObservedGeneration : 1 },
56+ },
57+ },
58+ }
59+ }
60+
61+ // TestCheckWebhookCertificates covers the cert-manager Certificate readiness gate that
62+ // prevents the operator from waiting on deployments that can't start because their webhook
63+ // certificate Secret hasn't been issued yet.
64+ func TestCheckWebhookCertificates (t * testing.T ) {
65+ ctx := context .Background ()
66+ instance := & operatorv1beta1.OpenStack {
67+ ObjectMeta : metav1.ObjectMeta {Name : "openstack" , Namespace : "openstack-operators" },
68+ }
69+
70+ t .Run ("requeues when a certificate is missing" , func (t * testing.T ) {
71+ g := NewWithT (t )
72+ s := newTestScheme (t )
73+ fakeClient := fakeclient .NewClientBuilder ().WithScheme (s ).WithObjects (instance ).Build ()
74+ r := & OpenStackReconciler {Client : fakeClient , Scheme : s }
75+
76+ result , err := r .checkWebhookCertificates (ctx , instance )
77+ g .Expect (err ).NotTo (HaveOccurred ())
78+ g .Expect (result .RequeueAfter ).NotTo (BeZero ())
79+ })
80+
81+ t .Run ("requeues when a certificate exists but isn't ready" , func (t * testing.T ) {
82+ g := NewWithT (t )
83+ s := newTestScheme (t )
84+ notReady := & certmgrv1.Certificate {
85+ ObjectMeta : metav1.ObjectMeta {Name : "openstack-operator-serving-cert" , Namespace : "openstack-operators" },
86+ }
87+ objs := []client.Object {
88+ instance , notReady ,
89+ readyCertificate ("infra-operator-serving-cert" , "openstack-operators" ),
90+ readyCertificate ("openstack-baremetal-operator-serving-cert" , "openstack-operators" ),
91+ }
92+ fakeClient := fakeclient .NewClientBuilder ().WithScheme (s ).WithObjects (objs ... ).Build ()
93+ r := & OpenStackReconciler {Client : fakeClient , Scheme : s }
94+
95+ result , err := r .checkWebhookCertificates (ctx , instance )
96+ g .Expect (err ).NotTo (HaveOccurred ())
97+ g .Expect (result .RequeueAfter ).NotTo (BeZero ())
98+ })
99+
100+ t .Run ("requeues when Ready is stale from a previous generation" , func (t * testing.T ) {
101+ g := NewWithT (t )
102+ s := newTestScheme (t )
103+ // Certificate spec changed (Generation bumped to 2) but status still reflects the old
104+ // generation's Ready condition - cert-manager hasn't reconciled the new spec yet.
105+ stale := & certmgrv1.Certificate {
106+ ObjectMeta : metav1.ObjectMeta {Name : "openstack-operator-serving-cert" , Namespace : "openstack-operators" , Generation : 2 },
107+ Status : certmgrv1.CertificateStatus {
108+ Conditions : []certmgrv1.CertificateCondition {
109+ {Type : certmgrv1 .CertificateConditionReady , Status : cmmeta .ConditionTrue , ObservedGeneration : 1 },
110+ },
111+ },
112+ }
113+ objs := []client.Object {
114+ instance , stale ,
115+ readyCertificate ("infra-operator-serving-cert" , "openstack-operators" ),
116+ readyCertificate ("openstack-baremetal-operator-serving-cert" , "openstack-operators" ),
117+ }
118+ fakeClient := fakeclient .NewClientBuilder ().WithScheme (s ).WithObjects (objs ... ).Build ()
119+ r := & OpenStackReconciler {Client : fakeClient , Scheme : s }
120+
121+ result , err := r .checkWebhookCertificates (ctx , instance )
122+ g .Expect (err ).NotTo (HaveOccurred ())
123+ g .Expect (result .RequeueAfter ).NotTo (BeZero (), "a Ready condition from a stale generation must not be treated as current" )
124+ })
125+
126+ t .Run ("proceeds when all certificates are ready" , func (t * testing.T ) {
127+ g := NewWithT (t )
128+ s := newTestScheme (t )
129+ objs := []client.Object {
130+ instance ,
131+ readyCertificate ("openstack-operator-serving-cert" , "openstack-operators" ),
132+ readyCertificate ("infra-operator-serving-cert" , "openstack-operators" ),
133+ readyCertificate ("openstack-baremetal-operator-serving-cert" , "openstack-operators" ),
134+ }
135+ fakeClient := fakeclient .NewClientBuilder ().WithScheme (s ).WithObjects (objs ... ).Build ()
136+ r := & OpenStackReconciler {Client : fakeClient , Scheme : s }
137+
138+ result , err := r .checkWebhookCertificates (ctx , instance )
139+ g .Expect (err ).NotTo (HaveOccurred ())
140+ g .Expect (result .RequeueAfter ).To (BeZero ())
141+ })
142+
143+ t .Run ("propagates a non-NotFound error from the API server" , func (t * testing.T ) {
144+ g := NewWithT (t )
145+ s := newTestScheme (t )
146+ fakeClient := fakeclient .NewClientBuilder ().
147+ WithScheme (s ).
148+ WithObjects (instance ).
149+ WithInterceptorFuncs (interceptor.Funcs {
150+ Get : func (ctx context.Context , c client.WithWatch , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
151+ if _ , ok := obj .(* certmgrv1.Certificate ); ok {
152+ return apierrors .NewServiceUnavailable ("etcd unavailable" )
153+ }
154+ return c .Get (ctx , key , obj , opts ... )
155+ },
156+ }).
157+ Build ()
158+ r := & OpenStackReconciler {Client : fakeClient , Scheme : s }
159+
160+ result , err := r .checkWebhookCertificates (ctx , instance )
161+ g .Expect (err ).To (HaveOccurred ())
162+ g .Expect (result ).To (Equal (ctrl.Result {}))
163+ })
164+
165+ t .Run ("skips disabled operators even if their certificate is missing" , func (t * testing.T ) {
166+ g := NewWithT (t )
167+ s := newTestScheme (t )
168+ disabledInstance := & operatorv1beta1.OpenStack {
169+ ObjectMeta : metav1.ObjectMeta {Name : "openstack" , Namespace : "openstack-operators" },
170+ Spec : operatorv1beta1.OpenStackSpec {
171+ OperatorOverrides : []operatorv1beta1.OperatorSpec {
172+ {Name : "infra" , Replicas : ptr .To (int32 (0 ))},
173+ {Name : "openstack-baremetal" , Replicas : ptr .To (int32 (0 ))},
174+ },
175+ },
176+ }
177+ objs := []client.Object {
178+ disabledInstance ,
179+ readyCertificate ("openstack-operator-serving-cert" , "openstack-operators" ),
180+ }
181+ fakeClient := fakeclient .NewClientBuilder ().WithScheme (s ).WithObjects (objs ... ).Build ()
182+ r := & OpenStackReconciler {Client : fakeClient , Scheme : s }
183+
184+ result , err := r .checkWebhookCertificates (ctx , disabledInstance )
185+ g .Expect (err ).NotTo (HaveOccurred ())
186+ g .Expect (result .RequeueAfter ).To (BeZero (), "disabled operators' missing certificates should not block" )
187+ })
188+ }
189+
43190// TestAdoptAwayServiceAccounts covers the ownership-migration logic: sub-operator
44191// ServiceAccounts left over from before openstack-operator#2109 are still
45192// controlled by the OpenStack instance, but this controller no longer manages them
0 commit comments