2019-04-17 06:43:15 +08:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2023-06-19 12:10:10 +08:00
|
|
|
"context"
|
2019-04-17 11:10:34 +08:00
|
|
|
"net"
|
2024-03-06 17:01:00 +08:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2019-04-17 11:10:34 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-06-19 12:10:10 +08:00
|
|
|
"github.com/docker/buildx/driver"
|
2021-10-15 22:03:44 +08:00
|
|
|
"github.com/docker/cli/opts"
|
2023-04-03 17:23:59 +08:00
|
|
|
"github.com/docker/docker/builder/remotecontext/urlutil"
|
|
|
|
"github.com/moby/buildkit/util/gitutil"
|
2019-04-17 11:10:34 +08:00
|
|
|
"github.com/pkg/errors"
|
2024-03-06 17:01:00 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-04-17 06:43:15 +08:00
|
|
|
)
|
|
|
|
|
2022-12-02 20:47:19 +08:00
|
|
|
const (
|
|
|
|
// archiveHeaderSize is the number of bytes in an archive header
|
|
|
|
archiveHeaderSize = 512
|
|
|
|
// mobyHostGatewayName defines a special string which users can append to
|
|
|
|
// --add-host to add an extra entry in /etc/hosts that maps
|
|
|
|
// host.docker.internal to the host IP
|
|
|
|
mobyHostGatewayName = "host-gateway"
|
|
|
|
)
|
2019-04-17 06:43:15 +08:00
|
|
|
|
2023-04-03 17:23:59 +08:00
|
|
|
func IsRemoteURL(c string) bool {
|
|
|
|
if urlutil.IsURL(c) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if _, err := gitutil.ParseGitRef(c); err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-17 06:43:15 +08:00
|
|
|
func isArchive(header []byte) bool {
|
|
|
|
for _, m := range [][]byte{
|
|
|
|
{0x42, 0x5A, 0x68}, // bzip2
|
|
|
|
{0x1F, 0x8B, 0x08}, // gzip
|
|
|
|
{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}, // xz
|
|
|
|
} {
|
|
|
|
if len(header) < len(m) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if bytes.Equal(m, header[:len(m)]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r := tar.NewReader(bytes.NewBuffer(header))
|
|
|
|
_, err := r.Next()
|
|
|
|
return err == nil
|
|
|
|
}
|
2019-04-17 11:10:34 +08:00
|
|
|
|
|
|
|
// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format
|
2023-06-19 12:10:10 +08:00
|
|
|
func toBuildkitExtraHosts(ctx context.Context, inp []string, nodeDriver *driver.DriverHandle) (string, error) {
|
2019-04-17 11:10:34 +08:00
|
|
|
if len(inp) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
hosts := make([]string, 0, len(inp))
|
|
|
|
for _, h := range inp {
|
2023-11-16 17:28:42 +08:00
|
|
|
host, ip, ok := strings.Cut(h, "=")
|
|
|
|
if !ok {
|
|
|
|
host, ip, ok = strings.Cut(h, ":")
|
|
|
|
}
|
2022-12-02 20:47:19 +08:00
|
|
|
if !ok || host == "" || ip == "" {
|
2019-04-17 11:10:34 +08:00
|
|
|
return "", errors.Errorf("invalid host %s", h)
|
|
|
|
}
|
2023-06-19 12:10:10 +08:00
|
|
|
// If the IP Address is a "host-gateway", replace this value with the
|
|
|
|
// IP address provided by the worker's label.
|
|
|
|
if ip == mobyHostGatewayName {
|
|
|
|
hgip, err := nodeDriver.HostGatewayIP(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "unable to derive the IP value for host-gateway")
|
2022-12-02 20:47:19 +08:00
|
|
|
}
|
2023-06-19 12:10:10 +08:00
|
|
|
ip = hgip.String()
|
2023-11-16 17:28:42 +08:00
|
|
|
} else {
|
|
|
|
// If the address is enclosed in square brackets, extract it (for IPv6, but
|
|
|
|
// permit it for IPv4 as well; we don't know the address family here, but it's
|
|
|
|
// unambiguous).
|
|
|
|
if len(ip) > 2 && ip[0] == '[' && ip[len(ip)-1] == ']' {
|
|
|
|
ip = ip[1 : len(ip)-1]
|
|
|
|
}
|
|
|
|
if net.ParseIP(ip) == nil {
|
|
|
|
return "", errors.Errorf("invalid host %s", h)
|
|
|
|
}
|
2022-12-02 20:47:19 +08:00
|
|
|
}
|
|
|
|
hosts = append(hosts, host+"="+ip)
|
2019-04-17 11:10:34 +08:00
|
|
|
}
|
|
|
|
return strings.Join(hosts, ","), nil
|
|
|
|
}
|
2021-10-15 22:03:44 +08:00
|
|
|
|
|
|
|
// toBuildkitUlimits converts ulimits from docker type=soft:hard format to buildkit's csv format
|
|
|
|
func toBuildkitUlimits(inp *opts.UlimitOpt) (string, error) {
|
|
|
|
if inp == nil || len(inp.GetList()) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
ulimits := make([]string, 0, len(inp.GetList()))
|
|
|
|
for _, ulimit := range inp.GetList() {
|
|
|
|
ulimits = append(ulimits, ulimit.String())
|
|
|
|
}
|
|
|
|
return strings.Join(ulimits, ","), nil
|
|
|
|
}
|
2024-03-06 17:01:00 +08:00
|
|
|
|
2024-03-28 04:13:43 +08:00
|
|
|
func notSupported(f driver.Feature, d *driver.DriverHandle, docs string) error {
|
2024-03-06 17:01:00 +08:00
|
|
|
return errors.Errorf(`%s is not supported for the %s driver.
|
|
|
|
Switch to a different driver, or turn on the containerd image store, and try again.
|
|
|
|
Learn more at %s`, f, d.Factory().Name(), docs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func noDefaultLoad() bool {
|
|
|
|
v, ok := os.LookupEnv("BUILDX_NO_DEFAULT_LOAD")
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
b, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("invalid non-bool value for BUILDX_NO_DEFAULT_LOAD: %s", v)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|