@@ -762,7 +762,7 @@ func (r *Request) Watch(ctx context.Context) (watch.Interface, error) {
762
762
763
763
func (r * Request ) watchInternal (ctx context.Context ) (watch.Interface , runtime.Decoder , error ) {
764
764
if r .body == nil {
765
- logBody (ctx , 2 , "Request Body" , r .bodyBytes )
765
+ logBody (klog . FromContext ( ctx ) , 2 , "Request Body" , r .bodyBytes )
766
766
}
767
767
768
768
// We specifically don't want to rate limit watches, so we
@@ -921,7 +921,7 @@ func (r WatchListResult) Into(obj runtime.Object) error {
921
921
// to see what parameters are currently required.
922
922
func (r * Request ) WatchList (ctx context.Context ) WatchListResult {
923
923
if r .body == nil {
924
- logBody (ctx , 2 , "Request Body" , r .bodyBytes )
924
+ logBody (klog . FromContext ( ctx ) , 2 , "Request Body" , r .bodyBytes )
925
925
}
926
926
927
927
if ! clientfeatures .FeatureGates ().Enabled (clientfeatures .WatchListClient ) {
@@ -1054,7 +1054,7 @@ func sanitize(req *Request, resp *http.Response, err error) (string, string) {
1054
1054
// If we can, we return that as an error. Otherwise, we create an error that lists the http status and the content of the response.
1055
1055
func (r * Request ) Stream (ctx context.Context ) (io.ReadCloser , error ) {
1056
1056
if r .body == nil {
1057
- logBody (ctx , 2 , "Request Body" , r .bodyBytes )
1057
+ logBody (klog . FromContext ( ctx ) , 2 , "Request Body" , r .bodyBytes )
1058
1058
}
1059
1059
1060
1060
if r .err != nil {
@@ -1290,16 +1290,17 @@ func (r *Request) request(ctx context.Context, fn func(*http.Request, *http.Resp
1290
1290
// - If the server responds with a status: *errors.StatusError or *errors.UnexpectedObjectError
1291
1291
// - http.Client.Do errors are returned directly.
1292
1292
func (r * Request ) Do (ctx context.Context ) Result {
1293
+ logger := klog .FromContext (ctx )
1293
1294
if r .body == nil {
1294
- logBody (ctx , 2 , "Request Body" , r .bodyBytes )
1295
+ logBody (logger , 2 , "Request Body" , r .bodyBytes )
1295
1296
}
1296
1297
1297
1298
var result Result
1298
1299
err := r .request (ctx , func (req * http.Request , resp * http.Response ) {
1299
1300
result = r .transformResponse (ctx , resp , req )
1300
1301
})
1301
1302
if err != nil {
1302
- return Result {err : err , loggingCtx : context . WithoutCancel ( ctx ) }
1303
+ return Result {err : err , logger : logger }
1303
1304
}
1304
1305
if result .err == nil || len (result .body ) > 0 {
1305
1306
metrics .ResponseSize .Observe (ctx , r .verb , r .URL ().Host , float64 (len (result .body )))
@@ -1309,14 +1310,15 @@ func (r *Request) Do(ctx context.Context) Result {
1309
1310
1310
1311
// DoRaw executes the request but does not process the response body.
1311
1312
func (r * Request ) DoRaw (ctx context.Context ) ([]byte , error ) {
1313
+ logger := klog .FromContext (ctx )
1312
1314
if r .body == nil {
1313
- logBody (ctx , 2 , "Request Body" , r .bodyBytes )
1315
+ logBody (logger , 2 , "Request Body" , r .bodyBytes )
1314
1316
}
1315
1317
1316
1318
var result Result
1317
1319
err := r .request (ctx , func (req * http.Request , resp * http.Response ) {
1318
1320
result .body , result .err = io .ReadAll (resp .Body )
1319
- logBody (ctx , 2 , "Response Body" , result .body )
1321
+ logBody (logger , 2 , "Response Body" , result .body )
1320
1322
if resp .StatusCode < http .StatusOK || resp .StatusCode > http .StatusPartialContent {
1321
1323
result .err = r .transformUnstructuredResponseError (resp , req , result .body )
1322
1324
}
@@ -1332,6 +1334,7 @@ func (r *Request) DoRaw(ctx context.Context) ([]byte, error) {
1332
1334
1333
1335
// transformResponse converts an API response into a structured API object
1334
1336
func (r * Request ) transformResponse (ctx context.Context , resp * http.Response , req * http.Request ) Result {
1337
+ logger := klog .FromContext (ctx )
1335
1338
var body []byte
1336
1339
if resp .Body != nil {
1337
1340
data , err := io .ReadAll (resp .Body )
@@ -1346,24 +1349,24 @@ func (r *Request) transformResponse(ctx context.Context, resp *http.Response, re
1346
1349
// 2. Apiserver sends back the headers and then part of the body
1347
1350
// 3. Apiserver closes connection.
1348
1351
// 4. client-go should catch this and return an error.
1349
- klog . FromContext ( ctx ) .V (2 ).Info ("Stream error when reading response body, may be caused by closed connection" , "err" , err )
1352
+ logger .V (2 ).Info ("Stream error when reading response body, may be caused by closed connection" , "err" , err )
1350
1353
streamErr := fmt .Errorf ("stream error when reading response body, may be caused by closed connection. Please retry. Original error: %w" , err )
1351
1354
return Result {
1352
- err : streamErr ,
1353
- loggingCtx : context . WithoutCancel ( ctx ) ,
1355
+ err : streamErr ,
1356
+ logger : logger ,
1354
1357
}
1355
1358
default :
1356
- klog . FromContext ( ctx ) .Error (err , "Unexpected error when reading response body" )
1359
+ logger .Error (err , "Unexpected error when reading response body" )
1357
1360
unexpectedErr := fmt .Errorf ("unexpected error when reading response body. Please retry. Original error: %w" , err )
1358
1361
return Result {
1359
- err : unexpectedErr ,
1360
- loggingCtx : context . WithoutCancel ( ctx ) ,
1362
+ err : unexpectedErr ,
1363
+ logger : logger ,
1361
1364
}
1362
1365
}
1363
1366
}
1364
1367
1365
1368
// Call depth is tricky. This one is okay for Do and DoRaw.
1366
- logBody (ctx , 7 , "Response Body" , body )
1369
+ logBody (logger , 7 , "Response Body" , body )
1367
1370
1368
1371
// verify the content type is accurate
1369
1372
var decoder runtime.Decoder
@@ -1375,7 +1378,7 @@ func (r *Request) transformResponse(ctx context.Context, resp *http.Response, re
1375
1378
var err error
1376
1379
mediaType , params , err := mime .ParseMediaType (contentType )
1377
1380
if err != nil {
1378
- return Result {err : errors .NewInternalError (err ), loggingCtx : context . WithoutCancel ( ctx ) }
1381
+ return Result {err : errors .NewInternalError (err ), logger : logger }
1379
1382
}
1380
1383
decoder , err = r .contentConfig .Negotiator .Decoder (mediaType , params )
1381
1384
if err != nil {
@@ -1384,14 +1387,14 @@ func (r *Request) transformResponse(ctx context.Context, resp *http.Response, re
1384
1387
case resp .StatusCode == http .StatusSwitchingProtocols :
1385
1388
// no-op, we've been upgraded
1386
1389
case resp .StatusCode < http .StatusOK || resp .StatusCode > http .StatusPartialContent :
1387
- return Result {err : r .transformUnstructuredResponseError (resp , req , body ), loggingCtx : context . WithoutCancel ( ctx ) }
1390
+ return Result {err : r .transformUnstructuredResponseError (resp , req , body ), logger : logger }
1388
1391
}
1389
1392
return Result {
1390
1393
body : body ,
1391
1394
contentType : contentType ,
1392
1395
statusCode : resp .StatusCode ,
1393
1396
warnings : handleWarnings (ctx , resp .Header , r .warningHandler ),
1394
- loggingCtx : context . WithoutCancel ( ctx ) ,
1397
+ logger : logger ,
1395
1398
}
1396
1399
}
1397
1400
}
@@ -1411,7 +1414,7 @@ func (r *Request) transformResponse(ctx context.Context, resp *http.Response, re
1411
1414
decoder : decoder ,
1412
1415
err : err ,
1413
1416
warnings : handleWarnings (ctx , resp .Header , r .warningHandler ),
1414
- loggingCtx : context . WithoutCancel ( ctx ) ,
1417
+ logger : logger ,
1415
1418
}
1416
1419
}
1417
1420
@@ -1421,7 +1424,7 @@ func (r *Request) transformResponse(ctx context.Context, resp *http.Response, re
1421
1424
statusCode : resp .StatusCode ,
1422
1425
decoder : decoder ,
1423
1426
warnings : handleWarnings (ctx , resp .Header , r .warningHandler ),
1424
- loggingCtx : context . WithoutCancel ( ctx ) ,
1427
+ logger : logger ,
1425
1428
}
1426
1429
}
1427
1430
@@ -1449,8 +1452,7 @@ func truncateBody(logger klog.Logger, body string) string {
1449
1452
// whether the body is printable.
1450
1453
//
1451
1454
// It needs to be called by all functions which send or receive the data.
1452
- func logBody (ctx context.Context , callDepth int , prefix string , body []byte ) {
1453
- logger := klog .FromContext (ctx )
1455
+ func logBody (logger klog.Logger , callDepth int , prefix string , body []byte ) {
1454
1456
if loggerV := logger .V (8 ); loggerV .Enabled () {
1455
1457
loggerV := loggerV .WithCallDepth (callDepth )
1456
1458
if bytes .IndexFunc (body , func (r rune ) bool {
@@ -1552,10 +1554,7 @@ type Result struct {
1552
1554
contentType string
1553
1555
err error
1554
1556
statusCode int
1555
-
1556
- // Log calls in Result methods use the same context for logging as the
1557
- // method which created the Result. This context has no cancellation.
1558
- loggingCtx context.Context
1557
+ logger klog.Logger
1559
1558
1560
1559
decoder runtime.Decoder
1561
1560
}
@@ -1661,11 +1660,7 @@ func (r Result) Error() error {
1661
1660
// to be backwards compatible with old servers that do not return a version, default to "v1"
1662
1661
out , _ , err := r .decoder .Decode (r .body , & schema.GroupVersionKind {Version : "v1" }, nil )
1663
1662
if err != nil {
1664
- ctx := r .loggingCtx
1665
- if ctx == nil {
1666
- ctx = context .Background ()
1667
- }
1668
- klog .FromContext (ctx ).V (5 ).Info ("Body was not decodable (unable to check for Status)" , "err" , err )
1663
+ r .logger .V (5 ).Info ("Body was not decodable (unable to check for Status)" , "err" , err )
1669
1664
return r .err
1670
1665
}
1671
1666
switch t := out .(type ) {
0 commit comments