forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crd.go
247 lines (230 loc) · 8.46 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
package manager
import (
"fmt"
"strings"
"strconv"
"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, updateOnly bool) error {
logrus.Debugf("Syncing catalog %s with templates", catalog.Name)
existingTemplates, err := m.templateClient.List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", CatalogNameLabel, catalog.Name),
})
if err != nil {
return err
}
templatesByName := map[string]v3.Template{}
for _, template := range templates {
if template.Spec.FolderName == "" {
continue
} else if template.Spec.Base == "" && template.Spec.FolderName != "" {
template.Name = fmt.Sprintf("%s-%s", catalog.Name, template.Spec.FolderName)
} else {
template.Name = fmt.Sprintf("%s-%s-%s", catalog.Name, template.Spec.Base, template.Spec.FolderName)
}
template.Name = strings.ToLower(template.Name)
templatesByName[template.Name] = template
}
existingTemplatesByName := map[string]v3.Template{}
for _, template := range existingTemplates.Items {
existingTemplatesByName[template.Name] = template
}
// templates is the one we should update, so for all the templates that were in existingTemplates
// 1. if it doesn't exist in templates, delete them
// 2. if it exists, update it
// done: hack
// todo: remove hack
//var errs []error
//for name, existingTemplate := range existingTemplatesByName {
// err := m.updateTemplate(name, existingTemplate, templatesByName, updateOnly)
// if err != nil {
// errs = append(errs, err)
// }
//}
// for templates that exist in template but not in existingTemplates, we should create them
for name, template := range templatesByName {
err := m.createTemplate(name, template, existingTemplatesByName, catalog)
if err != nil {
// done: hack
// todo: remove hack
logrus.Error(err)
//errs = append(errs, err)
}
}
//if len(errs) > 0 {
// return errors.Errorf("Multiple errors happens: %v", errs)
//}
v3.CatalogConditionRefreshed.True(catalog)
if _, err := m.catalogClient.Update(catalog); err != nil {
return err
}
return nil
}
func (m *Manager) createTemplate(name string, template v3.Template, existingTemplatesByName map[string]v3.Template, catalog *v3.Catalog) error {
if _, ok := existingTemplatesByName[name]; !ok {
template.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: catalog.APIVersion,
Kind: catalog.Kind,
Name: catalog.Name,
UID: catalog.UID,
BlockOwnerDeletion: &[]bool{true}[0],
},
}
template.Kind = v3.TemplateGroupVersionKind.Kind
template.APIVersion = v3.TemplateGroupVersionKind.Group + "/" + v3.TemplateGroupVersionKind.Version
template.Labels = map[string]string{}
template.Labels[CatalogNameLabel] = catalog.Name
versionFiles := template.Spec.Versions
// we are removing file fields so that the big chunk of data doesn't get stored in two places
var modifiedVersionFiles []v3.TemplateVersionSpec
for _, version := range template.Spec.Versions {
version.Files = nil
version.Readme = ""
modifiedVersionFiles = append(modifiedVersionFiles, version)
}
template.Spec.Versions = modifiedVersionFiles
logrus.Infof("Creating template %s", template.Name)
createdTemplate, err := m.templateClient.Create(&template)
if err != nil {
// hack for the image size that are too big
if strings.Contains(err.Error(), "request is too large") ||
strings.Contains(err.Error(), "exceeding the max size") ||
strings.Contains(err.Error(), "received message larger than max") {
logrus.Warnf("Template %s size is too large. Skipping", template.Name)
return nil
}
return errors.Wrapf(err, "failed to create template %s", template.Name)
}
if err := m.createTemplateVersions(versionFiles, *createdTemplate); err != nil {
return err
}
}
return nil
}
func (m *Manager) updateTemplate(name string, existingTemplate v3.Template, templatesByName map[string]v3.Template, updateOnly bool) error {
template, ok := templatesByName[name]
if !ok && !updateOnly {
// delete the template
logrus.Infof("Deleting templates %s", name)
if err := m.templateClient.Delete(name, &metav1.DeleteOptions{}); err != nil {
return errors.Wrapf(err, "failed to delete template %s", template.Name)
}
if err := m.deleteTemplateVersions(existingTemplate); err != nil {
return errors.Wrapf(err, "failed to delete templateVersion with template %s", template.Name)
}
} else if !ok && updateOnly {
return nil
}
updateTemplate, err := m.templateClient.Get(name, metav1.GetOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to get template %s", name)
} else if kerrors.IsNotFound(err) {
return nil
}
updateTemplate.Spec = template.Spec
template.Spec.Versions = updateTemplate.Spec.Versions
var modifiedVersionFiles []v3.TemplateVersionSpec
for _, version := range updateTemplate.Spec.Versions {
version.Files = nil
version.Readme = ""
modifiedVersionFiles = append(modifiedVersionFiles, version)
}
updateTemplate.Spec.Versions = modifiedVersionFiles
logrus.Infof("Updating template %s", name)
result, err := m.templateClient.Update(updateTemplate)
if err != nil {
if strings.Contains(err.Error(), "request is too large") || strings.Contains(err.Error(), "exceeding the max size") {
logrus.Warnf("Template %s size is too large. Skipping", template.Name)
return nil
}
return errors.Wrapf(err, "failed to update template %s", template.Name)
}
if err := m.deleteTemplateVersions(*result); err != nil {
return err
}
return m.createTemplateVersions(template.Spec.Versions, *result)
}
func (m *Manager) createTemplateVersions(versionsSpec []v3.TemplateVersionSpec, template v3.Template) error {
var createdTemplates []string
rollback := false
for _, spec := range versionsSpec {
templateVersion := v3.TemplateVersion{}
templateVersion.Spec = spec
revision := spec.Version
if spec.Revision != nil {
revision = strconv.Itoa(*spec.Revision)
}
templateVersion.APIVersion = v3.TemplateVersionGroupVersionKind.Group + "/" + v3.TemplateVersionGroupVersionKind.Version
templateVersion.Kind = v3.TemplateVersionGroupVersionKind.Kind
templateVersion.Name = fmt.Sprintf("%s-%v", template.Name, revision)
templateVersion.Labels = make(map[string]string)
templateVersion.Labels[TemplateNameLabel] = template.Name
templateVersion.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: template.APIVersion,
Kind: template.Kind,
Name: template.Name,
UID: template.UID,
BlockOwnerDeletion: &[]bool{true}[0],
},
}
logrus.Debugf("Creating templateVersion %s", templateVersion.Name)
_, err := m.templateVersionClient.Create(&templateVersion)
if err != nil {
logrus.Error(err)
rollback = true
break
}
createdTemplates = append(createdTemplates, templateVersion.Name)
}
if rollback {
logrus.Info("Rollback TemplateVersion")
for _, name := range createdTemplates {
logrus.Infof("Deleting templateVersion %s", name)
err := m.templateVersionClient.Delete(name, &metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to rollback and delete templateVersion %s", name)
}
}
}
return nil
}
func (m *Manager) deleteTemplateVersions(template v3.Template) 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")
}
for _, version := range templateVersions.Items {
logrus.Infof("Deleting templateVersion %s", version.Name)
if err := m.templateVersionClient.Delete(version.Name, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
return err
}
}
return 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
}