Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit ffbc1ef

Browse files
Merge pull request #1938 from ibuildthecloud/build-contet
Add support for multiple build contexts in Dockerfile builds
2 parents c950bc9 + 063071b commit ffbc1ef

File tree

15 files changed

+99
-23
lines changed

15 files changed

+99
-23
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ replace (
1313
require (
1414
cuelang.org/go v0.5.0
1515
github.com/AlecAivazis/survey/v2 v2.3.6
16-
github.com/acorn-io/aml v0.0.0-20230707055340-d9d7a8c927b2
16+
github.com/acorn-io/aml v0.0.0-20230714230100-7aded86d1c3e
1717
github.com/acorn-io/baaah v0.0.0-20230707151126-5d519d272865
1818
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500
1919
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ github.com/ThalesIgnite/crypto11 v1.2.5 h1:1IiIIEqYmBvUYFeMnHqRft4bwf/O36jryEUpY
9292
github.com/ThalesIgnite/crypto11 v1.2.5/go.mod h1:ILDKtnCKiQ7zRoNxcp36Y1ZR8LBPmR2E23+wTQe/MlE=
9393
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
9494
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
95-
github.com/acorn-io/aml v0.0.0-20230707055340-d9d7a8c927b2 h1:zx1YhJI+jecZ1nQ3MzFRblMU4KdACUG8DQfd0zB9XvY=
96-
github.com/acorn-io/aml v0.0.0-20230707055340-d9d7a8c927b2/go.mod h1:UEx5RRLFjryCEHN2pM59+d8A0mPJ3VAxggJOTzPymwg=
95+
github.com/acorn-io/aml v0.0.0-20230714230100-7aded86d1c3e h1:jx4uFAazHUwX2uMeCHNtOFSTXBMPjQtflQOnPjmkhms=
96+
github.com/acorn-io/aml v0.0.0-20230714230100-7aded86d1c3e/go.mod h1:UEx5RRLFjryCEHN2pM59+d8A0mPJ3VAxggJOTzPymwg=
9797
github.com/acorn-io/baaah v0.0.0-20230707151126-5d519d272865 h1:BPPGCEBgPxn7crFFWqLDJUlzdHQ23olFkdUqlXd3KA8=
9898
github.com/acorn-io/baaah v0.0.0-20230707151126-5d519d272865/go.mod h1:LtwaWrYK/VuGptWxeD5Sgl0sgJV1ksicpTzyLilow1U=
9999
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500 h1:tiM36bM+iMWuW9HM+YlM1GfNDXC7f565z8Be5epO0qM=

integration/build/build_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ func TestBuildDefault(t *testing.T) {
170170
assert.Len(t, image.ImageData.Containers, 1)
171171
}
172172

173+
func TestBuildMulticontext(t *testing.T) {
174+
c := helper.BuilderClient(t, system.DefaultUserNamespace)
175+
image, err := c.AcornImageBuild(helper.GetCTX(t), "./testdata/multicontextdir/Acornfile", &client.AcornImageBuildOptions{
176+
Cwd: "./testdata/multicontextdir",
177+
})
178+
if err != nil {
179+
t.Fatal(err)
180+
}
181+
assert.Len(t, image.ImageData.Containers, 1)
182+
}
183+
173184
func TestMultiArch(t *testing.T) {
174185
helper.StartController(t)
175186
cfg := helper.StartAPI(t)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
containers: {
2+
simple: {
3+
build: {
4+
dockerfile: "Dockerfile"
5+
context: "./files"
6+
additionalContexts: {
7+
"other-context": "./files/subdir"
8+
}
9+
}
10+
}
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM ghcr.io/acorn-io/images-mirror/busybox:latest AS not-default
2+
COPY test.sh /test.sh
3+
COPY --from=other-context /token /
4+
RUN test -f /test.sh && sh /test.sh
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
set -e -x
3+
test -f /token
4+
[ "hi" = "$(cat /token)" ]

pkg/apis/internal.acorn.io/v1/appspec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type AcornBuild struct {
4141

4242
type Build struct {
4343
Context string `json:"context,omitempty"`
44+
AdditionalContexts map[string]string `json:"additionalContexts,omitempty"`
4445
Dockerfile string `json:"dockerfile,omitempty"`
4546
DockerfileContents string `json:"dockerfileContents,omitempty"`
4647
Target string `json:"target,omitempty"`

pkg/apis/internal.acorn.io/v1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/appdefinition/appdefinition_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,35 +1017,39 @@ images: {
10171017
},
10181018
"build": {
10191019
Build: &v1.Build{
1020-
BuildArgs: map[string]string{},
1021-
Context: ".",
1022-
Dockerfile: "Dockerfile",
1020+
BuildArgs: map[string]string{},
1021+
AdditionalContexts: map[string]string{},
1022+
Context: ".",
1023+
Dockerfile: "Dockerfile",
10231024
},
10241025
Sidecars: map[string]v1.ContainerImageBuilderSpec{
10251026
"side": {
10261027
Build: &v1.Build{
1027-
BuildArgs: map[string]string{},
1028-
Context: ".",
1029-
Dockerfile: "Dockerfile",
1028+
BuildArgs: map[string]string{},
1029+
AdditionalContexts: map[string]string{},
1030+
Context: ".",
1031+
Dockerfile: "Dockerfile",
10301032
},
10311033
},
10321034
},
10331035
},
10341036
"buildcontext": {
10351037
Build: &v1.Build{
1036-
BuildArgs: map[string]string{},
1037-
Context: ".",
1038-
Dockerfile: "Dockerfile",
1038+
BuildArgs: map[string]string{},
1039+
AdditionalContexts: map[string]string{},
1040+
Context: ".",
1041+
Dockerfile: "Dockerfile",
10391042
ContextDirs: map[string]string{
10401043
"/var/tmp": "./foo/bar",
10411044
},
10421045
},
10431046
Sidecars: map[string]v1.ContainerImageBuilderSpec{
10441047
"side": {
10451048
Build: &v1.Build{
1046-
BuildArgs: map[string]string{},
1047-
Context: ".",
1048-
Dockerfile: "Dockerfile",
1049+
BuildArgs: map[string]string{},
1050+
AdditionalContexts: map[string]string{},
1051+
Context: ".",
1052+
Dockerfile: "Dockerfile",
10491053
ContextDirs: map[string]string{
10501054
"/var/tmp": "./foo/bar",
10511055
},

pkg/build/buildkit/build.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ func Build(ctx context.Context, pushRepo string, local bool, cwd string, platfor
9797
"dockerfile": filepath.Dir(filepath.Join(cwd, build.Dockerfile)),
9898
}
9999
} else {
100+
additionalContext := map[string]string{}
101+
for k, v := range build.AdditionalContexts {
102+
options.FrontendAttrs["context:"+k] = "local:" + k
103+
additionalContext[k] = filepath.Join(cwd, v)
104+
}
100105
options.Session = append(options.Session,
101106
buildclient.NewFileServer(messages,
102107
filepath.Join(cwd, build.Context),
108+
additionalContext,
103109
filepath.Join(cwd, build.Dockerfile),
104110
build.DockerfileContents))
105111
}

pkg/buildclient/filesync.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ const (
2020
type FileServer struct {
2121
messages Messages
2222
context string
23+
additionalContexts map[string]string
2324
dockerfilePath string
2425
dockerfileContents string
2526
}
2627

27-
func NewFileServer(messages Messages, context, dockerfilePath, dockerFileContents string) *FileServer {
28+
func NewFileServer(messages Messages, context string, additionalContexts map[string]string, dockerfilePath, dockerFileContents string) *FileServer {
2829
return &FileServer{
2930
messages: messages,
3031
context: context,
32+
additionalContexts: additionalContexts,
3133
dockerfilePath: dockerfilePath,
3234
dockerfileContents: dockerFileContents,
3335
}
@@ -49,6 +51,7 @@ func (f *FileServer) DiffCopy(server filesync.FileSync_DiffCopyServer) error {
4951
SyncOptions: &SyncOptions{
5052
Compress: true,
5153
Context: f.context,
54+
AdditionalContexts: f.additionalContexts,
5255
Dockerfile: f.dockerfilePath,
5356
DockerfileContents: f.dockerfileContents,
5457
OverrideExcludes: ctx.Get(keyOverrideExcludes),
@@ -77,8 +80,8 @@ func (f *FileServer) DiffCopy(server filesync.FileSync_DiffCopyServer) error {
7780
}()
7881

7982
for msg := range msgs {
80-
logrus.Tracef("file sync message msg.FileSessionID=%s sessionID=%s close=%v", msg.FileSessionID, sessionID, msg.FileSessionClose)
8183
if msg.FileSessionID == sessionID {
84+
logrus.Tracef("file sync message msg.FileSessionID=%s sessionID=%s close=%v", msg.FileSessionID, sessionID, msg.FileSessionClose)
8285
if msg.FileSessionClose {
8386
cancel()
8487
} else {

pkg/buildclient/filesyncclient.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func createFileMapInput(cwd string, opts *SyncOptions) (string, map[string]strin
8989
err error
9090
context = filepath.Join(cwd, opts.Context)
9191
dockerfile = filepath.Dir(filepath.Join(cwd, opts.Dockerfile))
92+
dirs = map[string]string{}
9293
)
9394
if opts.DockerfileContents != "" {
9495
tempDir, err = os.MkdirTemp("", "acorn")
@@ -101,10 +102,17 @@ func createFileMapInput(cwd string, opts *SyncOptions) (string, map[string]strin
101102
return "", nil, err
102103
}
103104
}
104-
return tempDir, map[string]string{
105-
"context": context,
106-
"dockerfile": dockerfile,
107-
}, nil
105+
106+
if len(opts.DirName) > 0 {
107+
if opts.DirName[0] == "context" {
108+
dirs["context"] = context
109+
} else if dir, ok := opts.AdditionalContexts[opts.DirName[0]]; ok {
110+
dirs[opts.DirName[0]] = filepath.Join(cwd, dir)
111+
}
112+
}
113+
114+
dirs["dockerfile"] = dockerfile
115+
return tempDir, dirs, nil
108116
}
109117

110118
func prepareSyncedDirs(localDirs map[string]string, dirNames []string, followPaths []string) (filesync.StaticDirSource, error) {
@@ -122,7 +130,7 @@ func prepareSyncedDirs(localDirs map[string]string, dirNames []string, followPat
122130
return nil, fmt.Errorf("%s not a directory", d)
123131
}
124132
for _, dirName := range dirNames {
125-
if dirName == "context" && localDirName == dirName {
133+
if dirName != "dockerfile" && localDirName == dirName {
126134
for _, followPath := range followPaths {
127135
if ignoreLocalFiles[followPath] {
128136
continue
@@ -204,10 +212,10 @@ func (s *fileSyncClient) RecvMsg(m interface{}) error {
204212
if !ok {
205213
return io.EOF
206214
}
207-
logrus.Tracef("fileSyncClient msg.fileSessionID=%s sessionID=%s packetNil=%v", nextMessage.FileSessionID, s.sessionID, nextMessage.Packet == nil)
208215
if nextMessage.Packet == nil || nextMessage.FileSessionID != s.sessionID {
209216
continue
210217
}
218+
logrus.Tracef("fileSyncClient msg.fileSessionID=%s sessionID=%s packetNil=%v", nextMessage.FileSessionID, s.sessionID, nextMessage.Packet == nil)
211219
n := m.(*types.Packet)
212220
*n = *nextMessage.Packet
213221
return nil

pkg/buildclient/messages.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (m *Message) String() string {
132132

133133
type SyncOptions struct {
134134
Context string
135+
AdditionalContexts map[string]string
135136
Dockerfile string
136137
DockerfileContents string
137138

pkg/openapi/generated/openapi_generated.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)