/
criservice.go
86 lines (75 loc) · 2.63 KB
/
criservice.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
package cri
import (
"fmt"
"github.com/alibaba/pouch/cri/stream"
criv1alpha2 "github.com/alibaba/pouch/cri/v1alpha2"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/hookplugins"
"github.com/alibaba/pouch/pkg/log"
)
// RunCriService start cri service if pouchd is specified with --enable-cri.
func RunCriService(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, volumeMgr mgr.VolumeMgr, criPlugin hookplugins.CriPlugin, streamRouterCh chan stream.Router, stopCh chan error, readyCh chan bool) {
var err error
defer func() {
stopCh <- err
close(stopCh)
}()
if !daemonconfig.IsCriEnabled {
// the CriService has been disabled, so send Ready and empty Stream Router
streamRouterCh <- nil
readyCh <- true
return
}
switch daemonconfig.CriConfig.CriVersion {
case "v1alpha2":
err = runv1alpha2(daemonconfig, containerMgr, imageMgr, volumeMgr, criPlugin, streamRouterCh, readyCh)
default:
streamRouterCh <- nil
readyCh <- false
err = fmt.Errorf("failed to start CRI service: invalid CRI version %s, expected to be v1alpha2", daemonconfig.CriConfig.CriVersion)
}
}
// Start CRI service with CRI version: v1alpha2
func runv1alpha2(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, volumeMgr mgr.VolumeMgr, criPlugin hookplugins.CriPlugin, streamRouterCh chan stream.Router, readyCh chan bool) error {
log.With(nil).Infof("Start CRI service with CRI version: v1alpha2")
criMgr, err := criv1alpha2.NewCriManager(daemonconfig, containerMgr, imageMgr, volumeMgr, criPlugin)
if err != nil {
streamRouterCh <- nil
readyCh <- false
return fmt.Errorf("failed to get CriManager with error: %v", err)
}
service, err := criv1alpha2.NewService(daemonconfig, criMgr)
if err != nil {
streamRouterCh <- nil
readyCh <- false
return fmt.Errorf("failed to start CRI service with error: %v", err)
}
errChan := make(chan error, 2)
// If the cri stream server share the port with pouchd,
// export the its router. Otherwise launch it.
if daemonconfig.CriConfig.StreamServerReusePort {
errChan = make(chan error, 1)
streamRouterCh <- criMgr.StreamRouter()
} else {
go func() {
errChan <- criMgr.StreamServerStart()
log.With(nil).Infof("CRI Stream server stopped")
}()
streamRouterCh <- nil
}
go func() {
errChan <- service.Serve()
log.With(nil).Infof("CRI GRPC server stopped")
}()
// the criservice has set up, send Ready
readyCh <- true
// Check for error
for i := 0; i < cap(errChan); i++ {
if err := <-errChan; err != nil {
return err
}
}
log.With(nil).Infof("CRI service stopped")
return nil
}