Skip to content

Commit 017707a

Browse files
author
Paulo Gomes
committed
Improve managed transport observability
Signed-off-by: Paulo Gomes <[email protected]>
1 parent b73f18a commit 017707a

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func main() {
228228
}()
229229

230230
if managed.Enabled() {
231-
managed.InitManagedTransport()
231+
managed.InitManagedTransport(ctrl.Log.WithName("managed-transport"))
232232
}
233233

234234
setupLog.Info("starting manager")

pkg/git/libgit2/managed/http.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import (
5454
"net/url"
5555
"sync"
5656

57-
"github.com/fluxcd/source-controller/internal/transport"
57+
pool "github.com/fluxcd/source-controller/internal/transport"
5858
git2go "github.com/libgit2/git2go/v33"
5959
)
6060

@@ -72,8 +72,10 @@ func registerManagedHTTP() error {
7272
}
7373

7474
func httpSmartSubtransportFactory(remote *git2go.Remote, transport *git2go.Transport) (git2go.SmartSubtransport, error) {
75+
traceLog.Info("[http]: httpSmartSubtransportFactory")
7576
sst := &httpSmartSubtransport{
76-
transport: transport,
77+
transport: transport,
78+
httpTransport: pool.NewOrIdle(nil),
7779
}
7880

7981
return sst, nil
@@ -104,9 +106,8 @@ func (t *httpSmartSubtransport) Action(targetUrl string, action git2go.SmartServ
104106
proxyFn = http.ProxyURL(parsedUrl)
105107
}
106108

107-
// reuses the http transport from a pool, or create new one on demand.
108-
t.httpTransport = transport.NewOrIdle(nil)
109109
t.httpTransport.Proxy = proxyFn
110+
t.httpTransport.DisableCompression = false
110111

111112
client, req, err := createClientRequest(targetUrl, action, t.httpTransport)
112113
if err != nil {
@@ -209,10 +210,18 @@ func createClientRequest(targetUrl string, action git2go.SmartServiceAction, t *
209210
}
210211

211212
func (t *httpSmartSubtransport) Close() error {
213+
traceLog.Info("[http]: httpSmartSubtransport.Close()")
212214
return nil
213215
}
214216

215217
func (t *httpSmartSubtransport) Free() {
218+
traceLog.Info("[http]: httpSmartSubtransport.Free()")
219+
220+
if t.httpTransport != nil {
221+
traceLog.Info("[http]: release http transport back to pool")
222+
pool.Release(t.httpTransport)
223+
t.httpTransport = nil
224+
}
216225
}
217226

218227
type httpSmartSubtransportStream struct {
@@ -277,6 +286,8 @@ func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) {
277286

278287
func (self *httpSmartSubtransportStream) Free() {
279288
if self.resp != nil {
289+
traceLog.Info("[http]: httpSmartSubtransportStream.Free()")
290+
280291
// ensure body is fully processed and closed
281292
// for increased likelihood of transport reuse in HTTP/1.x.
282293
// it should not be a problem to do this more than once.
@@ -344,6 +355,7 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
344355
}
345356

346357
req.SetBasicAuth(userName, password)
358+
traceLog.Info("[http]: new request", "method", req.Method, "URL", req.URL)
347359
resp, err = self.client.Do(req)
348360
if err != nil {
349361
return err
@@ -363,6 +375,7 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
363375
return err
364376
}
365377

378+
traceLog.Info("[http]: POST redirect", "URL", self.req.URL)
366379
continue
367380
}
368381

@@ -379,11 +392,6 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
379392
return fmt.Errorf("Unhandled HTTP error %s", resp.Status)
380393
}
381394

382-
if self.owner.httpTransport != nil {
383-
transport.Release(self.owner.httpTransport)
384-
self.owner.httpTransport = nil
385-
}
386-
387395
self.resp = resp
388396
self.sentRequest = true
389397
return nil

pkg/git/libgit2/managed/init.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package managed
1919
import (
2020
"sync"
2121
"time"
22+
23+
"github.com/fluxcd/pkg/runtime/logger"
24+
"github.com/go-logr/logr"
2225
)
2326

2427
var (
@@ -34,6 +37,9 @@ var (
3437
// regardless of the current operation (i.e. connection,
3538
// handshake, put/get).
3639
fullHttpClientTimeOut time.Duration = 10 * time.Minute
40+
41+
debugLog logr.Logger
42+
traceLog logr.Logger
3743
)
3844

3945
// InitManagedTransport initialises HTTP(S) and SSH managed transport
@@ -47,9 +53,14 @@ var (
4753
//
4854
// This function will only register managed transports once, subsequent calls
4955
// leads to no-op.
50-
func InitManagedTransport() error {
56+
func InitManagedTransport(log logr.Logger) error {
5157
var err error
58+
5259
once.Do(func() {
60+
log.Info("Enabling experimental managed transport")
61+
debugLog = log.V(logger.DebugLevel)
62+
traceLog = log.V(logger.TraceLevel)
63+
5364
if err = registerManagedHTTP(); err != nil {
5465
return
5566
}

pkg/git/libgit2/managed/managed_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/fluxcd/pkg/gittestserver"
2828
"github.com/fluxcd/pkg/ssh"
2929
"github.com/fluxcd/source-controller/pkg/git"
30+
"github.com/go-logr/logr"
3031

3132
git2go "github.com/libgit2/git2go/v33"
3233
. "github.com/onsi/gomega"
@@ -247,7 +248,7 @@ func TestManagedTransport_E2E(t *testing.T) {
247248
defer server.StopSSH()
248249

249250
// Force managed transport to be enabled
250-
InitManagedTransport()
251+
InitManagedTransport(logr.Discard())
251252

252253
repoPath := "test.git"
253254
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, repoPath)
@@ -312,7 +313,7 @@ func TestManagedTransport_HandleRedirect(t *testing.T) {
312313
defer os.RemoveAll(tmpDir)
313314

314315
// Force managed transport to be enabled
315-
InitManagedTransport()
316+
InitManagedTransport(logr.Discard())
316317

317318
// GitHub will cause a 301 and redirect to https
318319
repo, err := git2go.Clone("http://github.com/stefanprodan/podinfo", tmpDir, &git2go.CloneOptions{

pkg/git/libgit2/managed/ssh.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
186186
return nil, err
187187
}
188188

189-
t.session, err = t.client.NewSession()
190-
if err != nil {
189+
traceLog.Info("[ssh]: creating new ssh session")
191190
return nil, err
192191
}
193192

@@ -201,6 +200,7 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
201200
return nil, err
202201
}
203202

203+
traceLog.Info("[ssh]: run on remote", "cmd", cmd)
204204
if err := t.session.Start(cmd); err != nil {
205205
return nil, err
206206
}
@@ -214,6 +214,7 @@ func (t *sshSmartSubtransport) Action(urlString string, action git2go.SmartServi
214214
}
215215

216216
func (t *sshSmartSubtransport) Close() error {
217+
traceLog.Info("[ssh]: sshSmartSubtransport.Close()")
217218
t.currentStream = nil
218219
if t.client != nil {
219220
t.stdin.Close()
@@ -225,6 +226,7 @@ func (t *sshSmartSubtransport) Close() error {
225226
}
226227

227228
func (t *sshSmartSubtransport) Free() {
229+
traceLog.Info("[ssh]: sshSmartSubtransport.Free()")
228230
}
229231

230232
type sshSmartSubtransportStream struct {
@@ -240,6 +242,7 @@ func (stream *sshSmartSubtransportStream) Write(buf []byte) (int, error) {
240242
}
241243

242244
func (stream *sshSmartSubtransportStream) Free() {
245+
traceLog.Info("[ssh]: sshSmartSubtransportStream.Free()")
243246
}
244247

245248
func getSSHConfigFromCredential(cred *git2go.Credential) (*ssh.ClientConfig, error) {

0 commit comments

Comments
 (0)