Skip to content

Commit 31ad9d7

Browse files
committed
go scaffolding and small upgrades
1 parent 5d86d20 commit 31ad9d7

File tree

8 files changed

+88
-13
lines changed

8 files changed

+88
-13
lines changed

cmd/func-util/main.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,31 @@ func socat(ctx context.Context) error {
7474
}
7575

7676
func scaffold(ctx context.Context) error {
77-
78-
if len(os.Args) != 2 {
79-
return fmt.Errorf("expected exactly one positional argument (function project path)")
77+
if len(os.Args) != 3 {
78+
return fmt.Errorf("expected exactly two positional arguments (function project path, builder)")
8079
}
8180

8281
path := os.Args[1]
82+
builder := os.Args[2]
8383

8484
f, err := fn.NewFunction(path)
8585
if err != nil {
8686
return fmt.Errorf("cannot load func project: %w", err)
8787
}
8888

89+
// TODO: gauron99 - Set the builder from the passed argument if not already set in the function config.
90+
// This is necessary because the builder value needs to be known during scaffolding to
91+
// determine the correct build directory (.s2i/builds/last vs .func/builds/last), but it
92+
// may not be persisted to func.yaml yet. By passing it as an argument from the Tekton
93+
// pipeline, we ensure the correct builder is used even when the function config is incomplete.
94+
if f.Build.Builder == "" {
95+
f.Build.Builder = builder
96+
}
97+
98+
fmt.Printf("#### scaffold: builder='%s', runtime='%s'\n", f.Build.Builder, f.Runtime)
99+
89100
if f.Runtime != "go" && f.Runtime != "python" {
90-
// Scaffolding is for now supported/needed only for Go.
101+
// Scaffolding is for now supported/needed only for Go&Python
91102
return nil
92103
}
93104

@@ -97,13 +108,25 @@ func scaffold(ctx context.Context) error {
97108
}
98109

99110
appRoot := filepath.Join(f.Root, ".s2i", "builds", "last")
111+
if f.Build.Builder != "s2i" {
112+
// TODO: gauron99 - change this completely
113+
appRoot = filepath.Join(f.Root, ".func", "builds", "last")
114+
}
115+
fmt.Printf("appRoot is '%s'\n", appRoot)
100116
_ = os.RemoveAll(appRoot)
101117

118+
// build step now includes scaffolding for go-pack
102119
err = scaffolding.Write(appRoot, f.Root, f.Runtime, f.Invoke, embeddedRepo.FS())
103120
if err != nil {
104121
return fmt.Errorf("cannot write the scaffolding: %w", err)
105122
}
106123

124+
if f.Build.Builder != "s2i" {
125+
return nil
126+
}
127+
128+
// add s2i specific changes
129+
107130
if err := os.MkdirAll(filepath.Join(f.Root, ".s2i", "bin"), 0755); err != nil {
108131
return fmt.Errorf("unable to create .s2i bin dir. %w", err)
109132
}
@@ -121,7 +144,6 @@ func scaffold(ctx context.Context) error {
121144
if err := os.WriteFile(filepath.Join(f.Root, ".s2i", "bin", "assemble"), []byte(asm), 0755); err != nil {
122145
return fmt.Errorf("unable to write go assembler. %w", err)
123146
}
124-
125147
return nil
126148
}
127149

pkg/builders/buildpacks/builder.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ 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 = "ghcr.io/knative/builder-jammy-base:v2"
33+
var DefaultTinyBuilder = "ghcr.io/knative/builder-jammy-tiny:v2"
3334

3435
var (
3536
DefaultBuilderImages = map[string]string{
@@ -116,7 +117,7 @@ func WithTimestamp(v bool) Option {
116117
}
117118
}
118119

119-
var DefaultLifecycleImage = "docker.io/buildpacksio/lifecycle:553c041"
120+
var DefaultLifecycleImage = "docker.io/buildpacksio/lifecycle:3659764"
120121

121122
// Build the Function at path.
122123
func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platform) (err error) {
@@ -171,6 +172,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
171172
Volumes []string
172173
}{Network: "", Volumes: nil},
173174
}
175+
176+
// TODO: gauron99 this will be extracted into separate client.Scaffold method
177+
if err = scaffold(f); err != nil {
178+
return
179+
}
180+
174181
if b.withTimestamp {
175182
now := time.Now()
176183
opts.CreationTime = &now
@@ -186,6 +193,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
186193
opts.Env["BPE_DEFAULT_LISTEN_ADDRESS"] = "[::]:8080"
187194
}
188195

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

pkg/builders/buildpacks/builder_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestBuild_BuilderImageTrustedLocalhost(t *testing.T) {
5656
}
5757
}
5858

