-
Notifications
You must be signed in to change notification settings - Fork 113
/
cleaner.go
195 lines (167 loc) · 6.13 KB
/
cleaner.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
/*
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/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
v1 "k8s.io/api/core/v1"
)
// CleanupState represents the current state of the cleanup job
type CleanupState int
const (
// CleanupStateUnknown represents an unknown state of the cleanup job
CleanupStateUnknown CleanupState = iota + 1
// CleanupStateNotFound defines the state when a job does not exist
CleanupStateNotFound
// CleanupStateRunning represents a running cleanup job
CleanupStateRunning
// CleanupStateSucceeded represents that the cleanup job has been completed successfully
CleanupStateSucceeded
)
// VolumeMode defines the volume mode of the BlockDevice. It can be either block mode or
// filesystem mode
type VolumeMode string
const (
// VolumeModeBlock defines a raw block volume mode which means the block device should
// be treated as raw block device
VolumeModeBlock = "BlockVolumeMode"
// VolumeModeFileSystem defines that the blockdevice should be treated as a block
// formatted with filesystem and is mounted
VolumeModeFileSystem = "FileSystemVolumeMode"
)
// Cleaner handles BD cleanup
// For filesystem/mount based block devices, it deletes the contents of the directory
// For raw block devices, a `wipefs` command will be issued.
type Cleaner struct {
Client client.Client
Namespace string
CleanupStatus *CleanupStatusTracker
}
// CleanupStatusTracker is used to track the cleanup state using
// info provided by JobController
type CleanupStatusTracker struct {
JobController JobController
}
// NewCleaner creates a new cleaner which can be used for cleaning BD, and checking status of the job
func NewCleaner(client client.Client, namespace string, cleanupTracker *CleanupStatusTracker) *Cleaner {
return &Cleaner{
Client: client,
Namespace: namespace,
CleanupStatus: cleanupTracker,
}
}
// Clean will launch a job to delete data on the BD depending on the
// volume mode. Job will be launched only if another job is not running or a
// job is in unknown state
func (c *Cleaner) Clean(blockDevice *v1alpha1.BlockDevice) (bool, error) {
bdName := blockDevice.Name
// check if a cleanup job for the bd already exists and return
if c.CleanupStatus.InProgress(bdName) {
// check if the BD for which the cleanup is performed is still active,
// if not, cancel the cleanup job
if blockDevice.Status.State != v1alpha1.BlockDeviceActive {
// cancel the job
if err := c.CleanupStatus.CancelJob(bdName); err != nil {
return false, err
}
}
return false, nil
}
// Check if cleaning was just completed. if job was completed, it will be removed,
// The state of the job will be returned.
state, err := c.CleanupStatus.RemoveStatus(bdName)
if err != nil {
return false, nil
}
switch state {
case CleanupStateSucceeded:
return true, nil
case CleanupStateNotFound:
// if the BD is not active, do not start the job
if blockDevice.Status.State != v1alpha1.BlockDeviceActive {
return false, nil
}
// we are starting the job, since no job for the BD was found and
// BD is in active state
}
volMode := getVolumeMode(blockDevice.Spec)
// create a new job for the blockdevice
err = c.runJob(blockDevice, volMode)
return false, err
}
// InProgress returns whether a cleanup job is currently being done
// for the given BD
func (c *CleanupStatusTracker) InProgress(bdName string) bool {
return c.JobController.IsCleaningJobRunning(bdName)
}
// RemoveStatus returns the Cleanupstate of a job. If job is succeeded, it will
// be deleted
func (c *CleanupStatusTracker) RemoveStatus(bdName string) (CleanupState, error) {
return c.JobController.RemoveJob(bdName)
}
// CancelJob cancels a job without checking the status of the job.
func (c *CleanupStatusTracker) CancelJob(bdName string) error {
return c.JobController.CancelJob(bdName)
}
// runJob creates a new cleanup job in the namespace
func (c *Cleaner) runJob(bd *v1alpha1.BlockDevice, volumeMode VolumeMode) error {
// retrieve node Object to pass tolerations to the Job
nodeName := GetNodeName(bd)
selectedNode, err := c.getNodeObjectByNodeName(nodeName)
if err != nil {
return err
}
tolerations := getTolerationsForTaints(selectedNode.Spec.Taints...)
job, err := NewCleanupJob(bd, volumeMode, tolerations, c.Namespace)
if err != nil {
return err
}
return c.Client.Create(context.TODO(), job)
}
// getNodeObjectByNodeName returns Node Object, using NodeName
func (c *Cleaner) getNodeObjectByNodeName(nodeName string) (*v1.Node, error) {
node := &v1.Node{}
err := c.Client.Get(context.TODO(), client.ObjectKey{Namespace: "", Name: nodeName}, node)
if err != nil {
return nil, err
}
return node, nil
}
// getTolerationsForTaints returns tolerations, taking input as taints
func getTolerationsForTaints(taints ...v1.Taint) []v1.Toleration {
tolerations := []v1.Toleration{}
for i := range taints {
var toleration v1.Toleration
toleration.Key = taints[i].Key
toleration.Effect = taints[i].Effect
if len(taints[i].Value) == 0 {
toleration.Operator = v1.TolerationOpExists
} else {
toleration.Value = taints[i].Value
toleration.Operator = v1.TolerationOpEqual
}
tolerations = append(tolerations, toleration)
}
return tolerations
}
// getVolumeMode returns the volume mode of the BD that is being released
func getVolumeMode(spec v1alpha1.DeviceSpec) VolumeMode {
if spec.FileSystem.Mountpoint != "" {
return VolumeModeFileSystem
}
return VolumeModeBlock
}