@@ -36,7 +36,7 @@ func TestEnsureTrustedCABundleConfigMap(t *testing.T) {
3636 preReq func (r * Reconciler , cached * fakes.FakeCtrlClient , uncached * fakes.FakeCtrlClient )
3737 wantErr string
3838 wantCreate bool
39- wantUpdate bool
39+ wantPatch bool
4040 noProxy bool
4141 }{
4242 {
@@ -79,10 +79,9 @@ func TestEnsureTrustedCABundleConfigMap(t *testing.T) {
7979 return true , nil
8080 })
8181 },
82- wantUpdate : false ,
8382 },
8483 {
85- name : "ConfigMap exists with wrong labels, update triggered" ,
84+ name : "ConfigMap exists with wrong labels, patch triggered" ,
8685 resourceMetadata : testResourceMetadata (commontest .TestExternalSecretsConfig ()),
8786 preReq : func (_ * Reconciler , cached * fakes.FakeCtrlClient , _ * fakes.FakeCtrlClient ) {
8887 cached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , obj client.Object ) (bool , error ) {
@@ -97,73 +96,58 @@ func TestEnsureTrustedCABundleConfigMap(t *testing.T) {
9796 existing .DeepCopyInto (obj .(* corev1.ConfigMap ))
9897 return true , nil
9998 })
100- cached .UpdateWithRetryCalls (func (_ context.Context , obj client.Object , _ ... client.UpdateOption ) error {
99+ cached .PatchCalls (func (_ context.Context , obj client.Object , patch client. Patch , _ ... client.PatchOption ) error {
101100 cm , ok := obj .(* corev1.ConfigMap )
102101 if ! ok {
103102 t .Errorf ("expected ConfigMap, got %T" , obj )
104103 }
105104 if cm .Labels [trustedCABundleInjectLabel ] != "true" {
106- t .Errorf ("expected inject label restored , got %q" , cm .Labels [trustedCABundleInjectLabel ])
105+ t .Errorf ("expected inject label in patch target , got %q" , cm .Labels [trustedCABundleInjectLabel ])
107106 }
108- if cm . Data [ "ca-bundle.crt" ] != "cert-data" {
109- t .Errorf ("CNO-managed data should be preserved , got %v " , cm . Data )
107+ if patch . Type () != types . MergePatchType {
108+ t .Errorf ("expected MergePatch , got %s " , patch . Type () )
110109 }
111110 return nil
112111 })
113112 },
114- wantUpdate : true ,
113+ wantPatch : true ,
115114 },
116115 {
117116 // Regression test for: labels updating with app: external-secrets of cm
118117 // external-secrets-trusted-ca-bundle will block the reconcile in the http_proxy env.
119118 //
120119 // When the managed label (app=external-secrets) is removed from the ConfigMap,
121120 // the object falls out of the label-filtered cache. The cached Exists() returns
122- // false, Create() fails with AlreadyExists, and the controller must use the
123- // uncached client to fetch and restore the correct labels instead of returning
124- // an error that blocks reconciliation .
125- name : "Create returns AlreadyExists (label-filtered cache miss): uncached client restores labels " ,
121+ // false, Create() fails with AlreadyExists, and the controller must patch only
122+ // the metadata (labels/annotations) via the uncached client, leaving
123+ // CNO-managed Data/BinaryData untouched .
124+ name : "Create returns AlreadyExists (label-filtered cache miss): uncached client patches metadata " ,
126125 resourceMetadata : testResourceMetadata (commontest .TestExternalSecretsConfig ()),
127126 preReq : func (_ * Reconciler , cached * fakes.FakeCtrlClient , uncached * fakes.FakeCtrlClient ) {
128- // Cached client doesn't see the ConfigMap because its label was changed.
129127 cached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , _ client.Object ) (bool , error ) {
130128 return false , nil
131129 })
132130 cached .CreateCalls (func (_ context.Context , _ client.Object , _ ... client.CreateOption ) error {
133131 return apierrors .NewAlreadyExists (schema.GroupResource {Resource : "configmaps" }, trustedCABundleConfigMapName )
134132 })
135- // Uncached client fetches the real object from the API server.
136- uncached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , obj client.Object ) (bool , error ) {
137- existing := & corev1.ConfigMap {
138- ObjectMeta : metav1.ObjectMeta {
139- Name : trustedCABundleConfigMapName ,
140- Namespace : commontest .TestExternalSecretsNamespace ,
141- ResourceVersion : "1000" ,
142- Labels : map [string ]string {"app" : "update-secrets" },
143- },
144- Data : cnoData ,
145- }
146- existing .DeepCopyInto (obj .(* corev1.ConfigMap ))
147- return true , nil
148- })
149- uncached .UpdateWithRetryCalls (func (_ context.Context , obj client.Object , _ ... client.UpdateOption ) error {
133+ uncached .PatchCalls (func (_ context.Context , obj client.Object , patch client.Patch , _ ... client.PatchOption ) error {
150134 cm , ok := obj .(* corev1.ConfigMap )
151135 if ! ok {
152136 t .Errorf ("expected ConfigMap, got %T" , obj )
153137 }
154138 if cm .Labels ["app" ] != "external-secrets" {
155- t .Errorf ("expected app=external-secrets label restored , got %q" , cm .Labels ["app" ])
139+ t .Errorf ("expected app=external-secrets label in patch target , got %q" , cm .Labels ["app" ])
156140 }
157141 if cm .Labels [trustedCABundleInjectLabel ] != "true" {
158- t .Errorf ("expected inject label restored , got %q" , cm .Labels [trustedCABundleInjectLabel ])
142+ t .Errorf ("expected inject label in patch target , got %q" , cm .Labels [trustedCABundleInjectLabel ])
159143 }
160- if cm . Data [ "ca-bundle.crt" ] != "cert-data" {
161- t .Errorf ("CNO-managed data should be preserved , got %v " , cm . Data )
144+ if patch . Type () != types . MergePatchType {
145+ t .Errorf ("expected MergePatch , got %s " , patch . Type () )
162146 }
163147 return nil
164148 })
165149 },
166- wantUpdate : true ,
150+ wantPatch : true ,
167151 },
168152 {
169153 name : "proxy not configured: ConfigMap reconcile skipped" ,
@@ -194,23 +178,7 @@ func TestEnsureTrustedCABundleConfigMap(t *testing.T) {
194178 wantErr : "failed to create external-secrets/external-secrets-trusted-ca-bundle trusted CA bundle ConfigMap resource: test client error" ,
195179 },
196180 {
197- name : "uncached Exists fails after AlreadyExists from Create" ,
198- resourceMetadata : testResourceMetadata (commontest .TestExternalSecretsConfig ()),
199- preReq : func (_ * Reconciler , cached * fakes.FakeCtrlClient , uncached * fakes.FakeCtrlClient ) {
200- cached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , _ client.Object ) (bool , error ) {
201- return false , nil
202- })
203- cached .CreateCalls (func (_ context.Context , _ client.Object , _ ... client.CreateOption ) error {
204- return apierrors .NewAlreadyExists (schema.GroupResource {Resource : "configmaps" }, trustedCABundleConfigMapName )
205- })
206- uncached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , _ client.Object ) (bool , error ) {
207- return false , commontest .ErrTestClient
208- })
209- },
210- wantErr : "failed to fetch existing external-secrets/external-secrets-trusted-ca-bundle trusted CA bundle ConfigMap for restoration: test client error" ,
211- },
212- {
213- name : "uncached UpdateWithRetry fails after AlreadyExists from Create" ,
181+ name : "uncached Patch fails after AlreadyExists from Create" ,
214182 resourceMetadata : testResourceMetadata (commontest .TestExternalSecretsConfig ()),
215183 preReq : func (_ * Reconciler , cached * fakes.FakeCtrlClient , uncached * fakes.FakeCtrlClient ) {
216184 cached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , _ client.Object ) (bool , error ) {
@@ -219,26 +187,14 @@ func TestEnsureTrustedCABundleConfigMap(t *testing.T) {
219187 cached .CreateCalls (func (_ context.Context , _ client.Object , _ ... client.CreateOption ) error {
220188 return apierrors .NewAlreadyExists (schema.GroupResource {Resource : "configmaps" }, trustedCABundleConfigMapName )
221189 })
222- uncached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , obj client.Object ) (bool , error ) {
223- existing := & corev1.ConfigMap {
224- ObjectMeta : metav1.ObjectMeta {
225- Name : trustedCABundleConfigMapName ,
226- Namespace : commontest .TestExternalSecretsNamespace ,
227- ResourceVersion : "1000" ,
228- Labels : map [string ]string {"app" : "update-secrets" },
229- },
230- }
231- existing .DeepCopyInto (obj .(* corev1.ConfigMap ))
232- return true , nil
233- })
234- uncached .UpdateWithRetryCalls (func (_ context.Context , _ client.Object , _ ... client.UpdateOption ) error {
190+ uncached .PatchCalls (func (_ context.Context , _ client.Object , _ client.Patch , _ ... client.PatchOption ) error {
235191 return commontest .ErrTestClient
236192 })
237193 },
238- wantErr : "failed to restore external-secrets/external-secrets-trusted-ca-bundle trusted CA bundle ConfigMap to desired state : test client error" ,
194+ wantErr : "failed to patch external-secrets/external-secrets-trusted-ca-bundle trusted CA bundle ConfigMap metadata : test client error" ,
239195 },
240196 {
241- name : "cached UpdateWithRetry fails when ConfigMap has wrong labels" ,
197+ name : "cached Patch fails when ConfigMap has wrong labels" ,
242198 resourceMetadata : testResourceMetadata (commontest .TestExternalSecretsConfig ()),
243199 preReq : func (_ * Reconciler , cached * fakes.FakeCtrlClient , _ * fakes.FakeCtrlClient ) {
244200 cached .ExistsCalls (func (_ context.Context , _ types.NamespacedName , obj client.Object ) (bool , error ) {
@@ -252,11 +208,11 @@ func TestEnsureTrustedCABundleConfigMap(t *testing.T) {
252208 existing .DeepCopyInto (obj .(* corev1.ConfigMap ))
253209 return true , nil
254210 })
255- cached .UpdateWithRetryCalls (func (_ context.Context , _ client.Object , _ ... client.UpdateOption ) error {
211+ cached .PatchCalls (func (_ context.Context , _ client.Object , _ client. Patch , _ ... client.PatchOption ) error {
256212 return commontest .ErrTestClient
257213 })
258214 },
259- wantErr : "failed to update external-secrets/external-secrets-trusted-ca-bundle trusted CA bundle ConfigMap resource : test client error" ,
215+ wantErr : "failed to patch external-secrets/external-secrets-trusted-ca-bundle trusted CA bundle ConfigMap metadata : test client error" ,
260216 },
261217 }
262218
@@ -293,11 +249,11 @@ func TestEnsureTrustedCABundleConfigMap(t *testing.T) {
293249 if tt .wantCreate && cached .CreateCallCount () == 0 {
294250 t .Error ("expected Create to be called, but it wasn't" )
295251 }
296- if tt .wantUpdate && cached .UpdateWithRetryCallCount () == 0 && uncached .UpdateWithRetryCallCount () == 0 {
297- t .Error ("expected UpdateWithRetry to be called (on cached or uncached client), but it wasn't" )
252+ if tt .wantPatch && cached .PatchCallCount () == 0 && uncached .PatchCallCount () == 0 {
253+ t .Error ("expected Patch to be called (on cached or uncached client), but it wasn't" )
298254 }
299- if ! tt .wantUpdate && (cached .UpdateWithRetryCallCount () > 0 || uncached .UpdateWithRetryCallCount () > 0 ) {
300- t .Error ("expected no UpdateWithRetry call, but one was made" )
255+ if ! tt .wantPatch && (cached .PatchCallCount () > 0 || uncached .PatchCallCount () > 0 ) {
256+ t .Error ("expected no Patch call, but one was made" )
301257 }
302258 })
303259 }
0 commit comments