Skip to content

Commit 535b772

Browse files
committed
feat:scaffolding-go
1 parent c1b9a38 commit 535b772

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

pkg/builders/buildpacks/builder.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ 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

31-
var DefaultBaseBuilder = "ghcr.io/knative/builder-jammy-base:latest"
32-
var DefaultTinyBuilder = "ghcr.io/knative/builder-jammy-tiny:latest"
32+
var DefaultBaseBuilder = "localhost:5000/knative/builder-jammy-base:latest"
3333

34+
var DefaultTinyBuilder = "paketobuildpacks/builder-jammy-tiny:latest"
3435
var (
3536
DefaultBuilderImages = map[string]string{
3637
"node": DefaultBaseBuilder,
@@ -134,7 +135,6 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
134135
if len(buildpacks) == 0 {
135136
buildpacks = defaultBuildpacks[f.Runtime]
136137
}
137-
138138
// Reading .funcignore file
139139
var excludes []string
140140
filePath := filepath.Join(f.Root, ".funcignore")
@@ -171,6 +171,16 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
171171
Volumes []string
172172
}{Network: "", Volumes: nil},
173173
}
174+
175+
// NOTE: gauron99 - this might be even created into a Client function and
176+
// ran before the client.Build() all together (in the CLI). There are gonna
177+
// be commonalitites across the builders for scaffolding with some nuances
178+
// which could be handled by each "scaffolder" - similarly to builders.
179+
// scaffold
180+
if err = scaffold(f); err != nil {
181+
return
182+
}
183+
174184
if b.withTimestamp {
175185
now := time.Now()
176186
opts.CreationTime = &now
@@ -186,6 +196,11 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
186196
opts.Env["BPE_DEFAULT_LISTEN_ADDRESS"] = "[::]:8080"
187197
}
188198

199+
// go specific workdir set to where main is
200+
if _, ok := opts.Env["BP_GO_WORKDIR"]; !ok {
201+
opts.Env["BP_GO_WORKDIR"] = ".func/builds/last"
202+
}
203+
189204
var bindings = make([]string, 0, len(f.Build.Mounts))
190205
for _, m := range f.Build.Mounts {
191206
bindings = append(bindings, fmt.Sprintf("%s:%s", m.Source, m.Destination))
@@ -312,3 +327,32 @@ type ErrRuntimeNotSupported struct {
312327
func (e ErrRuntimeNotSupported) Error() string {
313328
return fmt.Sprintf("Pack builder has no default builder image for the '%v' language runtime. Please provide one.", e.Runtime)
314329
}
330+
331+
// TODO: gauron99 - unify this with other builders; temporary for the go pack
332+
//
333+
// scaffold the project
334+
func scaffold(f fn.Function) error {
335+
// Scafffolding is currently only supported by the Go runtime
336+
// Python currently uses an injector instead of this
337+
if f.Runtime != "go" {
338+
return nil
339+
}
340+
341+
contextDir := filepath.Join(".func", "builds", "last")
342+
appRoot := filepath.Join(f.Root, contextDir)
343+
_ = os.RemoveAll(appRoot)
344+
345+
// The embedded repository contains the scaffolding code itself which glues
346+
// together the middleware and a function via main
347+
embeddedRepo, err := fn.NewRepository("", "") // default is the embedded fs
348+
if err != nil {
349+
return fmt.Errorf("unable to load the embedded scaffolding. %w", err)
350+
}
351+
352+
// Write scaffolding to .func/builds/last
353+
err = scaffolding.Write(appRoot, f.Root, f.Runtime, f.Invoke, embeddedRepo.FS())
354+
if err != nil {
355+
return fmt.Errorf("unable to build due to a scaffold error. %w", err)
356+
}
357+
return nil
358+
}

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

0 commit comments

Comments
 (0)