@@ -11,10 +11,9 @@ import (
11
11
"errors"
12
12
"fmt"
13
13
"os"
14
- "strings"
15
14
15
+ "github.com/kata-containers/runtime/pkg/katautils"
16
16
vc "github.com/kata-containers/runtime/virtcontainers"
17
- vf "github.com/kata-containers/runtime/virtcontainers/factory"
18
17
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
19
18
"github.com/urfave/cli"
20
19
)
@@ -87,44 +86,11 @@ var createCLICommand = cli.Command{
87
86
},
88
87
}
89
88
90
- // Use a variable to allow tests to modify its value
91
- var getKernelParamsFunc = getKernelParams
92
-
93
- func handleFactory (ctx context.Context , runtimeConfig oci.RuntimeConfig ) {
94
- if ! runtimeConfig .FactoryConfig .Template {
95
- return
96
- }
97
-
98
- factoryConfig := vf.Config {
99
- Template : true ,
100
- VMConfig : vc.VMConfig {
101
- HypervisorType : runtimeConfig .HypervisorType ,
102
- HypervisorConfig : runtimeConfig .HypervisorConfig ,
103
- AgentType : runtimeConfig .AgentType ,
104
- AgentConfig : runtimeConfig .AgentConfig ,
105
- },
106
- }
107
-
108
- kataLog .WithField ("factory" , factoryConfig ).Info ("load vm factory" )
109
-
110
- f , err := vf .NewFactory (ctx , factoryConfig , true )
111
- if err != nil {
112
- kataLog .WithError (err ).Warn ("load vm factory failed, about to create new one" )
113
- f , err = vf .NewFactory (ctx , factoryConfig , false )
114
- if err != nil {
115
- kataLog .WithError (err ).Warn ("create vm factory failed" )
116
- return
117
- }
118
- }
119
-
120
- vci .SetFactory (ctx , f )
121
- }
122
-
123
89
func create (ctx context.Context , containerID , bundlePath , console , pidFilePath string , detach , systemdCgroup bool ,
124
90
runtimeConfig oci.RuntimeConfig ) error {
125
91
var err error
126
92
127
- span , ctx := trace (ctx , "create" )
93
+ span , ctx := katautils . Trace (ctx , "create" )
128
94
defer span .Finish ()
129
95
130
96
kataLog = kataLog .WithField ("container" , containerID )
@@ -157,19 +123,19 @@ func create(ctx context.Context, containerID, bundlePath, console, pidFilePath s
157
123
return err
158
124
}
159
125
160
- handleFactory (ctx , runtimeConfig )
126
+ katautils . HandleFactory (ctx , vci , & runtimeConfig )
161
127
162
128
disableOutput := noNeedForOutput (detach , ociSpec .Process .Terminal )
163
129
164
130
var process vc.Process
165
131
switch containerType {
166
132
case vc .PodSandbox :
167
- process , err = createSandbox (ctx , ociSpec , runtimeConfig , containerID , bundlePath , console , disableOutput , systemdCgroup )
133
+ _ , process , err = katautils . CreateSandbox (ctx , vci , ociSpec , runtimeConfig , containerID , bundlePath , console , disableOutput , systemdCgroup , false )
168
134
if err != nil {
169
135
return err
170
136
}
171
137
case vc .PodContainer :
172
- process , err = createContainer (ctx , ociSpec , containerID , bundlePath , console , disableOutput )
138
+ process , err = katautils . CreateContainer (ctx , vci , nil , ociSpec , containerID , bundlePath , console , disableOutput , false )
173
139
if err != nil {
174
140
return err
175
141
}
@@ -181,184 +147,8 @@ func create(ctx context.Context, containerID, bundlePath, console, pidFilePath s
181
147
return createPIDFile (ctx , pidFilePath , process .Pid )
182
148
}
183
149
184
- var systemdKernelParam = []vc.Param {
185
- {
186
- Key : "init" ,
187
- Value : "/usr/lib/systemd/systemd" ,
188
- },
189
- {
190
- Key : "systemd.unit" ,
191
- Value : systemdUnitName ,
192
- },
193
- {
194
- Key : "systemd.mask" ,
195
- Value : "systemd-networkd.service" ,
196
- },
197
- {
198
- Key : "systemd.mask" ,
199
- Value : "systemd-networkd.socket" ,
200
- },
201
- }
202
-
203
- func getKernelParams (needSystemd bool ) []vc.Param {
204
- p := []vc.Param {}
205
-
206
- if needSystemd {
207
- p = append (p , systemdKernelParam ... )
208
- }
209
-
210
- return p
211
- }
212
-
213
- func needSystemd (config vc.HypervisorConfig ) bool {
214
- return config .ImagePath != ""
215
- }
216
-
217
- // setKernelParams adds the user-specified kernel parameters (from the
218
- // configuration file) to the defaults so that the former take priority.
219
- func setKernelParams (containerID string , runtimeConfig * oci.RuntimeConfig ) error {
220
- defaultKernelParams := getKernelParamsFunc (needSystemd (runtimeConfig .HypervisorConfig ))
221
-
222
- if runtimeConfig .HypervisorConfig .Debug {
223
- strParams := vc .SerializeParams (defaultKernelParams , "=" )
224
- formatted := strings .Join (strParams , " " )
225
-
226
- kataLog .WithField ("default-kernel-parameters" , formatted ).Debug ()
227
- }
228
-
229
- // retrieve the parameters specified in the config file
230
- userKernelParams := runtimeConfig .HypervisorConfig .KernelParams
231
-
232
- // reset
233
- runtimeConfig .HypervisorConfig .KernelParams = []vc.Param {}
234
-
235
- // first, add default values
236
- for _ , p := range defaultKernelParams {
237
- if err := (runtimeConfig ).AddKernelParam (p ); err != nil {
238
- return err
239
- }
240
- }
241
-
242
- // now re-add the user-specified values so that they take priority.
243
- for _ , p := range userKernelParams {
244
- if err := (runtimeConfig ).AddKernelParam (p ); err != nil {
245
- return err
246
- }
247
- }
248
-
249
- return nil
250
- }
251
-
252
- func createSandbox (ctx context.Context , ociSpec oci.CompatOCISpec , runtimeConfig oci.RuntimeConfig ,
253
- containerID , bundlePath , console string , disableOutput , systemdCgroup bool ) (vc.Process , error ) {
254
- span , ctx := trace (ctx , "createSandbox" )
255
- defer span .Finish ()
256
-
257
- err := setKernelParams (containerID , & runtimeConfig )
258
- if err != nil {
259
- return vc.Process {}, err
260
- }
261
-
262
- sandboxConfig , err := oci .SandboxConfig (ociSpec , runtimeConfig , bundlePath , containerID , console , disableOutput , systemdCgroup )
263
- if err != nil {
264
- return vc.Process {}, err
265
- }
266
-
267
- // Important to create the network namespace before the sandbox is
268
- // created, because it is not responsible for the creation of the
269
- // netns if it does not exist.
270
- if err := setupNetworkNamespace (& sandboxConfig .NetworkConfig ); err != nil {
271
- return vc.Process {}, err
272
- }
273
-
274
- // Run pre-start OCI hooks.
275
- err = enterNetNS (sandboxConfig .NetworkConfig .NetNSPath , func () error {
276
- return preStartHooks (ctx , ociSpec , containerID , bundlePath )
277
- })
278
- if err != nil {
279
- return vc.Process {}, err
280
- }
281
-
282
- sandbox , err := vci .CreateSandbox (ctx , sandboxConfig )
283
- if err != nil {
284
- return vc.Process {}, err
285
- }
286
-
287
- sid := sandbox .ID ()
288
- kataLog = kataLog .WithField ("sandbox" , sid )
289
- setExternalLoggers (ctx , kataLog )
290
- span .SetTag ("sandbox" , sid )
291
-
292
- containers := sandbox .GetAllContainers ()
293
- if len (containers ) != 1 {
294
- return vc.Process {}, fmt .Errorf ("BUG: Container list from sandbox is wrong, expecting only one container, found %d containers" , len (containers ))
295
- }
296
-
297
- if err := addContainerIDMapping (ctx , containerID , sandbox .ID ()); err != nil {
298
- return vc.Process {}, err
299
- }
300
-
301
- return containers [0 ].Process (), nil
302
- }
303
-
304
- // setEphemeralStorageType sets the mount type to 'ephemeral'
305
- // if the mount source path is provisioned by k8s for ephemeral storage.
306
- // For the given pod ephemeral volume is created only once
307
- // backed by tmpfs inside the VM. For successive containers
308
- // of the same pod the already existing volume is reused.
309
- func setEphemeralStorageType (ociSpec oci.CompatOCISpec ) oci.CompatOCISpec {
310
- for idx , mnt := range ociSpec .Mounts {
311
- if IsEphemeralStorage (mnt .Source ) {
312
- ociSpec .Mounts [idx ].Type = "ephemeral"
313
- }
314
- }
315
- return ociSpec
316
- }
317
-
318
- func createContainer (ctx context.Context , ociSpec oci.CompatOCISpec , containerID , bundlePath ,
319
- console string , disableOutput bool ) (vc.Process , error ) {
320
-
321
- span , ctx := trace (ctx , "createContainer" )
322
- defer span .Finish ()
323
-
324
- ociSpec = setEphemeralStorageType (ociSpec )
325
-
326
- contConfig , err := oci .ContainerConfig (ociSpec , bundlePath , containerID , console , disableOutput )
327
- if err != nil {
328
- return vc.Process {}, err
329
- }
330
-
331
- sandboxID , err := ociSpec .SandboxID ()
332
- if err != nil {
333
- return vc.Process {}, err
334
- }
335
-
336
- kataLog = kataLog .WithField ("sandbox" , sandboxID )
337
- setExternalLoggers (ctx , kataLog )
338
- span .SetTag ("sandbox" , sandboxID )
339
-
340
- s , c , err := vci .CreateContainer (ctx , sandboxID , contConfig )
341
- if err != nil {
342
- return vc.Process {}, err
343
- }
344
-
345
- // Run pre-start OCI hooks.
346
- err = enterNetNS (s .GetNetNs (), func () error {
347
- return preStartHooks (ctx , ociSpec , containerID , bundlePath )
348
- })
349
- if err != nil {
350
- return vc.Process {}, err
351
- }
352
-
353
- if err := addContainerIDMapping (ctx , containerID , sandboxID ); err != nil {
354
- return vc.Process {}, err
355
- }
356
-
357
- return c .Process (), nil
358
- }
359
-
360
150
func createPIDFile (ctx context.Context , pidFilePath string , pid int ) error {
361
- span , _ := trace (ctx , "createPIDFile" )
151
+ span , _ := katautils . Trace (ctx , "createPIDFile" )
362
152
defer span .Finish ()
363
153
364
154
if pidFilePath == "" {
0 commit comments