-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
5ca412b
commit 18d4a8d
Showing
8 changed files
with
237 additions
and
108 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/krateoplatformops/unstructured-runtime/pkg/controller/objectref" | ||
"github.com/twmb/murmur3" | ||
) | ||
|
||
type cleanEvent struct { | ||
eventType string | ||
objectRef objectref.ObjectRef | ||
} | ||
|
||
// digestForEvent generates a hash digest for a given event. | ||
// It uses the Murmur3 hashing algorithm to create a 64-bit hash value. | ||
// The event is first cleaned and then converted to a byte slice before hashing. | ||
// The resulting hash is returned as a hexadecimal string. | ||
// | ||
// Parameters: | ||
// - ev: The event for which the digest is to be generated. | ||
// | ||
// Returns: | ||
// - A hexadecimal string representing the hash digest of the event. | ||
func DigestForEvent(ev Event) string { | ||
hasher := murmur3.New64() | ||
|
||
cleanEvent := cleanEvent{ | ||
eventType: string(ev.EventType), | ||
objectRef: ev.ObjectRef, | ||
} | ||
|
||
bin_buf := []byte(fmt.Sprintf("%v", cleanEvent)) | ||
|
||
hasher.Write(bin_buf) | ||
|
||
return strconv.FormatUint(hasher.Sum64(), 16) | ||
} |
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,48 @@ | ||
package event | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/krateoplatformops/unstructured-runtime/pkg/controller/objectref" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDigestForEvent(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
event Event | ||
expected string | ||
}{ | ||
{ | ||
name: "Test event 1", | ||
event: Event{ | ||
EventType: "create", | ||
ObjectRef: objectref.ObjectRef{ | ||
Kind: "Pod", | ||
Namespace: "default", | ||
Name: "mypod", | ||
}, | ||
}, | ||
expected: "2555f9c98d0663c1", // Replace with the actual expected hash | ||
}, | ||
{ | ||
name: "Test event 2", | ||
event: Event{ | ||
EventType: "delete", | ||
ObjectRef: objectref.ObjectRef{ | ||
Kind: "Service", | ||
Namespace: "default", | ||
Name: "myservice", | ||
}, | ||
}, | ||
expected: "c515572a42a933fd", // Replace with the actual expected hash | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := DigestForEvent(tt.event) | ||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
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,20 @@ | ||
package event | ||
|
||
import ( | ||
"github.com/krateoplatformops/unstructured-runtime/pkg/controller/objectref" | ||
) | ||
|
||
type EventType string | ||
|
||
const ( | ||
Observe EventType = "Observe" | ||
Create EventType = "Create" | ||
Update EventType = "Update" | ||
Delete EventType = "Delete" | ||
) | ||
|
||
type Event struct { | ||
Id string | ||
EventType EventType | ||
ObjectRef objectref.ObjectRef | ||
} |
Oops, something went wrong.