@@ -18,6 +18,7 @@ package client_test
18
18
19
19
import (
20
20
"bufio"
21
+ "bytes"
21
22
"context"
22
23
"encoding/json"
23
24
"errors"
@@ -43,6 +44,7 @@ import (
43
44
"k8s.io/apimachinery/pkg/runtime/schema"
44
45
"k8s.io/apimachinery/pkg/types"
45
46
kscheme "k8s.io/client-go/kubernetes/scheme"
47
+ "k8s.io/client-go/rest"
46
48
"k8s.io/utils/ptr"
47
49
48
50
"sigs.k8s.io/controller-runtime/examples/crd/pkg"
@@ -229,15 +231,19 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
229
231
})
230
232
231
233
Describe ("WarningHandler" , func () {
232
- It ("should log warnings when warning suppression is disabled " , func () {
234
+ It ("should log warnings with config.WarningHandler, if one is defined " , func () {
233
235
cache := & fakeReader {}
234
- cl , err := client .New (cfg , client.Options {
235
- WarningHandler : client.WarningHandlerOptions {SuppressWarnings : false }, Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}},
236
- })
236
+
237
+ testCfg := rest .CopyConfig (cfg )
238
+
239
+ var testLog bytes.Buffer
240
+ testCfg .WarningHandler = rest .NewWarningWriter (& testLog , rest.WarningWriterOptions {})
241
+
242
+ cl , err := client .New (testCfg , client.Options {Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}}})
237
243
Expect (err ).NotTo (HaveOccurred ())
238
244
Expect (cl ).NotTo (BeNil ())
239
245
240
- tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ws-disabled " }}
246
+ tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "wh-defined " }}
241
247
tns , err = clientset .CoreV1 ().Namespaces ().Create (ctx , tns , metav1.CreateOptions {})
242
248
Expect (err ).NotTo (HaveOccurred ())
243
249
Expect (tns ).NotTo (BeNil ())
@@ -257,45 +263,17 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
257
263
Expect (err ).NotTo (HaveOccurred ())
258
264
Expect (cl ).NotTo (BeNil ())
259
265
260
- scanner := bufio .NewScanner (& log )
261
- for scanner .Scan () {
262
- line := scanner .Text ()
266
+ scannerTestLog := bufio .NewScanner (& testLog )
267
+ for scannerTestLog .Scan () {
268
+ line := scannerTestLog .Text ()
263
269
if strings .Contains (
264
270
line ,
265
271
"unknown field \" status\" " ,
266
272
) {
267
273
return
268
274
}
269
275
}
270
- defer Fail ("expected to find one API server warning in the client log" )
271
- })
272
-
273
- It ("should not log warnings when warning suppression is enabled" , func () {
274
- cache := & fakeReader {}
275
- cl , err := client .New (cfg , client.Options {
276
- WarningHandler : client.WarningHandlerOptions {SuppressWarnings : true }, Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}},
277
- })
278
- Expect (err ).NotTo (HaveOccurred ())
279
- Expect (cl ).NotTo (BeNil ())
280
-
281
- tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ws-enabled" }}
282
- tns , err = clientset .CoreV1 ().Namespaces ().Create (ctx , tns , metav1.CreateOptions {})
283
- Expect (err ).NotTo (HaveOccurred ())
284
- Expect (tns ).NotTo (BeNil ())
285
-
286
- toCreate := & pkg.ChaosPod {
287
- ObjectMeta : metav1.ObjectMeta {
288
- Name : "example" ,
289
- Namespace : tns .Name ,
290
- },
291
- // The ChaosPod CRD does not define Status, so the field is unknown to the API server,
292
- // but field validation is not strict by default, so the API server returns a warning,
293
- // and we need a warning to check whether suppression works.
294
- Status : pkg.ChaosPodStatus {},
295
- }
296
- err = cl .Create (ctx , toCreate )
297
- Expect (err ).NotTo (HaveOccurred ())
298
- Expect (cl ).NotTo (BeNil ())
276
+ defer Fail ("expected to find one API server warning logged the config.WarningHandler" )
299
277
300
278
scanner := bufio .NewScanner (& log )
301
279
for scanner .Scan () {
@@ -308,7 +286,6 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
308
286
break
309
287
}
310
288
}
311
- deleteNamespace (ctx , tns )
312
289
})
313
290
})
314
291
0 commit comments