Skip to content

Commit 1acbe43

Browse files
committed
feat:scaffolding-go
1 parent d3a783c commit 1acbe43

File tree

8 files changed

+4469
-4413
lines changed

8 files changed

+4469
-4413
lines changed

cmd/func-util/main.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,38 @@ func scaffold(ctx context.Context) error {
9797
}
9898

9999
appRoot := filepath.Join(f.Root, ".s2i", "builds", "last")
100+
if f.Build.Builder != "s2i" {
101+
// TODO: gauron99 - change this completely
102+
// non-s2i override
103+
appRoot = filepath.Join(f.Root, ".func", "builds", "last")
104+
}
105+
fmt.Printf("appRoot is '%s'\n", appRoot)
100106
_ = os.RemoveAll(appRoot)
101107

108+
// build step now includes scaffolding for go-pack
102109
err = scaffolding.Write(appRoot, f.Root, f.Runtime, f.Invoke, embeddedRepo.FS())
103110
if err != nil {
104111
return fmt.Errorf("cannot write the scaffolding: %w", err)
105112
}
106113

107-
if err := os.MkdirAll(filepath.Join(f.Root, ".s2i", "bin"), 0755); err != nil {
108-
return fmt.Errorf("unable to create .s2i bin dir. %w", err)
109-
}
110-
111-
var asm string
112-
switch f.Runtime {
113-
case "go":
114-
asm = s2i.GoAssembler
115-
case "python":
116-
asm = s2i.PythonAssembler
117-
default:
118-
panic("unreachable")
119-
}
114+
if f.Build.Builder == "s2i" {
115+
if err := os.MkdirAll(filepath.Join(f.Root, ".s2i", "bin"), 0755); err != nil {
116+
return fmt.Errorf("unable to create .s2i bin dir. %w", err)
117+
}
118+
var asm string
119+
switch f.Runtime {
120+
case "go":
121+
asm = s2i.GoAssembler
122+
case "python":
123+
asm = s2i.PythonAssembler
124+
default:
125+
panic("unreachable")
126+
}
120127

121-
if err := os.WriteFile(filepath.Join(f.Root, ".s2i", "bin", "assemble"), []byte(asm), 0755); err != nil {
122-
return fmt.Errorf("unable to write go assembler. %w", err)
128+
if err := os.WriteFile(filepath.Join(f.Root, ".s2i", "bin", "assemble"), []byte(asm), 0755); err != nil {
129+
return fmt.Errorf("unable to write go assembler. %w", err)
130+
}
123131
}
124-
125132
return nil
126133
}
127134

generate/zz_filesystem_generated.go

Lines changed: 4384 additions & 4385 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/builders/buildpacks/builder.go

Lines changed: 50 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 = "quay.io/dfridric/knative/builder-jammy-base:latest"
33+
var DefaultTinyBuilder = "quay.io/dfridric/knative/builder-jammy-tiny:latest"
3334

3435
var (
3536
DefaultBuilderImages = map[string]string{
@@ -46,6 +47,7 @@ var (
4647
// Ensure that all entries in this list are terminated with a trailing "/"
4748
// See GHSA-5336-2g3f-9g3m for details
4849
trustedBuilderImagePrefixes = []string{
50+
"quay.io/dfridric/",
4951
"quay.io/boson/",
5052
"gcr.io/paketo-buildpacks/",
5153
"docker.io/paketobuildpacks/",
@@ -116,7 +118,7 @@ func WithTimestamp(v bool) Option {
116118
}
117119
}
118120

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

121123
// Build the Function at path.
122124
func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platform) (err error) {
@@ -171,6 +173,16 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
171173
Volumes []string
172174
}{Network: "", Volumes: nil},
173175
}
176+
177+
// NOTE: gauron99 - this might be even created into a Client function and
178+
// ran before the client.Build() all together (in the CLI). There are gonna
179+
// be commonalitites across the builders for scaffolding with some nuances
180+
// which could be handled by each "scaffolder" - similarly to builders.
181+
// scaffold
182+
if err = scaffold(f); err != nil {
183+
return
184+
}
185+
174186
if b.withTimestamp {
175187
now := time.Now()
176188
opts.CreationTime = &now
@@ -186,6 +198,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
186198
opts.Env["BPE_DEFAULT_LISTEN_ADDRESS"] = "[::]:8080"
187199
}
188200

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

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/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/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)

templates/springboot/manifest.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ buildEnvs:
33
value: "false"
44
- name: BP_JVM_VERSION
55
value: "21"
6-
- name: BP_MAVEN_ACTIVE_PROFILES
7-
value: "native"
86
healthEndpoints:
97
liveness: /actuator/health
108
readiness: /actuator/health

test/oncluster/nodejs_helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
// params, which is handy for test assertions.
1313
func WriteNewSimpleIndexJS(t *testing.T, nodeJsFuncProjectDir string, withBodyReturning string) {
1414
indexJsContent := fmt.Sprintf(`
15-
function invoke(context) {
16-
return { body: '%v' }
15+
const invoke = async (context, body) => {
16+
return { body: '%v' }
1717
}
18-
module.exports = invoke;
18+
module.exports = { invoke };
1919
`, withBodyReturning)
2020

2121
err := os.WriteFile(filepath.Join(nodeJsFuncProjectDir, "index.js"), []byte(indexJsContent), 0644)

0 commit comments

Comments
 (0)