-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
72 lines (66 loc) · 1.9 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
package manager
import (
"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 {
templates, err := m.getTemplateMap(key)
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
}
// always get a refresh catalog from etcd
catalog, err := m.catalogClient.Get(key, metav1.GetOptions{})
if err != nil {
return err
}
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)
v3.CatalogConditionRefreshed.Reason(catalog, "")
m.catalogClient.Update(catalog)
}
return nil
}
logrus.Infof("Updating catalog %s", catalog.Name)
return m.traverseAndUpdate(repoPath, commit, catalog)
}