-
Notifications
You must be signed in to change notification settings - Fork 155
/
pvwatcher_controller.go
172 lines (151 loc) · 5.47 KB
/
pvwatcher_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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 pvwatcher
import (
"context"
"fmt"
"strings"
"time"
"go.uber.org/zap"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
predicateutils "k8c.io/kubermatic/v2/pkg/controller/util/predicate"
"k8c.io/kubermatic/v2/pkg/provider/kubernetes"
"k8c.io/kubermatic/v2/pkg/resources"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
ControllerName = "kkp-persistent-volume-watcher"
)
type Reconciler struct {
log *zap.SugaredLogger
workerName string
ctrlruntimeclient.Client
recorder record.EventRecorder
}
// add the controller.
func Add(
log *zap.SugaredLogger,
mgr manager.Manager,
numWorkers int,
workerName string,
) error {
log = log.Named(ControllerName)
reconciler := &Reconciler{
log: log,
workerName: workerName,
Client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor(ControllerName),
}
c, err := controller.New(ControllerName, mgr, controller.Options{
Reconciler: reconciler,
MaxConcurrentReconciles: numWorkers,
})
if err != nil {
return fmt.Errorf("failed to create controller: %w", err)
}
// reconcile PVCs in ClaimLost phase only
LostClaimPredicates := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
newObj := e.ObjectNew.(*corev1.PersistentVolumeClaim)
return newObj.Status.Phase == corev1.ClaimLost
},
}
if err := c.Watch(&source.Kind{Type: &corev1.PersistentVolumeClaim{}}, &handler.EnqueueRequestForObject{},
LostClaimPredicates,
predicateutils.ByLabel(resources.AppLabelKey, resources.EtcdStatefulSetName)); err != nil {
return fmt.Errorf("failed to create watch for PersistentVolumeClaims: %w", err)
}
return nil
}
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := r.log.With("pvc", request)
log.Debug("Reconciling")
cluster, err := kubernetes.ClusterFromNamespace(ctx, r, request.Namespace)
if err != nil {
return reconcile.Result{}, err
}
if cluster == nil {
return reconcile.Result{}, nil
}
if cluster.Labels[kubermaticv1.WorkerNameLabelKey] != r.workerName || cluster.Spec.Pause {
return reconcile.Result{}, nil
}
if !cluster.Spec.Features[kubermaticv1.ClusterFeatureEtcdLauncher] {
return reconcile.Result{}, nil
}
result, err := r.reconcile(ctx, log, request)
if err != nil {
log.Errorw("Reconciling failed", zap.Error(err))
r.recorder.Event(cluster, corev1.EventTypeWarning, "ReconcilingError", err.Error())
}
return result, err
}
func (r *Reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, request reconcile.Request) (reconcile.Result, error) {
pvc := &corev1.PersistentVolumeClaim{}
if err := r.Get(ctx, types.NamespacedName{Name: request.Name, Namespace: request.Namespace}, pvc); err != nil {
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
if pvc.Status.Phase != corev1.ClaimLost {
return reconcile.Result{}, nil
}
// find the pvc pod, delete it
podName := strings.ReplaceAll(pvc.Name, "data-", "")
pvcPod := &corev1.Pod{}
if err := r.Get(ctx, types.NamespacedName{Name: podName, Namespace: pvc.Namespace}, pvcPod); err != nil {
return reconcile.Result{}, err
}
if err := r.Delete(ctx, pvcPod); err != nil && !apierrors.IsNotFound(err) {
return reconcile.Result{}, err
}
// When the pod is deleted and recreated it, it will be stuck in PodPending phase
if err := wait.Poll(5*time.Second, 30*time.Second, func() (bool, error) {
if err := r.Get(ctx, types.NamespacedName{Name: podName, Namespace: pvc.Namespace}, pvcPod); err != nil {
return false, err
}
return pvcPod.Status.Phase == corev1.PodPending, nil
}); err != nil {
return reconcile.Result{}, err
}
// delete the pvc, make sure it's deleted
if err := r.Delete(ctx, pvc); err != nil && !apierrors.IsNotFound(err) {
return reconcile.Result{}, err
}
if err := wait.Poll(5*time.Second, 30*time.Second, func() (bool, error) {
err := r.Get(ctx, types.NamespacedName{Name: pvc.Name, Namespace: pvc.Namespace}, pvc)
if err != nil && apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}); err != nil {
return reconcile.Result{}, err
}
// Workaround to force the sts to recreate the PVC/PV, we need to "reboot" the StatefulSet.
return reconcile.Result{},
r.DeleteAllOf(ctx, &corev1.Pod{}, ctrlruntimeclient.InNamespace(pvc.Namespace),
ctrlruntimeclient.MatchingLabels{resources.AppLabelKey: resources.EtcdStatefulSetName})
}