-
Notifications
You must be signed in to change notification settings - Fork 113
/
jobcontroller.go
233 lines (194 loc) · 6.6 KB
/
jobcontroller.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
/*
Copyright 2019 OpenEBS 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.
This pkg is inspired from the deleter pkg in local-static-provisioner
in kubernetes-sigs
https://github.com/kubernetes-sigs/sig-storage-local-static-provisioner/tree/master/pkg/deleter
*/
package cleaner
import (
"context"
"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
"github.com/openebs/node-disk-manager/pkg/apis/openebs/v1alpha1"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// JobContainerName is the name of the cleanup job container
JobContainerName = "cleaner"
// JobNamePrefix is the prefix for the cleanup job name
JobNamePrefix = "cleanup-"
// BDLabel is the label set on the job for identification of the BD
BDLabel = "blockdevice"
// BlockCleanerCommand is the command used to clean raw block
BlockCleanerCommand = "wipefs"
)
// JobController defines the interface for the job controller.
type JobController interface {
IsCleaningJobRunning(bdName string) bool
CancelJob(bdName string) error
RemoveJob(bdName string) (CleanupState, error)
}
var _ JobController = &jobController{}
type jobController struct {
client client.Client
namespace string
}
// NewCleanupJob creates a new cleanup job in the namespace. It returns a Job object which can be used to
// start the job
func NewCleanupJob(bd *v1alpha1.BlockDevice, volMode VolumeMode, tolerations []v1.Toleration, namespace string) (*batchv1.Job, error) {
nodeName := bd.Labels[controller.KubernetesHostNameLabel]
priv := true
jobContainer := v1.Container{
Name: JobContainerName,
Image: getCleanUpImage(),
SecurityContext: &v1.SecurityContext{
Privileged: &priv,
},
}
podSpec := v1.PodSpec{}
mountName := "vol-mount"
if volMode == VolumeModeBlock {
// wipefs erases the filesystem signature from the block
// -a wipe all magic strings
// -f force erasure
jobContainer.Command = getCommand(BlockCleanerCommand, "-af", bd.Spec.Path)
// in case of sparse disk, need to mount the sparse file directory
// and clear the sparse file
if bd.Spec.Details.DeviceType == controller.SparseBlockDeviceType {
volume, volumeMount := getVolumeMounts(bd.Spec.Path, bd.Spec.Path, mountName)
jobContainer.VolumeMounts = []v1.VolumeMount{volumeMount}
podSpec.Volumes = []v1.Volume{volume}
}
} else if volMode == VolumeModeFileSystem {
jobContainer.Command = []string{"/bin/sh", "-c"}
jobContainer.Args = []string{"find /tmp -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf"}
volume, volumeMount := getVolumeMounts(bd.Spec.FileSystem.Mountpoint, "/tmp", mountName)
jobContainer.VolumeMounts = []v1.VolumeMount{volumeMount}
podSpec.Volumes = []v1.Volume{volume}
}
podSpec.Tolerations = tolerations
podSpec.ServiceAccountName = getServiceAccount()
podSpec.Containers = []v1.Container{jobContainer}
podSpec.NodeSelector = map[string]string{controller.KubernetesHostNameLabel: nodeName}
podTemplate := v1.Pod{}
podTemplate.Spec = podSpec
labels := map[string]string{
controller.KubernetesHostNameLabel: nodeName,
BDLabel: bd.Name,
}
podTemplate.ObjectMeta = metav1.ObjectMeta{
Name: generateCleaningJobName(bd.Name),
Namespace: namespace,
Labels: labels,
}
job := &batchv1.Job{}
job.ObjectMeta = podTemplate.ObjectMeta
job.Spec.Template.Spec = podTemplate.Spec
job.Spec.Template.Spec.RestartPolicy = v1.RestartPolicyOnFailure
return job, nil
}
// NewJobController returns a job controller struct which can be used to get the status
// of the running job
func NewJobController(client client.Client, namespace string) *jobController {
return &jobController{
client: client,
namespace: namespace,
}
}
func (c *jobController) IsCleaningJobRunning(bdName string) bool {
jobName := generateCleaningJobName(bdName)
objKey := client.ObjectKey{
Namespace: c.namespace,
Name: jobName,
}
job := &batchv1.Job{}
err := c.client.Get(context.TODO(), objKey, job)
if errors.IsNotFound(err) {
return false
}
if err != nil {
// failed to check whether it is running, assuming job is still running
return true
}
return job.Status.Succeeded <= 0
}
func (c *jobController) RemoveJob(bdName string) (CleanupState, error) {
jobName := generateCleaningJobName(bdName)
objKey := client.ObjectKey{
Namespace: c.namespace,
Name: jobName,
}
job := &batchv1.Job{}
err := c.client.Get(context.TODO(), objKey, job)
if err != nil {
if errors.IsNotFound(err) {
return CleanupStateNotFound, nil
}
return CleanupStateUnknown, err
}
if job.Status.Succeeded == 0 {
return CleanupStateRunning, nil
}
// cancel the job
err = c.CancelJob(bdName)
if err != nil {
return CleanupStateUnknown, err
}
return CleanupStateSucceeded, nil
}
// CancelJob deletes a job, if it is present. if the job is not present, it will return an error.
func (c *jobController) CancelJob(bdName string) error {
jobName := generateCleaningJobName(bdName)
objKey := client.ObjectKey{
Namespace: c.namespace,
Name: jobName,
}
job := &batchv1.Job{}
err := c.client.Get(context.TODO(), objKey, job)
err = c.client.Delete(context.TODO(), job, client.PropagationPolicy(metav1.DeletePropagationForeground))
return err
}
func generateCleaningJobName(bdName string) string {
return JobNamePrefix + bdName
}
func getCommand(cmd string, args ...string) []string {
var command []string
command = append(command, cmd)
for _, arg := range args {
command = append(command, arg)
}
return command
}
// GetNodeName gets the Node name from BlockDevice
func GetNodeName(bd *v1alpha1.BlockDevice) string {
return bd.Spec.NodeAttributes.NodeName
}
// getVolumeMounts returns the volume and volume mount for the given hostpath and
// mountpath
func getVolumeMounts(hostPath, mountPath, mountName string) (v1.Volume, v1.VolumeMount) {
volumes := v1.Volume{
Name: mountName,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: hostPath,
},
},
}
volumeMount := v1.VolumeMount{
Name: mountName,
MountPath: mountPath,
}
return volumes, volumeMount
}