Skip to content

Commit 02338a2

Browse files
authored
Remove last-applied... annotation from metadata (#201)
I appear to have made an error in #152. It is looking for field annotations on objects which is never set, annotation are nested under metadata. Signed-off-by: Charlie Egan <[email protected]>
1 parent 12636ec commit 02338a2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

pkg/datagatherer/k8s/dynamic.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,21 @@ func redactList(list *unstructured.UnstructuredList) error {
200200
secret.Object["data"] = map[string]interface{}{}
201201
}
202202

203-
// Redact last-applied-configuration annotation if set
204-
annotations, present := secret.Object["annotations"].(map[string]interface{})
205-
if present {
206-
_, annotationPresent := annotations["kubectl.kubernetes.io/last-applied-configuration"]
207-
if annotationPresent {
208-
annotations["kubectl.kubernetes.io/last-applied-configuration"] = "redacted"
203+
metadata, metadataPresent := secret.Object["metadata"].(map[string]interface{})
204+
if metadataPresent {
205+
// Redact last-applied-configuration annotation if set
206+
annotations, present := metadata["annotations"].(map[string]interface{})
207+
if present {
208+
_, annotationPresent := annotations["kubectl.kubernetes.io/last-applied-configuration"]
209+
if annotationPresent {
210+
annotations["kubectl.kubernetes.io/last-applied-configuration"] = "redacted"
211+
}
212+
metadata["annotations"] = annotations
209213
}
210-
secret.Object["annotations"] = annotations
214+
secret.Object["metadata"] = metadata
211215
}
216+
// break when the object has been processed as a secret, no
217+
// other kinds have redact modifications
212218
break
213219
}
214220
}

pkg/datagatherer/k8s/dynamic_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/d4l3k/messagediff"
1011
"gopkg.in/yaml.v2"
1112
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1213
"k8s.io/apimachinery/pkg/runtime"
@@ -37,14 +38,16 @@ func getSecret(name, namespace string, data map[string]interface{}, isTLS bool,
3738
object.Object["type"] = "kubernetes.io/tls"
3839
}
3940

41+
metadata, _ := object.Object["metadata"].(map[string]interface{})
42+
4043
// if we're creating a 'raw' secret as scraped that was applied by kubectl
4144
if withLastApplied {
4245
jsonData, _ := json.Marshal(data)
43-
object.Object["annotations"] = map[string]interface{}{
46+
metadata["annotations"] = map[string]interface{}{
4447
"kubectl.kubernetes.io/last-applied-configuration": string(jsonData),
4548
}
4649
} else { // generate an expected redacted secret
47-
object.Object["annotations"] = map[string]interface{}{
50+
metadata["annotations"] = map[string]interface{}{
4851
"kubectl.kubernetes.io/last-applied-configuration": "redacted",
4952
}
5053
}
@@ -214,8 +217,8 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
214217
if err == nil && test.err {
215218
t.Errorf("expected to get an error but didn't get one")
216219
}
217-
if !reflect.DeepEqual(res, test.expected) {
218-
t.Errorf("unexpected difference: %v", diff.ObjectDiff(res, test.expected))
220+
if diff, equal := messagediff.PrettyDiff(res, test.expected); !equal {
221+
t.Errorf("\n%s", diff)
219222
}
220223
})
221224
}

0 commit comments

Comments
 (0)