-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
agent_poller.go
412 lines (363 loc) · 11.3 KB
/
agent_poller.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
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2018 Red Hat, Inc.
*
*/
package agentpoller
import (
"sync"
"time"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/cli"
)
// AgentCommand is a command executable on guest agent
type AgentCommand string
// Aliases for commands executed on guest agent
// TODO: when updated to libvirt 5.6.0 this can change to libvirt types
// Aliases are also used as keys to the store, it does not matter how the keys are named,
// only whether it relates to the right data
const (
GET_OSINFO AgentCommand = "guest-get-osinfo"
GET_HOSTNAME AgentCommand = "guest-get-host-name"
GET_INTERFACES AgentCommand = "guest-network-get-interfaces"
GET_TIMEZONE AgentCommand = "guest-get-timezone"
GET_USERS AgentCommand = "guest-get-users"
GET_FILESYSTEM AgentCommand = "guest-get-fsinfo"
GET_AGENT AgentCommand = "guest-info"
GET_FSFREEZE_STATUS AgentCommand = "guest-fsfreeze-status"
pollInitialInterval = 10 * time.Second
)
// AgentUpdatedEvent fire up when data is changes in the store
type AgentUpdatedEvent struct {
DomainInfo api.DomainGuestInfo
}
// AsyncAgentStore stores the agent data converted to api domain objects
// it offers methods to get the data and fire up an event when there
// is a change of the data
type AsyncAgentStore struct {
store sync.Map
AgentUpdated chan AgentUpdatedEvent
}
// NewAsyncAgentStore creates new agent store
func NewAsyncAgentStore() AsyncAgentStore {
return AsyncAgentStore{
store: sync.Map{},
AgentUpdated: make(chan AgentUpdatedEvent, 10),
}
}
// Store saves the value with a key to the storage, when there is a change in data
// it fires up updated event
func (s *AsyncAgentStore) Store(key AgentCommand, value interface{}) {
oldData, _ := s.store.Load(key)
updated := (oldData == nil) || !equality.Semantic.DeepEqual(oldData, value)
s.store.Store(key, value)
if updated {
domainInfo := api.DomainGuestInfo{}
switch key {
case GET_OSINFO, GET_INTERFACES, GET_FSFREEZE_STATUS:
domainInfo.OSInfo = s.GetGuestOSInfo()
domainInfo.Interfaces = s.GetInterfaceStatus()
domainInfo.FSFreezeStatus = s.GetFSFreezeStatus()
}
s.AgentUpdated <- AgentUpdatedEvent{
DomainInfo: domainInfo,
}
}
}
// GetSysInfo returns the sysInfo information packed together.
// Sysinfo comprises of:
// - Guest Hostname
// - Guest OS version and architecture
// - Guest Timezone
func (s *AsyncAgentStore) GetSysInfo() api.DomainSysInfo {
data, ok := s.store.Load(GET_OSINFO)
osinfo := api.GuestOSInfo{}
if ok {
osinfo = data.(api.GuestOSInfo)
}
data, ok = s.store.Load(GET_HOSTNAME)
hostname := ""
if ok {
hostname = data.(string)
}
data, ok = s.store.Load(GET_TIMEZONE)
timezone := api.Timezone{}
if ok {
timezone = data.(api.Timezone)
}
return api.DomainSysInfo{
Hostname: hostname,
OSInfo: osinfo,
Timezone: timezone,
}
}
// GetInterfaceStatus returns the interfaces Guest Agent reported
func (s *AsyncAgentStore) GetInterfaceStatus() []api.InterfaceStatus {
data, ok := s.store.Load(GET_INTERFACES)
if ok {
return data.([]api.InterfaceStatus)
}
return nil
}
// GetGuestOSInfo returns the Guest OS version and architecture
func (s *AsyncAgentStore) GetGuestOSInfo() *api.GuestOSInfo {
data, ok := s.store.Load(GET_OSINFO)
if ok {
osInfo := data.(api.GuestOSInfo)
return &osInfo
}
return nil
}
// GetGA returns guest agent record with its version if present
func (s *AsyncAgentStore) GetGA() AgentInfo {
data, ok := s.store.Load(GET_AGENT)
agent := AgentInfo{}
if !ok {
return agent
}
agent = data.(AgentInfo)
return agent
}
// GetFSFreezeStatus returns the Guest fsfreeze status
func (s *AsyncAgentStore) GetFSFreezeStatus() *api.FSFreeze {
data, ok := s.store.Load(GET_FSFREEZE_STATUS)
if !ok {
return nil
}
fsfreezeStatus := data.(api.FSFreeze)
return &fsfreezeStatus
}
// GetFS returns the filesystem list limited to the limit set
// set limit to -1 to return the whole list
func (s *AsyncAgentStore) GetFS(limit int) []api.Filesystem {
data, ok := s.store.Load(GET_FILESYSTEM)
filesystems := []api.Filesystem{}
if !ok {
return filesystems
}
filesystems = data.([]api.Filesystem)
if len(filesystems) <= limit || limit == -1 {
return filesystems
}
limitedFilesystems := make([]api.Filesystem, limit)
copy(limitedFilesystems, filesystems[:limit])
return limitedFilesystems
}
// GetUsers return the use list limited to the limit set
// set limit to -1 to return all users
func (s *AsyncAgentStore) GetUsers(limit int) []api.User {
data, ok := s.store.Load(GET_USERS)
users := []api.User{}
if !ok {
return users
}
users = data.([]api.User)
if len(users) <= limit || limit == -1 {
return users
}
limitedUsers := make([]api.User, limit)
copy(limitedUsers, users[:limit])
return limitedUsers
}
// PollerWorker collects the data from the guest agent
// only unique items are stored as configuration
type PollerWorker struct {
// AgentCommands is a list of commands executed on the guestAgent
AgentCommands []AgentCommand
// CallTick is how often to call this set of commands
CallTick time.Duration
}
type agentCommandsExecutor func(commands []AgentCommand)
// Poll is the call to the guestagent.
func (p *PollerWorker) Poll(execAgentCommands agentCommandsExecutor, closeChan chan struct{}, initialInterval time.Duration) {
log.Log.Infof("Polling command: %v", p.AgentCommands)
// Do the first round to fill the cache immediately.
execAgentCommands(p.AgentCommands)
pollMaxInterval := p.CallTick
pollInterval := pollMaxInterval
if initialInterval < pollMaxInterval {
pollInterval = initialInterval
}
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
for {
select {
case <-closeChan:
return
case <-ticker.C:
execAgentCommands(p.AgentCommands)
}
if pollInterval < pollMaxInterval {
pollInterval = incrementPollInterval(pollInterval, pollMaxInterval)
ticker.Reset(pollInterval)
}
}
}
func incrementPollInterval(interval time.Duration, maxInterval time.Duration) time.Duration {
interval *= 2
if interval > maxInterval {
interval = maxInterval
}
return interval
}
type AgentPoller struct {
Connection cli.Connection
VmiUID types.UID
domainName string
agentDone chan struct{}
workers []PollerWorker
agentStore *AsyncAgentStore
}
// CreatePoller creates the new structure that holds guest agent pollers
func CreatePoller(
connecton cli.Connection,
vmiUID types.UID,
domainName string,
store *AsyncAgentStore,
qemuAgentSysInterval time.Duration,
qemuAgentFileInterval time.Duration,
qemuAgentUserInterval time.Duration,
qemuAgentVersionInterval time.Duration,
qemuAgentFSFreezeStatusInterval time.Duration,
) *AgentPoller {
p := &AgentPoller{
Connection: connecton,
VmiUID: vmiUID,
domainName: domainName,
agentStore: store,
workers: []PollerWorker{},
}
// version command group
p.workers = append(p.workers, PollerWorker{
CallTick: qemuAgentVersionInterval,
AgentCommands: []AgentCommand{GET_AGENT},
})
// sys command group
p.workers = append(p.workers, PollerWorker{
CallTick: qemuAgentSysInterval,
AgentCommands: []AgentCommand{GET_INTERFACES, GET_OSINFO, GET_TIMEZONE, GET_HOSTNAME},
})
// filesystem command group
p.workers = append(p.workers, PollerWorker{
CallTick: qemuAgentFileInterval,
AgentCommands: []AgentCommand{GET_FILESYSTEM},
})
// user command group
p.workers = append(p.workers, PollerWorker{
CallTick: qemuAgentUserInterval,
AgentCommands: []AgentCommand{GET_USERS},
})
// fsfreeze command group
p.workers = append(p.workers, PollerWorker{
CallTick: qemuAgentFSFreezeStatusInterval,
AgentCommands: []AgentCommand{GET_FSFREEZE_STATUS},
})
return p
}
// Start the poller workers
func (p *AgentPoller) Start() {
if p.agentDone != nil {
return
}
p.agentDone = make(chan struct{})
for i := 0; i < len(p.workers); i++ {
log.Log.Infof("Starting agent poller with commands: %v", p.workers[i].AgentCommands)
go p.workers[i].Poll(func(commands []AgentCommand) {
executeAgentCommands(commands, p.Connection, p.agentStore, p.domainName)
}, p.agentDone, pollInitialInterval)
}
}
// Stop all poller workers
func (p *AgentPoller) Stop() {
if p.agentDone != nil {
close(p.agentDone)
p.agentDone = nil
}
}
// With libvirt 5.6.0 direct call to agent can be replaced with call to libvirt Domain.GetGuestInfo
func executeAgentCommands(commands []AgentCommand, con cli.Connection, agentStore *AsyncAgentStore, domainName string) {
for _, command := range commands {
// replace with direct call to libvirt function when 5.6.0 is available
cmdResult, err := con.QemuAgentCommand(`{"execute":"`+string(command)+`"}`, domainName)
if err != nil {
// skip the command on error, it is not vital
continue
}
// parse the json data and convert to domain api
// for libvirt 5.6.0 json conversion deprecated
switch command {
case GET_INTERFACES:
interfaces, err := parseInterfaces(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent interface %s", err.Error())
continue
}
agentStore.Store(GET_INTERFACES, interfaces)
case GET_OSINFO:
osInfo, err := parseGuestOSInfo(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent guestosinfo %s", err.Error())
continue
}
agentStore.Store(GET_OSINFO, osInfo)
case GET_HOSTNAME:
hostname, err := parseHostname(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent hostname %s", err.Error())
continue
}
agentStore.Store(GET_HOSTNAME, hostname)
case GET_TIMEZONE:
timezone, err := parseTimezone(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent timezone %s", err.Error())
continue
}
agentStore.Store(GET_TIMEZONE, timezone)
case GET_USERS:
users, err := parseUsers(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent users %s", err.Error())
continue
}
agentStore.Store(GET_USERS, users)
case GET_FSFREEZE_STATUS:
fsfreezeStatus, err := ParseFSFreezeStatus(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent fsfreeze status %s", err.Error())
continue
}
agentStore.Store(GET_FSFREEZE_STATUS, fsfreezeStatus)
case GET_FILESYSTEM:
filesystems, err := parseFilesystem(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent filesystem %s", err.Error())
continue
}
agentStore.Store(GET_FILESYSTEM, filesystems)
case GET_AGENT:
agent, err := parseAgent(cmdResult)
if err != nil {
log.Log.Errorf("Cannot parse guest agent information %s", err.Error())
continue
}
agentStore.Store(GET_AGENT, agent)
}
}
}