From ac08d4605772fadbf461a04bc4ee1dc0d0ceeadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Va=C5=A1ek?= Date: Wed, 19 Mar 2025 20:54:35 +0100 Subject: [PATCH 1/3] Allow host mounts for build pahse. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is in particular useful for using paket bindings, e.g. to inject git credentials into build process. Signed-off-by: Matej Vašek --- pkg/builders/builders_int_test.go | 24 +++++++++++++++++++++++- pkg/builders/buildpacks/builder.go | 6 ++++++ pkg/functions/function.go | 8 ++++++++ schema/func_yaml-schema.json | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/pkg/builders/builders_int_test.go b/pkg/builders/builders_int_test.go index cc222684a6..0ef622726d 100644 --- a/pkg/builders/builders_int_test.go +++ b/pkg/builders/builders_int_test.go @@ -35,7 +35,6 @@ import ( ) func TestPrivateGitRepository(t *testing.T) { - t.Skip("tested functionality not implemented yet") ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -65,6 +64,21 @@ func TestPrivateGitRepository(t *testing.T) { t.Fatal(ctx.Err()) } + gitCredsDir := t.TempDir() + err := os.WriteFile(filepath.Join(gitCredsDir, "type"), []byte(`git-credentials`), 0600) + if err != nil { + t.Fatal(err) + } + + gitCred := `url=https://git-private.127.0.0.1.sslip.io +username=developer +password=nbusr123 +` + err = os.WriteFile(filepath.Join(gitCredsDir, "credentials"), []byte(gitCred), 0600) + if err != nil { + t.Fatal(err) + } + builder := buildpacks.NewBuilder(buildpacks.WithVerbose(true)) f, err := fn.NewFunction(filepath.Join("testdata", "go-fn-with-private-deps")) if err != nil { @@ -73,6 +87,14 @@ func TestPrivateGitRepository(t *testing.T) { f.Build.Image = "localhost:50000/go-app:test" f.Build.Builder = "pack" f.Build.BuilderImages = map[string]string{"pack": builderImage} + f.Build.BuildEnvs = []fn.Env{{ + Name: ptr("SERVICE_BINDING_ROOT"), + Value: ptr("/bindings"), + }} + f.Build.Mounts = []fn.MountSpec{{ + Source: gitCredsDir, + Destination: "/bindings/git-binding", + }} err = builder.Build(ctx, f, nil) if err != nil { t.Fatal(err) diff --git a/pkg/builders/buildpacks/builder.go b/pkg/builders/buildpacks/builder.go index 83c6ba8510..c443f18bd5 100644 --- a/pkg/builders/buildpacks/builder.go +++ b/pkg/builders/buildpacks/builder.go @@ -178,6 +178,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf if runtime.GOOS == "linux" { opts.ContainerConfig.Network = "host" } + + var bindings = make([]string, 0, len(f.Build.Mounts)) + for _, m := range f.Build.Mounts { + bindings = append(bindings, fmt.Sprintf("%s:%s", m.Source, m.Destination)) + } + opts.ContainerConfig.Volumes = bindings // only trust our known builders opts.TrustBuilder = TrustBuilder diff --git a/pkg/functions/function.go b/pkg/functions/function.go index d5a79a4ab8..47c1bdb50b 100644 --- a/pkg/functions/function.go +++ b/pkg/functions/function.go @@ -148,6 +148,14 @@ type BuildSpec struct { // Image stores last built image name NOT in func.yaml, but instead // in .func/built-image Image string `yaml:"-"` + + // Mounts used in build phase. This is useful in particular for paketo bindings. + Mounts []MountSpec `yaml:"mounts,omitempty"` +} + +type MountSpec struct { + Source string `yaml:"source"` + Destination string `yaml:"destination"` } // RunSpec diff --git a/schema/func_yaml-schema.json b/schema/func_yaml-schema.json index 633336d914..74504ec390 100644 --- a/schema/func_yaml-schema.json +++ b/schema/func_yaml-schema.json @@ -48,6 +48,14 @@ "remoteStorageClass": { "type": "string", "description": "RemoteStorageClass specifies the storage class to use for the volume used\non-cluster during when built remotely." + }, + "mounts": { + "items": { + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/MountSpec" + }, + "type": "array", + "description": "Mounts used in build phase. This is useful in particular for paketo bindings." } }, "additionalProperties": false, @@ -269,6 +277,22 @@ "additionalProperties": false, "type": "object" }, + "MountSpec": { + "required": [ + "source", + "destination" + ], + "properties": { + "source": { + "type": "string" + }, + "destination": { + "type": "string" + } + }, + "additionalProperties": false, + "type": "object" + }, "Options": { "properties": { "scale": { From 1c3ac0a4759348b4977b161bfac34032be637e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Va=C5=A1ek?= Date: Thu, 20 Mar 2025 15:18:17 +0100 Subject: [PATCH 2/3] Mark test as a integration test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Vašek --- pkg/builders/builders_int_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/builders/builders_int_test.go b/pkg/builders/builders_int_test.go index 0ef622726d..b64db9c8d1 100644 --- a/pkg/builders/builders_int_test.go +++ b/pkg/builders/builders_int_test.go @@ -1,3 +1,5 @@ +//go:build integration + package builders_test import ( From 36adf39d4cac15fc189d11bd160f19105dbef647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Va=C5=A1ek?= Date: Thu, 20 Mar 2025 15:27:03 +0100 Subject: [PATCH 3/3] fixup: style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Vašek --- pkg/builders/buildpacks/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/builders/buildpacks/builder.go b/pkg/builders/buildpacks/builder.go index c443f18bd5..ab6f24cb40 100644 --- a/pkg/builders/buildpacks/builder.go +++ b/pkg/builders/buildpacks/builder.go @@ -178,7 +178,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf if runtime.GOOS == "linux" { opts.ContainerConfig.Network = "host" } - + var bindings = make([]string, 0, len(f.Build.Mounts)) for _, m := range f.Build.Mounts { bindings = append(bindings, fmt.Sprintf("%s:%s", m.Source, m.Destination))