Skip to content

Commit 5134b6c

Browse files
jedevccrazy-max
authored andcommitted
bake: use controller build options as an intermediate stage
With the previous changes to bring controllerapi.BuildOptions up to date with build.Options, we can have bake generate controllerapi.BuildOptions, and then convert those to build.Option using the controller/build package. This is an intermediate patch, designed to allow us to clean up some shared logic between both build and bake. The next step will be to modify bake to use the controller api, and completely skip the build.Options generation step. Signed-off-by: CrazyMax <[email protected]>
1 parent 442eab2 commit 5134b6c

File tree

1 file changed

+55
-69
lines changed

1 file changed

+55
-69
lines changed

bake/bake.go

+55-69
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ import (
1515
composecli "github.com/compose-spec/compose-go/cli"
1616
"github.com/docker/buildx/bake/hclparser"
1717
"github.com/docker/buildx/build"
18+
cbuild "github.com/docker/buildx/controller/build"
1819
controllerapi "github.com/docker/buildx/controller/pb"
1920
"github.com/docker/buildx/util/buildflags"
20-
"github.com/docker/buildx/util/platformutil"
21-
22-
"github.com/docker/cli/cli/config"
2321
hcl "github.com/hashicorp/hcl/v2"
2422
"github.com/moby/buildkit/client/llb"
25-
"github.com/moby/buildkit/session/auth/authprovider"
2623
"github.com/pkg/errors"
2724
"github.com/zclconf/go-cty/cty"
2825
"github.com/zclconf/go-cty/cty/convert"
@@ -914,7 +911,11 @@ func (t *Target) GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(
914911
func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Options, error) {
915912
m2 := make(map[string]build.Options, len(m))
916913
for k, v := range m {
917-
bo, err := toBuildOpt(v, inp)
914+
opts, err := toControllerOpt(v, inp)
915+
if err != nil {
916+
return nil, err
917+
}
918+
bo, err := cbuild.ToBuildOpts(*opts, nil)
918919
if err != nil {
919920
return nil, err
920921
}
@@ -923,14 +924,14 @@ func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Optio
923924
return m2, nil
924925
}
925926

926-
func updateContext(t *build.Inputs, inp *Input) {
927+
func updateContext(t *controllerapi.Inputs, inp *Input) error {
927928
if inp == nil || inp.State == nil {
928-
return
929+
return nil
929930
}
930931

931932
for k, v := range t.NamedContexts {
932933
if v.Path == "." {
933-
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
934+
t.NamedContexts[k] = &controllerapi.NamedContext{Path: inp.URL}
934935
}
935936
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
936937
continue
@@ -939,32 +940,41 @@ func updateContext(t *build.Inputs, inp *Input) {
939940
continue
940941
}
941942
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
942-
t.NamedContexts[k] = build.NamedContext{State: &st}
943+
def, err := st.Marshal(context.TODO())
944+
if err != nil {
945+
return err
946+
}
947+
t.NamedContexts[k] = &controllerapi.NamedContext{Definition: def.ToPB()}
943948
}
944949

945950
if t.ContextPath == "." {
946951
t.ContextPath = inp.URL
947-
return
952+
return nil
948953
}
949954
if strings.HasPrefix(t.ContextPath, "cwd://") {
950-
return
955+
return nil
951956
}
952957
if build.IsRemoteURL(t.ContextPath) {
953-
return
958+
return nil
954959
}
955960
st := llb.Scratch().File(
956961
llb.Copy(*inp.State, t.ContextPath, "/", &llb.CopyInfo{
957962
CopyDirContentsOnly: true,
958963
}),
959964
llb.WithCustomNamef("set context to %s", t.ContextPath),
960965
)
961-
t.ContextState = &st
966+
def, err := st.Marshal(context.TODO())
967+
if err != nil {
968+
return err
969+
}
970+
t.ContextDefinition = def.ToPB()
971+
return nil
962972
}
963973

964974
// validateContextsEntitlements is a basic check to ensure contexts do not
965975
// escape local directories when loaded from remote sources. This is to be
966976
// replaced with proper entitlements support in the future.
967-
func validateContextsEntitlements(t build.Inputs, inp *Input) error {
977+
func validateContextsEntitlements(t controllerapi.Inputs, inp *Input) error {
968978
if inp == nil || inp.State == nil {
969979
return nil
970980
}
@@ -973,13 +983,13 @@ func validateContextsEntitlements(t build.Inputs, inp *Input) error {
973983
return nil
974984
}
975985
}
976-
if t.ContextState == nil {
986+
if t.ContextDefinition == nil {
977987
if err := checkPath(t.ContextPath); err != nil {
978988
return err
979989
}
980990
}
981991
for _, v := range t.NamedContexts {
982-
if v.State != nil {
992+
if v.Definition != nil {
983993
continue
984994
}
985995
if err := checkPath(v.Path); err != nil {
@@ -1014,7 +1024,9 @@ func checkPath(p string) error {
10141024
return nil
10151025
}
10161026

1017-
func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
1027+
func toControllerOpt(t *Target, inp *Input) (*controllerapi.BuildOptions, error) {
1028+
var err error
1029+
10181030
if v := t.Context; v != nil && *v == "-" {
10191031
return nil, errors.Errorf("context from stdin not allowed in bake")
10201032
}
@@ -1034,24 +1046,24 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
10341046
dockerfilePath = *t.Dockerfile
10351047
}
10361048

1037-
bi := build.Inputs{
1049+
bi := controllerapi.Inputs{
10381050
ContextPath: contextPath,
1039-
DockerfilePath: dockerfilePath,
1051+
DockerfileName: dockerfilePath,
10401052
NamedContexts: toNamedContexts(t.Contexts),
10411053
}
10421054
if t.DockerfileInline != nil {
10431055
bi.DockerfileInline = *t.DockerfileInline
10441056
}
10451057
updateContext(&bi, inp)
1046-
if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !path.IsAbs(bi.DockerfilePath) {
1047-
bi.DockerfilePath = path.Join(bi.ContextPath, bi.DockerfilePath)
1058+
if !build.IsRemoteURL(bi.ContextPath) && bi.ContextDefinition == nil && !path.IsAbs(bi.DockerfileName) {
1059+
bi.DockerfileName = path.Join(bi.ContextPath, bi.DockerfileName)
10481060
}
10491061
if strings.HasPrefix(bi.ContextPath, "cwd://") {
10501062
bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://"))
10511063
}
10521064
for k, v := range bi.NamedContexts {
10531065
if strings.HasPrefix(v.Path, "cwd://") {
1054-
bi.NamedContexts[k] = build.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))}
1066+
bi.NamedContexts[k] = &controllerapi.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))}
10551067
}
10561068
}
10571069

@@ -1090,87 +1102,61 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
10901102
networkMode = *t.NetworkMode
10911103
}
10921104

1093-
bo := &build.Options{
1094-
Inputs: bi,
1105+
opts := &controllerapi.BuildOptions{
1106+
Inputs: &bi,
1107+
Opts: &controllerapi.CommonOptions{
1108+
NoCache: noCache,
1109+
Pull: pull,
1110+
Linked: t.linked,
1111+
},
10951112
Tags: t.Tags,
10961113
BuildArgs: args,
10971114
Labels: labels,
1098-
NoCache: noCache,
10991115
NoCacheFilter: t.NoCacheFilter,
1100-
Pull: pull,
11011116
NetworkMode: networkMode,
1102-
Linked: t.linked,
1117+
Platforms: t.Platforms,
11031118
}
11041119

1105-
platforms, err := platformutil.Parse(t.Platforms)
1106-
if err != nil {
1107-
return nil, err
1120+
if t.Target != nil {
1121+
opts.Target = *t.Target
11081122
}
1109-
bo.Platforms = platforms
11101123

1111-
dockerConfig := config.LoadDefaultConfigFile(os.Stderr)
1112-
bo.Session = append(bo.Session, authprovider.NewDockerAuthProvider(dockerConfig))
1113-
1114-
secrets, err := buildflags.ParseSecretSpecs(t.Secrets)
1115-
if err != nil {
1116-
return nil, err
1117-
}
1118-
secretAttachment, err := controllerapi.CreateSecrets(secrets)
1124+
opts.Secrets, err = buildflags.ParseSecretSpecs(t.Secrets)
11191125
if err != nil {
11201126
return nil, err
11211127
}
1122-
bo.Session = append(bo.Session, secretAttachment)
11231128

1124-
sshSpecs, err := buildflags.ParseSSHSpecs(t.SSH)
1125-
if err != nil {
1126-
return nil, err
1127-
}
1128-
if len(sshSpecs) == 0 && (buildflags.IsGitSSH(bi.ContextPath) || (inp != nil && buildflags.IsGitSSH(inp.URL))) {
1129-
sshSpecs = append(sshSpecs, &controllerapi.SSH{ID: "default"})
1130-
}
1131-
sshAttachment, err := controllerapi.CreateSSH(sshSpecs)
1129+
opts.SSH, err = buildflags.ParseSSHSpecs(t.SSH)
11321130
if err != nil {
11331131
return nil, err
11341132
}
1135-
bo.Session = append(bo.Session, sshAttachment)
1136-
1137-
if t.Target != nil {
1138-
bo.Target = *t.Target
1139-
}
11401133

1141-
cacheImports, err := buildflags.ParseCacheEntry(t.CacheFrom)
1134+
opts.CacheFrom, err = buildflags.ParseCacheEntry(t.CacheFrom)
11421135
if err != nil {
11431136
return nil, err
11441137
}
1145-
bo.CacheFrom = controllerapi.CreateCaches(cacheImports)
11461138

1147-
cacheExports, err := buildflags.ParseCacheEntry(t.CacheTo)
1139+
opts.CacheTo, err = buildflags.ParseCacheEntry(t.CacheTo)
11481140
if err != nil {
11491141
return nil, err
11501142
}
1151-
bo.CacheTo = controllerapi.CreateCaches(cacheExports)
11521143

1153-
outputs, err := buildflags.ParseExports(t.Outputs)
1154-
if err != nil {
1155-
return nil, err
1156-
}
1157-
bo.Exports, err = controllerapi.CreateExports(outputs)
1144+
opts.Exports, err = buildflags.ParseExports(t.Outputs)
11581145
if err != nil {
11591146
return nil, err
11601147
}
11611148

1162-
attests, err := buildflags.ParseAttests(t.Attest)
1149+
opts.Attests, err = buildflags.ParseAttests(t.Attest)
11631150
if err != nil {
11641151
return nil, err
11651152
}
1166-
bo.Attests = controllerapi.CreateAttestations(attests)
11671153

1168-
bo.SourcePolicy, err = build.ReadSourcePolicy()
1154+
opts.SourcePolicy, err = build.ReadSourcePolicy()
11691155
if err != nil {
11701156
return nil, err
11711157
}
11721158

1173-
return bo, nil
1159+
return opts, nil
11741160
}
11751161

11761162
func defaultTarget() *Target {
@@ -1259,10 +1245,10 @@ func sliceEqual(s1, s2 []string) bool {
12591245
return true
12601246
}
12611247

1262-
func toNamedContexts(m map[string]string) map[string]build.NamedContext {
1263-
m2 := make(map[string]build.NamedContext, len(m))
1248+
func toNamedContexts(m map[string]string) map[string]*controllerapi.NamedContext {
1249+
m2 := make(map[string]*controllerapi.NamedContext, len(m))
12641250
for k, v := range m {
1265-
m2[k] = build.NamedContext{Path: v}
1251+
m2[k] = &controllerapi.NamedContext{Path: v}
12661252
}
12671253
return m2
12681254
}

0 commit comments

Comments
 (0)