Skip to content

Commit 07b082b

Browse files
committed
ESO-83: Makes use of uncached client for unmanaged resources
1 parent 7f70c8b commit 07b082b

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

pkg/controller/external_secrets/certificate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (r *Reconciler) assertSecretRefExists(es *operatorv1alpha1.ExternalSecrets,
151151
}
152152
object := &corev1.Secret{}
153153

154-
if err := r.Get(r.ctx, namespacedName, object); err != nil {
154+
if err := r.UncachedClient.Get(r.ctx, namespacedName, object); err != nil {
155155
return fmt.Errorf("failed to fetch %q secret: %w", namespacedName, err)
156156
}
157157

@@ -172,7 +172,7 @@ func (r *Reconciler) getIssuer(issuerRef v1.ObjectReference, namespace string) (
172172
object = &certmanagerv1.Issuer{}
173173
}
174174

175-
if ifExists, err := r.Exists(r.ctx, namespacedName, object); err != nil {
175+
if ifExists, err := r.UncachedClient.Exists(r.ctx, namespacedName, object); err != nil {
176176
return ifExists, fmt.Errorf("failed to fetch %q issuer: %w", namespacedName, err)
177177
} else {
178178
return ifExists, nil

pkg/controller/external_secrets/certificate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ func TestCreateOrApplyCertificates(t *testing.T) {
450450
tt.preReq(r, mock)
451451
}
452452
r.CtrlClient = mock
453+
r.UncachedClient = mock
453454

454455
es := testExternalSecretsForCertificate()
455456
if tt.es != nil {

pkg/controller/external_secrets/controller.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var (
8181
// Reconciler reconciles a ExternalSecrets object
8282
type Reconciler struct {
8383
operatorclient.CtrlClient
84+
UncachedClient operatorclient.CtrlClient
8485
Scheme *runtime.Scheme
8586
ctx context.Context
8687
eventRecorder record.EventRecorder
@@ -122,14 +123,27 @@ func New(ctx context.Context, mgr ctrl.Manager) (*Reconciler, error) {
122123
esm: new(operatorv1alpha1.ExternalSecretsManager),
123124
optionalResourcesList: make(map[string]struct{}),
124125
}
126+
127+
// create a cached client for all the managed objects.
125128
c, err := NewClient(mgr, r)
126129
if err != nil {
127130
return nil, err
128131
}
129132
r.CtrlClient = c
133+
134+
// create an uncached client for the objects not managed by
135+
// the controller.
136+
uc, err := NewUncachedClient(mgr)
137+
if err != nil {
138+
return nil, err
139+
}
140+
r.UncachedClient = uc
141+
130142
return r, nil
131143
}
132144

145+
// NewClient is for creating a cached client, where the required objects are cached and informer are set to
146+
// update the cache.
133147
func NewClient(m manager.Manager, r *Reconciler) (operatorclient.CtrlClient, error) {
134148
c, err := BuildCustomClient(m, r)
135149
if err != nil {
@@ -140,6 +154,18 @@ func NewClient(m manager.Manager, r *Reconciler) (operatorclient.CtrlClient, err
140154
}, nil
141155
}
142156

157+
// NewUncachedClient is for creating an uncached client, and all the objects are read and written directly
158+
// through API server.
159+
func NewUncachedClient(m manager.Manager) (operatorclient.CtrlClient, error) {
160+
c, err := client.New(m.GetConfig(), client.Options{Scheme: m.GetScheme()})
161+
if err != nil {
162+
return nil, fmt.Errorf("failed to create uncached client: %w", err)
163+
}
164+
return &operatorclient.CtrlClientImpl{
165+
Client: c,
166+
}, nil
167+
}
168+
143169
// BuildCustomClient creates a custom client with a custom cache of required objects.
144170
// The corresponding informers receive events for objects matching label criteria.
145171
func BuildCustomClient(mgr ctrl.Manager, r *Reconciler) (client.Client, error) {
@@ -166,8 +192,6 @@ func BuildCustomClient(mgr ctrl.Manager, r *Reconciler) (client.Client, error) {
166192
objectList[&certmanagerv1.Certificate{}] = cache.ByObject{
167193
Label: managedResourceLabelReqSelector,
168194
}
169-
objectList[&certmanagerv1.ClusterIssuer{}] = cache.ByObject{}
170-
objectList[&certmanagerv1.Issuer{}] = cache.ByObject{}
171195
}
172196

173197
customCacheOpts := cache.Options{
@@ -192,14 +216,6 @@ func BuildCustomClient(mgr ctrl.Manager, r *Reconciler) (client.Client, error) {
192216
if err != nil {
193217
return nil, fmt.Errorf("failed to add informer for %s resource: %w", (&certmanagerv1.Certificate{}).GetObjectKind().GroupVersionKind().String(), err)
194218
}
195-
_, err = customCache.GetInformer(context.Background(), &certmanagerv1.ClusterIssuer{})
196-
if err != nil {
197-
return nil, fmt.Errorf("failed to add informer for %s resource: %w", (&certmanagerv1.ClusterIssuer{}).GetObjectKind().GroupVersionKind().String(), err)
198-
}
199-
_, err = customCache.GetInformer(context.Background(), &certmanagerv1.Issuer{})
200-
if err != nil {
201-
return nil, fmt.Errorf("failed to add informer for %s resource: %w", (&certmanagerv1.Issuer{}).GetObjectKind().GroupVersionKind().String(), err)
202-
}
203219
}
204220
_, err = customCache.GetInformer(context.Background(), ownObject)
205221
if err != nil {

0 commit comments

Comments
 (0)