-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extract pod selector labels #13
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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") | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at some point we should consider returning a struct to avoid breaking api all the time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see this commit:
d145476
@matthyx