This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
58 lines (47 loc) · 1.53 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package bkimage
import (
"context"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/filesync"
"github.com/moby/buildkit/session/testutil"
"github.com/pkg/errors"
)
const sessionName = "forge"
// Session creates a new session and its dialer.
//
// The localDirs should reference the "context" and "dockerfile" directories for a particular build.
func (c *Client) Session(ctx context.Context, localDirs map[string]string) (*session.Session, session.Dialer, error) {
// fetch the session manager
sm, err := c.getSessionManager()
if err != nil {
return nil, nil, err
}
// create and configure a new session
sess, err := session.NewSession(ctx, sessionName, "")
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create session")
}
var syncedDirs []filesync.SyncedDir
for name, dir := range localDirs {
syncedDirs = append(syncedDirs, filesync.SyncedDir{
Name: name,
Dir: dir,
})
}
sess.Allow(filesync.NewFSSyncProvider(syncedDirs))
sess.Allow(NewDynamicAuthProvider(c.getHostCredentials()))
// create a session dialer
dialer := session.Dialer(testutil.TestStream(testutil.Handler(sm.HandleConn)))
return sess, dialer, nil
}
// Creates an instance (singleton) of the buildkit session manager and adds it to the client.
func (c *Client) getSessionManager() (*session.Manager, error) {
if c.sessionManager == nil {
var err error
c.sessionManager, err = session.NewManager()
if err != nil {
return nil, errors.Wrap(err, "cannot create session manager")
}
}
return c.sessionManager, nil
}