Skip to content

Commit

Permalink
parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kooomix committed Jan 3, 2024
1 parent 3a908c5 commit d145476
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
107 changes: 107 additions & 0 deletions armometadata/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package armometadata

import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
AnnotationKeyStatus = "kubescape.io/status"
AnnotationValueIncomplete = "incomplete"

MetadataKeyResourceVersion = "resourceVersion"
)

type KubernetesObjectParser struct {
resourceVersion string
labels map[string]string
annotations map[string]string
creationStamp time.Time
ownerReferences metav1.OwnerReference
kind string
apiVersion string
podSelectorMatchLabels map[string]string
}

func NewKubernetesResourceParser(input []byte) (*KubernetesObjectParser, error) {
err, annotations, labels, ownerReferences, creationStamp, resourceVersion, kind, apiVersion, podSelectorMatchLabels := ExtractMetadataFromJsonBytes(input)

if err != nil {
return nil, err
}

creationStampTime, err := time.Parse(time.RFC3339, creationStamp)
if err != nil {
return nil, err
}

newOwnerReferences := metav1.OwnerReference{}

if len(ownerReferences) > 0 {
if value, ok := ownerReferences["name"]; ok {
newOwnerReferences.Name = value
}

if value, ok := ownerReferences["kind"]; ok {
newOwnerReferences.Kind = value
}

}

newKubernetesResourceParser := &KubernetesObjectParser{}
newKubernetesResourceParser.resourceVersion = resourceVersion
newKubernetesResourceParser.labels = labels
newKubernetesResourceParser.annotations = annotations
newKubernetesResourceParser.creationStamp = creationStampTime
newKubernetesResourceParser.ownerReferences = newOwnerReferences
newKubernetesResourceParser.kind = kind
newKubernetesResourceParser.apiVersion = apiVersion
newKubernetesResourceParser.podSelectorMatchLabels = podSelectorMatchLabels

return newKubernetesResourceParser, nil
}

func (k *KubernetesObjectParser) GetLabels() map[string]string {
return k.labels
}

func (k *KubernetesObjectParser) GetLabel(label string) string {
return k.labels[label]
}

func (k *KubernetesObjectParser) GetAnnotation(annotation string) string {
return k.annotations[annotation]
}

func (k *KubernetesObjectParser) GetCreationTimestamp() time.Time {
return k.creationStamp
}

func (k *KubernetesObjectParser) GetResourceVersion() string {
return k.resourceVersion
}

func (k *KubernetesObjectParser) GetOwnerReferencesKind() string {
return k.ownerReferences.Kind
}

func (k *KubernetesObjectParser) GetOwnerReferencesName() string {
return k.ownerReferences.Name
}

func (k *KubernetesObjectParser) GetStatus() string {
return k.annotations[AnnotationKeyStatus]
}

func (k *KubernetesObjectParser) GetKind() string {
return k.kind
}

func (k *KubernetesObjectParser) GetApiVersion() string {
return k.apiVersion
}

func (k *KubernetesObjectParser) GetPodSelectorMatchLabels() map[string]string {
return k.podSelectorMatchLabels
}
76 changes: 76 additions & 0 deletions armometadata/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package armometadata

import (
"reflect"
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestNewKubernetesResourceParser(t *testing.T) {
// Test case with valid JSON input
t.Run("valid input", func(t *testing.T) {
validInput := []byte(`{
"metadata": {
"annotations": {"kubescape.io/status": "active"},
"labels": {"kubescape.io/workload-name": "example"},
"ownerReferences": [{"name": "ownerName", "kind": "ownerKind"}],
"creationTimestamp": "2023-03-15T08:00:00Z",
"resourceVersion": "12345"
}
}`)

expectedCreationTimestamp, _ := time.Parse(time.RFC3339, "2023-03-15T08:00:00Z")
expectedParser := &KubernetesObjectParser{
resourceVersion: "12345",
labels: map[string]string{"kubescape.io/workload-name": "example"},
annotations: map[string]string{"kubescape.io/status": "active"},
creationStamp: expectedCreationTimestamp,
ownerReferences: metav1.OwnerReference{Name: "ownerName", Kind: "ownerKind"},
podSelectorMatchLabels: map[string]string{},
}

parser, err := NewKubernetesResourceParser(validInput)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if !reflect.DeepEqual(parser, expectedParser) {
t.Errorf("Expected parser %+v, got %+v", expectedParser, parser)
}
})

// Test case with invalid JSON input
t.Run("invalid json input", func(t *testing.T) {
invalidInput := []byte(`invalid json`)

_, err := NewKubernetesResourceParser(invalidInput)
if err == nil {
t.Errorf("Expected error, got nil")
}
})

// Test case with invalid date format
t.Run("invalid date format", func(t *testing.T) {
invalidDateInput := []byte(`{
"metadata": {
"creationTimestamp": "invalid-date-format"
}
}`)

_, err := NewKubernetesResourceParser(invalidDateInput)
if err == nil {
t.Errorf("Expected error parsing date, got nil")
}
})

// Test case with empty JSON
t.Run("empty json", func(t *testing.T) {
emptyJSON := []byte(`{}`)

_, err := NewKubernetesResourceParser(emptyJSON)
if err == nil {
t.Errorf("Expected error due to missing metadata, got nil")
}
})
}

0 comments on commit d145476

Please sign in to comment.