Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 55afcc1

Browse files
committed
chore(python, java): Extract common buildpack code
Signed-off-by: Andrew Meyer <[email protected]>
1 parent 86c3a76 commit 55afcc1

27 files changed

+2240
-580
lines changed

buildpacks/Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ buildpack.path := $(abspath $(path))
99
include $(buildpack.path)/rules.mk
1010

1111
# Include test case images
12-
buildpack.languages = \
12+
buildpack.subdirs = \
13+
common \
1314
python \
1415
java \
1516

16-
SUBDIRS := $(addprefix $(buildpack.path)/, $(buildpack.languages))
17+
SUBDIRS := $(addprefix $(buildpack.path)/, $(buildpack.subdirs))
1718
$(foreach dir,$(SUBDIRS),$(eval $(call INCLUDE_FILE, $(dir))))
1819
endif
1920

@@ -28,5 +29,5 @@ all .PHONY: buildpacks
2829
buildpacks.clean:
2930
clean .PHONY: buildpacks.clean
3031

31-
buildpacks.test:
32+
buildpacks.tests:
3233
tests .PHONY: buildpacks.clean

buildpacks/common/Makefile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
RULES.MK ?= ../../rules.mk
2+
include $(RULES.MK)
3+
4+
path ?= .
5+
buildpacks.common.path := $(abspath $(path))
6+
7+
include $(buildpacks.common.path)/../rules.mk
8+
9+
#########
10+
# Testing
11+
#########
12+
13+
buildpacks.common.tests.sources := \
14+
$(buildpacks.common.path)/go.mod \
15+
$(buildpacks.common.path)/go.sum \
16+
$(shell find '$(buildpacks.common.path)/tests' -type f -iname '*.go') \
17+
$(buildpacks.common.path)/tests \
18+
19+
#########
20+
# Targets
21+
#########
22+
23+
buildpacks.common.tests: $(buildpacks.common.tests.sources)
24+
cd $(buildpacks.common.path) && go test -v -count=1 -timeout 30s kn-fn/buildpacks/tests
25+
26+
buildpacks.tests .PHONY: buildpacks.common.tests

buildpacks/common/command/runner.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021-2022 VMware, Inc.
2+
// SPDX-License-Identifier: BSD-2-Clause
3+
4+
package command
5+
6+
import (
7+
"os/exec"
8+
)
9+
10+
//go:generate mockgen -destination ../mock_command/runner.go . Runner
11+
type Runner interface {
12+
Run(cmd *exec.Cmd) (output string, err error)
13+
}
14+
15+
type DefaultRunner struct{}
16+
17+
func NewDefaultRunner() *DefaultRunner {
18+
return &DefaultRunner{}
19+
}
20+
21+
func (dcr *DefaultRunner) Run(cmd *exec.Cmd) (output string, err error) {
22+
buff, err := cmd.CombinedOutput()
23+
return string(buff), err
24+
}

buildpacks/java/java/func_yaml.go buildpacks/common/config/func_yaml.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2021-2022 VMware, Inc.
22
// SPDX-License-Identifier: BSD-2-Clause
33

4-
package java
4+
package config
55

