@@ -14,14 +14,12 @@ import (
14
14
15
15
"github.com/docker/buildx/bake/hclparser"
16
16
"github.com/docker/buildx/build"
17
+ cbuild "github.com/docker/buildx/controller/build"
17
18
controllerapi "github.com/docker/buildx/controller/pb"
18
19
"github.com/docker/buildx/util/buildflags"
19
- "github.com/docker/buildx/util/platformutil"
20
- "github.com/docker/cli/cli/config"
21
20
"github.com/docker/docker/builder/remotecontext/urlutil"
22
21
hcl "github.com/hashicorp/hcl/v2"
23
22
"github.com/moby/buildkit/client/llb"
24
- "github.com/moby/buildkit/session/auth/authprovider"
25
23
"github.com/pkg/errors"
26
24
)
27
25
@@ -782,7 +780,11 @@ func (t *Target) AddOverrides(overrides map[string]Override) error {
782
780
func TargetsToBuildOpt (m map [string ]* Target , inp * Input ) (map [string ]build.Options , error ) {
783
781
m2 := make (map [string ]build.Options , len (m ))
784
782
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 )
786
788
if err != nil {
787
789
return nil , err
788
790
}
@@ -791,14 +793,14 @@ func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Optio
791
793
return m2 , nil
792
794
}
793
795
794
- func updateContext (t * build .Inputs , inp * Input ) {
796
+ func updateContext (t * controllerapi .Inputs , inp * Input ) error {
795
797
if inp == nil || inp .State == nil {
796
- return
798
+ return nil
797
799
}
798
800
799
801
for k , v := range t .NamedContexts {
800
802
if v .Path == "." {
801
- t .NamedContexts [k ] = build .NamedContext {Path : inp .URL }
803
+ t .NamedContexts [k ] = & controllerapi .NamedContext {Path : inp .URL }
802
804
}
803
805
if strings .HasPrefix (v .Path , "cwd://" ) || strings .HasPrefix (v .Path , "target:" ) || strings .HasPrefix (v .Path , "docker-image:" ) {
804
806
continue
@@ -807,27 +809,36 @@ func updateContext(t *build.Inputs, inp *Input) {
807
809
continue
808
810
}
809
811
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 ()}
811
817
}
812
818
813
819
if t .ContextPath == "." {
814
820
t .ContextPath = inp .URL
815
- return
821
+ return nil
816
822
}
817
823
if strings .HasPrefix (t .ContextPath , "cwd://" ) {
818
- return
824
+ return nil
819
825
}
820
826
if IsRemoteURL (t .ContextPath ) {
821
- return
827
+ return nil
822
828
}
823
829
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
825
836
}
826
837
827
838
// validateContextsEntitlements is a basic check to ensure contexts do not
828
839
// escape local directories when loaded from remote sources. This is to be
829
840
// 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 {
831
842
if inp == nil || inp .State == nil {
832
843
return nil
833
844
}
@@ -836,13 +847,13 @@ func validateContextsEntitlements(t build.Inputs, inp *Input) error {
836
847
return nil
837
848
}
838
849
}
839
- if t .ContextState == nil {
850
+ if t .ContextDefinition == nil {
840
851
if err := checkPath (t .ContextPath ); err != nil {
841
852
return err
842
853
}
843
854
}
844
855
for _ , v := range t .NamedContexts {
845
- if v .State != nil {
856
+ if v .Definition != nil {
846
857
continue
847
858
}
848
859
if err := checkPath (v .Path ); err != nil {
@@ -877,7 +888,7 @@ func checkPath(p string) error {
877
888
return nil
878
889
}
879
890
880
- func toBuildOpt (t * Target , inp * Input ) (* build. Options , error ) {
891
+ func toControllerOpt (t * Target , inp * Input ) (* controllerapi. BuildOptions , error ) {
881
892
if v := t .Context ; v != nil && * v == "-" {
882
893
return nil , errors .Errorf ("context from stdin not allowed in bake" )
883
894
}
@@ -930,9 +941,9 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
930
941
networkMode = * t .NetworkMode
931
942
}
932
943
933
- bi := build .Inputs {
944
+ bi := controllerapi .Inputs {
934
945
ContextPath : contextPath ,
935
- DockerfilePath : dockerfilePath ,
946
+ DockerfileName : dockerfilePath ,
936
947
NamedContexts : toNamedContexts (t .Contexts ),
937
948
}
938
949
if t .DockerfileInline != nil {
@@ -944,7 +955,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
944
955
}
945
956
for k , v := range bi .NamedContexts {
946
957
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://" ))}
948
959
}
949
960
}
950
961
@@ -954,82 +965,55 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
954
965
955
966
t .Context = & bi .ContextPath
956
967
957
- bo := & build. Options {
958
- Inputs : bi ,
968
+ opts := & controllerapi. BuildOptions {
969
+ Inputs : & bi ,
959
970
Tags : t .Tags ,
960
971
BuildArgs : args ,
961
972
Labels : labels ,
962
- NoCache : noCache ,
963
973
NoCacheFilter : t .NoCacheFilter ,
964
- Pull : pull ,
965
974
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 ,
967
981
}
982
+ var err error
968
983
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
985
986
}
986
- bo .Session = append (bo .Session , secretAttachment )
987
987
988
- sshSpecs , err : = buildflags .ParseSSHSpecs (t .SSH )
988
+ opts . Secrets , err = buildflags .ParseSecretSpecs (t .Secrets )
989
989
if err != nil {
990
990
return nil , err
991
991
}
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 )
996
993
if err != nil {
997
994
return nil , err
998
995
}
999
- bo .Session = append (bo .Session , sshAttachment )
1000
-
1001
- if t .Target != nil {
1002
- bo .Target = * t .Target
1003
- }
1004
996
1005
- cacheImports , err : = buildflags .ParseCacheEntry (t .CacheFrom )
997
+ opts . CacheFrom , err = buildflags .ParseCacheEntry (t .CacheFrom )
1006
998
if err != nil {
1007
999
return nil , err
1008
1000
}
1009
- bo .CacheFrom = controllerapi .CreateCaches (cacheImports )
1010
-
1011
- cacheExports , err := buildflags .ParseCacheEntry (t .CacheTo )
1001
+ opts .CacheTo , err = buildflags .ParseCacheEntry (t .CacheTo )
1012
1002
if err != nil {
1013
1003
return nil , err
1014
1004
}
1015
- bo .CacheTo = controllerapi .CreateCaches (cacheExports )
1016
1005
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 )
1022
1007
if err != nil {
1023
1008
return nil , err
1024
1009
}
1025
1010
1026
- attests , err : = buildflags .ParseAttests (t .Attest )
1011
+ opts . Attests , err = buildflags .ParseAttests (t .Attest )
1027
1012
if err != nil {
1028
1013
return nil , err
1029
1014
}
1030
- bo .Attests = controllerapi .CreateAttestations (attests )
1031
1015
1032
- return bo , nil
1016
+ return opts , nil
1033
1017
}
1034
1018
1035
1019
func defaultTarget () * Target {
@@ -1102,10 +1086,10 @@ func sliceEqual(s1, s2 []string) bool {
1102
1086
return true
1103
1087
}
1104
1088
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 ))
1107
1091
for k , v := range m {
1108
- m2 [k ] = build .NamedContext {Path : v }
1092
+ m2 [k ] = & controllerapi .NamedContext {Path : v }
1109
1093
}
1110
1094
return m2
1111
1095
}
0 commit comments