@@ -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,7 +231,64 @@ 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 () {
235
+ cache := & fakeReader {}
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 {}}}})
243
+ Expect (err ).NotTo (HaveOccurred ())
244
+ Expect (cl ).NotTo (BeNil ())
245
+
246
+ tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "wh-defined" }}
247
+ tns , err = clientset .CoreV1 ().Namespaces ().Create (ctx , tns , metav1.CreateOptions {})
248
+ Expect (err ).NotTo (HaveOccurred ())
249
+ Expect (tns ).NotTo (BeNil ())
250
+ defer deleteNamespace (ctx , tns )
251
+
252
+ toCreate := & pkg.ChaosPod {
253
+ ObjectMeta : metav1.ObjectMeta {
254
+ Name : "example" ,
255
+ Namespace : tns .Name ,
256
+ },
257
+ // The ChaosPod CRD does not define Status, so the field is unknown to the API server,
258
+ // but field validation is not strict by default, so the API server returns a warning,
259
+ // and we need a warning to check whether suppression works.
260
+ Status : pkg.ChaosPodStatus {},
261
+ }
262
+ err = cl .Create (ctx , toCreate )
263
+ Expect (err ).NotTo (HaveOccurred ())
264
+ Expect (cl ).NotTo (BeNil ())
265
+
266
+ scannerTestLog := bufio .NewScanner (& testLog )
267
+ for scannerTestLog .Scan () {
268
+ line := scannerTestLog .Text ()
269
+ if strings .Contains (
270
+ line ,
271
+ "unknown field \" status\" " ,
272
+ ) {
273
+ return
274
+ }
275
+ }
276
+ defer Fail ("expected to find one API server warning logged the config.WarningHandler" )
277
+
278
+ scanner := bufio .NewScanner (& log )
279
+ for scanner .Scan () {
280
+ line := scanner .Text ()
281
+ if strings .Contains (
282
+ line ,
283
+ "unknown field \" status\" " ,
284
+ ) {
285
+ defer Fail ("expected to find zero API server warnings in the client log" )
286
+ break
287
+ }
288
+ }
289
+ })
290
+
291
+ It ("(deprecated behavior) should log warnings when warning suppression is disabled" , func () {
233
292
cache := & fakeReader {}
234
293
cl , err := client .New (cfg , client.Options {
235
294
WarningHandler : client.WarningHandlerOptions {SuppressWarnings : false }, Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}},
@@ -270,7 +329,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
270
329
defer Fail ("expected to find one API server warning in the client log" )
271
330
})
272
331
273
- It ("should not log warnings when warning suppression is enabled" , func () {
332
+ It ("(deprecated behavior) should not log warnings when warning suppression is enabled" , func () {
274
333
cache := & fakeReader {}
275
334
cl , err := client .New (cfg , client.Options {
276
335
WarningHandler : client.WarningHandlerOptions {SuppressWarnings : true }, Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}},
0 commit comments