-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathdeleter.go
138 lines (121 loc) · 4.59 KB
/
deleter.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
/*
Copyright 2023 The Kubernetes 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 deleter
import (
"context"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/common"
cleanupmetrics "sigs.k8s.io/sig-storage-local-static-provisioner/pkg/metrics/node-cleanup"
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/util"
)
// Deleter handles cleanup of local PVs with an affinity to a deleted Node.
// Only PVs with a StorageClass listed in the storageClassNames will be considered for cleanup.
type Deleter struct {
client kubernetes.Interface
pvLister corelisters.PersistentVolumeLister
nodeLister corelisters.NodeLister
storageClassNames []string
}
// NewDeleter creates a Deleter object to handle the deletion of local PVs
// that have an affinity to a deleted Node and have a StorageClass listed in storageClassNames.
func NewDeleter(client kubernetes.Interface, pvLister corelisters.PersistentVolumeLister, nodeLister corelisters.NodeLister, storageClassNames []string) *Deleter {
return &Deleter{
client: client,
pvLister: pvLister,
nodeLister: nodeLister,
storageClassNames: storageClassNames,
}
}
// Run will delete stale PVs on a given interval until the given context is done.
func (d *Deleter) Run(ctx context.Context, discoveryInterval time.Duration) {
for {
select {
case <-ctx.Done():
klog.Info("Deleter stopped")
return
default:
d.DeletePVs(ctx)
time.Sleep(discoveryInterval)
}
}
}
// DeletePVs will scan through PVs and delete those that are
// local PVs with a StorageClass listed in storageClassNames and have an affinity to a deleted Node.
func (d *Deleter) DeletePVs(ctx context.Context) {
pvs, err := d.pvLister.List(labels.Everything())
if err != nil {
klog.Errorf("error listing pvs: %s", err.Error())
return
}
for _, pv := range pvs {
if !common.IsLocalPVWithStorageClass(pv, d.storageClassNames) {
// Either isn't a local PV or doesn't have matching storage class.
continue
}
if !d.referencesNonExistentNode(pv) {
// PV's node is up so PV is not stale
continue
}
phase := pv.Status.Phase
reclaimPolicy := pv.Spec.PersistentVolumeReclaimPolicy
// PV is a stale object since it references a deleted Node.
// Therefore it can safely be deleted in the two following cases.
isReleasedWithDeleteReclaim := phase == v1.VolumeReleased && reclaimPolicy == v1.PersistentVolumeReclaimDelete
isAvailable := phase == v1.VolumeAvailable
if isReleasedWithDeleteReclaim || isAvailable {
klog.Infof("Attempting to delete PV that has NodeAffinity to deleted Node, pv: %s", pv.Name)
if err = d.deletePV(ctx, pv.Name); err != nil {
cleanupmetrics.PersistentVolumeDeleteFailedTotal.WithLabelValues(string(phase)).Inc()
klog.Errorf("Error deleting PV %q: %v", pv.Name, err)
continue
}
// TODO: Cache successful deletion to avoid multiple delete calls
// when there is a short sync period
cleanupmetrics.PersistentVolumeDeleteTotal.WithLabelValues(string(phase)).Inc()
}
}
}
// referencesNonExistentNode returns true if the local PV has a NodeAffinity to
// a deleted Node. An error is returned if the local PV's NodeAffinity
// does not have the form:
//
// nodeAffinity:
// required:
// nodeSelectorTerms:
// - matchExpressions:
// - key: kubernetes.io/hostname
// operator: In
// values:
// - <node1>
func (d *Deleter) referencesNonExistentNode(localPV *v1.PersistentVolume) bool {
nodeNames := util.GetLocalPersistentVolumeNodeNames(localPV)
if nodeNames == nil {
return false
}
return !common.AnyNodeExists(d.nodeLister, nodeNames)
}
func (d *Deleter) deletePV(ctx context.Context, pvName string) error {
err := d.client.CoreV1().PersistentVolumes().Delete(ctx, pvName, metav1.DeleteOptions{})
if err != nil && errors.IsNotFound(err) {
klog.Warningf("PV %q no longer exists", pvName)
return nil
}
return err
}