forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_io.go
207 lines (168 loc) · 4.13 KB
/
container_io.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
package containerio
import (
"fmt"
"io"
"github.com/alibaba/pouch/pkg/ringbuff"
"github.com/sirupsen/logrus"
)
const (
stdout stdioType = iota
stderr
stdin
discard
)
type stdioType int
func (t stdioType) String() string {
switch t {
case stdout:
return "STDOUT"
case stdin:
return "STDIN"
case stderr:
return "STDERR"
case discard:
return "DISCARD"
}
return "INVALID"
}
// IO wraps the three container's ios of stdout, stderr, stdin.
type IO struct {
Stdout *ContainerIO
Stderr *ContainerIO
Stdin *ContainerIO
}
// NewIO creates the container's ios of stdout, stderr, stdin.
func NewIO(opt *Option) *IO {
backends := createBackend(opt)
return &IO{
Stdout: create(opt, stdout, backends),
Stderr: create(opt, stderr, backends),
Stdin: create(opt, stdin, backends),
}
}
// ContainerIO used to control the container's stdio.
type ContainerIO struct {
Option
backends map[string]containerBackend
total int64
typ stdioType
closed bool
}
func create(opt *Option, typ stdioType, backends map[string]containerBackend) *ContainerIO {
io := &ContainerIO{
backends: backends,
total: 0,
typ: typ,
closed: false,
Option: *opt,
}
if typ == stdin {
io.backends = make(map[string]containerBackend)
for _, b := range backends {
if b.backend.Name() == opt.stdinBackend {
io.backends[opt.stdinBackend] = b
break
}
}
}
return io
}
func createBackend(opt *Option) map[string]containerBackend {
backends := make(map[string]containerBackend)
for _, create := range backendFactorys {
backend := create()
if _, ok := opt.backends[backend.Name()]; !ok {
continue
}
if err := backend.Init(opt); err != nil {
// FIXME skip the backend.
logrus.Errorf("failed to initialize backend: %s, id: %s, %v", backend.Name(), opt.id, err)
continue
}
backends[backend.Name()] = containerBackend{
backend: backend,
ring: ringbuff.New(10),
}
}
// start to subscribe ring buffer.
for _, b := range backends {
// the goroutine don't exit forever.
go func(b containerBackend) {
subscribe(b.backend.Name(), opt.id, b.ring, b.backend.Out())
}(b)
}
return backends
}
// OpenStdin returns open container's stdin or not.
func (io *ContainerIO) OpenStdin() bool {
if io.typ != stdin {
return false
}
if io.closed {
return false
}
return len(io.backends) != 0
}
// Read implements the standard Read interface.
func (io *ContainerIO) Read(p []byte) (int, error) {
if io.typ != stdin {
return 0, fmt.Errorf("invalid container io type: %s, id: %s", io.typ, io.id)
}
if io.closed {
return 0, fmt.Errorf("container io is closed")
}
if len(io.backends) == 0 {
block := make(chan struct{})
<-block
}
backend := io.backends[io.stdinBackend]
return backend.backend.In().Read(p)
}
// Write implements the standard Write interface.
func (io *ContainerIO) Write(data []byte) (int, error) {
if io.typ == stdin {
return 0, fmt.Errorf("invalid container io type: %s, id: %s", io.typ, io.id)
}
if io.closed {
return 0, fmt.Errorf("container io is closed")
}
if io.typ == discard {
return len(data), nil
}
for _, b := range io.backends {
if cover := b.ring.Push(data); cover {
logrus.Warnf("cover data, backend: %s, id: %s", b.backend.Name(), io.id)
}
}
return len(data), nil
}
// Close implements the standard Close interface.
func (io *ContainerIO) Close() error {
for name, b := range io.backends {
b.backend.Close()
b.ring.Close()
logrus.Infof("close containerio backend: %s, id: %s", name, io.id)
}
io.closed = true
return nil
}
type containerBackend struct {
backend Backend
ring *ringbuff.RingBuff
}
// subscribe be called in a groutine.
func subscribe(name, id string, ring *ringbuff.RingBuff, out io.Writer) {
logrus.Infof("start to subscribe io, backend: %s, id: %s", name, id)
for {
value, closed := ring.Pop() // will block, if no element.
if b, ok := value.([]byte); ok {
if _, err := out.Write(b); err != nil {
logrus.Errorf("failed to write containerio backend: %s, id: %s, %v", name, id, err)
}
}
if value == nil && closed {
break
}
}
logrus.Infof("finished to subscribe io, backend: %s, id: %s", name, id)
}