-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistentvolume.go
131 lines (114 loc) · 3.82 KB
/
persistentvolume.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
package sources
import (
"regexp"
"github.com/overmindtech/discovery"
"github.com/overmindtech/sdp-go"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
func PersistentVolumeExtractor(resource *v1.PersistentVolume, scope string) ([]*sdp.LinkedItemQuery, error) {
queries := make([]*sdp.LinkedItemQuery, 0)
sd, err := ParseScope(scope, false)
if err != nil {
return nil, err
}
if resource.Spec.PersistentVolumeSource.AWSElasticBlockStore != nil {
// +overmind:link ec2-volume
// Link to EBS volume
queries = append(queries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "ec2-volume",
Method: sdp.QueryMethod_GET,
Query: resource.Spec.PersistentVolumeSource.AWSElasticBlockStore.VolumeID,
Scope: "*",
},
BlastPropagation: &sdp.BlastPropagation{
// Changes to the EBS volume can affect the PV
In: true,
// Changes to the PV might affect the EBS volume
Out: true,
},
})
}
if resource.Spec.CSI != nil {
// Link to an EFS file system access point
efsVolumeHandle := regexp.MustCompile(`fs-[a-f0-9]+::(fsap-[a-f0-9]+)`)
matches := efsVolumeHandle.FindStringSubmatch(resource.Spec.CSI.VolumeHandle)
if matches != nil {
if len(matches) == 2 {
// +overmind:link efs-access-point
queries = append(queries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "efs-access-point",
Method: sdp.QueryMethod_GET,
Query: matches[1],
Scope: "*",
},
BlastPropagation: &sdp.BlastPropagation{
// Changes to the EFS access point can affect the PV
In: true,
// Changes to the PV won't affect the EFS access point
Out: false,
},
})
}
}
}
if resource.Spec.ClaimRef != nil {
queries = append(queries, ObjectReferenceToQuery(resource.Spec.ClaimRef, sd, &sdp.BlastPropagation{
// Changing claim might not affect the PV
In: false,
// Changing the PV will definitely affect the claim
Out: true,
}))
}
if resource.Spec.StorageClassName != "" {
// +overmind:link StorageClass
queries = append(queries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "StorageClass",
Method: sdp.QueryMethod_GET,
Query: resource.Spec.StorageClassName,
Scope: sd.ClusterName,
},
BlastPropagation: &sdp.BlastPropagation{
// Changes to the storage class can affect the PV
In: true,
// Changes to the PV cannot affect the storage class
Out: false,
},
})
}
return queries, nil
}
//go:generate docgen ../docs-data
// +overmind:type PersistentVolume
// +overmind:descriptiveType Persistent Volume
// +overmind:get Get a persistent volume by name
// +overmind:list List all persistent volumes
// +overmind:search Search for a persistent volume using the ListOptions JSON format: https://github.com/overmindtech/k8s-source#search
// +overmind:group Kubernetes
// +overmind:terraform:queryMap kubernetes_persistent_volume.metadata[0].name
// +overmind:terraform:queryMap kubernetes_persistent_volume_v1.metadata[0].name
// +overmind:terraform:scope ${provider_mapping.cluster_name}.${values.metadata[0].namespace}
func newPersistentVolumeSource(cs *kubernetes.Clientset, cluster string, namespaces []string) discovery.Source {
return &KubeTypeSource[*v1.PersistentVolume, *v1.PersistentVolumeList]{
ClusterName: cluster,
Namespaces: namespaces,
TypeName: "PersistentVolume",
ClusterInterfaceBuilder: func() ItemInterface[*v1.PersistentVolume, *v1.PersistentVolumeList] {
return cs.CoreV1().PersistentVolumes()
},
ListExtractor: func(list *v1.PersistentVolumeList) ([]*v1.PersistentVolume, error) {
extracted := make([]*v1.PersistentVolume, len(list.Items))
for i := range list.Items {
extracted[i] = &list.Items[i]
}
return extracted, nil
},
LinkedItemQueryExtractor: PersistentVolumeExtractor,
}
}
func init() {
registerSourceLoader(newPersistentVolumeSource)
}