-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
178 lines (158 loc) · 5.79 KB
/
manager.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
package manager
import (
"fmt"
"time"
helmlib "github.com/rancher/rancher/pkg/catalog/helm"
"github.com/rancher/rancher/pkg/controllers/user/helm/common"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/types/apis/management.cattle.io/v3"
projectv3 "github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Manager struct {
catalogClient v3.CatalogInterface
CatalogLister v3.CatalogLister
templateClient v3.CatalogTemplateInterface
templateContentClient v3.TemplateContentInterface
templateVersionClient v3.CatalogTemplateVersionInterface
templateLister v3.CatalogTemplateLister
templateVersionLister v3.CatalogTemplateVersionLister
projectCatalogClient v3.ProjectCatalogInterface
ProjectCatalogLister v3.ProjectCatalogLister
clusterCatalogClient v3.ClusterCatalogInterface
ClusterCatalogLister v3.ClusterCatalogLister
appRevisionClient projectv3.AppRevisionInterface
lastUpdateTime time.Time
}
func New(management *config.ManagementContext) *Manager {
return &Manager{
catalogClient: management.Management.Catalogs(""),
CatalogLister: management.Management.Catalogs("").Controller().Lister(),
templateClient: management.Management.CatalogTemplates(""),
templateContentClient: management.Management.TemplateContents(""),
templateVersionClient: management.Management.CatalogTemplateVersions(""),
templateLister: management.Management.CatalogTemplates("").Controller().Lister(),
templateVersionLister: management.Management.CatalogTemplateVersions("").Controller().Lister(),
projectCatalogClient: management.Management.ProjectCatalogs(""),
ProjectCatalogLister: management.Management.ProjectCatalogs("").Controller().Lister(),
clusterCatalogClient: management.Management.ClusterCatalogs(""),
ClusterCatalogLister: management.Management.ClusterCatalogs("").Controller().Lister(),
appRevisionClient: management.Project.AppRevisions(""),
}
}
func (m *Manager) deleteChart(toDelete string, namespace string) error {
toDeleteTvs, err := m.getTemplateVersion(toDelete, namespace)
if err != nil {
return err
}
for tv := range toDeleteTvs {
if err := m.templateVersionClient.DeleteNamespaced(namespace, tv, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
return err
}
}
if err := m.templateClient.DeleteNamespaced(namespace, toDelete, &metav1.DeleteOptions{}); err != nil && !kerrors.IsNotFound(err) {
return err
}
return nil
}
func getKey(namespace, name string) string {
return fmt.Sprintf("%s/%s", namespace, name)
}
func (m *Manager) DeleteOldTemplateContent() bool {
// Template content is not used, remove old contents
if err := m.templateContentClient.DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
logrus.Warnf("Catalog-manager error deleting old template content: %s", err)
return false
}
return true
}
func (m *Manager) DeleteBadCatalogTemplates() bool {
// Orphaned catalog templates and template versions may exist, remove any where the catalog does not exist
errs := m.deleteBadCatalogTemplates()
if len(errs) == 0 {
return true
}
for _, err := range errs {
logrus.Errorf("Catalog-manager error deleting bad catalog templates: %s", err)
}
return false
}
func (m *Manager) deleteBadCatalogTemplates() []error {
templates, err := m.templateClient.List(metav1.ListOptions{})
if err != nil {
return []error{err}
}
templateVersions, err := m.templateVersionClient.List(metav1.ListOptions{})
if err != nil {
return []error{err}
}
var hasCatalog = map[string]bool{}
catalogs, err := m.catalogClient.List(metav1.ListOptions{})
if err != nil {
return []error{err}
}
for _, catalog := range catalogs.Items {
hasCatalog[getKey(namespace.GlobalNamespace, catalog.Name)] = true
}
clusterCatalogs, err := m.clusterCatalogClient.List(metav1.ListOptions{})
if err != nil {
return []error{err}
}
for _, clusterCatalog := range clusterCatalogs.Items {
hasCatalog[getKey(clusterCatalog.Namespace, clusterCatalog.Name)] = true
}
projectCatalogs, err := m.projectCatalogClient.List(metav1.ListOptions{})
if err != nil {
return []error{err}
}
for _, projectCatalog := range projectCatalogs.Items {
hasCatalog[getKey(projectCatalog.Namespace, projectCatalog.Name)] = true
}
var (
deleteCount int
errs []error
)
for _, template := range templates.Items {
var catalogName string
if template.Spec.CatalogID != "" {
catalogName = template.Spec.CatalogID
} else if template.Spec.ClusterCatalogID != "" {
catalogName = template.Spec.ClusterCatalogID
} else if template.Spec.ProjectCatalogID != "" {
catalogName = template.Spec.ProjectCatalogID
}
ns, name := helmlib.SplitNamespaceAndName(catalogName)
if ns == "" {
ns = namespace.GlobalNamespace
}
if !hasCatalog[getKey(ns, name)] {
if err := m.templateClient.DeleteNamespaced(template.Namespace, template.Name, &metav1.DeleteOptions{}); err != nil {
errs = append(errs, err)
}
deleteCount++
}
}
for _, templateVersion := range templateVersions.Items {
ns, name, _, _, _, err := common.SplitExternalID(templateVersion.Spec.ExternalID)
if err != nil {
logrus.Errorf("Catalog-manager error extracting namespace/name from template version: %s", err)
continue
}
if ns == "" {
ns = namespace.GlobalNamespace
}
if !hasCatalog[getKey(ns, name)] {
if err := m.templateVersionClient.DeleteNamespaced(templateVersion.Namespace, templateVersion.Name, &metav1.DeleteOptions{}); err != nil {
errs = append(errs, err)
}
deleteCount++
}
}
if deleteCount > 0 {
logrus.Infof("Catalog-manager deleted %d orphaned catalog template entries", deleteCount)
}
return errs
}