/
cleanup.go
260 lines (226 loc) · 10.1 KB
/
cleanup.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
/*
Copyright 2020 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 cluster
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/operator/ceph/cluster/mgr"
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
"github.com/rook/rook/pkg/operator/ceph/cluster/osd"
"github.com/rook/rook/pkg/operator/ceph/cluster/rbd"
"github.com/rook/rook/pkg/operator/ceph/controller"
"github.com/rook/rook/pkg/operator/ceph/file/mds"
"github.com/rook/rook/pkg/operator/ceph/file/mirror"
"github.com/rook/rook/pkg/operator/ceph/object"
"github.com/rook/rook/pkg/operator/k8sutil"
batch "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
clusterCleanUpPolicyRetryInterval = 5 //seconds
// CleanupAppName is the cluster clean up job name
CleanupAppName = "rook-ceph-cleanup"
)
var (
volumeName = "cleanup-volume"
dataDirHostPath = "ROOK_DATA_DIR_HOST_PATH"
namespaceDir = "ROOK_NAMESPACE_DIR"
monitorSecret = "ROOK_MON_SECRET"
clusterFSID = "ROOK_CLUSTER_FSID"
sanitizeMethod = "ROOK_SANITIZE_METHOD"
sanitizeDataSource = "ROOK_SANITIZE_DATA_SOURCE"
sanitizeIteration = "ROOK_SANITIZE_ITERATION"
sanitizeIterationDefault int32 = 1
)
func (c *ClusterController) startClusterCleanUp(context context.Context, cluster *cephv1.CephCluster, cephHosts []string, monSecret, clusterFSID string) {
logger.Infof("starting clean up for cluster %q", cluster.Name)
err := c.waitForCephDaemonCleanUp(context, cluster, time.Duration(clusterCleanUpPolicyRetryInterval)*time.Second)
if err != nil {
logger.Errorf("failed to wait till ceph daemons are destroyed. %v", err)
return
}
c.startCleanUpJobs(cluster, cephHosts, monSecret, clusterFSID)
}
func (c *ClusterController) startCleanUpJobs(cluster *cephv1.CephCluster, cephHosts []string, monSecret, clusterFSID string) {
for _, hostName := range cephHosts {
logger.Infof("starting clean up job on node %q", hostName)
jobName := k8sutil.TruncateNodeNameForJob("cluster-cleanup-job-%s", hostName)
podSpec := c.cleanUpJobTemplateSpec(cluster, monSecret, clusterFSID)
podSpec.Spec.NodeSelector = map[string]string{v1.LabelHostname: hostName}
labels := controller.AppLabels(CleanupAppName, cluster.Namespace)
labels[CleanupAppName] = "true"
job := &batch.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
Namespace: cluster.Namespace,
Labels: labels,
},
Spec: batch.JobSpec{
Template: podSpec,
},
}
// Apply annotations
cephv1.GetCleanupAnnotations(cluster.Spec.Annotations).ApplyToObjectMeta(&job.ObjectMeta)
cephv1.GetCleanupLabels(cluster.Spec.Labels).ApplyToObjectMeta(&job.ObjectMeta)
if err := k8sutil.RunReplaceableJob(c.OpManagerCtx, c.context.Clientset, job, true); err != nil {
logger.Errorf("failed to run cluster clean up job on node %q. %v", hostName, err)
}
}
}
func (c *ClusterController) cleanUpJobContainer(cluster *cephv1.CephCluster, monSecret, cephFSID string) v1.Container {
volumeMounts := []v1.VolumeMount{}
envVars := []v1.EnvVar{}
if cluster.Spec.DataDirHostPath != "" {
if cluster.Spec.CleanupPolicy.SanitizeDisks.Iteration == 0 {
cluster.Spec.CleanupPolicy.SanitizeDisks.Iteration = sanitizeIterationDefault
}
hostPathVolumeMount := v1.VolumeMount{Name: volumeName, MountPath: cluster.Spec.DataDirHostPath}
devMount := v1.VolumeMount{Name: "devices", MountPath: "/dev"}
volumeMounts = append(volumeMounts, hostPathVolumeMount)
volumeMounts = append(volumeMounts, devMount)
envVars = append(envVars, []v1.EnvVar{
{Name: dataDirHostPath, Value: cluster.Spec.DataDirHostPath},
{Name: namespaceDir, Value: cluster.Namespace},
{Name: monitorSecret, Value: monSecret},
{Name: clusterFSID, Value: cephFSID},
{Name: "ROOK_LOG_LEVEL", Value: "DEBUG"},
mon.PodNamespaceEnvVar(cluster.Namespace),
{Name: sanitizeMethod, Value: cluster.Spec.CleanupPolicy.SanitizeDisks.Method.String()},
{Name: sanitizeDataSource, Value: cluster.Spec.CleanupPolicy.SanitizeDisks.DataSource.String()},
{Name: sanitizeIteration, Value: strconv.Itoa(int(cluster.Spec.CleanupPolicy.SanitizeDisks.Iteration))},
}...)
}
// Run a UID 0 since ceph-volume does not support running non-root
// See https://tracker.ceph.com/issues/53511
// Also, it's hard to catch the ceph version since the cluster is being deleted so not
// implementing a version check and simply always run this as root
rootUserID := int64(0)
securityContext := osd.PrivilegedContext()
securityContext.RunAsUser = &rootUserID
return v1.Container{
Name: "host-cleanup",
Image: c.rookImage,
SecurityContext: securityContext,
VolumeMounts: volumeMounts,
Env: envVars,
Args: []string{"ceph", "clean"},
Resources: cephv1.GetCleanupResources(cluster.Spec.Resources),
}
}
func (c *ClusterController) cleanUpJobTemplateSpec(cluster *cephv1.CephCluster, monSecret, clusterFSID string) v1.PodTemplateSpec {
volumes := []v1.Volume{}
hostPathVolume := v1.Volume{Name: volumeName, VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: cluster.Spec.DataDirHostPath}}}
devVolume := v1.Volume{Name: "devices", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/dev"}}}
volumes = append(volumes, hostPathVolume)
volumes = append(volumes, devVolume)
podSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: CleanupAppName,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
c.cleanUpJobContainer(cluster, monSecret, clusterFSID),
},
Volumes: volumes,
RestartPolicy: v1.RestartPolicyOnFailure,
PriorityClassName: cephv1.GetCleanupPriorityClassName(cluster.Spec.PriorityClassNames),
},
}
cephv1.GetCleanupAnnotations(cluster.Spec.Annotations).ApplyToObjectMeta(&podSpec.ObjectMeta)
cephv1.GetCleanupLabels(cluster.Spec.Labels).ApplyToObjectMeta(&podSpec.ObjectMeta)
// Apply placement
getCleanupPlacement(cluster.Spec).ApplyToPodSpec(&podSpec.Spec)
return podSpec
}
// getCleanupPlacement returns the placement for the cleanup job
func getCleanupPlacement(c cephv1.ClusterSpec) cephv1.Placement {
// The cleanup jobs are assigned by the operator to a specific node, so the
// node affinity and other affinity are not needed for scheduling.
// The only placement required for the cleanup daemons is the tolerations.
tolerations := c.Placement[cephv1.KeyAll].Tolerations
tolerations = append(tolerations, c.Placement[cephv1.KeyCleanup].Tolerations...)
tolerations = append(tolerations, c.Placement[cephv1.KeyMonArbiter].Tolerations...)
tolerations = append(tolerations, c.Placement[cephv1.KeyMon].Tolerations...)
tolerations = append(tolerations, c.Placement[cephv1.KeyMgr].Tolerations...)
tolerations = append(tolerations, c.Placement[cephv1.KeyOSD].Tolerations...)
// Add the tolerations for all the device sets
for _, deviceSet := range c.Storage.StorageClassDeviceSets {
tolerations = append(tolerations, deviceSet.Placement.Tolerations...)
}
return cephv1.Placement{Tolerations: tolerations}
}
func (c *ClusterController) waitForCephDaemonCleanUp(context context.Context, cluster *cephv1.CephCluster, retryInterval time.Duration) error {
logger.Infof("waiting for all the ceph daemons to be cleaned up in the cluster %q", cluster.Namespace)
for {
select {
case <-time.After(retryInterval):
cephHosts, err := c.getCephHosts(cluster.Namespace)
if err != nil {
return errors.Wrap(err, "failed to list ceph daemon nodes")
}
if len(cephHosts) == 0 {
logger.Info("all ceph daemons are cleaned up")
return nil
}
logger.Debugf("waiting for ceph daemons in cluster %q to be cleaned up. Retrying in %q",
cluster.Namespace, retryInterval.String())
case <-context.Done():
return errors.Errorf("cancelling the host cleanup job. %s", context.Err())
}
}
}
// getCephHosts returns a list of host names where ceph daemon pods are running
func (c *ClusterController) getCephHosts(namespace string) ([]string, error) {
cephAppNames := []string{mon.AppName, mgr.AppName, osd.AppName, object.AppName, mds.AppName, rbd.AppName, mirror.AppName}
nodeNameList := sets.NewString()
hostNameList := []string{}
var b strings.Builder
// get all the node names where ceph daemons are running
for _, app := range cephAppNames {
appLabelSelector := fmt.Sprintf("app=%s", app)
podList, err := c.context.Clientset.CoreV1().Pods(namespace).List(c.OpManagerCtx, metav1.ListOptions{LabelSelector: appLabelSelector})
if err != nil {
return hostNameList, errors.Wrapf(err, "could not list the %q pods", app)
}
for _, cephPod := range podList.Items {
podNodeName := cephPod.Spec.NodeName
if podNodeName != "" && !nodeNameList.Has(podNodeName) {
nodeNameList.Insert(podNodeName)
}
}
fmt.Fprintf(&b, "%s: %d. ", app, len(podList.Items))
}
logger.Infof("existing ceph daemons in the namespace %q. %s", namespace, b.String())
for nodeName := range nodeNameList {
podHostName, err := k8sutil.GetNodeHostName(c.OpManagerCtx, c.context.Clientset, nodeName)
if err != nil {
return nil, errors.Wrapf(err, "failed to get hostname from node %q", nodeName)
}
hostNameList = append(hostNameList, podHostName)
}
return hostNameList, nil
}
func (c *ClusterController) getCleanUpDetails(namespace string) (string, string, error) {
clusterInfo, _, _, err := mon.LoadClusterInfo(c.context, c.OpManagerCtx, namespace)
if err != nil {
return "", "", errors.Wrap(err, "failed to get cluster info")
}
return clusterInfo.MonitorSecret, clusterInfo.FSID, nil
}