@@ -45,6 +45,7 @@ package managed
45
45
46
46
import (
47
47
"bytes"
48
+ "context"
48
49
"crypto/tls"
49
50
"crypto/x509"
50
51
"errors"
@@ -55,9 +56,12 @@ import (
55
56
"strings"
56
57
"sync"
57
58
59
+ "github.com/fluxcd/pkg/runtime/logger"
58
60
pool "github.com/fluxcd/source-controller/internal/transport"
59
61
"github.com/fluxcd/source-controller/pkg/git"
62
+ "github.com/go-logr/logr"
60
63
git2go "github.com/libgit2/git2go/v33"
64
+ ctrl "sigs.k8s.io/controller-runtime"
61
65
)
62
66
63
67
var actionSuffixes = []string {
@@ -81,10 +85,11 @@ func registerManagedHTTP() error {
81
85
}
82
86
83
87
func httpSmartSubtransportFactory (remote * git2go.Remote , transport * git2go.Transport ) (git2go.SmartSubtransport , error ) {
84
- traceLog .Info ("[http]: httpSmartSubtransportFactory" )
85
88
sst := & httpSmartSubtransport {
86
89
transport : transport ,
87
90
httpTransport : pool .NewOrIdle (nil ),
91
+ ctx : context .Background (),
92
+ logger : logr .Discard (),
88
93
}
89
94
90
95
return sst , nil
@@ -93,6 +98,21 @@ func httpSmartSubtransportFactory(remote *git2go.Remote, transport *git2go.Trans
93
98
type httpSmartSubtransport struct {
94
99
transport * git2go.Transport
95
100
httpTransport * http.Transport
101
+
102
+ // once is used to ensure that logger and ctx is set only once,
103
+ // on the initial (or only) Action call. Without this a mutex must
104
+ // be applied to ensure that ctx won't be changed, as this would be
105
+ // prone to race conditions in the stdout processing goroutine.
106
+ once sync.Once
107
+ // ctx defines the context to be used across long-running or
108
+ // cancellable operations.
109
+ // Defaults to context.Background().
110
+ ctx context.Context
111
+ // logger keeps a Logger instance for logging. This was preferred
112
+ // due to the need to have a correlation ID and URL set and
113
+ // reused across all log calls.
114
+ // If context is not set, this defaults to logr.Discard().
115
+ logger logr.Logger
96
116
}
97
117
98
118
func (t * httpSmartSubtransport ) Action (transportOptionsURL string , action git2go.SmartServiceAction ) (git2go.SmartSubtransportStream , error ) {
@@ -133,6 +153,15 @@ func (t *httpSmartSubtransport) Action(transportOptionsURL string, action git2go
133
153
}
134
154
t .httpTransport .DisableCompression = false
135
155
156
+ t .once .Do (func () {
157
+ if opts .Context != nil {
158
+ t .ctx = opts .Context
159
+ t .logger = ctrl .LoggerFrom (t .ctx ,
160
+ "transportType" , "http" ,
161
+ "url" , opts .TargetURL )
162
+ }
163
+ })
164
+
136
165
client , req , err := createClientRequest (targetURL , action , t .httpTransport , opts .AuthOpts )
137
166
if err != nil {
138
167
return nil , err
@@ -176,8 +205,10 @@ func (t *httpSmartSubtransport) Action(transportOptionsURL string, action git2go
176
205
opts .TargetURL = trimActionSuffix (newURL .String ())
177
206
AddTransportOptions (transportOptionsURL , * opts )
178
207
179
- debugLog .Info ("[http]: server responded with redirect" ,
180
- "newURL" , opts .TargetURL , "StatusCode" , req .Response .StatusCode )
208
+ // show as info, as this should be visible regardless of the
209
+ // chosen log-level.
210
+ t .logger .Info ("server responded with redirect" ,
211
+ "newUrl" , opts .TargetURL , "StatusCode" , req .Response .StatusCode )
181
212
}
182
213
}
183
214
}
@@ -270,15 +301,16 @@ func createClientRequest(targetURL string, action git2go.SmartServiceAction,
270
301
}
271
302
272
303
func (t * httpSmartSubtransport ) Close () error {
273
- traceLog . Info ("[http]: httpSmartSubtransport.Close()" )
304
+ t . logger . V ( logger . TraceLevel ). Info ("httpSmartSubtransport.Close()" )
274
305
return nil
275
306
}
276
307
277
308
func (t * httpSmartSubtransport ) Free () {
278
- traceLog . Info ("[http]: httpSmartSubtransport.Free()" )
309
+ t . logger . V ( logger . TraceLevel ). Info ("httpSmartSubtransport.Free()" )
279
310
280
311
if t .httpTransport != nil {
281
- traceLog .Info ("[http]: release http transport back to pool" )
312
+ t .logger .V (logger .TraceLevel ).Info ("release http transport back to pool" )
313
+
282
314
pool .Release (t .httpTransport )
283
315
t .httpTransport = nil
284
316
}
@@ -345,18 +377,18 @@ func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) {
345
377
346
378
func (self * httpSmartSubtransportStream ) Free () {
347
379
if self .resp != nil {
348
- traceLog . Info ("[http]: httpSmartSubtransportStream.Free()" )
380
+ self . owner . logger . V ( logger . TraceLevel ). Info ("httpSmartSubtransportStream.Free()" )
349
381
350
382
if self .resp .Body != nil {
351
383
// ensure body is fully processed and closed
352
384
// for increased likelihood of transport reuse in HTTP/1.x.
353
385
// it should not be a problem to do this more than once.
354
386
if _ , err := io .Copy (io .Discard , self .resp .Body ); err != nil {
355
- traceLog . Error (err , "[http]: cannot discard response body" )
387
+ self . owner . logger . V ( logger . TraceLevel ). Error (err , "cannot discard response body" )
356
388
}
357
389
358
390
if err := self .resp .Body .Close (); err != nil {
359
- traceLog . Error (err , "[http]: cannot close response body" )
391
+ self . owner . logger . V ( logger . TraceLevel ). Error (err , "cannot close response body" )
360
392
}
361
393
}
362
394
}
@@ -399,7 +431,7 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
399
431
req .ContentLength = - 1
400
432
}
401
433
402
- traceLog . Info ("[http]: new request" , "method" , req .Method , "URL " , req .URL )
434
+ self . owner . logger . V ( logger . TraceLevel ). Info ("new request" , "method" , req .Method , "postUrl " , req .URL )
403
435
resp , err = self .client .Do (req )
404
436
if err != nil {
405
437
return err
0 commit comments