2017-04-12 09:33:26 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-05-26 08:50:26 +02:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-04-12 09:33:26 +02:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flatteners
|
|
|
|
|
2017-05-26 08:50:26 +02:00
|
|
|
func flattenLabelSelector(in *metav1.LabelSelector) []interface{} {
|
2017-04-12 09:33:26 +02:00
|
|
|
att := make(map[string]interface{})
|
|
|
|
if len(in.MatchLabels) > 0 {
|
|
|
|
att["match_labels"] = in.MatchLabels
|
|
|
|
}
|
|
|
|
if len(in.MatchExpressions) > 0 {
|
|
|
|
att["match_expressions"] = flattenLabelSelectorRequirement(in.MatchExpressions)
|
|
|
|
}
|
|
|
|
return []interface{}{att}
|
|
|
|
}
|
|
|
|
|
2017-05-26 08:50:26 +02:00
|
|
|
func flattenLabelSelectorRequirement(in []metav1.LabelSelectorRequirement) []interface{} {
|
2017-04-12 09:33:26 +02:00
|
|
|
att := make([]interface{}, len(in), len(in))
|
|
|
|
for i, n := range in {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["key"] = n.Key
|
|
|
|
m["operator"] = n.Operator
|
|
|
|
m["values"] = newStringSet(schema.HashString, n.Values)
|
|
|
|
att[i] = m
|
|
|
|
}
|
|
|
|
return att
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return []interface{}{att}
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenResourceRequirements(in v1.ResourceRequirements) []interface{} {
|
|
|
|
att := make(map[string]interface{})
|
|
|
|
if len(in.Limits) > 0 {
|
|
|
|
att["limits"] = flattenResourceList(in.Limits)
|
|
|
|
}
|
|
|
|
if len(in.Requests) > 0 {
|
|
|
|
att["requests"] = flattenResourceList(in.Requests)
|
|
|
|
}
|
|
|
|
return []interface{}{att}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expanders
|
|
|
|
|
2017-05-26 08:50:26 +02:00
|
|
|
func expandLabelSelector(l []interface{}) *metav1.LabelSelector {
|
2017-04-12 09:33:26 +02:00
|
|
|
if len(l) == 0 || l[0] == nil {
|
2017-05-26 08:50:26 +02:00
|
|
|
return &metav1.LabelSelector{}
|
2017-04-12 09:33:26 +02:00
|
|
|
}
|
|
|
|
in := l[0].(map[string]interface{})
|
2017-05-26 08:50:26 +02:00
|
|
|
obj := &metav1.LabelSelector{}
|
2017-04-12 09:33:26 +02:00
|
|
|
if v, ok := in["match_labels"].(map[string]interface{}); ok && len(v) > 0 {
|
|
|
|
obj.MatchLabels = expandStringMap(v)
|
|
|
|
}
|
|
|
|
if v, ok := in["match_expressions"].([]interface{}); ok && len(v) > 0 {
|
|
|
|
obj.MatchExpressions = expandLabelSelectorRequirement(v)
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2017-05-26 08:50:26 +02:00
|
|
|
func expandLabelSelectorRequirement(l []interface{}) []metav1.LabelSelectorRequirement {
|
2017-04-12 09:33:26 +02:00
|
|
|
if len(l) == 0 || l[0] == nil {
|
2017-05-26 08:50:26 +02:00
|
|
|
return []metav1.LabelSelectorRequirement{}
|
2017-04-12 09:33:26 +02:00
|
|
|
}
|
2017-05-26 08:50:26 +02:00
|
|
|
obj := make([]metav1.LabelSelectorRequirement, len(l), len(l))
|
2017-04-12 09:33:26 +02:00
|
|
|
for i, n := range l {
|
|
|
|
in := n.(map[string]interface{})
|
2017-05-26 08:50:26 +02:00
|
|
|
obj[i] = metav1.LabelSelectorRequirement{
|
2017-04-12 09:33:26 +02:00
|
|
|
Key: in["key"].(string),
|
2017-05-26 08:50:26 +02:00
|
|
|
Operator: metav1.LabelSelectorOperator(in["operator"].(string)),
|
2017-04-12 09:33:26 +02:00
|
|
|
Values: sliceOfString(in["values"].(*schema.Set).List()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandPersistentVolumeClaimSpec(l []interface{}) (v1.PersistentVolumeClaimSpec, error) {
|
|
|
|
if len(l) == 0 || l[0] == nil {
|
|
|
|
return v1.PersistentVolumeClaimSpec{}, nil
|
|
|
|
}
|
|
|
|
in := l[0].(map[string]interface{})
|
|
|
|
resourceRequirements, err := expandResourceRequirements(in["resources"].([]interface{}))
|
|
|
|
if err != nil {
|
|
|
|
return v1.PersistentVolumeClaimSpec{}, err
|
|
|
|
}
|
|
|
|
obj := v1.PersistentVolumeClaimSpec{
|
|
|
|
AccessModes: expandPersistentVolumeAccessModes(in["access_modes"].(*schema.Set).List()),
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandResourceRequirements(l []interface{}) (v1.ResourceRequirements, error) {
|
|
|
|
if len(l) == 0 || l[0] == nil {
|
|
|
|
return v1.ResourceRequirements{}, nil
|
|
|
|
}
|
|
|
|
in := l[0].(map[string]interface{})
|
|
|
|
obj := v1.ResourceRequirements{}
|
|
|
|
if v, ok := in["limits"].(map[string]interface{}); ok && len(v) > 0 {
|
|
|
|
var err error
|
|
|
|
obj.Limits, err = expandMapToResourceList(v)
|
|
|
|
if err != nil {
|
|
|
|
return obj, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := in["requests"].(map[string]interface{}); ok && len(v) > 0 {
|
|
|
|
var err error
|
|
|
|
obj.Requests, err = expandMapToResourceList(v)
|
|
|
|
if err != nil {
|
|
|
|
return obj, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|