/
device.go
361 lines (306 loc) · 11.9 KB
/
device.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
package store
import (
"context"
b64 "encoding/base64"
"encoding/json"
"errors"
"reflect"
api "github.com/flightctl/flightctl/api/v1alpha1"
"github.com/flightctl/flightctl/internal/flterrors"
"github.com/flightctl/flightctl/internal/store/model"
"github.com/flightctl/flightctl/internal/util"
"github.com/flightctl/flightctl/pkg/log"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type Device interface {
Create(ctx context.Context, orgId uuid.UUID, device *api.Device, callback DeviceStoreCallback) (*api.Device, error)
List(ctx context.Context, orgId uuid.UUID, listParams ListParams) (*api.DeviceList, error)
Get(ctx context.Context, orgId uuid.UUID, name string) (*api.Device, error)
CreateOrUpdate(ctx context.Context, orgId uuid.UUID, device *api.Device, fieldsToUnset []string, fromAPI bool, callback DeviceStoreCallback) (*api.Device, bool, error)
UpdateStatus(ctx context.Context, orgId uuid.UUID, device *api.Device) (*api.Device, error)
DeleteAll(ctx context.Context, orgId uuid.UUID, callback DeviceStoreAllDeletedCallback) error
Delete(ctx context.Context, orgId uuid.UUID, name string, callback DeviceStoreCallback) error
UpdateAnnotations(ctx context.Context, orgId uuid.UUID, name string, annotations map[string]string, deleteKeys []string) error
GetRendered(ctx context.Context, orgId uuid.UUID, name string, knownOwner, knownTemplateVersion *string) (*api.RenderedDeviceSpec, error)
InitialMigration() error
}
type DeviceStore struct {
db *gorm.DB
log logrus.FieldLogger
}
type DeviceStoreCallback func(before *model.Device, after *model.Device)
type DeviceStoreAllDeletedCallback func(orgId uuid.UUID)
// Make sure we conform to Device interface
var _ Device = (*DeviceStore)(nil)
func NewDevice(db *gorm.DB, log logrus.FieldLogger) Device {
return &DeviceStore{db: db, log: log}
}
func (s *DeviceStore) InitialMigration() error {
if err := s.db.AutoMigrate(&model.Device{}); err != nil {
return err
}
// TODO: generalize this for fleet, enrollmentrequest, etc. Make part of the base resource
if !s.db.Migrator().HasIndex(&model.Device{}, "device_labels") {
// see https://github.com/go-gorm/gorm/discussions/6695
if s.db.Dialector.Name() == "postgres" {
// GiST index could also be used: https://www.postgresql.org/docs/9.1/textsearch-indexes.html
if err := s.db.Exec("CREATE INDEX device_labels ON devices USING GIN (labels)").Error; err != nil {
return err
}
} else {
return s.db.Migrator().CreateIndex(&model.Device{}, "Labels")
}
}
return nil
}
func (s *DeviceStore) Create(ctx context.Context, orgId uuid.UUID, resource *api.Device, callback DeviceStoreCallback) (*api.Device, error) {
if resource == nil {
return nil, flterrors.ErrResourceIsNil
}
device := model.NewDeviceFromApiResource(resource)
device.OrgID = orgId
device.Generation = util.Int64ToPtr(1)
result := s.db.Create(device)
callback(nil, device)
return resource, flterrors.ErrorFromGormError(result.Error)
}
func (s *DeviceStore) List(ctx context.Context, orgId uuid.UUID, listParams ListParams) (*api.DeviceList, error) {
var devices model.DeviceList
var nextContinue *string
var numRemaining *int64
query := BuildBaseListQuery(s.db.Model(&devices), orgId, listParams)
if listParams.Limit > 0 {
// Request 1 more than the user asked for to see if we need to return "continue"
query = AddPaginationToQuery(query, listParams.Limit+1, listParams.Continue)
}
result := query.Find(&devices)
// If we got more than the user requested, remove one record and calculate "continue"
if listParams.Limit > 0 && len(devices) > listParams.Limit {
nextContinueStruct := Continue{
Name: devices[len(devices)-1].Name,
Version: CurrentContinueVersion,
}
devices = devices[:len(devices)-1]
var numRemainingVal int64
if listParams.Continue != nil {
numRemainingVal = listParams.Continue.Count - int64(listParams.Limit)
if numRemainingVal < 1 {
numRemainingVal = 1
}
} else {
countQuery := BuildBaseListQuery(s.db.Model(&devices), orgId, listParams)
numRemainingVal = CountRemainingItems(countQuery, nextContinueStruct.Name)
}
nextContinueStruct.Count = numRemainingVal
contByte, _ := json.Marshal(nextContinueStruct)
contStr := b64.StdEncoding.EncodeToString(contByte)
nextContinue = &contStr
numRemaining = &numRemainingVal
}
apiDevicelist := devices.ToApiResource(nextContinue, numRemaining)
return &apiDevicelist, flterrors.ErrorFromGormError(result.Error)
}
func (s *DeviceStore) DeleteAll(ctx context.Context, orgId uuid.UUID, callback DeviceStoreAllDeletedCallback) error {
condition := model.Device{}
result := s.db.Unscoped().Where("org_id = ?", orgId).Delete(&condition)
if result.Error != nil {
return flterrors.ErrorFromGormError(result.Error)
}
callback(orgId)
return nil
}
func (s *DeviceStore) Get(ctx context.Context, orgId uuid.UUID, name string) (*api.Device, error) {
device := model.Device{
Resource: model.Resource{OrgID: orgId, Name: name},
}
result := s.db.First(&device)
if result.Error != nil {
return nil, flterrors.ErrorFromGormError(result.Error)
}
apiDevice := device.ToApiResource()
return &apiDevice, nil
}
func (s *DeviceStore) CreateOrUpdate(ctx context.Context, orgId uuid.UUID, resource *api.Device, fieldsToUnset []string, fromAPI bool, callback DeviceStoreCallback) (*api.Device, bool, error) {
if resource == nil {
return nil, false, flterrors.ErrResourceIsNil
}
if resource.Metadata.Name == nil {
return nil, false, flterrors.ErrResourceNameIsNil
}
device := model.NewDeviceFromApiResource(resource)
device.OrgID = orgId
created := false
var existingRecord *model.Device
err := s.db.Transaction(func(innerTx *gorm.DB) (err error) {
existingRecord = &model.Device{Resource: model.Resource{OrgID: device.OrgID, Name: device.Name}}
result := innerTx.First(existingRecord)
deviceExists := true
// NotFound is OK because in that case we will create the record, anything else is a real error
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
deviceExists = false
} else {
return flterrors.ErrorFromGormError(result.Error)
}
}
// We returned
if !deviceExists {
created = true
device.Generation = util.Int64ToPtr(1)
result = innerTx.Create(device)
if result.Error != nil {
return flterrors.ErrorFromGormError(result.Error)
}
} else {
sameSpec := reflect.DeepEqual(existingRecord.Spec.Data, device.Spec.Data)
if fromAPI && util.DefaultIfNil(existingRecord.Spec.Data.TemplateVersion, "") != util.DefaultIfNil(device.Spec.Data.TemplateVersion, "") {
return flterrors.ErrUpdatingTemplateVerionNotAllowed
}
// Update the generation if the spec was updated
if !sameSpec {
if fromAPI && existingRecord.Owner != nil && len(*existingRecord.Owner) != 0 {
return flterrors.ErrUpdatingResourceWithOwnerNotAllowed
}
if existingRecord.Generation == nil {
device.Generation = util.Int64ToPtr(1)
} else {
device.Generation = util.Int64ToPtr(*existingRecord.Generation + 1)
}
} else {
device.Generation = existingRecord.Generation
}
where := model.Device{Resource: model.Resource{OrgID: device.OrgID, Name: device.Name}}
query := innerTx.Model(where)
selectFields := []string{"spec"}
selectFields = append(selectFields, GetNonNilFieldsFromResource(device.Resource)...)
if len(fieldsToUnset) > 0 {
selectFields = append(selectFields, fieldsToUnset...)
}
query = query.Select(selectFields)
result := query.Updates(&device)
if result.Error != nil {
return flterrors.ErrorFromGormError(result.Error)
}
}
return nil
})
if err != nil {
return nil, false, err
}
if existingRecord != nil {
existingRecord.Owner = nil // Match the incoming device
}
callback(existingRecord, device)
updatedResource := device.ToApiResource()
return &updatedResource, created, nil
}
func (s *DeviceStore) UpdateStatus(ctx context.Context, orgId uuid.UUID, resource *api.Device) (*api.Device, error) {
if resource == nil {
return nil, flterrors.ErrResourceIsNil
}
if resource.Metadata.Name == nil {
return nil, flterrors.ErrResourceNameIsNil
}
device := model.Device{
Resource: model.Resource{OrgID: orgId, Name: *resource.Metadata.Name},
}
result := s.db.Model(&device).Updates(map[string]interface{}{
"status": model.MakeJSONField(resource.Status),
})
return resource, flterrors.ErrorFromGormError(result.Error)
}
func (s *DeviceStore) Delete(ctx context.Context, orgId uuid.UUID, name string, callback DeviceStoreCallback) error {
var existingRecord model.Device
log := log.WithReqIDFromCtx(ctx, s.log)
err := s.db.Transaction(func(innerTx *gorm.DB) (err error) {
existingRecord = model.Device{Resource: model.Resource{OrgID: orgId, Name: name}}
result := innerTx.First(&existingRecord)
if result.Error != nil {
return flterrors.ErrorFromGormError(result.Error)
}
associatedRecord := model.EnrollmentRequest{Resource: model.Resource{OrgID: orgId, Name: name}}
if err := innerTx.Unscoped().Delete(&existingRecord).Error; err != nil {
return flterrors.ErrorFromGormError(err)
}
if err := innerTx.Unscoped().Delete(&associatedRecord).Error; err != nil {
log.Warningf("failed to delete associated enrollment request: %v", err)
}
return nil
})
if err != nil {
if errors.Is(err, flterrors.ErrResourceNotFound) {
return nil
}
return err
}
callback(&existingRecord, nil)
return nil
}
func (s *DeviceStore) UpdateAnnotations(ctx context.Context, orgId uuid.UUID, name string, annotations map[string]string, deleteKeys []string) error {
return s.db.Transaction(func(innerTx *gorm.DB) (err error) {
existingRecord := model.Device{Resource: model.Resource{OrgID: orgId, Name: name}}
result := innerTx.First(&existingRecord)
if result.Error != nil {
return flterrors.ErrorFromGormError(result.Error)
}
existingAnnotations := util.LabelArrayToMap(existingRecord.Annotations)
existingAnnotations = util.MergeLabels(existingAnnotations, annotations)
for _, deleteKey := range deleteKeys {
delete(existingAnnotations, deleteKey)
}
annotationsArray := util.LabelMapToArray(&existingAnnotations)
result = innerTx.Model(existingRecord).Updates(map[string]interface{}{
"annotations": pq.StringArray(annotationsArray),
})
return flterrors.ErrorFromGormError(result.Error)
})
}
func (s *DeviceStore) GetRendered(ctx context.Context, orgId uuid.UUID, name string, knownOwner, knownTemplateVersion *string) (*api.RenderedDeviceSpec, error) {
var deviceOwner string
var templateVersion *model.TemplateVersion
err := s.db.Transaction(func(innerTx *gorm.DB) (err error) {
device := &model.Device{Resource: model.Resource{OrgID: orgId, Name: name}}
result := innerTx.First(device)
if result.Error != nil {
return flterrors.ErrorFromGormError(result.Error)
}
if device.Owner == nil {
return flterrors.ErrResourceOwnerIsNil
}
if device.Spec.Data.TemplateVersion == nil {
return flterrors.ErrTemplateVersionIsNil
}
if knownOwner != nil && knownTemplateVersion != nil && *device.Owner == *knownOwner && *device.Spec.Data.TemplateVersion == *knownTemplateVersion {
return nil
}
templateVersion = &model.TemplateVersion{
ResourceWithPrimaryKeyOwner: model.ResourceWithPrimaryKeyOwner{OrgID: orgId, Owner: device.Owner, Name: *device.Spec.Data.TemplateVersion},
}
result = s.db.First(templateVersion)
if result.Error != nil {
return flterrors.ErrorFromGormError(result.Error)
}
return nil
})
if err != nil {
return nil, err
}
if templateVersion == nil {
return nil, nil
}
if templateVersion.Valid == nil || !*templateVersion.Valid {
return nil, flterrors.ErrInvalidTemplateVersion
}
renderedConfig := api.RenderedDeviceSpec{
Owner: deviceOwner,
TemplateVersion: templateVersion.Name,
Config: templateVersion.RenderedConfig,
Containers: templateVersion.Status.Data.Containers,
Os: templateVersion.Status.Data.Os,
Systemd: templateVersion.Status.Data.Systemd,
}
return &renderedConfig, nil
}