Skip to content

Commit b6bfb2d

Browse files
authored
Allow host mounts for build phahse (knative#2753)
* Allow host mounts for build pahse. This is in particular useful for using paket bindings, e.g. to inject git credentials into build process. Signed-off-by: Matej Vašek <mvasek@redhat.com> * Mark test as a integration test Signed-off-by: Matej Vašek <mvasek@redhat.com> * fixup: style Signed-off-by: Matej Vašek <mvasek@redhat.com> --------- Signed-off-by: Matej Vašek <mvasek@redhat.com>
1 parent c7a95dd commit b6bfb2d

4 files changed

Lines changed: 63 additions & 1 deletion

File tree

pkg/builders/builders_int_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build integration
2+
13
package builders_test
24

35
import (
@@ -35,7 +37,6 @@ import (
3537
)
3638

3739
func TestPrivateGitRepository(t *testing.T) {
38-
t.Skip("tested functionality not implemented yet")
3940

4041
ctx, cancel := context.WithCancel(context.Background())
4142
defer cancel()
@@ -65,6 +66,21 @@ func TestPrivateGitRepository(t *testing.T) {
6566
t.Fatal(ctx.Err())
6667
}
6768

69+
gitCredsDir := t.TempDir()
70+
err := os.WriteFile(filepath.Join(gitCredsDir, "type"), []byte(`git-credentials`), 0600)
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
75+
gitCred := `url=https://git-private.127.0.0.1.sslip.io
76+
username=developer
77+
password=nbusr123
78+
`
79+
err = os.WriteFile(filepath.Join(gitCredsDir, "credentials"), []byte(gitCred), 0600)
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
6884
builder := buildpacks.NewBuilder(buildpacks.WithVerbose(true))
6985
f, err := fn.NewFunction(filepath.Join("testdata", "go-fn-with-private-deps"))
7086
if err != nil {
@@ -73,6 +89,14 @@ func TestPrivateGitRepository(t *testing.T) {
7389
f.Build.Image = "localhost:50000/go-app:test"
7490
f.Build.Builder = "pack"
7591
f.Build.BuilderImages = map[string]string{"pack": builderImage}
92+
f.Build.BuildEnvs = []fn.Env{{
93+
Name: ptr("SERVICE_BINDING_ROOT"),
94+
Value: ptr("/bindings"),
95+
}}
96+
f.Build.Mounts = []fn.MountSpec{{
97+
Source: gitCredsDir,
98+
Destination: "/bindings/git-binding",
99+
}}
76100
err = builder.Build(ctx, f, nil)
77101
if err != nil {
78102
t.Fatal(err)

pkg/builders/buildpacks/builder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
179179
opts.ContainerConfig.Network = "host"
180180
}
181181

182+
var bindings = make([]string, 0, len(f.Build.Mounts))
183+
for _, m := range f.Build.Mounts {
184+
bindings = append(bindings, fmt.Sprintf("%s:%s", m.Source, m.Destination))
185+
}
186+
opts.ContainerConfig.Volumes = bindings
187+
182188
// only trust our known builders
183189
opts.TrustBuilder = TrustBuilder
184190

pkg/functions/function.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ type BuildSpec struct {
148148
// Image stores last built image name NOT in func.yaml, but instead
149149
// in .func/built-image
150150
Image string `yaml:"-"`
151+
152+
// Mounts used in build phase. This is useful in particular for paketo bindings.
153+
Mounts []MountSpec `yaml:"mounts,omitempty"`
154+
}
155+
156+
type MountSpec struct {
157+
Source string `yaml:"source"`
158+
Destination string `yaml:"destination"`
151159
}
152160

153161
// RunSpec

schema/func_yaml-schema.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
"remoteStorageClass": {
4949
"type": "string",
5050
"description": "RemoteStorageClass specifies the storage class to use for the volume used\non-cluster during when built remotely."
51+
},
52+
"mounts": {
53+
"items": {
54+
"$schema": "http://json-schema.org/draft-04/schema#",
55+
"$ref": "#/definitions/MountSpec"
56+
},
57+
"type": "array",
58+
"description": "Mounts used in build phase. This is useful in particular for paketo bindings."
5159
}
5260
},
5361
"additionalProperties": false,
@@ -269,6 +277,22 @@
269277
"additionalProperties": false,
270278
"type": "object"
271279
},
280+
"MountSpec": {
281+
"required": [
282+
"source",
283+
"destination"
284+
],
285+
"properties": {
286+
"source": {
287+
"type": "string"
288+
},
289+
"destination": {
290+
"type": "string"
291+
}
292+
},
293+
"additionalProperties": false,
294+
"type": "object"
295+
},
272296
"Options": {
273297
"properties": {
274298
"scale": {

0 commit comments

Comments
 (0)