@@ -10,6 +10,7 @@ import (
10
10
"github.com/containerd/containerd/oci"
11
11
"github.com/opencontainers/runtime-spec/specs-go"
12
12
"github.com/rs/zerolog/log"
13
+ "github.com/sirrobot01/lamba/internal/function"
13
14
"strings"
14
15
"sync"
15
16
"syscall"
@@ -19,16 +20,13 @@ import (
19
20
const defaultNamespace = "lamba"
20
21
21
22
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
29
27
}
30
28
31
- func NewEngine (containerId , name , image , codePath string ) * Engine {
29
+ func NewEngine (image string ) * Engine {
32
30
cl , err := containerd .New (
33
31
"/run/containerd/containerd.sock" ,
34
32
containerd .WithDefaultNamespace ("lamba" ),
@@ -45,20 +43,11 @@ func NewEngine(containerId, name, image, codePath string) *Engine {
45
43
image = "docker.io/library/" + image
46
44
}
47
45
return & Engine {
48
- client : cl ,
49
- image : image ,
50
- containerID : containerId ,
51
- containerName : name ,
52
- CodePath : codePath ,
46
+ client : cl ,
47
+ image : image ,
53
48
}
54
49
}
55
50
56
- func (e * Engine ) UpdateEngine (containerId , name , codePath string ) {
57
- e .containerID = containerId
58
- e .containerName = name
59
- e .CodePath = codePath
60
- }
61
-
62
51
func (e * Engine ) PullImage (ctx context.Context ) error {
63
52
ctx = namespaces .WithNamespace (ctx , defaultNamespace )
64
53
@@ -73,22 +62,22 @@ func (e *Engine) PullImage(ctx context.Context) error {
73
62
return nil
74
63
}
75
64
76
- func (e * Engine ) GetOrCreateContainer (ctx context.Context ) ( string , error ) {
65
+ func (e * Engine ) GetOrCreateContainer (ctx context.Context , fn * function. Function ) error {
77
66
e .mutex .Lock ()
78
67
defer e .mutex .Unlock ()
79
68
80
69
ctx = namespaces .WithNamespace (ctx , defaultNamespace )
81
70
82
71
// 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 )
85
74
if err == nil {
86
75
task , err := container .Task (ctx , nil )
87
76
if err == nil {
88
77
status , err := task .Status (ctx )
89
78
if err == nil && status .Status == containerd .Running {
90
79
e .lastUsed = time .Now ()
91
- return container . ID (), nil
80
+ return nil
92
81
}
93
82
}
94
83
}
@@ -99,21 +88,21 @@ func (e *Engine) GetOrCreateContainer(ctx context.Context) (string, error) {
99
88
log .Info ().Msgf ("Pulling image..." )
100
89
image , err = e .client .Pull (ctx , e .image , containerd .WithPullUnpack )
101
90
if err != nil {
102
- return "" , err
91
+ return err
103
92
}
104
93
}
105
94
106
95
// Create container
107
96
container , err := e .client .NewContainer (
108
97
ctx ,
109
- e . containerName ,
110
- containerd .WithNewSnapshot (e . containerName + "- snapshot" , image ),
98
+ fn . ContainerName ,
99
+ containerd .WithNewSnapshot (fn . ContainerName + " snapshot" , image ),
111
100
containerd .WithNewSpec (
112
101
oci .WithImageConfig (image ),
113
102
oci .WithMounts ([]specs.Mount {
114
103
{
115
104
Type : "bind" ,
116
- Source : e .CodePath ,
105
+ Source : fn .CodePath ,
117
106
Destination : "/app" ,
118
107
Options : []string {"rbind" , "rw" },
119
108
},
@@ -122,33 +111,31 @@ func (e *Engine) GetOrCreateContainer(ctx context.Context) (string, error) {
122
111
),
123
112
)
124
113
if err != nil {
125
- return "" , err
114
+ return err
126
115
}
127
116
128
117
// Create and start the task
129
118
task , err := container .NewTask (ctx , cio .NewCreator (cio .WithStdio ))
130
119
if err != nil {
131
- return "" , err
120
+ return err
132
121
}
133
122
134
123
if err := task .Start (ctx ); err != nil {
135
- return "" , err
124
+ return err
136
125
}
137
- containerId := container .ID ()
138
- e .containerID = containerId
126
+ fn .ContainerID = container .ID ()
139
127
e .lastUsed = time .Now ()
140
- return containerId , nil
128
+ return nil
141
129
}
142
130
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 ) {
144
132
145
133
ctx = namespaces .WithNamespace (ctx , defaultNamespace )
146
134
147
- containerId , err := e .GetOrCreateContainer (ctx )
148
- if err != nil || containerId == "" {
135
+ if err := e .GetOrCreateContainer (ctx , fn ); err != nil || fn .ContainerID == "" {
149
136
return "" , "" , err
150
137
}
151
- container , err := e .client .LoadContainer (ctx , containerId )
138
+ container , err := e .client .LoadContainer (ctx , fn . ContainerID )
152
139
if err != nil {
153
140
return "" , "" , err
154
141
}
@@ -171,7 +158,7 @@ func (e *Engine) RunCommand(ctx context.Context, cmd []string) (string, string,
171
158
stderr := bytes .NewBuffer (nil )
172
159
173
160
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 ()),
175
162
spec .Process ,
176
163
cio .NewCreator (
177
164
cio .WithStreams (nil , stdout , stderr ),
@@ -213,15 +200,15 @@ func (e *Engine) RunCommand(ctx context.Context, cmd []string) (string, string,
213
200
return stdout .String (), stderr .String (), nil
214
201
}
215
202
216
- func (e * Engine ) Cleanup (force bool ) error {
203
+ func (e * Engine ) Cleanup (fn * function. Function , force bool ) error {
217
204
e .mutex .Lock ()
218
205
defer e .mutex .Unlock ()
219
206
ctx := context .Background ()
220
207
ctx = namespaces .WithNamespace (ctx , defaultNamespace )
221
208
222
- if e . containerID != "" && (time .Since (e .lastUsed ) > 10 * time .Minute || force ) {
209
+ if fn . ContainerID != "" && (time .Since (e .lastUsed ) > 10 * time .Minute || force ) {
223
210
224
- container , err := e .client .LoadContainer (ctx , e . containerID )
211
+ container , err := e .client .LoadContainer (ctx , fn . ContainerID )
225
212
if err != nil {
226
213
return err
227
214
}
@@ -252,7 +239,7 @@ func (e *Engine) Cleanup(force bool) error {
252
239
return err
253
240
}
254
241
255
- e . containerID = ""
242
+ fn . ContainerID = ""
256
243
}
257
244
return nil
258
245
}
@@ -282,19 +269,3 @@ func (e *Engine) isContainerHealthy(containerID string) bool {
282
269
func (e * Engine ) LastUsed () time.Time {
283
270
return e .lastUsed
284
271
}
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