2019-04-05 15:04:19 +08:00
|
|
|
package bake
|
|
|
|
|
|
|
|
import (
|
2019-07-31 07:44:05 +08:00
|
|
|
"os"
|
2022-08-10 09:24:33 +08:00
|
|
|
"path/filepath"
|
2019-07-31 07:44:05 +08:00
|
|
|
"strings"
|
2019-05-18 17:53:44 +08:00
|
|
|
|
2022-08-10 09:24:33 +08:00
|
|
|
"github.com/compose-spec/compose-go/dotenv"
|
2021-07-12 17:20:43 +08:00
|
|
|
"github.com/compose-spec/compose-go/loader"
|
|
|
|
compose "github.com/compose-spec/compose-go/types"
|
2022-01-26 17:29:17 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-06-15 05:20:35 +08:00
|
|
|
"gopkg.in/yaml.v3"
|
2019-04-05 15:04:19 +08:00
|
|
|
)
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
func ParseComposeFiles(fs []File) (*Config, error) {
|
|
|
|
envs, err := composeEnv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var cfgs []compose.ConfigFile
|
|
|
|
for _, f := range fs {
|
|
|
|
cfgs = append(cfgs, compose.ConfigFile{
|
|
|
|
Filename: f.Name,
|
|
|
|
Content: f.Data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ParseCompose(cfgs, envs)
|
|
|
|
}
|
2022-06-23 19:09:13 +08:00
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
func ParseCompose(cfgs []compose.ConfigFile, envs map[string]string) (*Config, error) {
|
2022-08-10 09:24:33 +08:00
|
|
|
cfg, err := loader.Load(compose.ConfigDetails{
|
2022-08-18 17:34:08 +08:00
|
|
|
ConfigFiles: cfgs,
|
2022-08-10 09:24:33 +08:00
|
|
|
Environment: envs,
|
2021-07-12 17:20:43 +08:00
|
|
|
}, func(options *loader.Options) {
|
|
|
|
options.SkipNormalization = true
|
2019-04-05 15:04:19 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Config
|
|
|
|
if len(cfg.Services) > 0 {
|
2020-04-16 09:00:18 +08:00
|
|
|
c.Groups = []*Group{}
|
|
|
|
c.Targets = []*Target{}
|
2019-04-05 15:04:19 +08:00
|
|
|
|
2020-04-16 09:00:18 +08:00
|
|
|
g := &Group{Name: "default"}
|
2019-04-05 15:04:19 +08:00
|
|
|
|
|
|
|
for _, s := range cfg.Services {
|
2022-06-23 19:09:13 +08:00
|
|
|
if s.Build == nil {
|
2022-08-18 17:34:08 +08:00
|
|
|
continue
|
2019-05-25 08:46:49 +08:00
|
|
|
}
|
|
|
|
|
2022-07-31 22:53:39 +08:00
|
|
|
targetName := sanitizeTargetName(s.Name)
|
|
|
|
if err = validateTargetName(targetName); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "invalid service name %q", targetName)
|
2022-01-26 17:29:17 +08:00
|
|
|
}
|
|
|
|
|
2019-04-20 13:54:34 +08:00
|
|
|
var contextPathP *string
|
|
|
|
if s.Build.Context != "" {
|
|
|
|
contextPath := s.Build.Context
|
|
|
|
contextPathP = &contextPath
|
|
|
|
}
|
|
|
|
var dockerfilePathP *string
|
|
|
|
if s.Build.Dockerfile != "" {
|
|
|
|
dockerfilePath := s.Build.Dockerfile
|
|
|
|
dockerfilePathP = &dockerfilePath
|
|
|
|
}
|
2022-04-14 07:27:55 +08:00
|
|
|
|
|
|
|
var secrets []string
|
|
|
|
for _, bs := range s.Build.Secrets {
|
|
|
|
secret, err := composeToBuildkitSecret(bs, cfg.Secrets[bs.Source])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
secrets = append(secrets, secret)
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:53:39 +08:00
|
|
|
g.Targets = append(g.Targets, targetName)
|
2020-04-16 09:00:18 +08:00
|
|
|
t := &Target{
|
2022-07-31 22:53:39 +08:00
|
|
|
Name: targetName,
|
2019-04-20 13:54:34 +08:00
|
|
|
Context: contextPathP,
|
|
|
|
Dockerfile: dockerfilePathP,
|
2022-06-15 00:41:16 +08:00
|
|
|
Tags: s.Build.Tags,
|
2019-04-05 15:04:19 +08:00
|
|
|
Labels: s.Build.Labels,
|
2021-07-30 23:56:40 +08:00
|
|
|
Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) {
|
2022-01-06 18:59:41 +08:00
|
|
|
if val, ok := s.Environment[val]; ok && val != nil {
|
|
|
|
return *val, true
|
|
|
|
}
|
2021-07-30 23:56:40 +08:00
|
|
|
val, ok := cfg.Environment[val]
|
|
|
|
return val, ok
|
|
|
|
})),
|
2021-11-25 19:35:10 +08:00
|
|
|
CacheFrom: s.Build.CacheFrom,
|
2022-06-24 03:44:06 +08:00
|
|
|
CacheTo: s.Build.CacheTo,
|
2021-11-25 19:35:10 +08:00
|
|
|
NetworkMode: &s.Build.Network,
|
2022-04-14 07:27:55 +08:00
|
|
|
Secrets: secrets,
|
2021-08-13 15:15:09 +08:00
|
|
|
}
|
|
|
|
if err = t.composeExtTarget(s.Build.Extensions); err != nil {
|
|
|
|
return nil, err
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
if s.Build.Target != "" {
|
2019-04-26 12:36:28 +08:00
|
|
|
target := s.Build.Target
|
|
|
|
t.Target = &target
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2021-08-13 15:15:09 +08:00
|
|
|
if len(t.Tags) == 0 && s.Image != "" {
|
2019-04-05 15:04:19 +08:00
|
|
|
t.Tags = []string{s.Image}
|
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
c.Targets = append(c.Targets, t)
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
c.Groups = append(c.Groups, g)
|
2019-04-05 15:04:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
func validateComposeFile(dt []byte, fn string) (bool, error) {
|
|
|
|
envs, err := composeEnv()
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
fnl := strings.ToLower(fn)
|
|
|
|
if strings.HasSuffix(fnl, ".yml") || strings.HasSuffix(fnl, ".yaml") {
|
|
|
|
return true, validateCompose(dt, envs)
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(fnl, ".json") || strings.HasSuffix(fnl, ".hcl") {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
err = validateCompose(dt, envs)
|
|
|
|
return err == nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateCompose(dt []byte, envs map[string]string) error {
|
|
|
|
_, err := loader.Load(compose.ConfigDetails{
|
|
|
|
ConfigFiles: []compose.ConfigFile{
|
|
|
|
{
|
|
|
|
Content: dt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Environment: envs,
|
|
|
|
}, func(options *loader.Options) {
|
|
|
|
options.SkipNormalization = true
|
|
|
|
// consistency is checked later in ParseCompose to ensure multiple
|
|
|
|
// compose files can be merged together
|
|
|
|
options.SkipConsistencyCheck = true
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func composeEnv() (map[string]string, error) {
|
|
|
|
envs := sliceToMap(os.Environ())
|
|
|
|
if wd, err := os.Getwd(); err == nil {
|
|
|
|
envs, err = loadDotEnv(envs, wd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return envs, nil
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:24:33 +08:00
|
|
|
func loadDotEnv(curenv map[string]string, workingDir string) (map[string]string, error) {
|
|
|
|
if curenv == nil {
|
|
|
|
curenv = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
ef, err := filepath.Abs(filepath.Join(workingDir, ".env"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(ef); os.IsNotExist(err) {
|
|
|
|
return curenv, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dt, err := os.ReadFile(ef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
envs, err := dotenv.UnmarshalBytes(dt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range envs {
|
|
|
|
if _, set := curenv[k]; set {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
curenv[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return curenv, nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 23:56:40 +08:00
|
|
|
func flatten(in compose.MappingWithEquals) compose.Mapping {
|
|
|
|
if len(in) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := compose.Mapping{}
|
2019-04-05 15:04:19 +08:00
|
|
|
for k, v := range in {
|
2021-07-30 23:56:40 +08:00
|
|
|
if v == nil {
|
|
|
|
continue
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2021-07-30 23:56:40 +08:00
|
|
|
out[k] = *v
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2021-07-30 23:56:40 +08:00
|
|
|
return out
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2021-08-13 15:15:09 +08:00
|
|
|
|
2022-06-15 05:20:35 +08:00
|
|
|
// xbake Compose build extension provides fields not (yet) available in
|
|
|
|
// Compose build specification: https://github.com/compose-spec/compose-spec/blob/master/build.md
|
|
|
|
type xbake struct {
|
|
|
|
Tags stringArray `yaml:"tags,omitempty"`
|
|
|
|
CacheFrom stringArray `yaml:"cache-from,omitempty"`
|
|
|
|
CacheTo stringArray `yaml:"cache-to,omitempty"`
|
|
|
|
Secrets stringArray `yaml:"secret,omitempty"`
|
|
|
|
SSH stringArray `yaml:"ssh,omitempty"`
|
|
|
|
Platforms stringArray `yaml:"platforms,omitempty"`
|
|
|
|
Outputs stringArray `yaml:"output,omitempty"`
|
|
|
|
Pull *bool `yaml:"pull,omitempty"`
|
|
|
|
NoCache *bool `yaml:"no-cache,omitempty"`
|
|
|
|
NoCacheFilter stringArray `yaml:"no-cache-filter,omitempty"`
|
2022-08-05 21:21:21 +08:00
|
|
|
Contexts stringMap `yaml:"contexts,omitempty"`
|
2022-06-15 05:20:35 +08:00
|
|
|
// don't forget to update documentation if you add a new field:
|
2022-11-05 02:47:00 +08:00
|
|
|
// docs/manuals/bake/compose-file.md#extension-field-with-x-bake
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
|
2022-08-05 21:21:21 +08:00
|
|
|
type stringMap map[string]string
|
2022-06-15 05:20:35 +08:00
|
|
|
type stringArray []string
|
|
|
|
|
|
|
|
func (sa *stringArray) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var multi []string
|
|
|
|
err := unmarshal(&multi)
|
|
|
|
if err != nil {
|
|
|
|
var single string
|
|
|
|
if err := unmarshal(&single); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*sa = strings.Fields(single)
|
|
|
|
} else {
|
|
|
|
*sa = multi
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-13 15:15:09 +08:00
|
|
|
// composeExtTarget converts Compose build extension x-bake to bake Target
|
|
|
|
// https://github.com/compose-spec/compose-spec/blob/master/spec.md#extension
|
|
|
|
func (t *Target) composeExtTarget(exts map[string]interface{}) error {
|
2022-06-15 05:20:35 +08:00
|
|
|
var xb xbake
|
|
|
|
|
|
|
|
ext, ok := exts["x-bake"]
|
|
|
|
if !ok || ext == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
yb, _ := yaml.Marshal(ext)
|
|
|
|
if err := yaml.Unmarshal(yb, &xb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(xb.Tags) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.Tags = dedupSlice(append(t.Tags, xb.Tags...))
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
if len(xb.CacheFrom) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.CacheFrom = dedupSlice(append(t.CacheFrom, xb.CacheFrom...))
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
if len(xb.CacheTo) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.CacheTo = dedupSlice(append(t.CacheTo, xb.CacheTo...))
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
if len(xb.Secrets) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.Secrets = dedupSlice(append(t.Secrets, xb.Secrets...))
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
if len(xb.SSH) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.SSH = dedupSlice(append(t.SSH, xb.SSH...))
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
if len(xb.Platforms) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.Platforms = dedupSlice(append(t.Platforms, xb.Platforms...))
|
2021-08-13 15:15:09 +08:00
|
|
|
}
|
2022-06-15 05:20:35 +08:00
|
|
|
if len(xb.Outputs) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.Outputs = dedupSlice(append(t.Outputs, xb.Outputs...))
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
if xb.Pull != nil {
|
|
|
|
t.Pull = xb.Pull
|
|
|
|
}
|
|
|
|
if xb.NoCache != nil {
|
|
|
|
t.NoCache = xb.NoCache
|
|
|
|
}
|
|
|
|
if len(xb.NoCacheFilter) > 0 {
|
2022-08-05 21:21:21 +08:00
|
|
|
t.NoCacheFilter = dedupSlice(append(t.NoCacheFilter, xb.NoCacheFilter...))
|
|
|
|
}
|
|
|
|
if len(xb.Contexts) > 0 {
|
|
|
|
t.Contexts = dedupMap(t.Contexts, xb.Contexts)
|
2022-06-15 05:20:35 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 15:15:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-14 07:27:55 +08:00
|
|
|
|
|
|
|
// composeToBuildkitSecret converts secret from compose format to buildkit's
|
|
|
|
// csv format.
|
|
|
|
func composeToBuildkitSecret(inp compose.ServiceSecretConfig, psecret compose.SecretConfig) (string, error) {
|
|
|
|
if psecret.External.External {
|
|
|
|
return "", errors.Errorf("unsupported external secret %s", psecret.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
var bkattrs []string
|
|
|
|
if inp.Source != "" {
|
|
|
|
bkattrs = append(bkattrs, "id="+inp.Source)
|
|
|
|
}
|
|
|
|
if psecret.File != "" {
|
|
|
|
bkattrs = append(bkattrs, "src="+psecret.File)
|
|
|
|
}
|
2022-06-15 00:45:23 +08:00
|
|
|
if psecret.Environment != "" {
|
|
|
|
bkattrs = append(bkattrs, "env="+psecret.Environment)
|
|
|
|
}
|
2022-04-14 07:27:55 +08:00
|
|
|
|
|
|
|
return strings.Join(bkattrs, ","), nil
|
|
|
|
}
|