-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifetime.go
110 lines (88 loc) · 2.27 KB
/
lifetime.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
// Copyright (c) 2022 Fujitsu Limited
package filesystem
import (
"fmt"
"os"
corev1 "k8s.io/api/core/v1"
)
type volumeController struct {
disableUsageControl bool
endpoint string
podNamespace string
podName string
dataContainerName string
persistentVolumeClaimKey string
}
type deletedChmodData struct {
directories []string
root string
}
type deletedRemoveData struct {
root string
}
func hasPersistentVolumeClaim(
persistentVolumeClaimName string, pod *corev1.Pod) error {
for _, volume := range pod.Spec.Volumes {
if volume.PersistentVolumeClaim != nil {
claimName := volume.PersistentVolumeClaim.ClaimName
if claimName == persistentVolumeClaimName {
return nil
}
}
}
return fmt.Errorf(
"Not find the persistent volume claim: %v", persistentVolumeClaimName)
}
func findDataCntainer(pod *corev1.Pod, name string) (corev1.Container, bool) {
for _, container := range pod.Spec.Containers {
if container.Name == name {
return container, true
}
}
return corev1.Container{}, false
}
func createPersistentVolumeClaimToIndex(
volumes []corev1.Volume, container corev1.Container) map[string]int {
volumeNameToPvc := map[string]string{}
for _, volume := range volumes {
if volume.PersistentVolumeClaim == nil {
continue
}
volumeNameToPvc[volume.Name] = volume.PersistentVolumeClaim.ClaimName
}
volumeMountToIndex := map[string]int{}
for index, volumeMount := range container.VolumeMounts {
if pvc, ok := volumeNameToPvc[volumeMount.Name]; ok {
volumeMountToIndex[pvc] = index
}
}
return volumeMountToIndex
}
func (dcd *deletedChmodData) chmodWalkFunc(
path string, sourceInfo os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf(
"prevent panic by handling failure accessing a local path %q: %v",
path, err)
}
if sourceInfo.IsDir() {
if dcd.root != path {
dcd.directories = append(dcd.directories, path)
}
return nil
}
err = os.Chmod(path, 0000)
if err != nil {
return fmt.Errorf("[path: %s] %s", path, err.Error())
}
return nil
}
func (drd *deletedRemoveData) removeFilesWalkFunc(
path string, sourceInfo os.FileInfo, err error) error {
if drd.root == path {
if sourceInfo.IsDir() {
return nil
}
}
return os.RemoveAll(path)
}