kubernetes: Ignore internal K8S annotations

This commit is contained in:
Radek Simko 2017-03-21 21:04:54 +00:00
parent 4d2e28aecb
commit ac878657a5
No known key found for this signature in database
GPG Key ID: 6823F3DCCE01BB19
2 changed files with 52 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package kubernetes
import ( import (
"fmt" "fmt"
"net/url"
"strings" "strings"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
@ -65,7 +66,7 @@ func expandStringMap(m map[string]interface{}) map[string]string {
func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} { func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} {
m := make(map[string]interface{}) m := make(map[string]interface{})
m["annotations"] = meta.Annotations m["annotations"] = filterAnnotations(meta.Annotations)
m["generate_name"] = meta.GenerateName m["generate_name"] = meta.GenerateName
m["labels"] = meta.Labels m["labels"] = meta.Labels
m["name"] = meta.Name m["name"] = meta.Name
@ -80,3 +81,21 @@ func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} {
return []map[string]interface{}{m} return []map[string]interface{}{m}
} }
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
}

View File

@ -0,0 +1,32 @@
package kubernetes
import (
"fmt"
"testing"
)
func TestIsInternalAnnotationKey(t *testing.T) {
testCases := []struct {
Key string
Expected bool
}{
{"", false},
{"anyKey", false},
{"any.hostname.io", false},
{"any.hostname.com/with/path", false},
{"any.kubernetes.io", true},
{"kubernetes.io", true},
{"pv.kubernetes.io/any/path", true},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
isInternal := isInternalAnnotationKey(tc.Key)
if tc.Expected && isInternal != tc.Expected {
t.Fatalf("Expected %q to be internal", tc.Key)
}
if !tc.Expected && isInternal != tc.Expected {
t.Fatalf("Expected %q not to be internal", tc.Key)
}
})
}
}