/
crash.go
296 lines (254 loc) · 10.6 KB
/
crash.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
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Copyright 2019 The Rook Authors. All rights reserved.
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 crash
import (
"fmt"
"path"
"k8s.io/api/batch/v1beta1"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/operator/ceph/config"
"github.com/rook/rook/pkg/operator/ceph/config/keyring"
"github.com/rook/rook/pkg/operator/ceph/controller"
cephver "github.com/rook/rook/pkg/operator/ceph/version"
"github.com/rook/rook/pkg/operator/k8sutil"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
crashCollectorKeyringUsername = "client.crash"
crashCollectorKeyName = "rook-ceph-crash-collector-keyring"
// pruneSchedule is scheduled to run every day at midnight.
pruneSchedule = "0 0 * * *"
)
// createOrUpdateCephCrash is a wrapper around controllerutil.CreateOrUpdate
func (r *ReconcileNode) createOrUpdateCephCrash(node corev1.Node, tolerations []corev1.Toleration, cephCluster cephv1.CephCluster, cephVersion *cephver.CephVersion) (controllerutil.OperationResult, error) {
// Create or Update the deployment default/foo
nodeHostnameLabel, ok := node.ObjectMeta.Labels[corev1.LabelHostname]
if !ok {
return controllerutil.OperationResultNone, errors.Errorf("label key %q does not exist on node %q", corev1.LabelHostname, node.GetName())
}
deploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: k8sutil.TruncateNodeName(fmt.Sprintf("%s-%%s", AppName), nodeHostnameLabel),
Namespace: cephCluster.GetNamespace(),
},
}
err := controllerutil.SetControllerReference(&cephCluster, deploy, r.scheme)
if err != nil {
return controllerutil.OperationResultNone, errors.Errorf("failed to set owner reference of crashcollector deployment %q", deploy.Name)
}
volumes := controller.DaemonVolumesBase(config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath), "")
volumes = append(volumes, keyring.Volume().CrashCollector())
mutateFunc := func() error {
// labels for the pod, the deployment, and the deploymentSelector
deploymentLabels := map[string]string{
corev1.LabelHostname: nodeHostnameLabel,
k8sutil.AppAttr: AppName,
NodeNameLabel: node.GetName(),
}
deploymentLabels[config.CrashType] = "crash"
deploymentLabels[controller.DaemonIDLabel] = "crash"
deploymentLabels[k8sutil.ClusterAttr] = cephCluster.GetNamespace()
selectorLabels := map[string]string{
corev1.LabelHostname: nodeHostnameLabel,
k8sutil.AppAttr: AppName,
NodeNameLabel: node.GetName(),
}
nodeSelector := map[string]string{corev1.LabelHostname: nodeHostnameLabel}
// Deployment selector is immutable so we set this value only if
// a new object is going to be created
if deploy.ObjectMeta.CreationTimestamp.IsZero() {
deploy.Spec.Selector = &metav1.LabelSelector{
MatchLabels: selectorLabels,
}
}
deploy.ObjectMeta.Labels = deploymentLabels
cephv1.GetCrashCollectorLabels(cephCluster.Spec.Labels).ApplyToObjectMeta(&deploy.ObjectMeta)
k8sutil.AddRookVersionLabelToDeployment(deploy)
if cephVersion != nil {
controller.AddCephVersionLabelToDeployment(*cephVersion, deploy)
}
deploy.Spec.Template = corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: deploymentLabels,
},
Spec: corev1.PodSpec{
NodeSelector: nodeSelector,
InitContainers: []corev1.Container{
getCrashDirInitContainer(cephCluster),
getCrashChownInitContainer(cephCluster),
},
Containers: []corev1.Container{
getCrashDaemonContainer(cephCluster, *cephVersion),
},
Tolerations: tolerations,
RestartPolicy: corev1.RestartPolicyAlways,
HostNetwork: cephCluster.Spec.Network.IsHost(),
Volumes: volumes,
},
}
return nil
}
return controllerutil.CreateOrUpdate(r.opManagerContext, r.client, deploy, mutateFunc)
}
// createOrUpdateCephCron is a wrapper around controllerutil.CreateOrUpdate
func (r *ReconcileNode) createOrUpdateCephCron(cephCluster cephv1.CephCluster, cephVersion *cephver.CephVersion, useCronJobV1 bool) (controllerutil.OperationResult, error) {
objectMeta := metav1.ObjectMeta{
Name: prunerName,
Namespace: cephCluster.GetNamespace(),
}
// Adding volumes to pods containing data needed to connect to the ceph cluster.
volumes := controller.DaemonVolumesBase(config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath), "")
volumes = append(volumes, keyring.Volume().CrashCollector())
// labels for the pod, the deployment, and the deploymentSelector
cronJobLabels := map[string]string{
k8sutil.AppAttr: prunerName,
}
cronJobLabels[k8sutil.ClusterAttr] = cephCluster.GetNamespace()
podTemplateSpec := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: cronJobLabels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
getCrashPruneContainer(cephCluster, *cephVersion),
},
RestartPolicy: corev1.RestartPolicyNever,
HostNetwork: cephCluster.Spec.Network.IsHost(),
Volumes: volumes,
},
}
// After 100 failures, the cron job will no longer run.
// To avoid this, the cronjob is configured to only count the failures
// that occurred in the last hour.
deadline := int64(60)
// minimum k8s version required for v1 cronJob is 'v1.21.0'. Apply v1 if k8s version is at least 'v1.21.0', else apply v1beta1 cronJob.
if useCronJobV1 {
// delete v1beta1 cronJob if it already exists
err := r.client.Delete(r.opManagerContext, &v1beta1.CronJob{ObjectMeta: objectMeta})
if err != nil && !apierrors.IsNotFound(err) {
return controllerutil.OperationResultNone, errors.Wrapf(err, "failed to delete CronJob v1Beta1 %q", prunerName)
}
cronJob := &v1.CronJob{ObjectMeta: objectMeta}
err = controllerutil.SetControllerReference(&cephCluster, cronJob, r.scheme)
if err != nil {
return controllerutil.OperationResultNone, errors.Errorf("failed to set owner reference of deployment %q", cronJob.Name)
}
mutateFunc := func() error {
cronJob.ObjectMeta.Labels = cronJobLabels
cronJob.Spec.JobTemplate.Spec.Template = podTemplateSpec
cronJob.Spec.Schedule = pruneSchedule
cronJob.Spec.StartingDeadlineSeconds = &deadline
return nil
}
return controllerutil.CreateOrUpdate(r.opManagerContext, r.client, cronJob, mutateFunc)
}
cronJob := &v1beta1.CronJob{ObjectMeta: objectMeta}
err := controllerutil.SetControllerReference(&cephCluster, cronJob, r.scheme)
if err != nil {
return controllerutil.OperationResultNone, errors.Errorf("failed to set owner reference of deployment %q", cronJob.Name)
}
mutateFunc := func() error {
cronJob.ObjectMeta.Labels = cronJobLabels
cronJob.Spec.JobTemplate.Spec.Template = podTemplateSpec
cronJob.Spec.Schedule = pruneSchedule
cronJob.Spec.StartingDeadlineSeconds = &deadline
return nil
}
return controllerutil.CreateOrUpdate(r.opManagerContext, r.client, cronJob, mutateFunc)
}
func getCrashDirInitContainer(cephCluster cephv1.CephCluster) corev1.Container {
dataPathMap := config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath)
crashPostedDir := path.Join(dataPathMap.ContainerCrashDir(), "posted")
container := corev1.Container{
Name: "make-container-crash-dir",
Command: []string{
"mkdir",
"-p",
},
Args: []string{
crashPostedDir,
},
Image: cephCluster.Spec.CephVersion.Image,
SecurityContext: controller.PodSecurityContext(),
Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources),
VolumeMounts: controller.DaemonVolumeMounts(dataPathMap, ""),
}
return container
}
func getCrashChownInitContainer(cephCluster cephv1.CephCluster) corev1.Container {
dataPathMap := config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath)
return controller.ChownCephDataDirsInitContainer(
*dataPathMap,
cephCluster.Spec.CephVersion.Image,
controller.DaemonVolumeMounts(dataPathMap, ""),
cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources),
controller.PodSecurityContext(),
)
}
func getCrashDaemonContainer(cephCluster cephv1.CephCluster, cephVersion cephver.CephVersion) corev1.Container {
cephImage := cephCluster.Spec.CephVersion.Image
dataPathMap := config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath)
crashEnvVar := generateCrashEnvVar()
envVars := append(controller.DaemonEnvVars(cephImage), crashEnvVar)
volumeMounts := controller.DaemonVolumeMounts(dataPathMap, "")
volumeMounts = append(volumeMounts, keyring.VolumeMount().CrashCollector())
container := corev1.Container{
Name: "ceph-crash",
Command: []string{
"ceph-crash",
},
Image: cephImage,
Env: envVars,
VolumeMounts: volumeMounts,
Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources),
SecurityContext: controller.PodSecurityContext(),
}
return container
}
func getCrashPruneContainer(cephCluster cephv1.CephCluster, cephVersion cephver.CephVersion) corev1.Container {
cephImage := cephCluster.Spec.CephVersion.Image
envVars := append(controller.DaemonEnvVars(cephImage), generateCrashEnvVar())
dataPathMap := config.NewDatalessDaemonDataPathMap(cephCluster.GetNamespace(), cephCluster.Spec.DataDirHostPath)
volumeMounts := controller.DaemonVolumeMounts(dataPathMap, "")
volumeMounts = append(volumeMounts, keyring.VolumeMount().CrashCollector())
container := corev1.Container{
Name: "ceph-crash-pruner",
Command: []string{
"ceph",
"-n",
crashClient,
"crash",
"prune",
},
Args: []string{
fmt.Sprintf("%d", cephCluster.Spec.CrashCollector.DaysToRetain),
},
Image: cephImage,
Env: envVars,
VolumeMounts: volumeMounts,
Resources: cephv1.GetCrashCollectorResources(cephCluster.Spec.Resources),
SecurityContext: controller.PodSecurityContext(),
}
return container
}
func generateCrashEnvVar() corev1.EnvVar {
val := fmt.Sprintf("-m $(ROOK_CEPH_MON_HOST) -k %s", keyring.VolumeMount().CrashCollectorKeyringFilePath())
env := corev1.EnvVar{Name: "CEPH_ARGS", Value: val}
return env
}