59-
// TestBuild_BuilderImageDefault ensures that a Function bing built which does not
59+
// TestBuild_BuilderImageDefault ensures that a Function being built which does not
6060
// define a Builder Image will get the internally-defined default.
6161
func TestBuild_BuilderImageDefault(t *testing.T) {
6262
var (
@@ -76,7 +76,6 @@ func TestBuild_BuilderImageDefault(t *testing.T) {
7676
if err := b.Build(context.Background(), f, nil); err != nil {
7777
t.Fatal(err)
7878
}
79-
8079
}
8180

8281
// TestBuild_BuildpacksDefault ensures that, if there are default buildpacks
@@ -100,7 +99,6 @@ func TestBuild_BuildpacksDefault(t *testing.T) {
10099
if err := b.Build(context.Background(), f, nil); err != nil {
101100
t.Fatal(err)
102101
}
103-
104102
}
105103

106104
// TestBuild_BuilderImageConfigurable ensures that the builder will use the builder

pkg/pipelines/tekton/tasks.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,16 @@ spec:
440440
- name: path
441441
description: Path to the function project
442442
default: ""
443+
- name: builder
444+
description: Builder to be used (pack or s2i)
445+
default: ""
443446
workspaces:
444447
- name: source
445448
description: The workspace containing the function project
446449
steps:
447450
- name: func-scaffold
448451
image: %s
449-
command: ["scaffold", "$(params.path)"]
452+
command: ["scaffold", "$(params.path)", "$(params.builder)"]
450453
`, ScaffoldImage)
451454
}
452455

pkg/pipelines/tekton/templates.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ func createAndApplyPipelineRunTemplate(f fn.Function, namespace string, labels m
413413
}
414414
}
415415

416+
// add BP_GO_WORKDIR for go-build buildpack
417+
if f.Runtime == "go" {
418+
buildEnvs = append(buildEnvs, "BP_GO_WORKDIR=.func/builds/last")
419+
}
420+
416421
s2iImageScriptsUrl := defaultS2iImageScriptsUrl
417422
if f.Runtime == "quarkus" {
418423
s2iImageScriptsUrl = quarkusS2iImageScriptsUrl

pkg/pipelines/tekton/templates_pack.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ spec:
4646
params:
4747
- name: path
4848
value: $(workspaces.source.path)/$(params.contextDir)
49+
- name: builder
50+
value: pack
4951
workspaces:
5052
- name: source
5153
workspace: source-workspace

pkg/pipelines/tekton/templates_s2i.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ spec:
5454
params:
5555
- name: path
5656
value: $(workspaces.source.path)/$(params.contextDir)
57+
- name: builder
58+
value: s2i
5759
workspaces:
5860
- name: source
5961
workspace: source-workspace

pkg/scaffolding/scaffold.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
// fs: filesytem which contains scaffolding at '[runtime]/scaffolding'
3232
// (exclusive with 'repo')
3333
func Write(out, src, runtime, invoke string, fs filesystem.Filesystem) (err error) {
34-
34+
fmt.Println("#### scaffolding.Write")
3535
// detect the signature of the source code in the given location, presuming
3636
// a runtime and invocation hint (default "http")
3737
s, err := detectSignature(src, runtime, invoke)

0 commit comments

Comments
 (0)