forked from containers/virtcontainers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpod.go
767 lines (608 loc) · 15.8 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package virtcontainers
import (
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/01org/ciao/ssntp/uuid"
"github.com/golang/glog"
)
// controlSocket is the pod control socket.
// It is an hypervisor resource, and for example qemu's control
// socket is the QMP one.
const controlSocket = "ctrl.sock"
// monitorSocket is the pod monitoring socket.
// It is an hypervisor resource, and is a qmp socket in the qemu case.
// This is a socket that any monitoring entity will listen to in order
// to understand if the VM is still alive or not.
const monitorSocket = "monitor.sock"
// stateString is a string representing a pod state.
type stateString string
const (
// StateReady represents a pod/container that's ready to be run
StateReady stateString = "ready"
// StateRunning represents a pod/container that's currently running.
StateRunning stateString = "running"
// StateStopped represents a pod/container that has been stopped.
StateStopped stateString = "stopped"
)
// State is a pod state structure.
type State struct {
State stateString `json:"state"`
}
// valid checks that the pod state is valid.
func (state *State) valid() bool {
for _, validState := range []stateString{StateReady, StateRunning, StateStopped} {
if state.State == validState {
return true
}
}
return false
}
// validTransition returns an error if we want to move to
// an unreachable state.
func (state *State) validTransition(oldState stateString, newState stateString) error {
if state.State != oldState {
return fmt.Errorf("Invalid state %s (Expecting %s)", state.State, oldState)
}
switch state.State {
case StateReady:
if newState == StateRunning {
return nil
}
case StateRunning:
if newState == StateStopped {
return nil
}
case StateStopped:
if newState == StateRunning {
return nil
}
}
return fmt.Errorf("Can not move from %s to %s",
state.State, newState)
}
// Volume is a shared volume between the host and the VM,
// defined by its mount tag and its host path.
type Volume struct {
// MountTag is a label used as a hint to the guest.
MountTag string
// HostPath is the host filesystem path for this volume.
HostPath string
}
// Volumes is a Volume list.
type Volumes []Volume
// Set assigns volume values from string to a Volume.
func (v *Volumes) Set(volStr string) error {
volSlice := strings.Split(volStr, " ")
const expectedVolLen = 2
const volDelimiter = ":"
for _, vol := range volSlice {
volArgs := strings.Split(vol, volDelimiter)
if len(volArgs) != expectedVolLen {
return fmt.Errorf("Wrong string format: %s, expecting only %v parameters separated with %q",
vol, expectedVolLen, volDelimiter)
}
if volArgs[0] == "" || volArgs[1] == "" {
return fmt.Errorf("Volume parameters cannot be empty")
}
volume := Volume{
MountTag: volArgs[0],
HostPath: volArgs[1],
}
*v = append(*v, volume)
}
return nil
}
// String converts a Volume to a string.
func (v *Volumes) String() string {
var volSlice []string
for _, volume := range *v {
volSlice = append(volSlice, fmt.Sprintf("%s:%s", volume.MountTag, volume.HostPath))
}
return strings.Join(volSlice, " ")
}
// Socket defines a socket to communicate between
// the host and any process inside the VM.
type Socket struct {
DeviceID string
ID string
HostPath string
Name string
}
// Sockets is a Socket list.
type Sockets []Socket
// Set assigns socket values from string to a Socket.
func (s *Sockets) Set(sockStr string) error {
sockSlice := strings.Split(sockStr, " ")
const expectedSockCount = 4
const sockDelimiter = ":"
for _, sock := range sockSlice {
sockArgs := strings.Split(sock, sockDelimiter)
if len(sockArgs) != expectedSockCount {
return fmt.Errorf("Wrong string format: %s, expecting only %v parameters separated with %q", sock, expectedSockCount, sockDelimiter)
}
for _, a := range sockArgs {
if a == "" {
return fmt.Errorf("Socket parameters cannot be empty")
}
}
socket := Socket{
DeviceID: sockArgs[0],
ID: sockArgs[1],
HostPath: sockArgs[2],
Name: sockArgs[3],
}
*s = append(*s, socket)
}
return nil
}
// String converts a Socket to a string.
func (s *Sockets) String() string {
var sockSlice []string
for _, sock := range *s {
sockSlice = append(sockSlice, fmt.Sprintf("%s:%s:%s:%s", sock.DeviceID, sock.ID, sock.HostPath, sock.Name))
}
return strings.Join(sockSlice, " ")
}
// EnvVar is a key/value structure representing a command
// environment variable.
type EnvVar struct {
Var string
Value string
}
// Cmd represents a command to execute in a running container.
type Cmd struct {
Args []string
Envs []EnvVar
WorkDir string
User string
Group string
}
// Resources describes VM resources configuration.
type Resources struct {
// VCPUs is the number of available virtual CPUs.
VCPUs uint
// Memory is the amount of available memory in MiB.
Memory uint
}
// PodStatus describes a pod status.
type PodStatus struct {
ID string
State State
Hypervisor HypervisorType
Agent AgentType
ContainersStatus []ContainerStatus
}
// PodConfig is a Pod configuration.
type PodConfig struct {
ID string
// Field specific to OCI specs, needed to setup all the hooks
Hooks Hooks
// VMConfig is the VM configuration to set for this pod.
VMConfig Resources
HypervisorType HypervisorType
HypervisorConfig HypervisorConfig
AgentType AgentType
AgentConfig interface{}
ProxyType ProxyType
ProxyConfig interface{}
NetworkModel NetworkModel
NetworkConfig NetworkConfig
// Volumes is a list of shared volumes between the host and the Pod.
Volumes []Volume
// Containers describe the list of containers within a Pod.
// This list can be empty and populated by adding containers
// to the Pod a posteriori.
Containers []ContainerConfig
}
// valid checks that the pod configuration is valid.
func (podConfig *PodConfig) valid() bool {
if _, err := newHypervisor(podConfig.HypervisorType); err != nil {
podConfig.HypervisorType = QemuHypervisor
}
if podConfig.ID == "" {
podConfig.ID = uuid.Generate().String()
}
return true
}
// lock locks any pod to prevent it from being accessed by other processes.
func lockPod(podID string) (*os.File, error) {
fs := filesystem{}
podlockFile, _, err := fs.podURI(podID, lockFileType)
if err != nil {
return nil, err
}
lockFile, err := os.Open(podlockFile)
if err != nil {
return nil, err
}
err = syscall.Flock(int(lockFile.Fd()), syscall.LOCK_EX)
if err != nil {
return nil, err
}
return lockFile, nil
}
// unlock unlocks any pod to allow it being accessed by other processes.
func unlockPod(lockFile *os.File) error {
err := syscall.Flock(int(lockFile.Fd()), syscall.LOCK_UN)
if err != nil {
return err
}
lockFile.Close()
return nil
}
// Pod is composed of a set of containers and a runtime environment.
// A Pod can be created, deleted, started, stopped, listed, entered, paused and restored.
type Pod struct {
id string
hypervisor hypervisor
agent agent
storage resourceStorage
network network
config *PodConfig
volumes []Volume
containers []*Container
runPath string
configPath string
url string
state State
lockFile *os.File
}
// ID returns the pod identifier string.
func (p *Pod) ID() string {
return p.id
}
// URL returns the pod URL for any runtime to connect to the proxy.
func (p *Pod) URL() string {
return p.url
}
// GetContainers returns a container config list.
func (p *Pod) GetContainers() []*Container {
return p.containers
}
func (p *Pod) createSetStates() error {
err := p.setPodState(StateReady)
if err != nil {
return err
}
err = p.setContainersState(StateReady)
if err != nil {
return err
}
return nil
}
// createPod creates a pod from a pod description, the containers list, the hypervisor
// and the agent passed through the Config structure.
// It will create and store the pod structure, and then ask the hypervisor
// to physically create that pod i.e. starts a VM for that pod to eventually
// be started.
func createPod(podConfig PodConfig) (*Pod, error) {
if podConfig.valid() == false {
return nil, fmt.Errorf("Invalid pod configuration")
}
agent := newAgent(podConfig.AgentType)
hypervisor, err := newHypervisor(podConfig.HypervisorType)
if err != nil {
return nil, err
}
err = hypervisor.init(podConfig.HypervisorConfig)
if err != nil {
return nil, err
}
network := newNetwork(podConfig.NetworkModel)
p := &Pod{
id: podConfig.ID,
hypervisor: hypervisor,
agent: agent,
storage: &filesystem{},
network: network,
config: &podConfig,
volumes: podConfig.Volumes,
runPath: filepath.Join(runStoragePath, podConfig.ID),
configPath: filepath.Join(configStoragePath, podConfig.ID),
state: State{},
}
containers, err := createContainers(p, podConfig.Containers)
if err != nil {
return nil, err
}
p.containers = containers
err = p.storage.createAllResources(*p)
if err != nil {
return nil, err
}
err = p.hypervisor.createPod(podConfig)
if err != nil {
p.storage.deletePodResources(p.id, nil)
return nil, err
}
var agentConfig interface{}
if podConfig.AgentConfig != nil {
switch podConfig.AgentConfig.(type) {
case (map[string]interface{}):
agentConfig = newAgentConfig(podConfig)
default:
agentConfig = podConfig.AgentConfig.(interface{})
}
} else {
agentConfig = nil
}
err = p.agent.init(p, agentConfig)
if err != nil {
p.storage.deletePodResources(p.id, nil)
return nil, err
}
state, err := p.storage.fetchPodState(p.id)
if err == nil && state.State != "" {
p.state = state
return p, nil
}
err = p.createSetStates()
if err != nil {
p.storage.deletePodResources(p.id, nil)
return nil, err
}
return p, nil
}
// storePod stores a pod config.
func (p *Pod) storePod() error {
err := p.storage.storePodResource(p.id, configFileType, *(p.config))
if err != nil {
return err
}
for _, container := range p.containers {
err = p.storage.storeContainerResource(p.id, container.id, configFileType, *(container.config))
if err != nil {
return err
}
}
return nil
}
// fetchPod fetches a pod config from a pod ID and returns a pod.
func fetchPod(podID string) (*Pod, error) {
fs := filesystem{}
config, err := fs.fetchPodConfig(podID)
if err != nil {
return nil, err
}
glog.Infof("Info structure:\n%+v\n", config)
return createPod(config)
}
// delete deletes an already created pod.
// The VM in which the pod is running will be shut down.
func (p *Pod) delete() error {
state, err := p.storage.fetchPodState(p.id)
if err != nil {
return err
}
if state.State != StateReady && state.State != StateStopped {
return fmt.Errorf("Pod not ready or stopped, impossible to delete")
}
err = p.storage.deletePodResources(p.id, nil)
if err != nil {
return err
}
return nil
}
func (p *Pod) startCheckStates() error {
state, err := p.storage.fetchPodState(p.id)
if err != nil {
return err
}
err = state.validTransition(StateReady, StateRunning)
if err != nil {
err = state.validTransition(StateStopped, StateRunning)
if err != nil {
return err
}
}
err = p.checkContainersState(StateReady)
if err != nil {
err = p.checkContainersState(StateStopped)
if err != nil {
return err
}
}
return nil
}
func (p *Pod) startSetStates() error {
err := p.setPodState(StateRunning)
if err != nil {
return err
}
err = p.setContainersState(StateRunning)
if err != nil {
return err
}
return nil
}
// startVM starts the VM, ensuring it is started before it returns or issuing
// an error in case of timeout. Then it connects to the agent inside the VM.
func (p *Pod) startVM() error {
vmStartedCh := make(chan struct{})
vmStoppedCh := make(chan struct{})
go func() {
p.network.run(p.config.NetworkConfig.NetNSPath, func() error {
err := p.hypervisor.startPod(vmStartedCh, vmStoppedCh)
return err
})
}()
// Wait for the pod started notification
select {
case <-vmStartedCh:
break
case <-time.After(time.Second):
return fmt.Errorf("Did not receive the pod started notification")
}
err := p.agent.start(p)
if err != nil {
p.stop()
return err
}
glog.Infof("VM started\n")
return nil
}
// start starts a pod. The containers that are making the pod
// will be started.
func (p *Pod) start() error {
err := p.startCheckStates()
if err != nil {
return err
}
err = p.agent.startPod(*p)
if err != nil {
p.stop()
return err
}
err = p.startSetStates()
if err != nil {
return err
}
glog.Infof("Started Pod %s\n", p.ID())
return nil
}
func (p *Pod) stopCheckStates() error {
state, err := p.storage.fetchPodState(p.id)
if err != nil {
return err
}
err = state.validTransition(StateRunning, StateStopped)
if err != nil {
return err
}
return nil
}
func (p *Pod) stopSetStates() error {
err := p.setContainersState(StateStopped)
if err != nil {
return err
}
err = p.setPodState(StateStopped)
if err != nil {
return err
}
return nil
}
// stopVM stops the agent inside the VM and shut down the VM itself.
func (p *Pod) stopVM() error {
err := p.agent.stop(*p)
if err != nil {
return err
}
err = p.hypervisor.stopPod()
if err != nil {
return err
}
return nil
}
// stop stops a pod. The containers that are making the pod
// will be destroyed.
func (p *Pod) stop() error {
err := p.stopCheckStates()
if err != nil {
return err
}
err = p.agent.stopPod(*p)
if err != nil {
return err
}
err = p.stopSetStates()
if err != nil {
return err
}
return nil
}
// list lists all pod running on the host.
func (p *Pod) list() ([]Pod, error) {
return nil, nil
}
// enter runs an executable within a pod.
func (p *Pod) enter(args []string) error {
return nil
}
func (p *Pod) setPodState(state stateString) error {
p.state = State{
State: state,
}
err := p.storage.storePodResource(p.id, stateFileType, p.state)
if err != nil {
return err
}
return nil
}
// endSession makes sure to end the session properly.
func (p *Pod) endSession() error {
return nil
}
func (p *Pod) setContainerState(contID string, state stateString) error {
contState := State{
State: state,
}
err := p.storage.storeContainerResource(p.id, contID, stateFileType, contState)
if err != nil {
return err
}
return nil
}
func (p *Pod) setContainersState(state stateString) error {
for _, container := range p.config.Containers {
err := p.setContainerState(container.ID, state)
if err != nil {
return err
}
}
return nil
}
func (p *Pod) deleteContainerState(contID string) error {
err := p.storage.deleteContainerResources(p.id, contID, []podResource{stateFileType})
if err != nil {
return err
}
return nil
}
func (p *Pod) deleteContainersState() error {
for _, container := range p.config.Containers {
err := p.deleteContainerState(container.ID)
if err != nil {
return err
}
}
return nil
}
func (p *Pod) checkContainerState(contID string, expectedState stateString) error {
state, err := p.storage.fetchContainerState(p.id, contID)
if err != nil {
return err
}
if state.State != expectedState {
return fmt.Errorf("Container %s not %s", contID, expectedState)
}
return nil
}
func (p *Pod) checkContainersState(state stateString) error {
for _, container := range p.config.Containers {
err := p.checkContainerState(container.ID, state)
if err != nil {
return err
}
}
return nil
}