-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog_common.go
161 lines (148 loc) · 4.69 KB
/
catalog_common.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
package manager
import (
"fmt"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
func hasAllUpdates(catalog *v3.Catalog) bool {
upgraded := v3.CatalogConditionUpgraded.IsTrue(catalog)
diskCached := v3.CatalogConditionDiskCached.IsTrue(catalog)
return upgraded && diskCached
}
func isUpToDate(commit string, catalog *v3.Catalog) bool {
commitsEqual := commit == catalog.Status.Commit
updated := hasAllUpdates(catalog)
return commitsEqual && updated
}
func setRefreshed(catalog *v3.Catalog) bool {
logrus.Debugf("Catalog %s is already up to date", catalog.Name)
if !v3.CatalogConditionRefreshed.IsTrue(catalog) {
v3.CatalogConditionRefreshed.True(catalog)
v3.CatalogConditionRefreshed.Reason(catalog, "")
v3.CatalogConditionRefreshed.Message(catalog, "")
return true
}
return false
}
func setRefreshedError(catalog *v3.Catalog, err error) {
v3.CatalogConditionRefreshed.False(catalog)
v3.CatalogConditionRefreshed.ReasonAndMessageFromError(catalog, err)
}
func (m *Manager) deleteTemplates(key string, namespace string) error {
templates, err := m.getTemplateMap(key, namespace)
if err != nil {
return err
}
tvToDelete := map[string]struct{}{}
for _, t := range templates {
tvs, err := m.getTemplateVersion(t.Name, namespace)
if err != nil {
return err
}
for k := range tvs {
tvToDelete[k] = struct{}{}
}
}
go func() {
for {
for k := range templates {
if err := m.templateClient.DeleteNamespaced(namespace, 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.DeleteNamespaced(namespace, k, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
logrus.Warnf("Deleting templateVersion %v doesn't succeed. Continue loop", k)
continue
}
}
break
}
}()
return nil
}
func getCatalogType(cmt *CatalogInfo) string {
if cmt.projectCatalog == nil && cmt.clusterCatalog == nil {
return client.CatalogType
} else if cmt.projectCatalog != nil {
return client.ProjectCatalogType
} else {
return client.ClusterCatalogType
}
}
func (m *Manager) updateCatalogInfo(cmt *CatalogInfo, catalogType string, templateName string, condition bool, updateOnly bool) (*CatalogInfo, error) {
var obj runtime.Object
if condition {
switch catalogType {
case client.CatalogType:
obj = runtime.Object(cmt.catalog)
case client.ProjectCatalogType:
obj = runtime.Object(cmt.projectCatalog)
case client.ClusterCatalogType:
obj = runtime.Object(cmt.clusterCatalog)
default:
return cmt, fmt.Errorf("incorrect catalog type")
}
v3.CatalogConditionRefreshed.Unknown(obj)
if templateName != "" {
v3.CatalogConditionRefreshed.Message(obj, fmt.Sprintf("syncing template %v", templateName))
} else {
v3.CatalogConditionRefreshed.Message(obj, fmt.Sprintf(""))
}
}
if updateOnly {
switch catalogType {
case client.CatalogType:
if _, err := m.catalogClient.Update(cmt.catalog); err != nil {
return nil, err
}
case client.ProjectCatalogType:
if _, err := m.projectCatalogClient.Update(cmt.projectCatalog); err != nil {
return nil, err
}
case client.ClusterCatalogType:
if _, err := m.clusterCatalogClient.Update(cmt.clusterCatalog); err != nil {
return nil, err
}
default:
return cmt, fmt.Errorf("incorrect catalog type")
}
return cmt, nil
}
switch catalogType {
case client.CatalogType:
catalog := cmt.catalog
if newCatalog, err := m.catalogClient.Update(cmt.catalog); err == nil {
catalog = newCatalog
} else {
catalog, _ = m.catalogClient.Get(catalog.Name, metav1.GetOptions{})
}
cmt.catalog = catalog
case client.ProjectCatalogType:
projectCatalog := cmt.projectCatalog
if newCatalog, err := m.projectCatalogClient.Update(projectCatalog); err == nil {
projectCatalog = newCatalog
} else {
projectCatalog, _ = m.projectCatalogClient.Get(projectCatalog.Name, metav1.GetOptions{})
}
cmt.catalog = &projectCatalog.Catalog
cmt.projectCatalog = projectCatalog
case client.ClusterCatalogType:
clusterCatalog := cmt.clusterCatalog
if newCatalog, err := m.clusterCatalogClient.Update(clusterCatalog); err == nil {
clusterCatalog = newCatalog
} else {
clusterCatalog, _ = m.clusterCatalogClient.Get(clusterCatalog.Name, metav1.GetOptions{})
}
cmt.catalog = &clusterCatalog.Catalog
cmt.clusterCatalog = clusterCatalog
default:
return cmt, fmt.Errorf("incorrect catalog type")
}
return cmt, nil
}