Skip to content

Commit b4a4209

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

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

pkg/builders/buildpacks/builder.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ 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"
33+
var DefaultBaseBuilder = "quay.io/dfridric/custom-jammy-base:latest"
3334

35+
var DefaultTinyBuilder = "quay.io/dfridric/custom-jammy-tiny:latest"
3436
var (
3537
DefaultBuilderImages = map[string]string{
3638
"node": DefaultBaseBuilder,
@@ -134,7 +136,6 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
134136
if len(buildpacks) == 0 {
135137
buildpacks = defaultBuildpacks[f.Runtime]
136138
}
137-
138139
// Reading .funcignore file
139140
var excludes []string
140141
filePath := filepath.Join(f.Root, ".funcignore")
@@ -171,6 +172,16 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
171172
Volumes []string
172173
}{Network: "", Volumes: nil},
173174
}
175+
176+
// NOTE: gauron99 - this might be even created into a Client function and
177+
// ran before the client.Build() all together (in the CLI). There are gonna
178+
// be commonalitites across the builders for scaffolding with some nuances
179+
// which could be handled by each "scaffolder" - similarly to builders.
180+
// scaffold
181+
if err = scaffold(f); err != nil {
182+
return
183+
}
184+
174185
if b.withTimestamp {
175186
now := time.Now()
176187
opts.CreationTime = &now
@@ -186,6 +197,11 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
186197
opts.Env["BPE_DEFAULT_LISTEN_ADDRESS"] = "[::]:8080"
187198
}
188199

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

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)