@@ -19,8 +19,7 @@ so it does not change the status code or response body in any way.
19
19
This allows the server to send warnings easily readable by any API client, while remaining compatible with previous client versions.
20
20
21
21
Warnings are surfaced by ` kubectl ` v1.19+ in ` stderr ` output, and by the ` k8s.io/client-go ` client library v0.19.0+ in log output.
22
- The ` k8s.io/client-go ` behavior can be overridden [ per-process] ( https://godoc.org/k8s.io/client-go/rest#SetDefaultWarningHandler )
23
- or [ per-client] ( https://godoc.org/k8s.io/client-go/rest#Config ) .
22
+ The ` k8s.io/client-go ` behavior can be [ overridden per-process or per-client] ( #customize-client-handling ) .
24
23
25
24
## Deprecation Warnings
26
25
@@ -173,7 +172,7 @@ apiserver_requested_deprecated_apis{removed_version="1.22"} * on(group,version,r
173
172
group_right() apiserver_request_total
174
173
```
175
174
176
- ### Audit annotations
175
+ ### Audit Annotations
177
176
178
177
Metrics are a fast way to check whether deprecated APIs are being used, and at what rate,
179
178
but they don't include enough information to identify particular clients or API objects.
@@ -251,7 +250,63 @@ Here are a couple ideas to get you started:
251
250
to allow trying out a policy to verify it is working as expected before starting to enforce it
252
251
* "lint" or "vet"-style webhooks, inspecting objects and surfacing warnings when best practices are not followed
253
252
254
- ## Kubectl strict mode
253
+ ## Customize Client Handling
254
+
255
+ Applications that use the ` k8s.io/client-go ` library to make API requests can customize
256
+ how warnings returned from the server are handled. By default, warnings are logged to
257
+ stderr as they are received, but this behavior can be customized
258
+ [ per-process] ( https://godoc.org/k8s.io/client-go/rest#SetDefaultWarningHandler )
259
+ or [ per-client] ( https://godoc.org/k8s.io/client-go/rest#Config ) .
260
+
261
+ This example shows how to make your application behave like ` kubectl ` ,
262
+ overriding message handling process-wide to deduplicate warnings
263
+ and highlighting messages using colored output where supported:
264
+
265
+ ``` go
266
+ import (
267
+ " os"
268
+ " k8s.io/client-go/rest"
269
+ " k8s.io/kubectl/pkg/util/term"
270
+ ...
271
+ )
272
+
273
+ func main () {
274
+ rest.SetDefaultWarningHandler (
275
+ rest.NewWarningWriter (os.Stderr , rest.WarningWriterOptions {
276
+ // only print a given warning the first time we receive it
277
+ Deduplicate: true ,
278
+ // highlight the output with color when the output supports it
279
+ Color: term.AllowsColorOutput (os.Stderr ),
280
+ },
281
+ ),
282
+ )
283
+
284
+ ...
285
+ ` ` `
286
+
287
+ The next example shows how to construct a client that ignores warnings.
288
+ This is useful for clients that operate on metadata for all resource types
289
+ (found dynamically at runtime using the discovery API)
290
+ and do not benefit from warnings about a particular resource being deprecated.
291
+ Suppressing deprecation warnings is not recommended for clients that require use of particular APIs.
292
+
293
+ ` ` ` go
294
+ import (
295
+ " k8s.io/client-go/rest"
296
+ " k8s.io/client-go/kubernetes"
297
+ )
298
+
299
+ func getClientWithoutWarnings (config *rest.Config ) (kubernetes.Interface , error ) {
300
+ // copy to avoid mutating the passed-in config
301
+ config = rest.CopyConfig (config)
302
+ // set the warning handler for this client to ignore warnings
303
+ config.WarningHandler = rest.NoWarnings {}
304
+ // construct and return the client
305
+ return kubernetes.NewForConfig (config)
306
+ }
307
+ ` ` `
308
+
309
+ ## Kubectl Strict Mode
255
310
256
311
If you want to be sure you notice deprecations as soon as possible and get a jump start on addressing them,
257
312
` kubectl` added a ` --warnings-as-errors` option in v1.19. When invoked with this option,
0 commit comments