Skip to content

Commit

Permalink
extend ExtractMetadataFromJsonBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
kooomix committed Nov 27, 2023
1 parent b2f26df commit 6594dd8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions armometadata/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,28 @@ 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, map[string]string, string, string) {
func ExtractMetadataFromJsonBytes(input []byte) (error, map[string]string, map[string]string, map[string]string, string, string, string, string) {
// output values
annotations := map[string]string{}
labels := map[string]string{}
ownerReferences := map[string]string{}
creationTs := ""
resourceVersion := ""
kind := ""
apiVersion := ""
// ujson parsing
var parent string
err := ujson.Walk(input, func(level int, key, value []byte) bool {
switch level {
case 1:
if bytes.EqualFold(key, []byte(`"kind"`)) {
kind = unquote(value)
}

if bytes.EqualFold(key, []byte(`"apiVersion"`)) {
apiVersion = unquote(value)
}

// skip everything except metadata
if !bytes.EqualFold(key, []byte(`"metadata"`)) {
return false
Expand Down Expand Up @@ -151,7 +161,7 @@ func ExtractMetadataFromJsonBytes(input []byte) (error, map[string]string, map[s
}
return true
})
return err, annotations, labels, ownerReferences, creationTs, resourceVersion
return err, annotations, labels, ownerReferences, creationTs, resourceVersion, kind, apiVersion
}

func unquote(value []byte) string {
Expand Down
14 changes: 12 additions & 2 deletions armometadata/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
ownerReferences map[string]string
creationTs string
resourceVersion string
kind string
apiVersion string
}{
{
name: "applicationactivity",
Expand All @@ -148,6 +150,8 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
ownerReferences: map[string]string{},
creationTs: "2023-11-16T10:15:05Z",
resourceVersion: "1",
kind: "ApplicationActivity",
apiVersion: "spdx.softwarecomposition.kubescape.io/v1beta1",
},
{
name: "pod",
Expand Down Expand Up @@ -176,6 +180,8 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
},
creationTs: "2023-11-16T10:12:35Z",
resourceVersion: "59348379",
kind: "Pod",
apiVersion: "v1",
},
{
name: "sbom",
Expand All @@ -190,19 +196,23 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
ownerReferences: map[string]string{},
creationTs: "2023-11-16T10:13:40Z",
resourceVersion: "1",
kind: "SBOMSPDXv2p3",
apiVersion: "spdx.softwarecomposition.kubescape.io/v1beta1",
},
}
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, ownerReferences, creationTs, resourceVersion := ExtractMetadataFromJsonBytes(input)
got, annotations, labels, ownerReferences, creationTs, resourceVersion, kind, apiVersion := 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)
assert.Equal(t, tt.kind, kind)
assert.Equal(t, tt.apiVersion, apiVersion)
})
}
}
Expand All @@ -211,6 +221,6 @@ func BenchmarkExtractMetadataFromJsonBytes(b *testing.B) {
input, err := os.ReadFile("testdata/applicationactivity.json")
assert.NoError(b, err)
for i := 0; i < b.N; i++ {
_, _, _, _, _, _ = ExtractMetadataFromJsonBytes(input)
_, _, _, _, _, _, _, _ = ExtractMetadataFromJsonBytes(input)
}
}

0 comments on commit 6594dd8

Please sign in to comment.