2021-01-20 11:35:48 +00:00
|
|
|
package imgworker
|
|
|
|
|
|
|
|
// FROM Slightly adapted from genuinetools/img worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/namespaces"
|
2021-03-11 16:04:26 +00:00
|
|
|
dockertypes "github.com/docker/docker/api/types"
|
2021-01-20 11:35:48 +00:00
|
|
|
"github.com/genuinetools/img/types"
|
|
|
|
"github.com/moby/buildkit/control"
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
"github.com/moby/buildkit/worker/base"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client holds the information for the client we will use for communicating
|
|
|
|
// with the buildkit controller.
|
|
|
|
type Client struct {
|
|
|
|
backend string
|
|
|
|
localDirs map[string]string
|
|
|
|
root string
|
|
|
|
|
|
|
|
sessionManager *session.Manager
|
|
|
|
controller *control.Controller
|
|
|
|
opts *base.WorkerOpt
|
|
|
|
|
|
|
|
sess *session.Session
|
|
|
|
ctx context.Context
|
2021-03-11 16:04:26 +00:00
|
|
|
auth *dockertypes.AuthConfig
|
2021-01-20 11:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new client for communicating with the buildkit controller.
|
2021-03-11 16:04:26 +00:00
|
|
|
func New(root string, auth *dockertypes.AuthConfig) (*Client, error) {
|
2021-01-20 11:35:48 +00:00
|
|
|
// Native backend is fine, our images have just one layer. No need to depend on anything
|
|
|
|
backend := types.NativeBackend
|
|
|
|
|
|
|
|
// Create the root/
|
|
|
|
root = filepath.Join(root, "runc", backend)
|
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c := &Client{
|
|
|
|
backend: types.NativeBackend,
|
|
|
|
root: root,
|
|
|
|
localDirs: nil,
|
2021-03-11 16:04:26 +00:00
|
|
|
auth: auth,
|
2021-01-20 11:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.prepare(); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed preparing client")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the start of the client.
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Close() {
|
|
|
|
c.sess.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) prepare() error {
|
|
|
|
ctx := appcontext.Context()
|
|
|
|
sess, sessDialer, err := c.Session(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed creating Session")
|
|
|
|
}
|
|
|
|
ctx = session.NewContext(ctx, sess.ID())
|
|
|
|
ctx = namespaces.WithNamespace(ctx, "buildkit")
|
|
|
|
|
|
|
|
c.ctx = ctx
|
|
|
|
c.sess = sess
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sess.Run(ctx, sessDialer)
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|