66
import (
77
"os"

buildpacks/common/go.mod

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module kn-fn/buildpacks
2+
3+
go 1.19
4+
5+
require (
6+
github.com/paketo-buildpacks/libpak v1.61.0
7+
gopkg.in/yaml.v3 v3.0.1
8+
knative.dev/kn-plugin-func v0.19.0
9+
knative.dev/pkg v0.0.0-20210902173607-844a6bc45596
10+
)
11+
12+
require (
13+
github.com/BurntSushi/toml v1.1.0 // indirect
14+
github.com/Masterminds/semver/v3 v3.1.1 // indirect
15+
github.com/Microsoft/go-winio v0.5.0 // indirect
16+
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
17+
github.com/acomagu/bufpipe v1.0.3 // indirect
18+
github.com/buildpacks/libcnb v1.26.0 // indirect
19+
github.com/emirpasic/gods v1.12.0 // indirect
20+
github.com/go-git/gcfg v1.5.0 // indirect
21+
github.com/go-git/go-billy/v5 v5.3.1 // indirect
22+
github.com/go-git/go-git/v5 v5.4.2 // indirect
23+
github.com/gobuffalo/here v0.6.0 // indirect
24+
github.com/gogo/protobuf v1.3.2 // indirect
25+
github.com/heroku/color v0.0.6 // indirect
26+
github.com/imdario/mergo v0.3.13 // indirect
27+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
28+
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
29+
github.com/markbates/pkger v0.17.1 // indirect
30+
github.com/mattn/go-colorable v0.1.8 // indirect
31+
github.com/mattn/go-isatty v0.0.12 // indirect
32+
github.com/mitchellh/go-homedir v1.1.0 // indirect
33+
github.com/onsi/gomega v1.20.2 // indirect
34+
github.com/sergi/go-diff v1.1.0 // indirect
35+
github.com/xanzy/ssh-agent v0.3.0 // indirect
36+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
37+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
38+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
39+
gopkg.in/inf.v0 v0.9.1 // indirect
40+
gopkg.in/warnings.v0 v0.1.2 // indirect
41+
gopkg.in/yaml.v2 v2.4.0 // indirect
42+
k8s.io/apimachinery v0.20.7 // indirect
43+
)

buildpacks/common/go.sum

+1,919
Large diffs are not rendered by default.

buildpacks/java/tests/func_yaml_test.go buildpacks/common/tests/func_yaml_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
package tests
55

66
import (
7-
"kn-fn/java-function-buildpack/java"
87
"testing"
98

109
"gopkg.in/yaml.v3"
1110
knfn "knative.dev/kn-plugin-func"
1211
"knative.dev/pkg/ptr"
12+
13+
"kn-fn/buildpacks/config"
1314
)
1415

1516
func TestParseFuncYaml_FileDoesNotExist(t *testing.T) {
1617
appDir, cleanup := SetupTestDirectory()
1718
defer cleanup()
18-
result := java.ParseFuncYaml(appDir, NewLogger())
19+
result := config.ParseFuncYaml(appDir, NewLogger())
1920
if result.Exists {
2021
t.Logf("File should not exists but was detected")
2122
t.Fail()
@@ -25,7 +26,7 @@ func TestParseFuncYaml_FileDoesNotExist(t *testing.T) {
2526
func TestParseFuncYaml_FileExistsButEmpty(t *testing.T) {
2627
appDir, cleanup := SetupTestDirectory(WithFuncYaml())
2728
defer cleanup()
28-
result := java.ParseFuncYaml(appDir, NewLogger())
29+
result := config.ParseFuncYaml(appDir, NewLogger())
2930
if !result.Exists {
3031
t.Logf("File should exists but was not detected")
3132
t.Fail()
@@ -40,7 +41,7 @@ func TestParseFuncYaml_HasEnvs(t *testing.T) {
4041

4142
appDir, cleanup := SetupTestDirectory(WithFuncEnvs(envs))
4243
defer cleanup()
43-
result := java.ParseFuncYaml(appDir, NewLogger())
44+
result := config.ParseFuncYaml(appDir, NewLogger())
4445

4546
for k, v := range result.Envs {
4647
expected, found := envs[k]
@@ -67,7 +68,7 @@ func TestParseFuncYaml_HasScale(t *testing.T) {
6768

6869
appDir, cleanup := SetupTestDirectory(WithFuncScale(scaleOption))
6970
defer cleanup()
70-
result := java.ParseFuncYaml(appDir, NewLogger())
71+
result := config.ParseFuncYaml(appDir, NewLogger())
7172
resultScaleOptions := &knfn.ScaleOptions{}
7273
yaml.Unmarshal([]byte(result.Options["options-scale"]), resultScaleOptions)
7374

@@ -101,7 +102,7 @@ func TestParseFuncYaml_HasRequests(t *testing.T) {
101102

102103
appDir, cleanup := SetupTestDirectory(WithFuncResourceRequests(requestOptions))
103104
defer cleanup()
104-
result := java.ParseFuncYaml(appDir, NewLogger())
105+
result := config.ParseFuncYaml(appDir, NewLogger())
105106
resultRequestOptions := &knfn.ResourcesRequestsOptions{}
106107
yaml.Unmarshal([]byte(result.Options["options-resources-requests"]), resultRequestOptions)
107108

@@ -124,7 +125,7 @@ func TestParseFuncYaml_HasLimits(t *testing.T) {
124125

125126
appDir, cleanup := SetupTestDirectory(WithFuncResourceLimits(limitOptions))
126127
defer cleanup()
127-
result := java.ParseFuncYaml(appDir, NewLogger())
128+
result := config.ParseFuncYaml(appDir, NewLogger())
128129
resultLimitOptions := &knfn.ResourcesLimitsOptions{}
129130
yaml.Unmarshal([]byte(result.Options["options-resources-limits"]), resultLimitOptions)
130131

buildpacks/common/tests/setup.go

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright 2021-2022 VMware, Inc.
2+
// SPDX-License-Identifier: BSD-2-Clause
3+
4+
package tests
5+
6+
import (
7+
"bytes"
8+
"fmt"
9+
"io/ioutil"
10+
"log"
11+
"os"
12+
13+
"github.com/paketo-buildpacks/libpak/bard"
14+
knfn "knative.dev/kn-plugin-func"
15+
)
16+
17+
func NewLogger() bard.Logger {
18+
buf := bytes.NewBuffer(nil)
19+
return bard.NewLogger(buf)
20+
}
21+
22+
type SetupOpts func(directory string)
23+
24+
func SetupTestDirectory(opts ...SetupOpts) (string, func()) {
25+
dir, err := ioutil.TempDir(os.TempDir(), "python-functions-buildpack-*")
26+
if err != nil {
27+
panic(fmt.Sprintf("unable to create test directory: %v", err))
28+
}
29+
30+
for _, opt := range opts {
31+
opt(dir)
32+
}
33+
34+
cleanup := func() {
35+
if err := os.RemoveAll(dir); err != nil {
36+
log.Printf("Failed to delete temp directory %s: %v", dir, err)
37+
}
38+
39+
}
40+
return dir, cleanup
41+
}
42+
43+
func WithFuncYaml() SetupOpts {
44+
return func(directory string) {
45+
cfg, err := knfn.NewFunction(directory)
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
err = cfg.WriteConfig()
51+
if err != nil {
52+
panic(err)
53+
}
54+
}
55+
}
56+
57+
func WithFuncEnvs(envs map[string]string) SetupOpts {
58+
return func(directory string) {
59+
cfg, err := knfn.NewFunction(directory)
60+
if err != nil {
61+
panic(err)
62+
}
63+
64+
for envName, envValue := range envs {
65+
name := envName
66+
value := envValue
67+
cfg.Envs = append(cfg.Envs, knfn.Env{
68+
Name: &name,
69+
Value: &value,
70+
})
71+
}
72+
73+
err = cfg.WriteConfig()
74+
if err != nil {
75+
panic(err)
76+
}
77+
}
78+
}
79+
80+
func WithFuncScale(scale knfn.ScaleOptions) SetupOpts {
81+
return func(directory string) {
82+
cfg, err := knfn.NewFunction(directory)
83+
if err != nil {
84+
panic(err)
85+
}
86+
87+
cfg.Options.Scale = &scale
88+
89+
err = cfg.WriteConfig()
90+
if err != nil {
91+
panic(err)
92+
}
93+
}
94+
}
95+
96+
func WithFuncResourceRequests(requests knfn.ResourcesRequestsOptions) SetupOpts {
97+
return func(directory string) {
98+
cfg, err := knfn.NewFunction(directory)
99+
if err != nil {
100+
panic(err)
101+
}
102+
103+
if cfg.Options.Resources == nil {
104+
cfg.Options.Resources = &knfn.ResourcesOptions{}
105+
}
106+
107+
cfg.Options.Resources.Requests = &requests
108+
109+
err = cfg.WriteConfig()
110+
if err != nil {
111+
panic(err)
112+
}
113+
}
114+
}
115+
116+
func WithFuncResourceLimits(limits knfn.ResourcesLimitsOptions) SetupOpts {
117+
return func(directory string) {
118+
cfg, err := knfn.NewFunction(directory)
119+
if err != nil {
120+
panic(err)
121+
}
122+
123+
if cfg.Options.Resources == nil {
124+
cfg.Options.Resources = &knfn.ResourcesOptions{}
125+
}
126+
127+
cfg.Options.Resources.Limits = &limits
128+
129+
err = cfg.WriteConfig()
130+
if err != nil {
131+
panic(err)
132+
}
133+
}
134+
}

buildpacks/java/go.mod

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ go 1.19
44

55
require (
66
github.com/buildpacks/libcnb v1.26.0
7-
github.com/onsi/gomega v1.19.0
7+
github.com/onsi/gomega v1.20.2
88
github.com/paketo-buildpacks/libpak v1.61.0
99
github.com/sclevine/spec v1.4.0
10-
gopkg.in/yaml.v3 v3.0.1
10+
kn-fn/buildpacks v0.0.0
1111
knative.dev/kn-plugin-func v0.19.0
1212
knative.dev/pkg v0.0.0-20210902173607-844a6bc45596
1313
)
@@ -25,6 +25,7 @@ require (
2525
github.com/go-git/go-git/v5 v5.4.2 // indirect
2626
github.com/gobuffalo/here v0.6.0 // indirect
2727
github.com/gogo/protobuf v1.3.2 // indirect
28+
github.com/google/go-cmp v0.5.8 // indirect
2829
github.com/h2non/filetype v1.1.3 // indirect
2930
github.com/heroku/color v0.0.6 // indirect
3031
github.com/imdario/mergo v0.3.13 // indirect
@@ -40,11 +41,14 @@ require (
4041
github.com/xanzy/ssh-agent v0.3.0 // indirect
4142
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
4243
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
43-
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
44-
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
44+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
45+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
4546
golang.org/x/text v0.3.7 // indirect
4647
gopkg.in/inf.v0 v0.9.1 // indirect
4748
gopkg.in/warnings.v0 v0.1.2 // indirect
4849
gopkg.in/yaml.v2 v2.4.0 // indirect
50+
gopkg.in/yaml.v3 v3.0.1 // indirect
4951
k8s.io/apimachinery v0.20.7 // indirect
5052
)
53+
54+
replace kn-fn/buildpacks => ../common

0 commit comments

Comments
 (0)