-
Notifications
You must be signed in to change notification settings - Fork 30
/
controller.go
283 lines (237 loc) · 9.85 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package purge
import (
"context"
"fmt"
"strings"
"time"
"github.com/go-logr/logr"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/kyma-project/lifecycle-manager/internal/event"
"github.com/kyma-project/lifecycle-manager/internal/pkg/metrics"
"github.com/kyma-project/lifecycle-manager/internal/remote"
"github.com/kyma-project/lifecycle-manager/pkg/log"
"github.com/kyma-project/lifecycle-manager/pkg/matcher"
"github.com/kyma-project/lifecycle-manager/pkg/status"
"github.com/kyma-project/lifecycle-manager/pkg/util"
)
const (
setFinalizerFailure event.Reason = "SettingPurgeFinalizerFailed"
removeFinalizerFailure event.Reason = "RemovingPurgeFinalizerFailed"
)
type Reconciler struct {
client.Client
event.Event
SkrContextFactory remote.SkrContextProvider
PurgeFinalizerTimeout time.Duration
SkipCRDs matcher.CRDMatcherFunc
IsManagedKyma bool
Metrics *metrics.PurgeMetrics
}
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := logf.FromContext(ctx)
logger.V(log.DebugLevel).Info("Purge reconciliation started")
kyma := &v1beta2.Kyma{}
if err := r.Get(ctx, req.NamespacedName, kyma); err != nil {
return handleKymaNotFoundError(logger, kyma, err)
}
if kyma.DeletionTimestamp.IsZero() {
return r.handleKymaNotMarkedForDeletion(ctx, kyma)
}
if requeueAfter := r.calculateRequeueAfterTime(kyma); requeueAfter != 0 {
return handlePurgeNotDue(logger, kyma, requeueAfter)
}
start := time.Now()
err := r.SkrContextFactory.Init(ctx, kyma.GetNamespacedName())
if err != nil {
return r.handleSkrNotFoundError(ctx, kyma, err)
}
skrContext, err := r.SkrContextFactory.Get(kyma.GetNamespacedName())
if err != nil {
return r.handleSkrNotFoundError(ctx, kyma, err)
}
return r.handlePurge(ctx, kyma, skrContext.Client, start)
}
func (r *Reconciler) UpdateStatus(ctx context.Context, kyma *v1beta2.Kyma, state shared.State, message string) error {
if err := status.Helper(r).UpdateStatusForExistingModules(ctx, kyma, state, message); err != nil {
return fmt.Errorf("failed updating status to %s because of %s: %w", state, message, err)
}
return nil
}
func (r *Reconciler) UpdateMetrics(_ context.Context, _ *v1beta2.Kyma) {}
func (r *Reconciler) IsKymaManaged() bool {
return r.IsManagedKyma
}
func handleKymaNotFoundError(logger logr.Logger, kyma *v1beta2.Kyma, err error) (ctrl.Result, error) {
if util.IsNotFound(err) {
logger.V(log.DebugLevel).Info(fmt.Sprintf("Kyma %s not found, probably already deleted", kyma.GetName()))
return ctrl.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("failed getting Kyma %s: %w", kyma.GetName(), err)
}
func (r *Reconciler) handleKymaNotMarkedForDeletion(ctx context.Context, kyma *v1beta2.Kyma) (ctrl.Result, error) {
if err := r.ensurePurgeFinalizer(ctx, kyma); err != nil {
logf.FromContext(ctx).V(log.DebugLevel).Info(fmt.Sprintf("Failed setting purge finalizer for Kyma %s: %s", kyma.GetName(), err))
r.Event.Warning(kyma, setFinalizerFailure, err)
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func handlePurgeNotDue(logger logr.Logger, kyma *v1beta2.Kyma, requeueAfter time.Duration) (ctrl.Result, error) {
logger.Info(fmt.Sprintf("Purge reconciliation for Kyma %s will be requeued after %s", kyma.GetName(), requeueAfter))
return ctrl.Result{RequeueAfter: requeueAfter}, nil
}
func (r *Reconciler) handleRemovingPurgeFinalizerFailedError(ctx context.Context, kyma *v1beta2.Kyma, err error) (ctrl.Result, error) {
logf.FromContext(ctx).Error(err, fmt.Sprintf("Failed removing purge finalizer from Kyma %s/%s", kyma.GetNamespace(), kyma.GetName()))
r.Event.Warning(kyma, removeFinalizerFailure, err)
r.Metrics.SetPurgeError(ctx, kyma, metrics.ErrPurgeFinalizerRemoval)
return ctrl.Result{}, err
}
func (r *Reconciler) handleSkrNotFoundError(ctx context.Context, kyma *v1beta2.Kyma, err error) (ctrl.Result, error) {
if util.IsNotFound(err) {
dropped, err := r.dropPurgeFinalizer(ctx, kyma)
if err != nil {
return r.handleRemovingPurgeFinalizerFailedError(ctx, kyma, err)
}
if dropped {
logf.FromContext(ctx).Info("Removed purge finalizer for Kyma " + kyma.GetName())
}
r.Metrics.DeletePurgeError(ctx, kyma, metrics.ErrPurgeFinalizerRemoval)
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, fmt.Errorf("failed getting remote client for Kyma %s: %w", kyma.GetName(), err)
}
func (r *Reconciler) handleCleanupError(ctx context.Context, kyma *v1beta2.Kyma, err error) (ctrl.Result, error) {
logf.FromContext(ctx).Error(err, "failed purge cleanup for Kyma "+kyma.GetName())
r.Metrics.SetPurgeError(ctx, kyma, metrics.ErrCleanup)
return ctrl.Result{}, err
}
func (r *Reconciler) handlePurge(ctx context.Context, kyma *v1beta2.Kyma, remoteClient client.Client, start time.Time) (ctrl.Result, error) {
logger := logf.FromContext(ctx)
r.Metrics.UpdatePurgeCount()
handledResources, err := r.performCleanup(ctx, remoteClient)
if len(handledResources) > 0 {
logger.Info(fmt.Sprintf("Removed all finalizers for Kyma %s related resources %s", kyma.GetName(), strings.Join(handledResources, ", ")))
}
if err != nil {
return r.handleCleanupError(ctx, kyma, err)
}
r.Metrics.DeletePurgeError(ctx, kyma, metrics.ErrCleanup)
dropped, err := r.dropPurgeFinalizer(ctx, kyma)
if dropped {
logger.Info("Removed purge finalizer for Kyma " + kyma.GetName())
}
if err != nil {
return r.handleRemovingPurgeFinalizerFailedError(ctx, kyma, err)
}
r.Metrics.DeletePurgeError(ctx, kyma, metrics.ErrPurgeFinalizerRemoval)
r.Metrics.UpdatePurgeTime(time.Since(start))
return ctrl.Result{}, nil
}
func (r *Reconciler) ensurePurgeFinalizer(ctx context.Context, kyma *v1beta2.Kyma) error {
if controllerutil.AddFinalizer(kyma, shared.PurgeFinalizer) {
if err := r.Update(ctx, kyma); err != nil {
return fmt.Errorf("failed updating object: %w", err)
}
}
return nil
}
func (r *Reconciler) dropPurgeFinalizer(ctx context.Context, kyma *v1beta2.Kyma) (bool, error) {
if controllerutil.RemoveFinalizer(kyma, shared.PurgeFinalizer) {
if err := r.Update(ctx, kyma); err != nil {
return false, fmt.Errorf("failed updating object: %w", err)
}
return true, nil
}
return false, nil
}
func (r *Reconciler) calculateRequeueAfterTime(kyma *v1beta2.Kyma) time.Duration {
deletionDeadline := kyma.DeletionTimestamp.Add(r.PurgeFinalizerTimeout)
if time.Now().Before(deletionDeadline) {
return time.Until(deletionDeadline.Add(time.Second))
}
return 0
}
func (r *Reconciler) performCleanup(ctx context.Context, remoteClient client.Client) ([]string, error) {
crdList := apiextensionsv1.CustomResourceDefinitionList{}
if err := remoteClient.List(ctx, &crdList); err != nil {
return nil, fmt.Errorf("failed fetching CRDs from remote cluster: %w", err)
}
var allHandledResources []string
for _, crd := range crdList.Items {
if shouldSkip(crd, r.SkipCRDs) {
continue
}
staleResources, err := getAllRemainingCRs(ctx, remoteClient, crd)
if err != nil {
return allHandledResources, fmt.Errorf("failed fetching stale resources from remote cluster: %w", err)
}
handledResources, err := dropFinalizers(ctx, remoteClient, staleResources)
if err != nil {
return allHandledResources, fmt.Errorf("failed removing finalizers from stale resources: %w", err)
}
allHandledResources = append(allHandledResources, handledResources...)
}
return allHandledResources, nil
}
func shouldSkip(crd apiextensionsv1.CustomResourceDefinition, matcher matcher.CRDMatcherFunc) bool {
if crd.Spec.Group == v1beta2.GroupVersion.Group && crd.Spec.Names.Kind == string(shared.KymaKind) {
return true
}
return matcher(crd)
}
func getAllRemainingCRs(ctx context.Context, remoteClient client.Client,
crd apiextensionsv1.CustomResourceDefinition,
) (unstructured.UnstructuredList, error) {
staleResources := unstructured.UnstructuredList{}
// Since there are multiple possible versions, we are choosing storage version
var gvk schema.GroupVersionKind
for _, version := range crd.Spec.Versions {
if version.Storage {
gvk = schema.GroupVersionKind{
Group: crd.Spec.Group,
Kind: crd.Spec.Names.Kind,
Version: version.Name,
}
break
}
}
staleResources.SetGroupVersionKind(gvk)
if err := remoteClient.List(ctx, &staleResources); err != nil {
return unstructured.UnstructuredList{}, fmt.Errorf("failed fetching resources: %w", err)
}
return staleResources, nil
}
func dropFinalizers(ctx context.Context, remoteClient client.Client,
staleResources unstructured.UnstructuredList,
) ([]string, error) {
handledResources := make([]string, 0, len(staleResources.Items))
for index := range staleResources.Items {
resource := staleResources.Items[index]
resource.SetFinalizers(nil)
if err := remoteClient.Update(ctx, &resource); err != nil {
return handledResources, fmt.Errorf("failed updating resource: %w", err)
}
handledResources = append(handledResources, fmt.Sprintf("%s/%s", resource.GetNamespace(), resource.GetName()))
}
return handledResources, nil
}