Skip to content

Commit 69ccef6

Browse files
committed
feat:scaffolding-go
1 parent c1b9a38 commit 69ccef6

File tree

11 files changed

+62
-5
lines changed

11 files changed

+62
-5
lines changed

pkg/builders/buildpacks/builder.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@ import (
2323
"knative.dev/func/pkg/builders"
2424
"knative.dev/func/pkg/docker"
2525
fn "knative.dev/func/pkg/functions"
26+
"knative.dev/func/pkg/scaffolding"
2627
)
2728

2829
// DefaultName when no WithName option is provided to NewBuilder
2930
const DefaultName = builders.Pack
3031

3132
var DefaultBaseBuilder = "ghcr.io/knative/builder-jammy-base:latest"
33+
34+
// var DefaultGoBaseBuilder = "paketobuildpacks/builder-jammy-base:latest"
35+
3236
var DefaultTinyBuilder = "ghcr.io/knative/builder-jammy-tiny:latest"
37+
var DefaultGoTinyBuilder = "paketobuildpacks/builder-jammy-tiny:latest"
3338

3439
var (
3540
DefaultBuilderImages = map[string]string{
3641
"node": DefaultBaseBuilder,
3742
"nodejs": DefaultBaseBuilder,
3843
"typescript": DefaultBaseBuilder,
39-
"go": DefaultTinyBuilder,
44+
"go": DefaultGoTinyBuilder,
4045
"python": DefaultBaseBuilder,
4146
"quarkus": DefaultTinyBuilder,
4247
"rust": DefaultBaseBuilder,
@@ -134,7 +139,6 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
134139
if len(buildpacks) == 0 {
135140
buildpacks = defaultBuildpacks[f.Runtime]
136141
}
137-
138142
// Reading .funcignore file
139143
var excludes []string
140144
filePath := filepath.Join(f.Root, ".funcignore")
@@ -171,6 +175,16 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
171175
Volumes []string
172176
}{Network: "", Volumes: nil},
173177
}
178+
179+
// NOTE: gauron99 - this might be even created into a Client function and
180+
// ran before the client.Build() all together (in the CLI). There are gonna
181+
// be commonalitites across the builders for scaffolding with some nuances
182+
// which could be handled by each "scaffolder" - similarly to builders.
183+
// scaffold
184+
if err = scaffold(f); err != nil {
185+
return
186+
}
187+
174188
if b.withTimestamp {
175189
now := time.Now()
176190
opts.CreationTime = &now
@@ -186,6 +200,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
186200
opts.Env["BPE_DEFAULT_LISTEN_ADDRESS"] = "[::]:8080"
187201
}
188202

203+
if f.Runtime == "go" {
204+
if _, ok := opts.Env["BP_GO_WORKDIR"]; !ok {
205+
opts.Env["BP_GO_WORKDIR"] = ".func/builds/last"
206+
}
207+
}
208+
189209
var bindings = make([]string, 0, len(f.Build.Mounts))
190210
for _, m := range f.Build.Mounts {
191211
bindings = append(bindings, fmt.Sprintf("%s:%s", m.Source, m.Destination))
@@ -312,3 +332,34 @@ type ErrRuntimeNotSupported struct {
312332
func (e ErrRuntimeNotSupported) Error() string {
313333
return fmt.Sprintf("Pack builder has no default builder image for the '%v' language runtime. Please provide one.", e.Runtime)
314334
}
335+
336+
// TODO: gauron99 - unify this with other builders; temporary for the go pack
337+
// to work for release.
338+
//
339+
// scaffold the project
340+
// Returns a config with settings suitable for building runtimes which
341+
// support scaffolding.
342+
func scaffold(f fn.Function) error {
343+
// Scafffolding is currently only supported by the Go and Python runtimes
344+
if f.Runtime != "go" && f.Runtime != "python" {
345+
return nil
346+
}
347+
348+
contextDir := filepath.Join(".func", "builds", "last")
349+
appRoot := filepath.Join(f.Root, contextDir)
350+
_ = os.RemoveAll(appRoot)
351+
352+
// The enbedded repository contains the scaffolding code itself which glues
353+
// together the middleware and a function via main
354+
embeddedRepo, err := fn.NewRepository("", "") // default is the embedded fs
355+
if err != nil {
356+
return fmt.Errorf("unable to load the embedded scaffolding. %w", err)
357+
}
358+
359+
// Write scaffolding to .func/builds/last
360+
err = scaffolding.Write(appRoot, f.Root, f.Runtime, f.Invoke, embeddedRepo.FS())
361+
if err != nil {
362+
return fmt.Errorf("unable to build due to a scaffold error. %w", err)
363+
}
364+
return nil
365+
}

pkg/builders/buildpacks/builder_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ func TestBuild_BuildpacksDefault(t *testing.T) {
8686
var (
8787
i = &mockImpl{}
8888
b = NewBuilder(WithImpl(i))
89-
f = fn.Function{Runtime: "go"}
89+
f = fn.Function{Runtime: "node"}
9090
)
9191

9292
i.BuildFn = func(ctx context.Context, opts pack.BuildOptions) error {
93-
expected := defaultBuildpacks["go"]
93+
expected := defaultBuildpacks["node"]
9494
if !reflect.DeepEqual(expected, opts.Buildpacks) {
9595
t.Fatalf("expected buildpacks '%v', got '%v'", expected, opts.Buildpacks)
9696
}
@@ -141,7 +141,7 @@ func TestBuild_BuilderImageExclude(t *testing.T) {
141141
b = NewBuilder( // Func Builder logic
142142
WithName(builders.Pack), WithImpl(i))
143143
f = fn.Function{
144-
Runtime: "go",
144+
Runtime: "node",
145145
}
146146
)
147147
funcIgnoreContent := []byte(`#testing comments

pkg/creds/auth.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"credsStore": ""
3+
}

pkg/functions/testdata/default_home/.config/go/telemetry/local/upload.token

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/var/example/absolute/link

0 commit comments

Comments
 (0)