generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 18
/
uninstall.go
160 lines (136 loc) · 4.36 KB
/
uninstall.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
package chart
import (
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type FilterFunc func(unstructured.Unstructured) bool
func Uninstall(config *Config, filterFunc ...FilterFunc) error {
spec, err := config.Cache.Get(config.Ctx, config.CacheKey)
if err != nil {
return fmt.Errorf("could not render manifest from chart: %s", err.Error())
}
objs, err := parseManifest(spec.Manifest)
if err != nil {
return fmt.Errorf("could not parse chart manifest: %s", err.Error())
}
err2 := uninstallObjects(config, objs, filterFunc...)
if err2 != nil {
return err2
}
err3 := uninstallOrphanedResources(config)
if err3 != nil {
return err3
}
return config.Cache.Delete(config.Ctx, config.CacheKey)
}
func uninstallObjects(config *Config, objs []unstructured.Unstructured, filterFunc ...FilterFunc) error {
for i := range objs {
u := objs[i]
if !fitToFilters(u, filterFunc...) {
continue
}
config.Log.Debugf("deleting %s %s", u.GetKind(), u.GetName())
err := config.Cluster.Client.Delete(config.Ctx, &u)
if k8serrors.IsNotFound(err) {
config.Log.Debugf("deletion skipped for %s %s", u.GetKind(), u.GetName())
continue
}
if err != nil {
return fmt.Errorf("could not uninstall object %s/%s: %s", u.GetNamespace(), u.GetName(), err.Error())
}
}
return nil
}
func UninstallSecrets(config *Config, filterFunc ...FilterFunc) (error, bool) {
spec, err := config.Cache.Get(config.Ctx, config.CacheKey)
if err != nil {
return fmt.Errorf("could not render manifest from chart: %s", err.Error()), false
}
objs, err := parseManifest(spec.Manifest)
if err != nil {
return fmt.Errorf("could not parse chart manifest: %s", err.Error()), false
}
err2, done := uninstallSecrets(config, objs, filterFunc...)
if err2 != nil {
return err2, false
}
return nil, done
}
func uninstallSecrets(config *Config, objs []unstructured.Unstructured, filterFunc ...FilterFunc) (error, bool) {
done := true
for i := range objs {
u := objs[i]
if !fitToFilters(u, filterFunc...) {
continue
}
if u.GetKind() != "Secret" {
continue
}
config.Log.Debugf("deleting %s %s", u.GetKind(), u.GetName())
err := config.Cluster.Client.Delete(config.Ctx, &u)
if k8serrors.IsNotFound(err) {
config.Log.Debugf("deletion skipped for %s %s", u.GetKind(), u.GetName())
continue
}
if err != nil {
return fmt.Errorf("could not uninstall object %s/%s: %s", u.GetNamespace(), u.GetName(), err.Error()), false
}
done = false
}
return nil, done
}
func WithoutCRDFilter(u unstructured.Unstructured) bool {
return !isCRD(u)
}
func fitToFilters(u unstructured.Unstructured, filterFunc ...FilterFunc) bool {
for _, fn := range filterFunc {
if !fn(u) {
return false
}
}
return true
}
func uninstallOrphanedResources(config *Config) error {
//TODO: move this to finalizers logic in controller
var namespaces corev1.NamespaceList
if err := config.Cluster.Client.List(config.Ctx, &namespaces); err != nil {
return errors.Wrap(err, "couldn't get namespaces during Serverless uninstallation")
}
if err := uninstallOrphanedConfigMaps(config, namespaces); err != nil {
return err
}
if err := uninstallOrphanedServiceAccounts(config, namespaces); err != nil {
return err
}
return nil
}
func uninstallOrphanedServiceAccounts(config *Config, namespaces corev1.NamespaceList) error {
for _, namespace := range namespaces.Items {
err := config.Cluster.Client.DeleteAllOf(config.Ctx, &corev1.ServiceAccount{},
client.InNamespace(namespace.GetName()),
client.MatchingLabels{"serverless.kyma-project.io/config": "service-account"})
if err != nil {
return errors.Wrapf(err,
"couldn't delete ServiceAccount from namespace \"%s\" during Serverless uninstallation",
namespace.GetName())
}
}
return nil
}
func uninstallOrphanedConfigMaps(config *Config, namespaces corev1.NamespaceList) error {
for _, namespace := range namespaces.Items {
err := config.Cluster.Client.DeleteAllOf(config.Ctx, &corev1.ConfigMap{},
client.InNamespace(namespace.GetName()),
client.MatchingLabels{"serverless.kyma-project.io/config": "runtime"})
if err != nil {
return errors.Wrapf(err,
"couldn't delete ConfigMap from namespace \"%s\" during Serverless uninstallation",
namespace.GetName())
}
}
return nil
}