forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crd.go
280 lines (259 loc) · 8.4 KB
/
crd.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
package manager
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/hex"
"fmt"
"crypto/sha256"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/catalog/utils"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
CatalogNameLabel = "catalog.cattle.io/name"
TemplateNameLabel = "catalog.cattle.io/template_name"
)
// update will sync templates with catalog without costing too much
func (m *Manager) update(catalog *v3.Catalog, templates []v3.Template, versionCommits map[string]v3.VersionCommits, commit string) error {
logrus.Debugf("Syncing catalog %s with templates", catalog.Name)
// list all existing templates
templateMap, err := m.getTemplateMap(catalog.Name)
if err != nil {
return err
}
// list all templateContent tag
templateContentMap := map[string]struct{}{}
templateContentList, err := m.templateContentClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, t := range templateContentList.Items {
templateContentMap[t.Name] = struct{}{}
}
errs := []error{}
for _, template := range templates {
var temErr error
// look template by name, if not found then create it, otherwise do update
if existing, ok := templateMap[template.Name]; ok {
if err := m.updateTemplate(&existing, template, templateContentMap); err != nil {
temErr = err
}
} else {
if err := m.createTemplate(template, catalog, templateContentMap); err != nil {
temErr = err
}
}
if temErr != nil {
delete(versionCommits, template.Spec.DisplayName)
errs = append(errs, temErr)
}
}
catalog.Status.HelmVersionCommits = versionCommits
if len(errs) > 0 {
if _, err := m.catalogClient.Update(catalog); err != nil {
return err
}
return errors.Errorf("failed to update templates. Multiple error occurred: %v", errs)
}
v3.CatalogConditionRefreshed.True(catalog)
catalog.Status.Commit = commit
if _, err := m.catalogClient.Update(catalog); err != nil {
return err
}
return nil
}
func (m *Manager) createTemplate(template v3.Template, catalog *v3.Catalog, tagMap map[string]struct{}) error {
template.Labels = map[string]string{
CatalogNameLabel: catalog.Name,
}
versionFiles := make([]v3.TemplateVersionSpec, len(template.Spec.Versions))
copy(versionFiles, template.Spec.Versions)
for i := range template.Spec.Versions {
template.Spec.Versions[i].Files = nil
template.Spec.Versions[i].Readme = ""
template.Spec.Versions[i].AppReadme = ""
}
if err := m.convertTemplateIcon(&template, tagMap); err != nil {
return err
}
logrus.Infof("Creating template %s", template.Name)
createdTemplate, err := m.templateClient.Create(&template)
if err != nil {
return errors.Wrapf(err, "failed to create template %s", template.Name)
}
return m.createTemplateVersions(versionFiles, createdTemplate, tagMap)
}
func (m *Manager) getTemplateMap(catalogName string) (map[string]v3.Template, error) {
templateList, err := m.templateClient.List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", CatalogNameLabel, catalogName),
})
if err != nil {
return nil, err
}
templateMap := map[string]v3.Template{}
for _, t := range templateList.Items {
templateMap[t.Name] = t
}
return templateMap, nil
}
func (m *Manager) convertTemplateIcon(template *v3.Template, tagMap map[string]struct{}) error {
tag, content, err := zipAndHash(template.Spec.Icon)
if err != nil {
return err
}
if _, ok := tagMap[tag]; !ok {
templateContent := &v3.TemplateContent{}
templateContent.Name = tag
templateContent.Data = content
if _, err := m.templateContentClient.Create(templateContent); err != nil {
return err
}
tagMap[tag] = struct{}{}
}
template.Spec.Icon = tag
return nil
}
func (m *Manager) updateTemplate(template *v3.Template, toUpdate v3.Template, tagMap map[string]struct{}) error {
templateVersions, err := m.templateVersionClient.List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", TemplateNameLabel, template.Name),
})
if err != nil {
return errors.Wrapf(err, "failed to list templateVersions")
}
tvByVersion := map[string]v3.TemplateVersion{}
for _, ver := range templateVersions.Items {
tvByVersion[ver.Spec.Version] = ver
}
/*
for each templateVersion in toUpdate, calculate each hash value and if doesn't match, do update.
For version that doesn't exist, create a new one
*/
for _, toUpdateVer := range toUpdate.Spec.Versions {
// gzip each file to store the hash value into etcd. Next time if it already exists in etcd then use the existing tag
templateVersion := &v3.TemplateVersion{}
templateVersion.Spec = toUpdateVer
if err := m.convertFile(templateVersion, toUpdateVer, tagMap); err != nil {
return err
}
if tv, ok := tvByVersion[toUpdateVer.Version]; ok {
if tv.Spec.Digest != toUpdateVer.Digest {
tv.Spec = templateVersion.Spec
logrus.Infof("Updating templateVersion %v", tv.Name)
if _, err := m.templateVersionClient.Update(&tv); err != nil {
return err
}
}
} else {
toCreate := &v3.TemplateVersion{}
toCreate.Name = fmt.Sprintf("%s-%v", template.Name, toUpdateVer.Version)
toCreate.Labels = map[string]string{
TemplateNameLabel: template.Name,
}
toCreate.Spec = templateVersion.Spec
logrus.Infof("Creating templateVersion %v", toCreate.Name)
if _, err := m.templateVersionClient.Create(toCreate); err != nil {
return err
}
}
}
for i := range toUpdate.Spec.Versions {
toUpdate.Spec.Versions[i].Files = nil
toUpdate.Spec.Versions[i].Readme = ""
toUpdate.Spec.Versions[i].AppReadme = ""
}
template.Spec = toUpdate.Spec
if err := m.convertTemplateIcon(template, tagMap); err != nil {
return err
}
if _, err := m.templateClient.Update(template); err != nil {
return err
}
return nil
}
func (m *Manager) getTemplateVersion(templateName string) (map[string]struct{}, error) {
templateVersions, err := m.templateVersionClient.List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", TemplateNameLabel, templateName),
})
if err != nil {
return nil, err
}
tVersion := map[string]struct{}{}
for _, ver := range templateVersions.Items {
tVersion[ver.Name] = struct{}{}
}
return tVersion, nil
}
func (m *Manager) createTemplateVersions(versionsSpec []v3.TemplateVersionSpec, template *v3.Template, tagMap map[string]struct{}) error {
for _, spec := range versionsSpec {
templateVersion := &v3.TemplateVersion{}
templateVersion.Spec = spec
templateVersion.Name = fmt.Sprintf("%s-%v", template.Name, spec.Version)
templateVersion.Labels = map[string]string{
TemplateNameLabel: template.Name,
}
// gzip each file to store the hash value into etcd. Next time if it already exists in etcd then use the existing tag
if err := m.convertFile(templateVersion, spec, tagMap); err != nil {
return err
}
logrus.Debugf("Creating templateVersion %s", templateVersion.Name)
if _, err := m.templateVersionClient.Create(templateVersion); err != nil && !kerrors.IsAlreadyExists(err) {
return err
}
}
return nil
}
func (m *Manager) convertFile(templateVersion *v3.TemplateVersion, spec v3.TemplateVersionSpec, tagMap map[string]struct{}) error {
for name, file := range spec.Files {
tag, content, err := zipAndHash(file)
if err != nil {
return err
}
if _, ok := tagMap[tag]; !ok {
templateContent := &v3.TemplateContent{}
templateContent.Name = tag
templateContent.Data = content
if _, err := m.templateContentClient.Create(templateContent); err != nil {
return err
}
tagMap[tag] = struct{}{}
}
templateVersion.Spec.Files[name] = tag
if file == spec.Readme {
templateVersion.Spec.Readme = tag
}
if file == spec.AppReadme {
templateVersion.Spec.AppReadme = tag
}
}
return nil
}
func zipAndHash(content string) (string, string, error) {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
if _, err := zw.Write([]byte(content)); err != nil {
return "", "", err
}
zw.Close()
digest := sha256.New()
compressedData := buf.Bytes()
digest.Write(compressedData)
tag := hex.EncodeToString(digest.Sum(nil))
return tag, base64.StdEncoding.EncodeToString(compressedData), nil
}
func showUpgradeLinks(version, upgradeVersion, upgradeFrom string) bool {
if !utils.VersionGreaterThan(upgradeVersion, version) {
return false
}
if upgradeFrom != "" {
satisfiesRange, err := utils.VersionSatisfiesRange(version, upgradeFrom)
if err != nil {
return false
}
return satisfiesRange
}
return true
}