-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
}) | ||
} |