forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
266 lines (205 loc) · 9.14 KB
/
agent.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
// Copyright (c) 2016 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"fmt"
"syscall"
"time"
"github.com/hfyeh/agent/protocols/grpc"
persistapi "github.com/hfyeh/runtime/virtcontainers/persist/api"
vcTypes "github.com/hfyeh/runtime/virtcontainers/pkg/types"
"github.com/hfyeh/runtime/virtcontainers/types"
"github.com/mitchellh/mapstructure"
specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/net/context"
)
// AgentType describes the type of guest agent a Sandbox should run.
type AgentType string
// ProcessListOptions contains the options used to list running
// processes inside the container
type ProcessListOptions struct {
// Format describes the output format to list the running processes.
// Formats are unrelated to ps(1) formats, only two formats can be specified:
// "json" and "table"
Format string
// Args contains the list of arguments to run ps(1) command.
// If Args is empty the agent will use "-ef" as options to ps(1).
Args []string
}
// ProcessList represents the list of running processes inside the container
type ProcessList []byte
const (
// NoopAgentType is the No-Op agent.
NoopAgentType AgentType = "noop"
// KataContainersAgent is the Kata Containers agent.
KataContainersAgent AgentType = "kata"
// SocketTypeVSOCK is a VSOCK socket type for talking to an agent.
SocketTypeVSOCK = "vsock"
// SocketTypeUNIX is a UNIX socket type for talking to an agent.
// It typically means the agent is living behind a host proxy.
SocketTypeUNIX = "unix"
)
// Set sets an agent type based on the input string.
func (agentType *AgentType) Set(value string) error {
switch value {
case "noop":
*agentType = NoopAgentType
return nil
case "kata":
*agentType = KataContainersAgent
return nil
default:
return fmt.Errorf("Unknown agent type %s", value)
}
}
// String converts an agent type to a string.
func (agentType *AgentType) String() string {
switch *agentType {
case NoopAgentType:
return string(NoopAgentType)
case KataContainersAgent:
return string(KataContainersAgent)
default:
return ""
}
}
// newAgent returns an agent from an agent type.
func newAgent(agentType AgentType) agent {
switch agentType {
case NoopAgentType:
return &noopAgent{}
case KataContainersAgent:
return &kataAgent{}
default:
return &noopAgent{}
}
}
// newAgentConfig returns an agent config from a generic SandboxConfig interface.
func newAgentConfig(agentType AgentType, agentConfig interface{}) (interface{}, error) {
switch agentType {
case NoopAgentType:
return nil, nil
case KataContainersAgent:
var kataAgentConfig KataAgentConfig
err := mapstructure.Decode(agentConfig, &kataAgentConfig)
if err != nil {
return nil, err
}
return kataAgentConfig, nil
default:
return nil, nil
}
}
// agent is the virtcontainers agent interface.
// Agents are running in the guest VM and handling
// communications between the host and guest.
type agent interface {
// init is used to pass agent specific configuration to the agent implementation.
// agent implementations also will typically start listening for agent events from
// init().
// After init() is called, agent implementations should be initialized and ready
// to handle all other Agent interface methods.
init(ctx context.Context, sandbox *Sandbox, config interface{}) (disableVMShutdown bool, err error)
// capabilities should return a structure that specifies the capabilities
// supported by the agent.
capabilities() types.Capabilities
// check will check the agent liveness
check() error
// tell whether the agent is long live connected or not
longLiveConn() bool
// disconnect will disconnect the connection to the agent
disconnect() error
// start the proxy
startProxy(sandbox *Sandbox) error
// set to use an existing proxy
setProxy(sandbox *Sandbox, proxy proxy, pid int, url string) error
// set to use an existing proxy from Grpc
setProxyFromGrpc(proxy proxy, pid int, url string)
// get agent url
getAgentURL() (string, error)
// update the agent using some elements from another agent
reuseAgent(agent agent) error
// createSandbox will tell the agent to perform necessary setup for a Sandbox.
createSandbox(sandbox *Sandbox) error
// exec will tell the agent to run a command in an already running container.
exec(sandbox *Sandbox, c Container, cmd types.Cmd) (*Process, error)
// startSandbox will tell the agent to start all containers related to the Sandbox.
startSandbox(sandbox *Sandbox) error
// stopSandbox will tell the agent to stop all containers related to the Sandbox.
stopSandbox(sandbox *Sandbox) error
// createContainer will tell the agent to create a container related to a Sandbox.
createContainer(sandbox *Sandbox, c *Container) (*Process, error)
// startContainer will tell the agent to start a container related to a Sandbox.
startContainer(sandbox *Sandbox, c *Container) error
// stopContainer will tell the agent to stop a container related to a Sandbox.
stopContainer(sandbox *Sandbox, c Container) error
// signalProcess will tell the agent to send a signal to a
// container or a process related to a Sandbox. If all is true, all processes in
// the container will be sent the signal.
signalProcess(c *Container, processID string, signal syscall.Signal, all bool) error
// winsizeProcess will tell the agent to set a process' tty size
winsizeProcess(c *Container, processID string, height, width uint32) error
// writeProcessStdin will tell the agent to write a process stdin
writeProcessStdin(c *Container, ProcessID string, data []byte) (int, error)
// closeProcessStdin will tell the agent to close a process stdin
closeProcessStdin(c *Container, ProcessID string) error
// readProcessStdout will tell the agent to read a process stdout
readProcessStdout(c *Container, processID string, data []byte) (int, error)
// readProcessStderr will tell the agent to read a process stderr
readProcessStderr(c *Container, processID string, data []byte) (int, error)
// processListContainer will list the processes running inside the container
processListContainer(sandbox *Sandbox, c Container, options ProcessListOptions) (ProcessList, error)
// updateContainer will update the resources of a running container
updateContainer(sandbox *Sandbox, c Container, resources specs.LinuxResources) error
// waitProcess will wait for the exit code of a process
waitProcess(c *Container, processID string) (int32, error)
// onlineCPUMem will online CPUs and Memory inside the Sandbox.
// This function should be called after hot adding vCPUs or Memory.
// cpus specifies the number of CPUs that were added and the agent should online
// cpuOnly specifies that we should online cpu or online memory or both
onlineCPUMem(cpus uint32, cpuOnly bool) error
// memHotplugByProbe will notify the guest kernel about memory hotplug event through
// probe interface.
// This function should be called after hot adding Memory and before online memory.
// addr specifies the address of the recently hotplugged or unhotplugged memory device.
memHotplugByProbe(addr uint64, sizeMB uint32, memorySectionSizeMB uint32) error
// statsContainer will tell the agent to get stats from a container related to a Sandbox
statsContainer(sandbox *Sandbox, c Container) (*ContainerStats, error)
// pauseContainer will pause a container
pauseContainer(sandbox *Sandbox, c Container) error
// resumeContainer will resume a paused container
resumeContainer(sandbox *Sandbox, c Container) error
// configure will update agent settings based on provided arguments
configure(h hypervisor, id, sharePath string, builtin bool, config interface{}) error
// configureFromGrpc will update agent settings based on provided arguments which from Grpc
configureFromGrpc(h hypervisor, id string, builtin bool, config interface{}) error
// reseedRNG will reseed the guest random number generator
reseedRNG(data []byte) error
// updateInterface will tell the agent to update a nic for an existed Sandbox.
updateInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error)
// listInterfaces will tell the agent to list interfaces of an existed Sandbox
listInterfaces() ([]*vcTypes.Interface, error)
// updateRoutes will tell the agent to update route table for an existed Sandbox.
updateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error)
// listRoutes will tell the agent to list routes of an existed Sandbox
listRoutes() ([]*vcTypes.Route, error)
// getGuestDetails will tell the agent to get some information of guest
getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error)
// setGuestDateTime asks the agent to set guest time to the provided one
setGuestDateTime(time.Time) error
// copyFile copies file from host to container's rootfs
copyFile(src, dst string) error
// markDead tell agent that the guest is dead
markDead()
// cleanup removes all on disk information generated by the agent
cleanup(s *Sandbox)
// return data for saving
save() persistapi.AgentState
// load data from disk
load(persistapi.AgentState)
// getOOMEvent will wait on OOM events that occur in the sandbox.
// Will return the ID of the container where the event occurred.
getOOMEvent() (string, error)
}