@@ -45,6 +45,7 @@ package managed
4545
4646import (
4747 "bytes"
48+ "context"
4849 "crypto/tls"
4950 "crypto/x509"
5051 "errors"
@@ -55,9 +56,12 @@ import (
5556 "strings"
5657 "sync"
5758
59+ "github.com/fluxcd/pkg/runtime/logger"
5860 pool "github.com/fluxcd/source-controller/internal/transport"
5961 "github.com/fluxcd/source-controller/pkg/git"
62+ "github.com/go-logr/logr"
6063 git2go "github.com/libgit2/git2go/v33"
64+ ctrl "sigs.k8s.io/controller-runtime"
6165)
6266
6367var actionSuffixes = []string {
@@ -81,10 +85,11 @@ func registerManagedHTTP() error {
8185}
8286
8387func httpSmartSubtransportFactory (remote * git2go.Remote , transport * git2go.Transport ) (git2go.SmartSubtransport , error ) {
84- traceLog .Info ("[http]: httpSmartSubtransportFactory" )
8588 sst := & httpSmartSubtransport {
8689 transport : transport ,
8790 httpTransport : pool .NewOrIdle (nil ),
91+ ctx : context .Background (),
92+ logger : logr .Discard (),
8893 }
8994
9095 return sst , nil
@@ -93,6 +98,21 @@ func httpSmartSubtransportFactory(remote *git2go.Remote, transport *git2go.Trans
9398type httpSmartSubtransport struct {
9499 transport * git2go.Transport
95100 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
96116}
97117
98118func (t * httpSmartSubtransport ) Action (transportOptionsURL string , action git2go.SmartServiceAction ) (git2go.SmartSubtransportStream , error ) {
@@ -133,6 +153,15 @@ func (t *httpSmartSubtransport) Action(transportOptionsURL string, action git2go
133153 }
134154 t .httpTransport .DisableCompression = false
135155
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+
136165 client , req , err := createClientRequest (targetURL , action , t .httpTransport , opts .AuthOpts )
137166 if err != nil {
138167 return nil , err
@@ -176,8 +205,10 @@ func (t *httpSmartSubtransport) Action(transportOptionsURL string, action git2go
176205 opts .TargetURL = trimActionSuffix (newURL .String ())
177206 AddTransportOptions (transportOptionsURL , * opts )
178207
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 )
181212 }
182213 }
183214 }
@@ -270,15 +301,16 @@ func createClientRequest(targetURL string, action git2go.SmartServiceAction,
270301}
271302
272303func (t * httpSmartSubtransport ) Close () error {
273- traceLog . Info ("[http]: httpSmartSubtransport.Close()" )
304+ t . logger . V ( logger . TraceLevel ). Info ("httpSmartSubtransport.Close()" )
274305 return nil
275306}
276307
277308func (t * httpSmartSubtransport ) Free () {
278- traceLog . Info ("[http]: httpSmartSubtransport.Free()" )
309+ t . logger . V ( logger . TraceLevel ). Info ("httpSmartSubtransport.Free()" )
279310
280311 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+
282314 pool .Release (t .httpTransport )
283315 t .httpTransport = nil
284316 }
@@ -345,18 +377,18 @@ func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) {
345377
346378func (self * httpSmartSubtransportStream ) Free () {
347379 if self .resp != nil {
348- traceLog . Info ("[http]: httpSmartSubtransportStream.Free()" )
380+ self . owner . logger . V ( logger . TraceLevel ). Info ("httpSmartSubtransportStream.Free()" )
349381
350382 if self .resp .Body != nil {
351383 // ensure body is fully processed and closed
352384 // for increased likelihood of transport reuse in HTTP/1.x.
353385 // it should not be a problem to do this more than once.
354386 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" )
356388 }
357389
358390 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" )
360392 }
361393 }
362394 }
@@ -399,7 +431,7 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
399431 req .ContentLength = - 1
400432 }
401433
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 )
403435 resp , err = self .client .Do (req )
404436 if err != nil {
405437 return err
0 commit comments