/
mariadb_controller_storage.go
161 lines (143 loc) · 5.26 KB
/
mariadb_controller_storage.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
package controller
import (
"context"
"errors"
"fmt"
"time"
mariadbv1alpha1 "github.com/mariadb-operator/mariadb-operator/api/v1alpha1"
"github.com/mariadb-operator/mariadb-operator/pkg/builder"
labels "github.com/mariadb-operator/mariadb-operator/pkg/builder/labels"
condition "github.com/mariadb-operator/mariadb-operator/pkg/condition"
"github.com/mariadb-operator/mariadb-operator/pkg/pvc"
stsobj "github.com/mariadb-operator/mariadb-operator/pkg/statefulset"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
func (r *MariaDBReconciler) reconcileStorage(ctx context.Context, mariadb *mariadbv1alpha1.MariaDB) (ctrl.Result, error) {
if mariadb.IsWaitingForStorageResize() {
return r.waitForStorageResize(ctx, mariadb)
}
key := client.ObjectKeyFromObject(mariadb)
var existingSts appsv1.StatefulSet
if err := r.Get(ctx, key, &existingSts); err != nil {
return ctrl.Result{}, err
}
existingSize := stsobj.GetStorageSize(&existingSts, builder.StorageVolume)
desiredSize := mariadb.Spec.Storage.GetSize()
if existingSize == nil {
return ctrl.Result{}, errors.New("invalid existing storage size")
}
if desiredSize == nil {
return ctrl.Result{}, errors.New("invalid desired storage size")
}
sizeCmp := desiredSize.Cmp(*existingSize)
if sizeCmp == 0 {
return ctrl.Result{}, nil
}
if sizeCmp < 0 {
return ctrl.Result{}, fmt.Errorf("cannot decrease storage size from '%s' to '%s'", existingSize, desiredSize)
}
if err := r.patchStatus(ctx, mariadb, func(status *mariadbv1alpha1.MariaDBStatus) error {
condition.SetReadyStorageResizing(status)
return nil
}); err != nil {
return ctrl.Result{}, fmt.Errorf("error patching status: %v", err)
}
if result, err := r.resizeInUsePVCs(ctx, mariadb, *desiredSize); !result.IsZero() || err != nil {
return result, err
}
if result, err := r.resizeStatefulSet(ctx, mariadb, &existingSts); !result.IsZero() || err != nil {
return result, err
}
if err := r.patchStatus(ctx, mariadb, func(status *mariadbv1alpha1.MariaDBStatus) error {
condition.SetReadyWaitingStorageResize(status)
return nil
}); err != nil {
return ctrl.Result{}, fmt.Errorf("error patching status: %v", err)
}
return r.waitForStorageResize(ctx, mariadb)
}
func (r *MariaDBReconciler) resizeInUsePVCs(ctx context.Context, mariadb *mariadbv1alpha1.MariaDB,
size resource.Quantity) (ctrl.Result, error) {
if !ptr.Deref(mariadb.Spec.Storage.ResizeInUseVolumes, true) {
return ctrl.Result{}, nil
}
pvcs, err := r.getStoragePVCs(ctx, mariadb)
if err != nil {
return ctrl.Result{}, err
}
for _, pvc := range pvcs {
patch := client.MergeFrom(pvc.DeepCopy())
pvc.Spec.Resources.Requests[corev1.ResourceStorage] = size
if err := r.Patch(ctx, &pvc, patch); err != nil {
return ctrl.Result{}, fmt.Errorf("error patching PVC '%s': %v", pvc.Name, err)
}
}
return ctrl.Result{}, nil
}
func (r *MariaDBReconciler) resizeStatefulSet(ctx context.Context, mariadb *mariadbv1alpha1.MariaDB,
sts *appsv1.StatefulSet) (ctrl.Result, error) {
if err := r.Delete(ctx, sts, &client.DeleteOptions{PropagationPolicy: ptr.To(metav1.DeletePropagationOrphan)}); err != nil {
return ctrl.Result{}, fmt.Errorf("error deleting StatefulSet: %v", err)
}
return r.reconcileStatefulSet(ctx, mariadb)
}
func (r *MariaDBReconciler) waitForStorageResize(ctx context.Context, mdb *mariadbv1alpha1.MariaDB) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.V(1).Info("Waiting for storage resize")
if ptr.Deref(mdb.Spec.Storage.ResizeInUseVolumes, true) && ptr.Deref(mdb.Spec.Storage.WaitForVolumeResize, true) {
pvcs, err := r.getStoragePVCs(ctx, mdb)
if err != nil {
return ctrl.Result{}, err
}
for _, p := range pvcs {
if pvc.IsResizing(&p) {
logger.V(1).Info("Waiting for PVC resize", "pvc", p.Name)
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
}
}
}
key := client.ObjectKeyFromObject(mdb)
var sts appsv1.StatefulSet
if err := r.Get(ctx, key, &sts); err != nil {
return ctrl.Result{}, err
}
if sts.Status.ReadyReplicas != mdb.Spec.Replicas {
logger.V(1).Info(
"Waiting for StatefulSet ready",
"ready-replicas", sts.Status.ReadyReplicas,
"expected-replicas", mdb.Spec.Replicas,
)
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
}
if err := r.patchStatus(ctx, mdb, func(status *mariadbv1alpha1.MariaDBStatus) error {
condition.SetReadyStorageResized(status)
return nil
}); err != nil {
return ctrl.Result{}, fmt.Errorf("error patching status: %v", err)
}
return ctrl.Result{}, nil
}
func (r *MariaDBReconciler) getStoragePVCs(ctx context.Context, mdb *mariadbv1alpha1.MariaDB) ([]corev1.PersistentVolumeClaim, error) {
pvcList := corev1.PersistentVolumeClaimList{}
listOpts := client.ListOptions{
LabelSelector: klabels.SelectorFromSet(
labels.NewLabelsBuilder().
WithMariaDB(mdb).
WithPVCRole(builder.StorageVolumeRole).
Build(),
),
Namespace: mdb.GetNamespace(),
}
if err := r.List(ctx, &pvcList, &listOpts); err != nil {
return nil, fmt.Errorf("error listing PVCs: %v", err)
}
return pvcList.Items, nil
}