-
Notifications
You must be signed in to change notification settings - Fork 111
/
node-managers-process-node.go
264 lines (235 loc) · 10.1 KB
/
node-managers-process-node.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
package pgdb
import (
"encoding/json"
"fmt"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/chef/automate/api/interservice/nodemanager/manager"
"github.com/chef/automate/api/interservice/nodemanager/nodes"
)
const sqlUpsertByIDRunData = `
INSERT INTO nodes
(id, name, platform, platform_version, source_state,
last_contact, source_id, source_region, source_account_id, last_run, projects_data, manager)
VALUES ($1, $2, $3, $4, $5, $6, NULLIF($7,''), NULLIF($8,''), NULLIF($9,''), $10, $11, $12)
ON CONFLICT (id)
DO UPDATE
SET name = $2, platform = $3, platform_version = $4, source_state = $5,
last_contact = $6, source_id = NULLIF($7,''), source_region = NULLIF($8,''), source_account_id = NULLIF($9,''), last_run = $10, projects_data = $11
WHERE nodes.source_state != 'TERMINATED';
`
const sqlUpsertByIDScanData = `
INSERT INTO nodes
(id, name, platform, platform_version, source_state,
last_contact, source_id, source_region, source_account_id, last_job, last_scan, projects_data, manager)
VALUES ($1, $2, $3, $4, $5, $6, NULLIF($7,''), NULLIF($8,''), NULLIF($9,''), $10, $11, $12, $13)
ON CONFLICT (id)
DO UPDATE
SET name = $2, platform = $3, platform_version = $4, source_state = $5,
last_contact = $6, source_id = NULLIF($7,''), source_region = NULLIF($8,''), source_account_id = NULLIF($9,''), last_job = $10, last_scan = $11, projects_data = $12
WHERE nodes.source_state != 'TERMINATED';
`
const sqlUpsertBySourceIDRunData = `
SELECT upsert_by_source_id_run_data($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);`
const sqlUpsertBySourceIDScanData = `
SELECT upsert_by_source_id_scan_data($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);`
const sqlGetCurrentRunStatus = `
SELECT coalesce(last_run ->> 'Status', '') AS status
FROM nodes
WHERE id = $1 OR source_id = $2 AND source_region = $3 AND source_account_id = $4;
`
const sqlGetCurrentScanStatus = `
SELECT coalesce(last_scan ->> 'Status', '') AS status
FROM nodes
WHERE id = $1 OR source_id = $2 AND source_region = $3 AND source_account_id = $4;
`
type lastContactData struct {
ID string
Status string
PenultimateStatus string
EndTime string
}
func (db *DB) ProcessIncomingNode(node *manager.NodeMetadata) error {
logrus.Debugf("processing node %s with uuid %s", node.Name, node.Uuid)
// if node.LastContact is less than 10 min ago, we can assume node to be in "running" state.
// if it is more than 10 min ago, we don't know what the state is, so we set to empty string
tenMinAgo := time.Now().UTC().Add(time.Minute * -10)
timestamp, err := ptypes.Timestamp(node.LastContact)
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to parse node last contact")
}
nodeState := ""
if timestamp.After(tenMinAgo) {
nodeState = manager.NodeState_RUNNING.String()
}
lastContact := ptypes.TimestampString(node.LastContact)
// note: we shouldn't be able to ever get here without a node id, because this function is called from the
// ingestion process, and the ingestion process will reject a report with no node uuid
if len(node.GetUuid()) == 0 {
return fmt.Errorf("no uuid included in message. aborting.")
}
// warn if manager is empty; it should not be
if len(node.GetManagerType()) == 0 {
logrus.Warnf("manager value is empty for node %s", node.GetName())
}
// the incoming node may hit any of these cases:
// 1) it is already registered in our db with same uuid as incoming report: update the node entry
// 2) it is already registered in our db with diff uuid, same source_id, region, acct id: update the node by source_id
// 3) it is not in our db, we must add it
lastContactInfo, err := db.handleIncomingLastContactData(node)
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to parse node last contact data")
}
lastContactDataByte, err := json.Marshal(lastContactInfo)
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to marshal last contact data")
}
projectsDataByte, err := json.Marshal(node.GetProjectsData())
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to marshal projects data")
}
err = Transact(db, func(tx *DBTrans) error {
logrus.Debugf("processing node %s with cloud info %s %s %s", node.GetName(), node.GetSourceId(), node.GetSourceAccountId(), node.GetSourceRegion())
nodeDetails := nodeDetails{
nodeState: nodeState,
lastContact: lastContact,
mgrType: node.GetManagerType(),
lastContactDataByte: lastContactDataByte,
projectsDataByte: projectsDataByte,
}
if !hasCloudInformation(node) {
if node.GetManagerType() == "azure-api" && node.SourceRegion == "" {
node.SourceRegion = "azure-api-region"
}
err = tx.upsertByID(node, nodeDetails)
} else {
node.Uuid, err = tx.upsertByCloudDetails(node, nodeDetails)
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to upsert with cloudDetails")
}
}
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to process node")
}
if node.ManagerId != "" {
_, err = tx.Exec(sqlInsertNodeManagerNode, node.ManagerId, node.Uuid)
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to create manager-node association")
}
}
tags, err := tx.addTags(node.GetTags())
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to add tags")
}
err = tx.tagNode(node.GetUuid(), tags)
if err != nil {
return errors.Wrap(err, "ProcessIncomingNode unable to tag node")
}
return tx.updateNodeProjects(node.GetUuid(), node.GetProjects())
})
return err
}
type nodeDetails struct {
nodeState string
lastContact string
mgrType string
lastContactDataByte []byte
projectsDataByte []byte
}
func (tx *DBTrans) upsertByID(node *manager.NodeMetadata, details nodeDetails) error {
var err error
if node.GetScanData() != nil {
_, err = tx.Exec(sqlUpsertByIDScanData, node.GetUuid(),
node.GetName(), node.GetPlatformName(), node.GetPlatformRelease(),
details.nodeState, details.lastContact, node.GetSourceId(), node.GetSourceRegion(), node.GetSourceAccountId(),
node.GetJobUuid(), details.lastContactDataByte, details.projectsDataByte, details.mgrType)
} else if node.GetRunData() != nil {
_, err = tx.Exec(sqlUpsertByIDRunData, node.GetUuid(),
node.GetName(), node.GetPlatformName(), node.GetPlatformRelease(),
details.nodeState, details.lastContact, node.GetSourceId(), node.GetSourceRegion(), node.GetSourceAccountId(),
details.lastContactDataByte, details.projectsDataByte, details.mgrType)
}
return err
}
func (tx *DBTrans) upsertByCloudDetails(node *manager.NodeMetadata, details nodeDetails) (string, error) {
var id string
var err error
if node.GetScanData() != nil {
id, err = tx.SelectStr(sqlUpsertBySourceIDScanData, node.GetUuid(),
node.GetName(), node.GetPlatformName(), node.GetPlatformRelease(),
details.nodeState, details.lastContact, node.GetSourceId(), node.GetSourceRegion(),
node.GetSourceAccountId(), node.GetJobUuid(), details.lastContactDataByte, details.projectsDataByte, details.mgrType)
} else if node.GetRunData() != nil {
id, err = tx.SelectStr(sqlUpsertBySourceIDRunData, node.GetUuid(),
node.GetName(), node.GetPlatformName(), node.GetPlatformRelease(),
details.nodeState, details.lastContact, node.GetSourceId(), node.GetSourceRegion(),
node.GetSourceAccountId(), details.lastContactDataByte, details.projectsDataByte, details.mgrType)
}
if len(id) > 0 {
logrus.Debugf("found match for node %s with details: %s %s %s. Node was updated.", node.GetName(), node.GetSourceId(), node.GetSourceRegion(), node.GetSourceAccountId())
return id, err
}
return node.Uuid, err
}
func (db *DB) handleIncomingLastContactData(node *manager.NodeMetadata) (lastContactData, error) {
var err error
var lastContactInfo lastContactData
if node.GetScanData() == nil && node.GetRunData() == nil {
msg := "invalid request: scan_data or run_data must be provided"
logrus.Errorf(msg)
return lastContactInfo, fmt.Errorf(msg)
}
if node.GetScanData() != nil {
lastContactInfo, err = translateToDBStruct(node.GetScanData())
if err != nil {
return lastContactInfo, errors.Wrap(err, "ProcessIncomingNode unable to translate struct to db struct")
}
lastContactInfo.PenultimateStatus, err = db.getCurrentScanStatus(node)
if err != nil {
return lastContactInfo, errors.Wrap(err, "ProcessIncomingNode unable to get current scan status")
}
} else if node.GetRunData() != nil {
lastContactInfo, err = translateToDBStruct(node.GetRunData())
if err != nil {
return lastContactInfo, errors.Wrap(err, "ProcessIncomingNode unable to translate struct to db struct")
}
lastContactInfo.PenultimateStatus, err = db.getCurrentRunStatus(node)
if err != nil {
return lastContactInfo, errors.Wrap(err, "ProcessIncomingNode unable to get current scan status")
}
}
return lastContactInfo, nil
}
func hasCloudInformation(node *manager.NodeMetadata) bool {
return len(node.GetSourceId()) != 0 && len(node.GetSourceAccountId()) != 0 && len(node.GetSourceRegion()) != 0
}
func translateToDBStruct(nodeData *nodes.LastContactData) (lastContactData, error) {
lastContactData := lastContactData{
ID: nodeData.GetId(),
Status: nodeData.GetStatus().String(),
PenultimateStatus: nodeData.GetPenultimateStatus().String(),
}
if nodeData.GetEndTime() != nil {
time := ptypes.TimestampString(nodeData.GetEndTime())
lastContactData.EndTime = time
}
return lastContactData, nil
}
func (db *DB) getCurrentScanStatus(node *manager.NodeMetadata) (string, error) {
status, err := db.SelectStr(sqlGetCurrentScanStatus, node.GetUuid(),
node.GetSourceId(), node.GetSourceRegion(), node.GetSourceAccountId())
if err != nil {
return "", errors.Wrap(err, "unable to read status")
}
return status, nil
}
func (db *DB) getCurrentRunStatus(node *manager.NodeMetadata) (string, error) {
status, err := db.SelectStr(sqlGetCurrentRunStatus, node.GetUuid(),
node.GetSourceId(), node.GetSourceRegion(), node.GetSourceAccountId())
if err != nil {
return "", errors.Wrap(err, "unable to read status")
}
return status, nil
}