2017-03-02 12:26:06 +01:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-03-21 22:04:54 +01:00
|
|
|
"net/url"
|
2017-03-16 15:51:01 +01:00
|
|
|
"strings"
|
2017-03-02 12:26:06 +01:00
|
|
|
|
2017-03-30 10:24:40 +02:00
|
|
|
"encoding/base64"
|
2017-03-21 22:01:18 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-03-02 12:26:06 +01:00
|
|
|
api "k8s.io/kubernetes/pkg/api/v1"
|
|
|
|
)
|
|
|
|
|
2017-03-16 15:51:01 +01:00
|
|
|
func idParts(id string) (string, string) {
|
|
|
|
parts := strings.Split(id, "/")
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildId(meta api.ObjectMeta) string {
|
|
|
|
return meta.Namespace + "/" + meta.Name
|
|
|
|
}
|
|
|
|
|
2017-03-02 12:26:06 +01:00
|
|
|
func expandMetadata(in []interface{}) api.ObjectMeta {
|
|
|
|
meta := api.ObjectMeta{}
|
|
|
|
if len(in) < 1 {
|
|
|
|
return meta
|
|
|
|
}
|
|
|
|
m := in[0].(map[string]interface{})
|
|
|
|
|
|
|
|
meta.Annotations = expandStringMap(m["annotations"].(map[string]interface{}))
|
|
|
|
meta.Labels = expandStringMap(m["labels"].(map[string]interface{}))
|
|
|
|
|
|
|
|
if v, ok := m["generate_name"]; ok {
|
|
|
|
meta.GenerateName = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := m["name"]; ok {
|
|
|
|
meta.Name = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := m["namespace"]; ok {
|
|
|
|
meta.Namespace = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta
|
|
|
|
}
|
|
|
|
|
2017-03-21 22:01:18 +01:00
|
|
|
func patchMetadata(keyPrefix, pathPrefix string, d *schema.ResourceData) PatchOperations {
|
|
|
|
ops := make([]PatchOperation, 0, 0)
|
|
|
|
if d.HasChange(keyPrefix + "annotations") {
|
|
|
|
oldV, newV := d.GetChange(keyPrefix + "annotations")
|
|
|
|
diffOps := diffStringMap(pathPrefix+"annotations", oldV.(map[string]interface{}), newV.(map[string]interface{}))
|
|
|
|
ops = append(ops, diffOps...)
|
|
|
|
}
|
|
|
|
if d.HasChange(keyPrefix + "labels") {
|
|
|
|
oldV, newV := d.GetChange(keyPrefix + "labels")
|
|
|
|
diffOps := diffStringMap(pathPrefix+"labels", oldV.(map[string]interface{}), newV.(map[string]interface{}))
|
|
|
|
ops = append(ops, diffOps...)
|
|
|
|
}
|
|
|
|
return ops
|
|
|
|
}
|
|
|
|
|
2017-03-02 12:26:06 +01:00
|
|
|
func expandStringMap(m map[string]interface{}) map[string]string {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for k, v := range m {
|
|
|
|
result[k] = v.(string)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} {
|
|
|
|
m := make(map[string]interface{})
|
2017-03-21 22:04:54 +01:00
|
|
|
m["annotations"] = filterAnnotations(meta.Annotations)
|
2017-04-02 08:08:16 +02:00
|
|
|
if meta.GenerateName != "" {
|
|
|
|
m["generate_name"] = meta.GenerateName
|
|
|
|
}
|
2017-03-02 12:26:06 +01:00
|
|
|
m["labels"] = meta.Labels
|
|
|
|
m["name"] = meta.Name
|
|
|
|
m["resource_version"] = meta.ResourceVersion
|
|
|
|
m["self_link"] = meta.SelfLink
|
|
|
|
m["uid"] = fmt.Sprintf("%v", meta.UID)
|
|
|
|
m["generation"] = meta.Generation
|
|
|
|
|
|
|
|
if meta.Namespace != "" {
|
|
|
|
m["namespace"] = meta.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
return []map[string]interface{}{m}
|
|
|
|
}
|
2017-03-21 22:04:54 +01:00
|
|
|
|
|
|
|
func filterAnnotations(m map[string]string) map[string]string {
|
|
|
|
for k, _ := range m {
|
|
|
|
if isInternalAnnotationKey(k) {
|
|
|
|
delete(m, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func isInternalAnnotationKey(annotationKey string) bool {
|
|
|
|
u, err := url.Parse("//" + annotationKey)
|
|
|
|
if err == nil && strings.HasSuffix(u.Hostname(), "kubernetes.io") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2017-03-30 10:24:40 +02:00
|
|
|
|
|
|
|
func byteMapToStringMap(m map[string][]byte) map[string]string {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for k, v := range m {
|
|
|
|
result[k] = string(v)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func base64EncodeStringMap(m map[string]interface{}) map[string]interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
for k, v := range m {
|
|
|
|
value := v.(string)
|
|
|
|
result[k] = (base64.StdEncoding.EncodeToString([]byte(value)))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|