Skip to content

Commit

Permalink
add more fields to ExtractMetadataFromJsonBytes
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Nov 16, 2023
1 parent 1f3a3e1 commit ea914f8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
21 changes: 18 additions & 3 deletions armometadata/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ func LoadConfig(configPath string) (*ClusterConfig, error) {
}

// ExtractMetadataFromBytes extracts metadata from the JSON bytes of a Kubernetes object
func ExtractMetadataFromJsonBytes(input []byte) (error, map[string]string, map[string]string, string) {
func ExtractMetadataFromJsonBytes(input []byte) (error, map[string]string, map[string]string, map[string]string, string, string) {
// output values
annotations := map[string]string{}
labels := map[string]string{}
ownerReferences := map[string]string{}
creationTs := ""
resourceVersion := ""
// ujson parsing
var parent string
err := ujson.Walk(input, func(level int, key, value []byte) bool {
Expand All @@ -125,6 +127,10 @@ func ExtractMetadataFromJsonBytes(input []byte) (error, map[string]string, map[s
if bytes.EqualFold(key, []byte(`"creationTimestamp"`)) {
creationTs = unquote(value)
}
// read resourceVersion
if bytes.EqualFold(key, []byte(`"resourceVersion"`)) {
resourceVersion = unquote(value)
}
// record parent for level 3
parent = unquote(key)
case 3:
Expand All @@ -136,13 +142,22 @@ func ExtractMetadataFromJsonBytes(input []byte) (error, map[string]string, map[s
if parent == "labels" {
labels[unquote(key)] = unquote(value)
}
case 4:
// read ownerReferences
if parent == "ownerReferences" {
ownerReferences[unquote(key)] = unquote(value)
}

}
return true
})
return err, annotations, labels, creationTs
return err, annotations, labels, ownerReferences, creationTs, resourceVersion
}

func unquote(value []byte) string {
buf, _ := ujson.Unquote(value)
buf, err := ujson.Unquote(value)
if err != nil {
return string(value)
}
return string(buf)
}
35 changes: 26 additions & 9 deletions armometadata/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ func BoolPtr(b bool) *bool {

func TestExtractMetadataFromJsonBytes(t *testing.T) {
tests := []struct {
name string
want error
annotations map[string]string
labels map[string]string
creationTs string
name string
want error
annotations map[string]string
labels map[string]string
ownerReferences map[string]string
creationTs string
resourceVersion string
}{
{
name: "applicationactivity",
Expand All @@ -143,7 +145,9 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
"kubescape.io/workload-name": "storage",
"kubescape.io/workload-namespace": "kubescape",
},
creationTs: "2023-11-16T10:15:05Z",
ownerReferences: map[string]string{},
creationTs: "2023-11-16T10:15:05Z",
resourceVersion: "1",
},
{
name: "pod",
Expand All @@ -162,7 +166,16 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
"pod-template-hash": "549f95c69",
"tier": "ks-control-plane",
},
creationTs: "2023-11-16T10:12:35Z",
ownerReferences: map[string]string{
"apiVersion": "apps/v1",
"blockOwnerDeletion": "true",
"controller": "true",
"kind": "ReplicaSet",
"name": "kubescape-549f95c69",
"uid": "c0ff7d3b-4183-482c-81c5-998faf0b6150",
},
creationTs: "2023-11-16T10:12:35Z",
resourceVersion: "59348379",
},
{
name: "sbom",
Expand All @@ -174,18 +187,22 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
"kubescape.io/image-id": "quay-io-kubescape-kubescape-sha256-608b85d3de51caad84a2bfe089ec",
"kubescape.io/image-name": "quay-io-kubescape-kubescape",
},
creationTs: "2023-11-16T10:13:40Z",
ownerReferences: map[string]string{},
creationTs: "2023-11-16T10:13:40Z",
resourceVersion: "1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input, err := os.ReadFile(fmt.Sprintf("testdata/%s.json", tt.name))
assert.NoError(t, err)
got, annotations, labels, creationTs := ExtractMetadataFromJsonBytes(input)
got, annotations, labels, ownerReferences, creationTs, resourceVersion := ExtractMetadataFromJsonBytes(input)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.annotations, annotations)
assert.Equal(t, tt.labels, labels)
assert.Equal(t, tt.ownerReferences, ownerReferences)
assert.Equal(t, tt.creationTs, creationTs)
assert.Equal(t, tt.resourceVersion, resourceVersion)
})
}
}

0 comments on commit ea914f8

Please sign in to comment.