-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathcontenttreetypes.go
246 lines (217 loc) · 8.95 KB
/
contenttreetypes.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
// Copyright (c) 2020 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"fmt"
"strings"
"time"
"github.com/google/go-cmp/cmp"
zconfig "github.com/lf-edge/eve-api/go/config"
"github.com/lf-edge/eve/pkg/pillar/base"
uuid "github.com/satori/go.uuid"
)
// KubeContainerImagePrefix : Container Image prefix adding to kubevirt container image name
const KubeContainerImagePrefix = "docker.io/"
// ContentTreeConfig specifies the needed information for content tree
// which might need to be downloaded and verified
type ContentTreeConfig struct {
ContentID uuid.UUID
DatastoreIDList []uuid.UUID
RelativeURL string
Format zconfig.Format // this is the format of the content tree itself, not necessarily of the datastore
ContentSha256 string
MaxDownloadSize uint64
GenerationCounter int64
DisplayName string
CustomMeta string
}
// Key is content info UUID which will be unique
func (config ContentTreeConfig) Key() string {
return config.ContentID.String()
}
// LogCreate :
func (config ContentTreeConfig) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.ContentTreeConfigLogType, config.DisplayName,
config.ContentID, config.LogKey())
if logObject == nil {
return
}
uuids := strings.Join(UuidsToStrings(config.DatastoreIDList), ",")
logObject.CloneAndAddField("datastore-ids", uuids).
AddField("relative-URL", config.RelativeURL).
AddField("format", config.Format).
AddField("content-sha256", config.ContentSha256).
AddField("max-download-size-int64", config.MaxDownloadSize).
Noticef("Content tree config create")
}
// LogModify :
func (config ContentTreeConfig) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.ContentTreeConfigLogType, config.DisplayName,
config.ContentID, config.LogKey())
oldConfig, ok := old.(ContentTreeConfig)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of ContentTreeConfig type")
}
uuids := strings.Join(UuidsToStrings(config.DatastoreIDList), ",")
oldUuids := strings.Join(UuidsToStrings(oldConfig.DatastoreIDList), ",")
if uuids != oldUuids ||
oldConfig.RelativeURL != config.RelativeURL ||
oldConfig.Format != config.Format ||
oldConfig.ContentSha256 != config.ContentSha256 ||
oldConfig.MaxDownloadSize != config.MaxDownloadSize {
logObject.CloneAndAddField("datastore-ids", uuids).
AddField("relative-URL", config.RelativeURL).
AddField("format", config.Format).
AddField("content-sha256", config.ContentSha256).
AddField("max-download-size-int64", config.MaxDownloadSize).
AddField("old-datastore-ids", oldUuids).
AddField("old-relative-URL", oldConfig.RelativeURL).
AddField("old-format", oldConfig.Format).
AddField("old-content-sha256", oldConfig.ContentSha256).
AddField("old-max-download-size-int64", oldConfig.MaxDownloadSize).
Noticef("Content tree config modify")
} else {
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldConfig, config)).
Noticef("Content tree config modify other change")
}
}
// LogDelete :
func (config ContentTreeConfig) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.ContentTreeConfigLogType, config.DisplayName,
config.ContentID, config.LogKey())
uuids := strings.Join(UuidsToStrings(config.DatastoreIDList), ",")
logObject.CloneAndAddField("datastore-ids", uuids).
AddField("relative-URL", config.RelativeURL).
AddField("format", config.Format).
AddField("content-sha256", config.ContentSha256).
AddField("max-download-size-int64", config.MaxDownloadSize).
Noticef("Content tree config delete")
base.DeleteLogObject(logBase, config.LogKey())
}
// LogKey :
func (config ContentTreeConfig) LogKey() string {
return string(base.ContentTreeConfigLogType) + "-" + config.Key()
}
// ContentTreeStatus is response from volumemgr about status of content tree
type ContentTreeStatus struct {
ContentID uuid.UUID
DatastoreIDList []uuid.UUID
DatastoreTypesList []string
AllDatastoresResolved bool
IsOCIRegistry bool
RelativeURL string
Format zconfig.Format
ContentSha256 string
MaxDownloadSize uint64
GenerationCounter int64
DisplayName string
HasResolverRef bool
State SwState
// XXX RefCount not needed?
// RefCount uint
// LastRefCountChangeTime time.Time
CreateTime time.Time // When LOADED
TotalSize int64 // expected size as reported by the downloader, if any
CurrentSize int64 // current total downloaded size as reported by the downloader
Progress uint // In percent i.e., 0-100
FileLocation string // Location of filestystem
NameIsURL bool
// Blobs the sha256 hashes of the blobs that are in this tree, the first of which always is the root
Blobs []string
HVTypeKube bool
ErrorAndTimeWithSource
}
// Key is content info UUID which will be unique
func (status ContentTreeStatus) Key() string {
return status.ContentID.String()
}
// ResolveKey will return the key of resolver config/status
func (status ContentTreeStatus) ResolveKey() string {
uuids := strings.Join(UuidsToStrings(status.DatastoreIDList), ",")
return fmt.Sprintf("%s+%s+%v", uuids,
status.RelativeURL, status.GenerationCounter)
}
// IsContainer will return true if content tree is of container type
func (status ContentTreeStatus) IsContainer() bool {
return status.Format == zconfig.Format_CONTAINER
}
// ReferenceID get the image reference ID
func (status ContentTreeStatus) ReferenceID() string {
if status.HVTypeKube && status.IsContainer() {
return fmt.Sprintf("%s%s-%s", KubeContainerImagePrefix, status.ContentID.String(), status.RelativeURL)
}
return fmt.Sprintf("%s-%s", status.ContentID.String(), status.RelativeURL)
}
// UpdateFromContentTreeConfig sets up ContentTreeStatus based on ContentTreeConfig struct
// Be aware: don't expect all fields are updated from the config
func (status *ContentTreeStatus) UpdateFromContentTreeConfig(config ContentTreeConfig) {
status.ContentID = config.ContentID
status.DatastoreIDList = config.DatastoreIDList
status.RelativeURL = config.RelativeURL
status.Format = config.Format
status.ContentSha256 = config.ContentSha256
status.MaxDownloadSize = config.MaxDownloadSize
status.GenerationCounter = config.GenerationCounter
status.DisplayName = config.DisplayName
}
// LogCreate :
func (status ContentTreeStatus) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.ContentTreeStatusLogType, status.DisplayName,
status.ContentID, status.LogKey())
if logObject == nil {
return
}
logObject.CloneAndAddField("content-sha256", status.ContentSha256).
AddField("max-download-size-int64", status.MaxDownloadSize).
AddField("state", status.State.String()).
AddField("progress", status.Progress).
AddField("filelocation", status.FileLocation).
Noticef("Content tree status create")
}
// LogModify :
func (status ContentTreeStatus) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.ContentTreeStatusLogType, status.DisplayName,
status.ContentID, status.LogKey())
oldStatus, ok := old.(ContentTreeStatus)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of ContentTreeStatus type")
}
if oldStatus.ContentSha256 != status.ContentSha256 ||
oldStatus.MaxDownloadSize != status.MaxDownloadSize ||
oldStatus.State != status.State ||
oldStatus.Progress != status.Progress ||
oldStatus.FileLocation != status.FileLocation {
logObject.CloneAndAddField("content-sha256", status.ContentSha256).
AddField("max-download-size-int64", status.MaxDownloadSize).
AddField("state", status.State.String()).
AddField("progress", status.Progress).
AddField("filelocation", status.FileLocation).
AddField("old-content-sha256", oldStatus.ContentSha256).
AddField("old-max-download-size-int64", oldStatus.MaxDownloadSize).
AddField("old-state", oldStatus.State.String()).
AddField("old-progress", oldStatus.Progress).
AddField("old-filelocation", oldStatus.FileLocation).
Noticef("Content tree status modify")
} else {
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldStatus, status)).
Noticef("Content tree status modify other change")
}
}
// LogDelete :
func (status ContentTreeStatus) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.ContentTreeStatusLogType, status.DisplayName,
status.ContentID, status.LogKey())
logObject.CloneAndAddField("content-sha256", status.ContentSha256).
AddField("max-download-size-int64", status.MaxDownloadSize).
AddField("state", status.State.String()).
AddField("progress", status.Progress).
AddField("filelocation", status.FileLocation).
Noticef("Content tree status delete")
base.DeleteLogObject(logBase, status.LogKey())
}
// LogKey :
func (status ContentTreeStatus) LogKey() string {
return string(base.ContentTreeStatusLogType) + "-" + status.Key()
}