-
Notifications
You must be signed in to change notification settings - Fork 80
/
replacements.go
279 lines (234 loc) · 9.25 KB
/
replacements.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
/*
* replacements.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021 Apple Inc. and the FoundationDB project authors
*
* 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 replacements
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/FoundationDB/fdb-kubernetes-operator/pkg/podmanager"
"sigs.k8s.io/controller-runtime/pkg/client"
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
"github.com/FoundationDB/fdb-kubernetes-operator/internal"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"
)
// ReplaceMisconfiguredProcessGroups checks if the cluster has any misconfigured process groups that must be replaced.
func ReplaceMisconfiguredProcessGroups(ctx context.Context, podManager podmanager.PodLifecycleManager, client client.Client, log logr.Logger, cluster *fdbv1beta2.FoundationDBCluster, pvcMap map[fdbv1beta2.ProcessGroupID]corev1.PersistentVolumeClaim) (bool, error) {
hasReplacements := false
maxReplacements, _ := getReplacementInformation(cluster, cluster.GetMaxConcurrentReplacements())
for _, processGroup := range cluster.Status.ProcessGroups {
if maxReplacements <= 0 {
log.Info("Early abort, reached limit of concurrent replacements")
break
}
if processGroup.IsMarkedForRemoval() {
continue
}
needsRemoval, err := ProcessGroupNeedsRemoval(ctx, podManager, client, log, cluster, processGroup, pvcMap)
// Do not mark for removal if there is an error
if err != nil {
continue
}
if needsRemoval {
processGroup.MarkForRemoval()
hasReplacements = true
maxReplacements--
}
}
return hasReplacements, nil
}
// ProcessGroupNeedsRemoval checks if a process group needs to be removed.
func ProcessGroupNeedsRemoval(ctx context.Context, podManager podmanager.PodLifecycleManager, client client.Client, log logr.Logger, cluster *fdbv1beta2.FoundationDBCluster, processGroup *fdbv1beta2.ProcessGroupStatus, pvcMap map[fdbv1beta2.ProcessGroupID]corev1.PersistentVolumeClaim) (bool, error) {
// TODO(johscheuer): Fix how we fetch the pvc to make better use of the controller runtime cache.
pvc, hasPVC := pvcMap[processGroup.ProcessGroupID]
pod, podErr := podManager.GetPod(ctx, client, cluster, processGroup.GetPodName(cluster))
if hasPVC {
needsPVCRemoval, err := processGroupNeedsRemovalForPVC(cluster, pvc, log, processGroup)
if err != nil {
return false, err
}
if needsPVCRemoval && podErr == nil {
return true, nil
}
} else if processGroup.ProcessClass.IsStateful() {
log.V(1).Info("Could not find PVC for process group ID",
"processGroupID", processGroup.ProcessGroupID)
}
if podErr != nil {
log.V(1).Info("Could not find Pod for process group ID",
"processGroupID", processGroup.ProcessGroupID)
return false, podErr
}
return processGroupNeedsRemovalForPod(cluster, pod, processGroup, log)
}
func processGroupNeedsRemovalForPVC(cluster *fdbv1beta2.FoundationDBCluster, pvc corev1.PersistentVolumeClaim, log logr.Logger, processGroup *fdbv1beta2.ProcessGroupStatus) (bool, error) {
processGroupID := internal.GetProcessGroupIDFromMeta(cluster, pvc.ObjectMeta)
logger := log.WithValues("namespace", cluster.Namespace, "cluster", cluster.Name, "pvc", pvc.Name, "processGroupID", processGroupID)
ownedByCluster := !cluster.ShouldFilterOnOwnerReferences()
if !ownedByCluster {
for _, ownerReference := range pvc.OwnerReferences {
if ownerReference.UID == cluster.UID {
ownedByCluster = true
break
}
}
}
if !ownedByCluster {
logger.Info("Ignoring PVC that is not owned by the cluster")
return false, nil
}
desiredPVC, err := internal.GetPvc(cluster, processGroup)
if err != nil {
return false, err
}
pvcHash, err := internal.GetJSONHash(desiredPVC.Spec)
if err != nil {
return false, err
}
if pvc.Annotations[fdbv1beta2.LastSpecKey] != pvcHash {
logger.Info("Replace process group",
"reason", fmt.Sprintf("PVC spec has changed from %s to %s", pvcHash, pvc.Annotations[fdbv1beta2.LastSpecKey]))
return true, nil
}
if pvc.Name != desiredPVC.Name {
logger.Info("Replace process group",
"reason", fmt.Sprintf("PVC name has changed from %s to %s", desiredPVC.Name, pvc.Name))
return true, nil
}
return false, nil
}
func processGroupNeedsRemovalForPod(cluster *fdbv1beta2.FoundationDBCluster, pod *corev1.Pod, processGroupStatus *fdbv1beta2.ProcessGroupStatus, log logr.Logger) (bool, error) {
if pod == nil {
return false, nil
}
logger := log.WithValues("namespace", cluster.Namespace, "cluster", cluster.Name, "processGroupID", processGroupStatus.ProcessGroupID)
if processGroupStatus.IsMarkedForRemoval() {
return false, nil
}
idNum, err := processGroupStatus.ProcessGroupID.GetIDNumber()
if err != nil {
return false, err
}
_, desiredProcessGroupID := cluster.GetProcessGroupID(processGroupStatus.ProcessClass, idNum)
if processGroupStatus.ProcessGroupID != desiredProcessGroupID {
logger.Info("Replace process group",
"reason", fmt.Sprintf("expect process group ID: %s", desiredProcessGroupID))
return true, nil
}
ipSource, err := internal.GetPublicIPSource(pod)
if err != nil {
return false, err
}
if ipSource != cluster.GetPublicIPSource() {
logger.Info("Replace process group",
"reason", fmt.Sprintf("publicIP source has changed from %s to %s", ipSource, cluster.GetPublicIPSource()))
return true, nil
}
serversPerPod, err := internal.GetServersPerPodForPod(pod, processGroupStatus.ProcessClass)
if err != nil {
return false, err
}
desiredServersPerPod := cluster.GetDesiredServersPerPod(processGroupStatus.ProcessClass)
// Replace the process group if the expected servers differ from the desired servers
if serversPerPod != desiredServersPerPod {
logger.Info("Replace process group",
"serversPerPod", serversPerPod,
"desiredServersPerPod", desiredServersPerPod,
"reason", fmt.Sprintf("serversPerPod have changes from current: %d to desired: %d", serversPerPod, desiredServersPerPod))
return true, nil
}
expectedNodeSelector := cluster.GetProcessSettings(processGroupStatus.ProcessClass).PodTemplate.Spec.NodeSelector
if !equality.Semantic.DeepEqual(pod.Spec.NodeSelector, expectedNodeSelector) {
specHash, err := internal.GetPodSpecHash(cluster, processGroupStatus, nil)
if err != nil {
return false, err
}
if pod.ObjectMeta.Annotations[fdbv1beta2.LastSpecKey] != specHash {
logger.Info("Replace process group",
"reason", fmt.Sprintf("nodeSelector has changed from %s to %s", pod.Spec.NodeSelector, expectedNodeSelector))
return true, nil
}
}
if cluster.NeedsReplacement(processGroupStatus) {
spec, err := internal.GetPodSpec(cluster, processGroupStatus)
if err != nil {
return false, err
}
specHash, err := internal.GetPodSpecHash(cluster, processGroupStatus, spec)
if err != nil {
return false, err
}
if pod.ObjectMeta.Annotations[fdbv1beta2.LastSpecKey] != specHash {
jsonSpec, err := json.Marshal(spec)
if err != nil {
return false, err
}
logger.Info("Replace process group",
"reason", "specHash has changed",
"desiredSpecHash", specHash,
"currentSpecHash", pod.ObjectMeta.Annotations[fdbv1beta2.LastSpecKey],
"desiredSpec", base64.StdEncoding.EncodeToString(jsonSpec),
)
return true, nil
}
}
if pointer.BoolDeref(cluster.Spec.ReplaceInstancesWhenResourcesChange, false) {
desiredSpec, err := internal.GetPodSpec(cluster, processGroupStatus)
if err != nil {
return false, err
}
if resourcesNeedsReplacement(desiredSpec.Containers, pod.Spec.Containers) {
logger.Info("Replace process group",
"reason", "Resource requests have changed")
return true, nil
}
if resourcesNeedsReplacement(desiredSpec.InitContainers, pod.Spec.InitContainers) {
logger.Info("Replace process group",
"reason", "Resource requests have changed")
return true, nil
}
}
return false, nil
}
func resourcesNeedsReplacement(desired []corev1.Container, current []corev1.Container) bool {
// We only care about requests since limits are ignored during scheduling
desiredCPURequests, desiredMemoryRequests := getCPUandMemoryRequests(desired)
currentCPURequests, currentMemoryRequests := getCPUandMemoryRequests(current)
return desiredCPURequests.Cmp(*currentCPURequests) == 1 || desiredMemoryRequests.Cmp(*currentMemoryRequests) == 1
}
func getCPUandMemoryRequests(containers []corev1.Container) (*resource.Quantity, *resource.Quantity) {
cpuRequests := &resource.Quantity{}
memoryRequests := &resource.Quantity{}
for _, container := range containers {
cpu := container.Resources.Requests.Cpu()
if cpu != nil {
cpuRequests.Add(*cpu)
}
memory := container.Resources.Requests.Memory()
if memory != nil {
memoryRequests.Add(*memory)
}
}
return cpuRequests, memoryRequests
}