forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
239 lines (218 loc) · 6.01 KB
/
controller.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package controller
import (
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
osclient "github.com/openshift/origin/pkg/client"
projectutil "github.com/openshift/origin/pkg/project/util"
)
// NamespaceController is responsible for participating in Kubernetes Namespace termination
// Use the NamespaceControllerFactory to create this controller.
type NamespaceController struct {
// Client is an OpenShift client.
Client osclient.Interface
// KubeClient is a Kubernetes client.
KubeClient kclient.Interface
}
// fatalError is an error which can't be retried.
type fatalError string
// Error implements the interface for errors
func (e fatalError) Error() string { return "fatal error handling namespace: " + string(e) }
// Handle processes a namespace and deletes content in origin if its terminating
func (c *NamespaceController) Handle(namespace *kapi.Namespace) (err error) {
// if namespace is not terminating, ignore it
if namespace.Status.Phase != kapi.NamespaceTerminating {
return nil
}
// if we already processed this namespace, ignore it
if projectutil.Finalized(namespace) {
return nil
}
// there may still be content for us to remove
err = deleteAllContent(c.Client, namespace.Name)
if err != nil {
return err
}
// we have removed content, so mark it finalized by us
_, err = projectutil.Finalize(c.KubeClient, namespace)
if err != nil {
return err
}
return nil
}
// deleteAllContent will purge all content in openshift in the specified namespace
func deleteAllContent(client osclient.Interface, namespace string) (err error) {
err = deleteBuildConfigs(client, namespace)
if err != nil {
return err
}
err = deleteBuilds(client, namespace)
if err != nil {
return err
}
err = deleteDeploymentConfigs(client, namespace)
if err != nil {
return err
}
err = deleteImageStreams(client, namespace)
if err != nil {
return err
}
err = deletePolicies(client, namespace)
if err != nil {
return err
}
err = deletePolicyBindings(client, namespace)
if err != nil {
return err
}
err = deleteRoleBindings(client, namespace)
if err != nil {
return err
}
err = deleteRoles(client, namespace)
if err != nil {
return err
}
err = deleteRoutes(client, namespace)
if err != nil {
return err
}
err = deleteTemplates(client, namespace)
if err != nil {
return err
}
return nil
}
func deleteTemplates(client osclient.Interface, ns string) error {
items, err := client.Templates(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.Templates(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deleteRoutes(client osclient.Interface, ns string) error {
items, err := client.Routes(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.Routes(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deleteRoles(client osclient.Interface, ns string) error {
items, err := client.Roles(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.Roles(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deleteRoleBindings(client osclient.Interface, ns string) error {
items, err := client.RoleBindings(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.RoleBindings(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deletePolicyBindings(client osclient.Interface, ns string) error {
items, err := client.PolicyBindings(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.PolicyBindings(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deletePolicies(client osclient.Interface, ns string) error {
items, err := client.Policies(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.Policies(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deleteImageStreams(client osclient.Interface, ns string) error {
items, err := client.ImageStreams(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.ImageStreams(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deleteDeploymentConfigs(client osclient.Interface, ns string) error {
items, err := client.DeploymentConfigs(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.DeploymentConfigs(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deleteBuilds(client osclient.Interface, ns string) error {
items, err := client.Builds(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.Builds(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}
func deleteBuildConfigs(client osclient.Interface, ns string) error {
items, err := client.BuildConfigs(ns).List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
for i := range items.Items {
err := client.BuildConfigs(ns).Delete(items.Items[i].Name)
if err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}