Skip to content

Commit

Permalink
kubernetes: Ignore internal K8S annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 27, 2017
1 parent 863182e commit be2d4d1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
21 changes: 20 additions & 1 deletion builtin/providers/kubernetes/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"fmt"
"net/url"
"strings"

"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -65,7 +66,7 @@ func expandStringMap(m map[string]interface{}) map[string]string {

func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} {
m := make(map[string]interface{})
m["annotations"] = meta.Annotations
m["annotations"] = filterAnnotations(meta.Annotations)
m["generate_name"] = meta.GenerateName
m["labels"] = meta.Labels
m["name"] = meta.Name
Expand All @@ -80,3 +81,21 @@ func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} {

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
}
32 changes: 32 additions & 0 deletions builtin/providers/kubernetes/structures_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit be2d4d1

Please sign in to comment.