@@ -15,14 +15,11 @@ import (
15
15
composecli "github.com/compose-spec/compose-go/cli"
16
16
"github.com/docker/buildx/bake/hclparser"
17
17
"github.com/docker/buildx/build"
18
+ cbuild "github.com/docker/buildx/controller/build"
18
19
controllerapi "github.com/docker/buildx/controller/pb"
19
20
"github.com/docker/buildx/util/buildflags"
20
- "github.com/docker/buildx/util/platformutil"
21
-
22
- "github.com/docker/cli/cli/config"
23
21
hcl "github.com/hashicorp/hcl/v2"
24
22
"github.com/moby/buildkit/client/llb"
25
- "github.com/moby/buildkit/session/auth/authprovider"
26
23
"github.com/pkg/errors"
27
24
"github.com/zclconf/go-cty/cty"
28
25
"github.com/zclconf/go-cty/cty/convert"
@@ -914,7 +911,11 @@ func (t *Target) GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(
914
911
func TargetsToBuildOpt (m map [string ]* Target , inp * Input ) (map [string ]build.Options , error ) {
915
912
m2 := make (map [string ]build.Options , len (m ))
916
913
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 )
918
919
if err != nil {
919
920
return nil , err
920
921
}
@@ -923,14 +924,14 @@ func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Optio
923
924
return m2 , nil
924
925
}
925
926
926
- func updateContext (t * build .Inputs , inp * Input ) {
927
+ func updateContext (t * controllerapi .Inputs , inp * Input ) error {
927
928
if inp == nil || inp .State == nil {
928
- return
929
+ return nil
929
930
}
930
931
931
932
for k , v := range t .NamedContexts {
932
933
if v .Path == "." {
933
- t .NamedContexts [k ] = build .NamedContext {Path : inp .URL }
934
+ t .NamedContexts [k ] = & controllerapi .NamedContext {Path : inp .URL }
934
935
}
935
936
if strings .HasPrefix (v .Path , "cwd://" ) || strings .HasPrefix (v .Path , "target:" ) || strings .HasPrefix (v .Path , "docker-image:" ) {
936
937
continue
@@ -939,32 +940,41 @@ func updateContext(t *build.Inputs, inp *Input) {
939
940
continue
940
941
}
941
942
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 ()}
943
948
}
944
949
945
950
if t .ContextPath == "." {
946
951
t .ContextPath = inp .URL
947
- return
952
+ return nil
948
953
}
949
954
if strings .HasPrefix (t .ContextPath , "cwd://" ) {
950
- return
955
+ return nil
951
956
}
952
957
if build .IsRemoteURL (t .ContextPath ) {
953
- return
958
+ return nil
954
959
}
955
960
st := llb .Scratch ().File (
956
961
llb .Copy (* inp .State , t .ContextPath , "/" , & llb.CopyInfo {
957
962
CopyDirContentsOnly : true ,
958
963
}),
959
964
llb .WithCustomNamef ("set context to %s" , t .ContextPath ),
960
965
)
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
962
972
}
963
973
964
974
// validateContextsEntitlements is a basic check to ensure contexts do not
965
975
// escape local directories when loaded from remote sources. This is to be
966
976
// 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 {
968
978
if inp == nil || inp .State == nil {
969
979
return nil
970
980
}
@@ -973,13 +983,13 @@ func validateContextsEntitlements(t build.Inputs, inp *Input) error {
973
983
return nil
974
984
}
975
985
}
976
- if t .ContextState == nil {
986
+ if t .ContextDefinition == nil {
977
987
if err := checkPath (t .ContextPath ); err != nil {
978
988
return err
979
989
}
980
990
}
981
991
for _ , v := range t .NamedContexts {
982
- if v .State != nil {
992
+ if v .Definition != nil {
983
993
continue
984
994
}
985
995
if err := checkPath (v .Path ); err != nil {
@@ -1014,7 +1024,9 @@ func checkPath(p string) error {
1014
1024
return nil
1015
1025
}
1016
1026
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
+
1018
1030
if v := t .Context ; v != nil && * v == "-" {
1019
1031
return nil , errors .Errorf ("context from stdin not allowed in bake" )
1020
1032
}
@@ -1034,24 +1046,24 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
1034
1046
dockerfilePath = * t .Dockerfile
1035
1047
}
1036
1048
1037
- bi := build .Inputs {
1049
+ bi := controllerapi .Inputs {
1038
1050
ContextPath : contextPath ,
1039
- DockerfilePath : dockerfilePath ,
1051
+ DockerfileName : dockerfilePath ,
1040
1052
NamedContexts : toNamedContexts (t .Contexts ),
1041
1053
}
1042
1054
if t .DockerfileInline != nil {
1043
1055
bi .DockerfileInline = * t .DockerfileInline
1044
1056
}
1045
1057
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 )
1048
1060
}
1049
1061
if strings .HasPrefix (bi .ContextPath , "cwd://" ) {
1050
1062
bi .ContextPath = path .Clean (strings .TrimPrefix (bi .ContextPath , "cwd://" ))
1051
1063
}
1052
1064
for k , v := range bi .NamedContexts {
1053
1065
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://" ))}
1055
1067
}
1056
1068
}
1057
1069
@@ -1090,87 +1102,61 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
1090
1102
networkMode = * t .NetworkMode
1091
1103
}
1092
1104
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
+ },
1095
1112
Tags : t .Tags ,
1096
1113
BuildArgs : args ,
1097
1114
Labels : labels ,
1098
- NoCache : noCache ,
1099
1115
NoCacheFilter : t .NoCacheFilter ,
1100
- Pull : pull ,
1101
1116
NetworkMode : networkMode ,
1102
- Linked : t . linked ,
1117
+ Platforms : t . Platforms ,
1103
1118
}
1104
1119
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
1108
1122
}
1109
- bo .Platforms = platforms
1110
1123
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 )
1119
1125
if err != nil {
1120
1126
return nil , err
1121
1127
}
1122
- bo .Session = append (bo .Session , secretAttachment )
1123
1128
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 )
1132
1130
if err != nil {
1133
1131
return nil , err
1134
1132
}
1135
- bo .Session = append (bo .Session , sshAttachment )
1136
-
1137
- if t .Target != nil {
1138
- bo .Target = * t .Target
1139
- }
1140
1133
1141
- cacheImports , err : = buildflags .ParseCacheEntry (t .CacheFrom )
1134
+ opts . CacheFrom , err = buildflags .ParseCacheEntry (t .CacheFrom )
1142
1135
if err != nil {
1143
1136
return nil , err
1144
1137
}
1145
- bo .CacheFrom = controllerapi .CreateCaches (cacheImports )
1146
1138
1147
- cacheExports , err : = buildflags .ParseCacheEntry (t .CacheTo )
1139
+ opts . CacheTo , err = buildflags .ParseCacheEntry (t .CacheTo )
1148
1140
if err != nil {
1149
1141
return nil , err
1150
1142
}
1151
- bo .CacheTo = controllerapi .CreateCaches (cacheExports )
1152
1143
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 )
1158
1145
if err != nil {
1159
1146
return nil , err
1160
1147
}
1161
1148
1162
- attests , err : = buildflags .ParseAttests (t .Attest )
1149
+ opts . Attests , err = buildflags .ParseAttests (t .Attest )
1163
1150
if err != nil {
1164
1151
return nil , err
1165
1152
}
1166
- bo .Attests = controllerapi .CreateAttestations (attests )
1167
1153
1168
- bo .SourcePolicy , err = build .ReadSourcePolicy ()
1154
+ opts .SourcePolicy , err = build .ReadSourcePolicy ()
1169
1155
if err != nil {
1170
1156
return nil , err
1171
1157
}
1172
1158
1173
- return bo , nil
1159
+ return opts , nil
1174
1160
}
1175
1161
1176
1162
func defaultTarget () * Target {
@@ -1259,10 +1245,10 @@ func sliceEqual(s1, s2 []string) bool {
1259
1245
return true
1260
1246
}
1261
1247
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 ))
1264
1250
for k , v := range m {
1265
- m2 [k ] = build .NamedContext {Path : v }
1251
+ m2 [k ] = & controllerapi .NamedContext {Path : v }
1266
1252
}
1267
1253
return m2
1268
1254
}
0 commit comments