Skip to content

Commit 6718ee5

Browse files
committed
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: Justin Chadwell <[email protected]>
1 parent fe09994 commit 6718ee5

File tree

1 file changed

+51
-67
lines changed

1 file changed

+51
-67
lines changed

bake/bake.go

+51-67
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ import (
1414

1515
"github.com/docker/buildx/bake/hclparser"
1616
"github.com/docker/buildx/build"
17+
cbuild "github.com/docker/buildx/controller/build"
1718
controllerapi "github.com/docker/buildx/controller/pb"
1819
"github.com/docker/buildx/util/buildflags"
19-
"github.com/docker/buildx/util/platformutil"
20-
"github.com/docker/cli/cli/config"
2120
"github.com/docker/docker/builder/remotecontext/urlutil"
2221
hcl "github.com/hashicorp/hcl/v2"
2322
"github.com/moby/buildkit/client/llb"
24-
"github.com/moby/buildkit/session/auth/authprovider"
2523
"github.com/pkg/errors"
2624
)
2725

@@ -782,7 +780,11 @@ func (t *Target) AddOverrides(overrides map[string]Override) error {
782780
func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Options, error) {
783781
m2 := make(map[string]build.Options, len(m))
784782
for k, v := range m {
785-
bo, err := toBuildOpt(v, inp)
783+
opts, err := toControllerOpt(v, inp)
784+
if err != nil {
785+
return nil, err
786+
}
787+
bo, err := cbuild.ToBuildOpts(*opts, nil)
786788
if err != nil {
787789
return nil, err
788790
}
@@ -791,14 +793,14 @@ func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Optio
791793
return m2, nil
792794
}
793795

794-
func updateContext(t *build.Inputs, inp *Input) {
796+
func updateContext(t *controllerapi.Inputs, inp *Input) error {
795797
if inp == nil || inp.State == nil {
796-
return
798+
return nil
797799
}
798800

799801
for k, v := range t.NamedContexts {
800802
if v.Path == "." {
801-
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
803+
t.NamedContexts[k] = &controllerapi.NamedContext{Path: inp.URL}
802804
}
803805
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
804806
continue
@@ -807,27 +809,36 @@ func updateContext(t *build.Inputs, inp *Input) {
807809
continue
808810
}
809811
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
810-
t.NamedContexts[k] = build.NamedContext{State: &st}
812+
def, err := st.Marshal(context.TODO())
813+
if err != nil {
814+
return err
815+
}
816+
t.NamedContexts[k] = &controllerapi.NamedContext{Definition: def.ToPB()}
811817
}
812818

813819
if t.ContextPath == "." {
814820
t.ContextPath = inp.URL
815-
return
821+
return nil
816822
}
817823
if strings.HasPrefix(t.ContextPath, "cwd://") {
818-
return
824+
return nil
819825
}
820826
if IsRemoteURL(t.ContextPath) {
821-
return
827+
return nil
822828
}
823829
st := llb.Scratch().File(llb.Copy(*inp.State, t.ContextPath, "/"), llb.WithCustomNamef("set context to %s", t.ContextPath))
824-
t.ContextState = &st
830+
def, err := st.Marshal(context.TODO())
831+
if err != nil {
832+
return err
833+
}
834+
t.ContextDefinition = def.ToPB()
835+
return nil
825836
}
826837

827838
// validateContextsEntitlements is a basic check to ensure contexts do not
828839
// escape local directories when loaded from remote sources. This is to be
829840
// replaced with proper entitlements support in the future.
830-
func validateContextsEntitlements(t build.Inputs, inp *Input) error {
841+
func validateContextsEntitlements(t controllerapi.Inputs, inp *Input) error {
831842
if inp == nil || inp.State == nil {
832843
return nil
833844
}
@@ -836,13 +847,13 @@ func validateContextsEntitlements(t build.Inputs, inp *Input) error {
836847
return nil
837848
}
838849
}
839-
if t.ContextState == nil {
850+
if t.ContextDefinition == nil {
840851
if err := checkPath(t.ContextPath); err != nil {
841852
return err
842853
}
843854
}
844855
for _, v := range t.NamedContexts {
845-
if v.State != nil {
856+
if v.Definition != nil {
846857
continue
847858
}
848859
if err := checkPath(v.Path); err != nil {
@@ -877,7 +888,7 @@ func checkPath(p string) error {
877888
return nil
878889
}
879890

880-
func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
891+
func toControllerOpt(t *Target, inp *Input) (*controllerapi.BuildOptions, error) {
881892
if v := t.Context; v != nil && *v == "-" {
882893
return nil, errors.Errorf("context from stdin not allowed in bake")
883894
}
@@ -930,9 +941,9 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
930941
networkMode = *t.NetworkMode
931942
}
932943

933-
bi := build.Inputs{
944+
bi := controllerapi.Inputs{
934945
ContextPath: contextPath,
935-
DockerfilePath: dockerfilePath,
946+
DockerfileName: dockerfilePath,
936947
NamedContexts: toNamedContexts(t.Contexts),
937948
}
938949
if t.DockerfileInline != nil {
@@ -944,7 +955,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
944955
}
945956
for k, v := range bi.NamedContexts {
946957
if strings.HasPrefix(v.Path, "cwd://") {
947-
bi.NamedContexts[k] = build.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))}
958+
bi.NamedContexts[k] = &controllerapi.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))}
948959
}
949960
}
950961

@@ -954,82 +965,55 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
954965

955966
t.Context = &bi.ContextPath
956967

957-
bo := &build.Options{
958-
Inputs: bi,
968+
opts := &controllerapi.BuildOptions{
969+
Inputs: &bi,
959970
Tags: t.Tags,
960971
BuildArgs: args,
961972
Labels: labels,
962-
NoCache: noCache,
963973
NoCacheFilter: t.NoCacheFilter,
964-
Pull: pull,
965974
NetworkMode: networkMode,
966-
Linked: t.linked,
975+
Opts: &controllerapi.CommonOptions{
976+
NoCache: noCache,
977+
Pull: pull,
978+
Linked: t.linked,
979+
},
980+
Platforms: t.Platforms,
967981
}
982+
var err error
968983

969-
platforms, err := platformutil.Parse(t.Platforms)
970-
if err != nil {
971-
return nil, err
972-
}
973-
bo.Platforms = platforms
974-
975-
dockerConfig := config.LoadDefaultConfigFile(os.Stderr)
976-
bo.Session = append(bo.Session, authprovider.NewDockerAuthProvider(dockerConfig))
977-
978-
secrets, err := buildflags.ParseSecretSpecs(t.Secrets)
979-
if err != nil {
980-
return nil, err
981-
}
982-
secretAttachment, err := controllerapi.CreateSecrets(secrets)
983-
if err != nil {
984-
return nil, err
984+
if t.Target != nil {
985+
opts.Target = *t.Target
985986
}
986-
bo.Session = append(bo.Session, secretAttachment)
987987

988-
sshSpecs, err := buildflags.ParseSSHSpecs(t.SSH)
988+
opts.Secrets, err = buildflags.ParseSecretSpecs(t.Secrets)
989989
if err != nil {
990990
return nil, err
991991
}
992-
if len(sshSpecs) == 0 && buildflags.IsGitSSH(contextPath) {
993-
sshSpecs = append(sshSpecs, &controllerapi.SSH{ID: "default"})
994-
}
995-
sshAttachment, err := controllerapi.CreateSSH(sshSpecs)
992+
opts.SSH, err = buildflags.ParseSSHSpecs(t.SSH)
996993
if err != nil {
997994
return nil, err
998995
}
999-
bo.Session = append(bo.Session, sshAttachment)
1000-
1001-
if t.Target != nil {
1002-
bo.Target = *t.Target
1003-
}
1004996

1005-
cacheImports, err := buildflags.ParseCacheEntry(t.CacheFrom)
997+
opts.CacheFrom, err = buildflags.ParseCacheEntry(t.CacheFrom)
1006998
if err != nil {
1007999
return nil, err
10081000
}
1009-
bo.CacheFrom = controllerapi.CreateCaches(cacheImports)
1010-
1011-
cacheExports, err := buildflags.ParseCacheEntry(t.CacheTo)
1001+
opts.CacheTo, err = buildflags.ParseCacheEntry(t.CacheTo)
10121002
if err != nil {
10131003
return nil, err
10141004
}
1015-
bo.CacheTo = controllerapi.CreateCaches(cacheExports)
10161005

1017-
outputs, err := buildflags.ParseExports(t.Outputs)
1018-
if err != nil {
1019-
return nil, err
1020-
}
1021-
bo.Exports, err = controllerapi.CreateExports(outputs)
1006+
opts.Exports, err = buildflags.ParseExports(t.Outputs)
10221007
if err != nil {
10231008
return nil, err
10241009
}
10251010

1026-
attests, err := buildflags.ParseAttests(t.Attest)
1011+
opts.Attests, err = buildflags.ParseAttests(t.Attest)
10271012
if err != nil {
10281013
return nil, err
10291014
}
1030-
bo.Attests = controllerapi.CreateAttestations(attests)
10311015

1032-
return bo, nil
1016+
return opts, nil
10331017
}
10341018

10351019
func defaultTarget() *Target {
@@ -1102,10 +1086,10 @@ func sliceEqual(s1, s2 []string) bool {
11021086
return true
11031087
}
11041088

1105-
func toNamedContexts(m map[string]string) map[string]build.NamedContext {
1106-
m2 := make(map[string]build.NamedContext, len(m))
1089+
func toNamedContexts(m map[string]string) map[string]*controllerapi.NamedContext {
1090+
m2 := make(map[string]*controllerapi.NamedContext, len(m))
11071091
for k, v := range m {
1108-
m2[k] = build.NamedContext{Path: v}
1092+
m2[k] = &controllerapi.NamedContext{Path: v}
11091093
}
11101094
return m2
11111095
}

0 commit comments

Comments
 (0)