Skip to content

Commit 97440f3

Browse files
authored
Correct last-applied-configuration annotations key (#205)
This isn't essential since we're now selecting fields but it should still be fixed. Signed-off-by: Charlie Egan <[email protected]>
1 parent 77dc6df commit 97440f3

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

pkg/datagatherer/k8s/fieldfilter.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ var SecretSelectedFields = []string{
2121
"/data/ca.crt",
2222
}
2323

24+
// RedactFields are removed from all objects
25+
var RedactFields = []string{
26+
"metadata.managedFields",
27+
"/metadata.annotations/kubectl.kubernetes.io~1last-applied-configuration",
28+
}
29+
2430
// Select removes all but the supplied fields from the resource
2531
func Select(fields []string, resource *unstructured.Unstructured) error {
2632
// convert the object to JSON for field filtering
@@ -45,8 +51,11 @@ func Select(fields []string, resource *unstructured.Unstructured) error {
4551
// fail to select field if missing, just continue
4652
continue
4753
}
48-
pathComponents := strings.Split(v, "/")
49-
filteredObject.Set(gObject.Data(), pathComponents[1:]...)
54+
pathComponents, err := gabs.JSONPointerToSlice(v)
55+
if err != nil {
56+
return fmt.Errorf("invalid JSONPointer: %s", v)
57+
}
58+
filteredObject.Set(gObject.Data(), pathComponents...)
5059
} else {
5160
if jsonParsed.ExistsP(v) {
5261
filteredObject.SetP(jsonParsed.Path(v).Data(), v)
@@ -81,7 +90,10 @@ func Redact(fields []string, resource *unstructured.Unstructured) error {
8190
for _, v := range fields {
8291
// also support JSONPointers for keys containing '.' chars
8392
if strings.HasPrefix(v, "/") {
84-
pathComponents := strings.Split(v, "/")[1:]
93+
pathComponents, err := gabs.JSONPointerToSlice(v)
94+
if err != nil {
95+
return fmt.Errorf("invalid JSONPointer: %s", v)
96+
}
8597
if jsonParsed.Exists(pathComponents...) {
8698
jsonParsed.Delete(pathComponents...)
8799
}

pkg/datagatherer/k8s/fieldfilter_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ func TestSelect(t *testing.T) {
1313
"apiVersion": "v1",
1414
"kind": "Secret",
1515
"metadata": map[string]interface{}{
16-
"name": "example",
17-
"namespace": "example",
18-
"last-applied-configuration": "secret",
16+
"name": "example",
17+
"namespace": "example",
18+
"annotations": map[string]string{
19+
"kubectl.kubernetes.io/last-applied-configuration": "secret",
20+
},
1921
},
2022
"type": "kubernetes.io/tls",
2123
"data": map[string]interface{}{
@@ -90,9 +92,12 @@ func TestRedact(t *testing.T) {
9092
"apiVersion": "v1",
9193
"kind": "Secret",
9294
"metadata": map[string]interface{}{
93-
"name": "example",
94-
"namespace": "example",
95-
"last-applied-configuration": "secret",
95+
"name": "example",
96+
"namespace": "example",
97+
"annotations": map[string]string{
98+
"kubectl.kubernetes.io/last-applied-configuration": "secret",
99+
},
100+
"managedFields": nil,
96101
},
97102
"type": "kubernetes.io/tls",
98103
"data": map[string]interface{}{
@@ -103,7 +108,8 @@ func TestRedact(t *testing.T) {
103108
}
104109

105110
fieldsToRedact := []string{
106-
"metadata.last-applied-configuration",
111+
"metadata.managedFields",
112+
"/metadata/annotations/kubectl.kubernetes.io~1last-applied-configuration",
107113
"/data/tls.key",
108114
}
109115

@@ -120,6 +126,7 @@ func TestRedact(t *testing.T) {
120126
},
121127
"kind": "Secret",
122128
"metadata": {
129+
"annotations": {},
123130
"name": "example",
124131
"namespace": "example"
125132
},

0 commit comments

Comments
 (0)