forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcriservice.go
141 lines (122 loc) · 4.2 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
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
package cri
import (
"fmt"
"github.com/alibaba/pouch/cri/stream"
criv1alpha1 "github.com/alibaba/pouch/cri/v1alpha1"
servicev1alpha1 "github.com/alibaba/pouch/cri/v1alpha1/service"
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/sirupsen/logrus"
)
// 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 "v1alpha1":
err = runv1alpha1(daemonconfig, containerMgr, imageMgr, streamRouterCh, readyCh)
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 v1alpha1 or v1alpha2", daemonconfig.CriConfig.CriVersion)
}
}
// Start CRI service with CRI version: v1alpha1
func runv1alpha1(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, streamRouterCh chan stream.Router, readyCh chan bool) error {
logrus.Infof("Start CRI service with CRI version: v1alpha1")
criMgr, err := criv1alpha1.NewCriManager(daemonconfig, containerMgr, imageMgr)
if err != nil {
streamRouterCh <- nil
readyCh <- false
return fmt.Errorf("failed to get CriManager with error: %v", err)
}
service, err := servicev1alpha1.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()
logrus.Infof("CRI Stream server stopped")
}()
streamRouterCh <- nil
}
go func() {
errChan <- service.Serve()
logrus.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
}
}
logrus.Infof("CRI service stopped")
return nil
}
// 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 {
logrus.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()
logrus.Infof("CRI Stream server stopped")
}()
streamRouterCh <- nil
}
go func() {
errChan <- service.Serve()
logrus.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
}
}
logrus.Infof("CRI service stopped")
return nil
}