/
persistent_volume_claim.go
184 lines (171 loc) · 5.59 KB
/
persistent_volume_claim.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
package kubernetes
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/rh01/terraform-provider-kubeflow-training/kubeflowtraining/utils"
api "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)
func persistentVolumeClaimSpecFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"access_modes": {
Type: schema.TypeSet,
Description: "A set of the desired access modes the volume should have. More info: http://kubernetes.io/docs/user-guide/persistent-volumes#access-modes-1",
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"ReadWriteOnce",
"ReadOnlyMany",
"ReadWriteMany",
}, false),
},
Set: schema.HashString,
},
"resources": {
Type: schema.TypeList,
Description: "A list of the minimum resources the volume should have. More info: http://kubernetes.io/docs/user-guide/persistent-volumes#resources",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"limits": {
Type: schema.TypeMap,
Description: "Map describing the maximum amount of compute resources allowed. More info: http://kubernetes.io/docs/user-guide/compute-resources/",
Optional: true,
ForceNew: true,
},
// This is the only field the API will allow modifying in-place, so ForceNew is not used.
"requests": {
Type: schema.TypeMap,
Description: "Map describing the minimum amount of compute resources required. If this is omitted for a container, it defaults to `limits` if that is explicitly specified, otherwise to an implementation-defined value. More info: http://kubernetes.io/docs/user-guide/compute-resources/",
Optional: true,
},
},
},
},
"selector": {
Type: schema.TypeList,
Description: "A label query over volumes to consider for binding.",
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: labelSelectorFields(false),
},
},
"volume_name": {
Type: schema.TypeString,
Description: "The binding reference to the PersistentVolume backing this claim.",
Optional: true,
ForceNew: true,
Computed: true,
},
"storage_class_name": {
Type: schema.TypeString,
Description: "Name of the storage class requested by the claim",
Optional: true,
Computed: true,
ForceNew: true,
},
}
}
func PersistentVolumeClaimSpecSchema() *schema.Schema {
fields := persistentVolumeClaimSpecFields()
return &schema.Schema{
Type: schema.TypeList,
Description: "PVC is a pointer to the PVC Spec we want to use.",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: fields,
},
}
}
// Flatteners
func FlattenPersistentVolumeClaimSpec(in v1.PersistentVolumeClaimSpec) []interface{} {
att := make(map[string]interface{})
att["access_modes"] = flattenPersistentVolumeAccessModes(in.AccessModes)
att["resources"] = flattenResourceRequirements(in.Resources)
if in.Selector != nil {
att["selector"] = flattenLabelSelector(in.Selector)
}
if in.VolumeName != "" {
att["volume_name"] = in.VolumeName
}
if in.StorageClassName != nil {
att["storage_class_name"] = *in.StorageClassName
}
return []interface{}{att}
}
func flattenResourceRequirements(in v1.ResourceRequirements) []interface{} {
att := make(map[string]interface{})
if len(in.Limits) > 0 {
att["limits"] = utils.FlattenStringMap(utils.FlattenResourceList(in.Limits))
}
if len(in.Requests) > 0 {
att["requests"] = utils.FlattenStringMap(utils.FlattenResourceList(in.Requests))
}
return []interface{}{att}
}
// Expanders
func ExpandPersistentVolumeClaimSpec(l []interface{}) (*v1.PersistentVolumeClaimSpec, error) {
obj := &v1.PersistentVolumeClaimSpec{}
if len(l) == 0 || l[0] == nil {
return obj, nil
}
in := l[0].(map[string]interface{})
resourceRequirements, err := expandResourceRequirements(in["resources"].([]interface{}))
if err != nil {
return nil, err
}
obj.AccessModes = expandPersistentVolumeAccessModes(in["access_modes"].(*schema.Set).List())
obj.Resources = *resourceRequirements
if v, ok := in["selector"].([]interface{}); ok && len(v) > 0 {
obj.Selector = expandLabelSelector(v)
}
if v, ok := in["volume_name"].(string); ok {
obj.VolumeName = v
}
if v, ok := in["storage_class_name"].(string); ok && v != "" {
obj.StorageClassName = utils.PtrToString(v)
}
return obj, nil
}
func expandResourceRequirements(l []interface{}) (*v1.ResourceRequirements, error) {
obj := &v1.ResourceRequirements{}
if len(l) == 0 || l[0] == nil {
return obj, nil
}
in := l[0].(map[string]interface{})
if v, ok := in["limits"].(map[string]interface{}); ok && len(v) > 0 {
rl, err := utils.ExpandMapToResourceList(v)
if err != nil {
return obj, err
}
obj.Limits = *rl
}
if v, ok := in["requests"].(map[string]interface{}); ok && len(v) > 0 {
rq, err := utils.ExpandMapToResourceList(v)
if err != nil {
return obj, err
}
obj.Requests = *rq
}
return obj, nil
}
func expandPersistentVolumeAccessModes(s []interface{}) []api.PersistentVolumeAccessMode {
out := make([]api.PersistentVolumeAccessMode, len(s), len(s))
for i, v := range s {
out[i] = api.PersistentVolumeAccessMode(v.(string))
}
return out
}
func flattenPersistentVolumeAccessModes(in []api.PersistentVolumeAccessMode) *schema.Set {
var out = make([]interface{}, len(in), len(in))
for i, v := range in {
out[i] = string(v)
}
return schema.NewSet(schema.HashString, out)
}