-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathpatchenvelopestypes.go
166 lines (146 loc) · 4.86 KB
/
patchenvelopestypes.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
// Copyright (c) 2023 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package types
// PatchEnvelopeInfoList will be shared with zedrouter after parsing
// in zedagent
type PatchEnvelopeInfoList struct {
Envelopes []PatchEnvelopeInfo
}
// Get returns list of patch envelopes, which are available to appUUID
func (pe *PatchEnvelopeInfoList) Get(appUUID string) PatchEnvelopeInfoList {
var result []PatchEnvelopeInfo
for _, envelope := range pe.Envelopes {
for _, allowedUUID := range envelope.AllowedApps {
if allowedUUID == appUUID {
result = append(result, envelope)
break
}
}
}
return PatchEnvelopeInfoList{
Envelopes: result,
}
}
// PatchEnvelopeInfo - information
// about patch envelopes
type PatchEnvelopeInfo struct {
Name string
Version string
AllowedApps []string
PatchID string
Errors []string
State PatchEnvelopeState
BinaryBlobs []BinaryBlobCompleted
VolumeRefs []BinaryBlobVolumeRef
}
// Size returns sum of all sizes of BinaryBlobs of given PatchEnvelope
func (pe *PatchEnvelopeInfo) Size() (size int64) {
for _, blob := range pe.BinaryBlobs {
size += blob.Size
}
return
}
// Key for pubsub
func (pe *PatchEnvelopeInfo) Key() string {
return pe.PatchID + "v" + pe.Version
}
// PatchEnvelopeState repeats constants from patch_envelope.pb.go from info API
type PatchEnvelopeState int32
const (
// PatchEnvelopeStateError - there is an error with config or during download
// or verification failed
PatchEnvelopeStateError PatchEnvelopeState = iota
// PatchEnvelopeStateRecieved - configuration received but no downloads started
PatchEnvelopeStateRecieved
// PatchEnvelopeStateDownloading - artifact/Volume download started
// One or more of the artifacts are being downloaded
PatchEnvelopeStateDownloading
// PatchEnvelopeStateDownloaded - all downloads finished, verified and added to content tree
PatchEnvelopeStateDownloaded
// PatchEnvelopeStateReady - patch envelope ready for application instances
// application instances will still not be
// allowed to fetch the patch envelope contents
PatchEnvelopeStateReady
// PatchEnvelopeStateActive - application instances are now allowed to fetch contents
PatchEnvelopeStateActive
)
// Key for pubsub
func (pe *PatchEnvelopeInfoList) Key() string {
return "global"
}
// FindPatchEnvelopeByID returns patch envelope with given patchId
func (pe *PatchEnvelopeInfoList) FindPatchEnvelopeByID(patchID string) *PatchEnvelopeInfo {
for _, pe := range pe.Envelopes {
if pe.PatchID == patchID {
return &pe
}
}
return nil
}
// BinaryBlobCompleted is representation of
// binary blob ready to be downloaded by app instance
type BinaryBlobCompleted struct {
FileName string `json:"fileName"`
FileSha string `json:"fileSha"`
// FileMetadata is related to file, i.e. env variables, cli arguments
FileMetadata string `json:"fileMetaData"`
// ArtifactMetadata is generic info i.e. user info, desc etc.
ArtifactMetadata string `json:"artifactMetaData"`
URL string `json:"url"` //nolint:var-naming
Size int64 `json:"size"`
}
// CompletedBinaryBlobIdxByName returns index of element in blobs list
// which FileName matches name
func CompletedBinaryBlobIdxByName(blobs []BinaryBlobCompleted, name string) int {
for i := range blobs {
if blobs[i].FileName == name {
return i
}
}
return -1
}
// BinaryBlobVolumeRef is representation of
// external binary blobs, which has not yet been
// downloaded
type BinaryBlobVolumeRef struct {
FileName string `json:"fileName"`
ImageName string `json:"imageName"`
// FileMetadata is related to file, i.e. env variables, cli arguments
FileMetadata string `json:"fileMetaData"`
// ArtifactMetadata is generic info i.e. user info, desc etc.
ArtifactMetadata string `json:"artifactMetaData"`
ImageID string `json:"imageId"`
}
// PatchEnvelopeUsage stores information on how patchEnvelopes are
// used by App Instances to send this information back to controller
// reflects ZInfoPatchEnvelopeUsage proto message
type PatchEnvelopeUsage struct {
AppUUID string
PatchID string
Version string
// count the number of times app instance called patch APIs
PatchAPICallCount uint64
// count the number of times app instance actually downloaded
// whole patch envelope or part of it
DownloadCount uint64
}
// Key for pubsub
func (pe *PatchEnvelopeUsage) Key() string {
return "patchEnvelopeUsage:" + pe.PatchID +
"-v-" + pe.Version +
"-app-" + pe.AppUUID
}
// PatchEnvelopeUsageFromInfo returns PatchEnvelopeUsage structure from
// PatchEnvelopeInfo struct
func PatchEnvelopeUsageFromInfo(peInfo PatchEnvelopeInfo) []PatchEnvelopeUsage {
result := make([]PatchEnvelopeUsage, 0, len(peInfo.AllowedApps))
for _, appUUID := range peInfo.AllowedApps {
usage := PatchEnvelopeUsage{
AppUUID: appUUID,
PatchID: peInfo.PatchID,
Version: peInfo.Version,
}
result = append(result, usage)
}
return result
}