This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import buildx code - unmodified from commit e5217f2
- Loading branch information
Daniel Hiltgen
committed
Oct 31, 2020
1 parent
a3af3c9
commit de35dc3
Showing
39 changed files
with
4,988 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package build | ||
|
||
import ( | ||
"encoding/csv" | ||
"strings" | ||
|
||
"github.com/moby/buildkit/client" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func ParseCacheEntry(in []string) ([]client.CacheOptionsEntry, error) { | ||
imports := make([]client.CacheOptionsEntry, 0, len(in)) | ||
for _, in := range in { | ||
csvReader := csv.NewReader(strings.NewReader(in)) | ||
fields, err := csvReader.Read() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if isRefOnlyFormat(fields) { | ||
for _, field := range fields { | ||
imports = append(imports, client.CacheOptionsEntry{ | ||
Type: "registry", | ||
Attrs: map[string]string{"ref": field}, | ||
}) | ||
} | ||
continue | ||
} | ||
im := client.CacheOptionsEntry{ | ||
Attrs: map[string]string{}, | ||
} | ||
for _, field := range fields { | ||
parts := strings.SplitN(field, "=", 2) | ||
if len(parts) != 2 { | ||
return nil, errors.Errorf("invalid value %s", field) | ||
} | ||
key := strings.ToLower(parts[0]) | ||
value := parts[1] | ||
switch key { | ||
case "type": | ||
im.Type = value | ||
default: | ||
im.Attrs[key] = value | ||
} | ||
} | ||
if im.Type == "" { | ||
return nil, errors.Errorf("type required form> %q", in) | ||
} | ||
imports = append(imports, im) | ||
} | ||
return imports, nil | ||
} | ||
|
||
func isRefOnlyFormat(in []string) bool { | ||
for _, v := range in { | ||
if strings.Contains(v, "=") { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package build | ||
|
||
import ( | ||
"github.com/moby/buildkit/util/entitlements" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func ParseEntitlements(in []string) ([]entitlements.Entitlement, error) { | ||
out := make([]entitlements.Entitlement, 0, len(in)) | ||
for _, v := range in { | ||
switch v { | ||
case "security.insecure": | ||
out = append(out, entitlements.EntitlementSecurityInsecure) | ||
case "network.host": | ||
out = append(out, entitlements.EntitlementNetworkHost) | ||
default: | ||
return nil, errors.Errorf("invalid entitlement: %v", v) | ||
} | ||
} | ||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package build | ||
|
||
import ( | ||
"encoding/csv" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/containerd/console" | ||
"github.com/moby/buildkit/client" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func ParseOutputs(inp []string) ([]client.ExportEntry, error) { | ||
var outs []client.ExportEntry | ||
if len(inp) == 0 { | ||
return nil, nil | ||
} | ||
for _, s := range inp { | ||
csvReader := csv.NewReader(strings.NewReader(s)) | ||
fields, err := csvReader.Read() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out := client.ExportEntry{ | ||
Attrs: map[string]string{}, | ||
} | ||
if len(fields) == 1 && fields[0] == s && !strings.HasPrefix(s, "type=") { | ||
if s != "-" { | ||
outs = append(outs, client.ExportEntry{ | ||
Type: client.ExporterLocal, | ||
OutputDir: s, | ||
}) | ||
continue | ||
} | ||
out = client.ExportEntry{ | ||
Type: client.ExporterTar, | ||
Attrs: map[string]string{ | ||
"dest": s, | ||
}, | ||
} | ||
} | ||
|
||
if out.Type == "" { | ||
for _, field := range fields { | ||
parts := strings.SplitN(field, "=", 2) | ||
if len(parts) != 2 { | ||
return nil, errors.Errorf("invalid value %s", field) | ||
} | ||
key := strings.TrimSpace(strings.ToLower(parts[0])) | ||
value := parts[1] | ||
switch key { | ||
case "type": | ||
out.Type = value | ||
default: | ||
out.Attrs[key] = value | ||
} | ||
} | ||
} | ||
if out.Type == "" { | ||
return nil, errors.Errorf("type is required for output") | ||
} | ||
|
||
// handle client side | ||
switch out.Type { | ||
case client.ExporterLocal: | ||
dest, ok := out.Attrs["dest"] | ||
if !ok { | ||
return nil, errors.Errorf("dest is required for local output") | ||
} | ||
out.OutputDir = dest | ||
delete(out.Attrs, "dest") | ||
case client.ExporterOCI, client.ExporterDocker, client.ExporterTar: | ||
dest, ok := out.Attrs["dest"] | ||
if !ok { | ||
if out.Type != client.ExporterDocker { | ||
dest = "-" | ||
} | ||
} | ||
if dest == "-" { | ||
if _, err := console.ConsoleFromFile(os.Stdout); err == nil { | ||
return nil, errors.Errorf("output file is required for %s exporter. refusing to write to console", out.Type) | ||
} | ||
out.Output = wrapWriteCloser(os.Stdout) | ||
} else if dest != "" { | ||
fi, err := os.Stat(dest) | ||
if err != nil && !os.IsNotExist(err) { | ||
return nil, errors.Wrapf(err, "invalid destination file: %s", dest) | ||
} | ||
if err == nil && fi.IsDir() { | ||
return nil, errors.Errorf("destination file %s is a directory", dest) | ||
} | ||
f, err := os.Create(dest) | ||
if err != nil { | ||
return nil, errors.Errorf("failed to open %s", err) | ||
} | ||
out.Output = wrapWriteCloser(f) | ||
} | ||
delete(out.Attrs, "dest") | ||
case "registry": | ||
out.Type = client.ExporterImage | ||
out.Attrs["push"] = "true" | ||
} | ||
|
||
outs = append(outs, out) | ||
} | ||
return outs, nil | ||
} | ||
|
||
func wrapWriteCloser(wc io.WriteCloser) func(map[string]string) (io.WriteCloser, error) { | ||
return func(map[string]string) (io.WriteCloser, error) { | ||
return wc, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package build | ||
|
||
import ( | ||
"encoding/csv" | ||
"strings" | ||
|
||
"github.com/moby/buildkit/session" | ||
"github.com/moby/buildkit/session/secrets/secretsprovider" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func ParseSecretSpecs(sl []string) (session.Attachable, error) { | ||
fs := make([]secretsprovider.FileSource, 0, len(sl)) | ||
for _, v := range sl { | ||
s, err := parseSecret(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fs = append(fs, *s) | ||
} | ||
store, err := secretsprovider.NewFileStore(fs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return secretsprovider.NewSecretProvider(store), nil | ||
} | ||
|
||
func parseSecret(value string) (*secretsprovider.FileSource, error) { | ||
csvReader := csv.NewReader(strings.NewReader(value)) | ||
fields, err := csvReader.Read() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to parse csv secret") | ||
} | ||
|
||
fs := secretsprovider.FileSource{} | ||
|
||
for _, field := range fields { | ||
parts := strings.SplitN(field, "=", 2) | ||
key := strings.ToLower(parts[0]) | ||
|
||
if len(parts) != 2 { | ||
return nil, errors.Errorf("invalid field '%s' must be a key=value pair", field) | ||
} | ||
|
||
value := parts[1] | ||
switch key { | ||
case "type": | ||
if value != "file" { | ||
return nil, errors.Errorf("unsupported secret type %q", value) | ||
} | ||
case "id": | ||
fs.ID = value | ||
case "source", "src": | ||
fs.FilePath = value | ||
default: | ||
return nil, errors.Errorf("unexpected key '%s' in '%s'", key, field) | ||
} | ||
} | ||
return &fs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package build | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/moby/buildkit/session" | ||
"github.com/moby/buildkit/session/sshforward/sshprovider" | ||
) | ||
|
||
func ParseSSHSpecs(sl []string) (session.Attachable, error) { | ||
configs := make([]sshprovider.AgentConfig, 0, len(sl)) | ||
for _, v := range sl { | ||
c, err := parseSSH(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configs = append(configs, *c) | ||
} | ||
return sshprovider.NewSSHAgentProvider(configs) | ||
} | ||
|
||
func parseSSH(value string) (*sshprovider.AgentConfig, error) { | ||
parts := strings.SplitN(value, "=", 2) | ||
cfg := sshprovider.AgentConfig{ | ||
ID: parts[0], | ||
} | ||
if len(parts) > 1 { | ||
cfg.Paths = strings.Split(parts[1], ",") | ||
} | ||
return &cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package build | ||
|
||
import ( | ||
"archive/tar" | ||
"bytes" | ||
"net" | ||
"os" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// archiveHeaderSize is the number of bytes in an archive header | ||
const archiveHeaderSize = 512 | ||
|
||
func isLocalDir(c string) bool { | ||
st, err := os.Stat(c) | ||
return err == nil && st.IsDir() | ||
} | ||
|
||
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 | ||
} | ||
|
||
// toBuildkitExtraHosts converts hosts from docker key:value format to buildkit's csv format | ||
func toBuildkitExtraHosts(inp []string) (string, error) { | ||
if len(inp) == 0 { | ||
return "", nil | ||
} | ||
hosts := make([]string, 0, len(inp)) | ||
for _, h := range inp { | ||
parts := strings.Split(h, ":") | ||
|
||
if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil { | ||
return "", errors.Errorf("invalid host %s", h) | ||
} | ||
hosts = append(hosts, parts[0]+"="+parts[1]) | ||
} | ||
return strings.Join(hosts, ","), nil | ||
} |
Oops, something went wrong.