forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
80 lines (73 loc) · 2.08 KB
/
sync.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
package manager
import (
"github.com/pkg/errors"
"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"
)
func (m *Manager) Sync(key string, obj *v3.Catalog) error {
if obj == nil {
return nil
}
if obj.DeletionTimestamp != nil {
templates, err := m.getTemplateMap(obj.Name)
if err != nil {
return err
}
tvToDelete := map[string]struct{}{}
for _, t := range templates {
tvs, err := m.getTemplateVersion(t.Name)
if err != nil {
return err
}
for k := range tvs {
tvToDelete[k] = struct{}{}
}
}
go func() {
for {
for k := range templates {
if err := m.templateClient.Delete(k, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
logrus.Warnf("Deleting template %v doesn't succeed. Continue loop", k)
continue
}
}
for k := range tvToDelete {
if err := m.templateVersionClient.Delete(k, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
logrus.Warnf("Deleting templateVersion %v doesn't succeed. Continue loop", k)
continue
}
}
break
}
}()
return nil
}
catalog := obj.DeepCopy()
repoPath, commit, err := m.prepareRepoPath(*catalog)
if err != nil {
v3.CatalogConditionRefreshed.False(catalog)
v3.CatalogConditionRefreshed.ReasonAndMessageFromError(catalog, err)
m.catalogClient.Update(catalog)
return err
}
if commit == catalog.Status.Commit {
logrus.Debugf("Catalog %s is already up to date", catalog.Name)
if v3.CatalogConditionRefreshed.IsUnknown(catalog) {
v3.CatalogConditionRefreshed.True(catalog)
m.catalogClient.Update(catalog)
}
return nil
}
catalog.Status.Commit = commit
templates, versionCommits, errs, err := traverseFiles(repoPath, catalog)
if err != nil {
return errors.Wrap(err, "Repo traversal failed")
}
if len(errs) != 0 {
logrus.Errorf("Errors while parsing repo: %v", errs)
}
logrus.Infof("Updating catalog %s", catalog.Name)
return m.update(catalog, templates, versionCommits, commit)
}