Skip to content

Commit b78cfa7

Browse files
committed
Wrap up v0.0.1
1 parent e1baed0 commit b78cfa7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+658
-579
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tmp_dir = "tmp"
55
cmd = "templ generate && go build -o ./tmp/lamba ."
66
bin = "tmp/lamba"
77
full_bin = "./tmp/lamba"
8-
args_bin = ["--port", "8000", "--engine", "containerd", "--debug"]
8+
args_bin = ["--port", "8000", "--engine", "docker", "--debug"]
99
include_ext = ["go", "tpl", "tmpl", "html", "so", "templ"]
1010
exclude_dir = ["assets", "tmp", "vendor"]
1111
exclude_regex = [".*_templ.go"]

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ sudo ./lamba --engine containerd --port 8080
3434
```
3535

3636
## Function Structure
37+
You can find examples in the [examples](examples) directory.
3738
```
38-
function/
39-
├── function.py
40-
├── requirements.txt
39+
function_name/
40+
├── function_name.py
4141
4242
# Create deployment package
4343
zip -r function.zip function

cmd/cmd.go renamed to cmd/lamba/cmd.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package cmd
1+
package lamba
22

33
import (
44
"flag"
55
"fmt"
66
"github.com/rs/zerolog"
77
"github.com/rs/zerolog/log"
8-
"github.com/sirrobot01/lamba/pkg/event"
9-
"github.com/sirrobot01/lamba/pkg/executor"
10-
"github.com/sirrobot01/lamba/pkg/function"
11-
"github.com/sirrobot01/lamba/pkg/runtime"
12-
"github.com/sirrobot01/lamba/pkg/runtime/engines"
13-
"github.com/sirrobot01/lamba/server"
8+
"github.com/sirrobot01/lamba/internal/engines"
9+
"github.com/sirrobot01/lamba/internal/event"
10+
"github.com/sirrobot01/lamba/internal/executor"
11+
"github.com/sirrobot01/lamba/internal/function"
12+
"github.com/sirrobot01/lamba/internal/runtime"
13+
"github.com/sirrobot01/lamba/internal/server/handlers"
1414
"os"
1515
"strings"
1616
)
@@ -76,8 +76,8 @@ func Start() error {
7676
log.Info().Err(err).Msg("Failed to register runtimes")
7777
return err
7878
}
79-
s := server.NewServer(executor.NewExecutor(registry, runtimeManager, eventManager, memory), port)
80-
if err := s.Start(); err != nil {
79+
h := handlers.New(executor.New(registry, runtimeManager, eventManager, memory), port)
80+
if err := h.Start(); err != nil {
8181
log.Info().Err(err).Msgf("Failed to start server")
8282
return err
8383
}

examples/golang/hello/hello.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func Runner(event map[string]interface{}, context map[string]interface{}) map[string]interface{} {
4+
return event
5+
}

examples/nodejs/hello/hello.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function runner(event, context) {
2+
return event;
3+
}
4+
5+
module.exports = {
6+
runner
7+
};

examples/python/hello/hello.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def handler(event, context):
2+
return event

pkg/runtime/engines/containerd/containerd.go renamed to internal/engines/containerd/containerd.go

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/containerd/containerd/oci"
1111
"github.com/opencontainers/runtime-spec/specs-go"
1212
"github.com/rs/zerolog/log"
13+
"github.com/sirrobot01/lamba/internal/function"
1314
"strings"
1415
"sync"
1516
"syscall"
@@ -19,16 +20,13 @@ import (
1920
const defaultNamespace = "lamba"
2021

2122
type Engine struct {
22-
image string
23-
containerID string
24-
containerName string
25-
lastUsed time.Time
26-
mutex sync.Mutex
27-
CodePath string
28-
client *containerd.Client
23+
image string
24+
lastUsed time.Time
25+
mutex sync.Mutex
26+
client *containerd.Client
2927
}
3028

31-
func NewEngine(containerId, name, image, codePath string) *Engine {
29+
func NewEngine(image string) *Engine {
3230
cl, err := containerd.New(
3331
"/run/containerd/containerd.sock",
3432
containerd.WithDefaultNamespace("lamba"),
@@ -45,20 +43,11 @@ func NewEngine(containerId, name, image, codePath string) *Engine {
4543
image = "docker.io/library/" + image
4644
}
4745
return &Engine{
48-
client: cl,
49-
image: image,
50-
containerID: containerId,
51-
containerName: name,
52-
CodePath: codePath,
46+
client: cl,
47+
image: image,
5348
}
5449
}
5550

56-
func (e *Engine) UpdateEngine(containerId, name, codePath string) {
57-
e.containerID = containerId
58-
e.containerName = name
59-
e.CodePath = codePath
60-
}
61-
6251
func (e *Engine) PullImage(ctx context.Context) error {
6352
ctx = namespaces.WithNamespace(ctx, defaultNamespace)
6453

@@ -73,22 +62,22 @@ func (e *Engine) PullImage(ctx context.Context) error {
7362
return nil
7463
}
7564

76-
func (e *Engine) GetOrCreateContainer(ctx context.Context) (string, error) {
65+
func (e *Engine) GetOrCreateContainer(ctx context.Context, fn *function.Function) error {
7766
e.mutex.Lock()
7867
defer e.mutex.Unlock()
7968

8069
ctx = namespaces.WithNamespace(ctx, defaultNamespace)
8170

8271
// Check if container exists
83-
if e.containerID != "" {
84-
container, err := e.client.LoadContainer(ctx, e.containerID)
72+
if fn.ContainerID != "" {
73+
container, err := e.client.LoadContainer(ctx, fn.ContainerID)
8574
if err == nil {
8675
task, err := container.Task(ctx, nil)
8776
if err == nil {
8877
status, err := task.Status(ctx)
8978
if err == nil && status.Status == containerd.Running {
9079
e.lastUsed = time.Now()
91-
return container.ID(), nil
80+
return nil
9281
}
9382
}
9483
}
@@ -99,21 +88,21 @@ func (e *Engine) GetOrCreateContainer(ctx context.Context) (string, error) {
9988
log.Info().Msgf("Pulling image...")
10089
image, err = e.client.Pull(ctx, e.image, containerd.WithPullUnpack)
10190
if err != nil {
102-
return "", err
91+
return err
10392
}
10493
}
10594

10695
// Create container
10796
container, err := e.client.NewContainer(
10897
ctx,
109-
e.containerName,
110-
containerd.WithNewSnapshot(e.containerName+"-snapshot", image),
98+
fn.ContainerName,
99+
containerd.WithNewSnapshot(fn.ContainerName+"snapshot", image),
111100
containerd.WithNewSpec(
112101
oci.WithImageConfig(image),
113102
oci.WithMounts([]specs.Mount{
114103
{
115104
Type: "bind",
116-
Source: e.CodePath,
105+
Source: fn.CodePath,
117106
Destination: "/app",
118107
Options: []string{"rbind", "rw"},
119108
},
@@ -122,33 +111,31 @@ func (e *Engine) GetOrCreateContainer(ctx context.Context) (string, error) {
122111
),
123112
)
124113
if err != nil {
125-
return "", err
114+
return err
126115
}
127116

128117
// Create and start the task
129118
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
130119
if err != nil {
131-
return "", err
120+
return err
132121
}
133122

134123
if err := task.Start(ctx); err != nil {
135-
return "", err
124+
return err
136125
}
137-
containerId := container.ID()
138-
e.containerID = containerId
126+
fn.ContainerID = container.ID()
139127
e.lastUsed = time.Now()
140-
return containerId, nil
128+
return nil
141129
}
142130

143-
func (e *Engine) RunCommand(ctx context.Context, cmd []string) (string, string, error) {
131+
func (e *Engine) RunCommand(ctx context.Context, fn *function.Function, cmd []string) (string, string, error) {
144132

145133
ctx = namespaces.WithNamespace(ctx, defaultNamespace)
146134

147-
containerId, err := e.GetOrCreateContainer(ctx)
148-
if err != nil || containerId == "" {
135+
if err := e.GetOrCreateContainer(ctx, fn); err != nil || fn.ContainerID == "" {
149136
return "", "", err
150137
}
151-
container, err := e.client.LoadContainer(ctx, containerId)
138+
container, err := e.client.LoadContainer(ctx, fn.ContainerID)
152139
if err != nil {
153140
return "", "", err
154141
}
@@ -171,7 +158,7 @@ func (e *Engine) RunCommand(ctx context.Context, cmd []string) (string, string,
171158
stderr := bytes.NewBuffer(nil)
172159

173160
process, err := task.Exec(ctx,
174-
fmt.Sprintf("%s-exec-%d", e.containerName, time.Now().UnixNano()),
161+
fmt.Sprintf("%s-exec-%d", fn.ContainerName, time.Now().UnixNano()),
175162
spec.Process,
176163
cio.NewCreator(
177164
cio.WithStreams(nil, stdout, stderr),
@@ -213,15 +200,15 @@ func (e *Engine) RunCommand(ctx context.Context, cmd []string) (string, string,
213200
return stdout.String(), stderr.String(), nil
214201
}
215202

216-
func (e *Engine) Cleanup(force bool) error {
203+
func (e *Engine) Cleanup(fn *function.Function, force bool) error {
217204
e.mutex.Lock()
218205
defer e.mutex.Unlock()
219206
ctx := context.Background()
220207
ctx = namespaces.WithNamespace(ctx, defaultNamespace)
221208

222-
if e.containerID != "" && (time.Since(e.lastUsed) > 10*time.Minute || force) {
209+
if fn.ContainerID != "" && (time.Since(e.lastUsed) > 10*time.Minute || force) {
223210

224-
container, err := e.client.LoadContainer(ctx, e.containerID)
211+
container, err := e.client.LoadContainer(ctx, fn.ContainerID)
225212
if err != nil {
226213
return err
227214
}
@@ -252,7 +239,7 @@ func (e *Engine) Cleanup(force bool) error {
252239
return err
253240
}
254241

255-
e.containerID = ""
242+
fn.ContainerID = ""
256243
}
257244
return nil
258245
}
@@ -282,19 +269,3 @@ func (e *Engine) isContainerHealthy(containerID string) bool {
282269
func (e *Engine) LastUsed() time.Time {
283270
return e.lastUsed
284271
}
285-
286-
func (e *Engine) ContainerID() string {
287-
return e.containerID
288-
}
289-
290-
func (e *Engine) ContainerName() string {
291-
return e.containerName
292-
}
293-
294-
func (e *Engine) Image() string {
295-
return e.image
296-
}
297-
298-
func (e *Engine) GetCodePath() string {
299-
return e.CodePath
300-
}

0 commit comments

Comments
 (0)