This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Add DM intents merkle root check via k8s adapter #12
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
06b38a3
init TODO comment
vx416 d35ce84
feature/intents_sync: impl intent merkel tree
yanun0323 1159cdd
test(intents_sync): add merkle sync regression coverage
yanun0323 26182c2
Merge branch 'main' into feature/intents_sync
yanun0323 113b81d
fix: scope intent merkle roots by node and guard concurrent reads
yanun0323 0db927c
chore: generate mock code
yanun0323 c3a6a9f
fix: service lock copy
yanun0323 ea929c6
Merge remote-tracking branch 'origin/main' into feature/intents_sync
ianchen0119 79c95b5
fix: pod-pid mapping not found
ianchen0119 8b3cd2a
feat: show pod-pid mapping based on node name
ianchen0119 5dd5dad
chore: update mock & swag docs
ianchen0119 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/Gthulhu/api/pkg/util" | ||
| ) | ||
|
|
||
| type TraverseIntentMerkleTreeOptions struct { | ||
| RootHash string | ||
| Depth int64 | ||
| } | ||
|
|
||
| type Node struct { | ||
| Hash string | ||
| Left *Node | ||
| Right *Node | ||
| } | ||
|
|
||
| type TraverseIntentMerkleTreeResp struct { | ||
| RootNode *Node | ||
| } | ||
|
|
||
| func (svc *Service) TraverseIntentMerkleTree(ctx context.Context, req *TraverseIntentMerkleTreeOptions) (resp *TraverseIntentMerkleTreeResp, err error) { | ||
| if req == nil { | ||
| return nil, errors.New("nil request") | ||
| } | ||
|
|
||
| svc.refreshIntentMerkleTreeIfNeeded() | ||
|
|
||
| svc.intentCacheMu.RLock() | ||
| root := svc.intentMerkleRoot | ||
| svc.intentCacheMu.RUnlock() | ||
| if req.RootHash != "" && root != nil { | ||
| found := util.FindMerkleNode(root, req.RootHash) | ||
| if found == nil { | ||
| return &TraverseIntentMerkleTreeResp{RootNode: nil}, nil | ||
| } | ||
| root = found | ||
| } | ||
|
|
||
| truncated := util.TruncateMerkleTree(root, req.Depth) | ||
| return &TraverseIntentMerkleTreeResp{RootNode: convertMerkleNode(truncated)}, nil | ||
| } | ||
|
|
||
| func convertMerkleNode(node *util.MerkleNode) *Node { | ||
| if node == nil { | ||
| return nil | ||
| } | ||
| return &Node{ | ||
| Hash: node.Hash, | ||
| Left: convertMerkleNode(node.Left), | ||
| Right: convertMerkleNode(node.Right), | ||
| } | ||
| } |
This file contains hidden or 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,210 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/Gthulhu/api/decisionmaker/domain" | ||
| "github.com/Gthulhu/api/pkg/util" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestTraverseIntentMerkleTreeNilRequest(t *testing.T) { | ||
| svc := &Service{} | ||
|
|
||
| resp, err := svc.TraverseIntentMerkleTree(context.Background(), nil) | ||
| require.Error(t, err) | ||
| assert.Nil(t, resp) | ||
| } | ||
|
|
||
| func TestTraverseIntentMerkleTreeDepthZero(t *testing.T) { | ||
| root := util.BuildMerkleTree([]string{ | ||
| util.HashStringSHA256Hex("leaf-a"), | ||
| util.HashStringSHA256Hex("leaf-b"), | ||
| }) | ||
| svc := &Service{intentMerkleRoot: root} | ||
|
|
||
| resp, err := svc.TraverseIntentMerkleTree(context.Background(), &TraverseIntentMerkleTreeOptions{ | ||
| Depth: 0, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, resp) | ||
| require.NotNil(t, resp.RootNode) | ||
| assert.Equal(t, root.Hash, resp.RootNode.Hash) | ||
| assert.Nil(t, resp.RootNode.Left) | ||
| assert.Nil(t, resp.RootNode.Right) | ||
| } | ||
|
|
||
| func TestTraverseIntentMerkleTreeFindSubTreeByRootHash(t *testing.T) { | ||
| root := util.BuildMerkleTree([]string{ | ||
| util.HashStringSHA256Hex("leaf-a"), | ||
| util.HashStringSHA256Hex("leaf-b"), | ||
| util.HashStringSHA256Hex("leaf-c"), | ||
| util.HashStringSHA256Hex("leaf-d"), | ||
| }) | ||
| require.NotNil(t, root) | ||
| require.NotNil(t, root.Left) | ||
| require.NotNil(t, root.Right) | ||
|
|
||
| svc := &Service{intentMerkleRoot: root} | ||
| resp, err := svc.TraverseIntentMerkleTree(context.Background(), &TraverseIntentMerkleTreeOptions{ | ||
| RootHash: root.Left.Hash, | ||
| Depth: 1, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, resp) | ||
| require.NotNil(t, resp.RootNode) | ||
| assert.Equal(t, root.Left.Hash, resp.RootNode.Hash) | ||
| require.NotNil(t, resp.RootNode.Left) | ||
| require.NotNil(t, resp.RootNode.Right) | ||
| assert.Nil(t, resp.RootNode.Left.Left) | ||
| assert.Nil(t, resp.RootNode.Left.Right) | ||
| assert.Nil(t, resp.RootNode.Right.Left) | ||
| assert.Nil(t, resp.RootNode.Right.Right) | ||
| } | ||
|
|
||
| func TestTraverseIntentMerkleTreeRootHashNotFound(t *testing.T) { | ||
| svc := &Service{ | ||
| intentMerkleRoot: util.BuildMerkleTree([]string{ | ||
| util.HashStringSHA256Hex("leaf-a"), | ||
| util.HashStringSHA256Hex("leaf-b"), | ||
| }), | ||
| } | ||
|
|
||
| resp, err := svc.TraverseIntentMerkleTree(context.Background(), &TraverseIntentMerkleTreeOptions{ | ||
| RootHash: "missing-hash", | ||
| Depth: 0, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, resp) | ||
| assert.Nil(t, resp.RootNode) | ||
| } | ||
|
|
||
| func TestTraverseIntentMerkleTreeRefreshesRootFromIntentCache(t *testing.T) { | ||
| intentA := &domain.Intent{ | ||
| PodName: "pod-a", | ||
| PodID: "pod-id-a", | ||
| NodeID: "node-a", | ||
| K8sNamespace: "default", | ||
| CommandRegex: "nginx", | ||
| Priority: 1, | ||
| ExecutionTime: 100, | ||
| PodLabels: map[string]string{ | ||
| "z": "2", | ||
| "a": "1", | ||
| }, | ||
| } | ||
| intentB := &domain.Intent{ | ||
| PodName: "pod-b", | ||
| PodID: "pod-id-b", | ||
| NodeID: "node-b", | ||
| K8sNamespace: "kube-system", | ||
| CommandRegex: "busybox", | ||
| Priority: 0, | ||
| ExecutionTime: 200, | ||
| PodLabels: map[string]string{ | ||
| "k2": "v2", | ||
| }, | ||
| } | ||
| svc := &Service{ | ||
| intentCache: []*domain.Intent{ | ||
| nil, // ensure nil input is normalized away | ||
| intentB, | ||
| intentA, | ||
| }, | ||
| } | ||
|
|
||
| resp, err := svc.TraverseIntentMerkleTree(context.Background(), &TraverseIntentMerkleTreeOptions{ | ||
| Depth: 0, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, resp) | ||
| require.NotNil(t, resp.RootNode) | ||
| require.NotNil(t, svc.intentMerkleRoot) | ||
| assert.Equal(t, svc.intentMerkleRoot.Hash, resp.RootNode.Hash) | ||
| assert.Equal(t, svc.intentMerkleRoot.Hash, svc.intentMerkleRootHash) | ||
| } | ||
|
|
||
| func TestHashIntentLabelOrderIndependent(t *testing.T) { | ||
| intentA := &domain.Intent{ | ||
| PodName: "pod", | ||
| PodID: "pod-id", | ||
| NodeID: "node-id", | ||
| K8sNamespace: "default", | ||
| CommandRegex: "nginx", | ||
| Priority: 1, | ||
| ExecutionTime: 42, | ||
| PodLabels: map[string]string{ | ||
| "b": "2", | ||
| "a": "1", | ||
| }, | ||
| } | ||
| intentB := &domain.Intent{ | ||
| PodName: "pod", | ||
| PodID: "pod-id", | ||
| NodeID: "node-id", | ||
| K8sNamespace: "default", | ||
| CommandRegex: "nginx", | ||
| Priority: 1, | ||
| ExecutionTime: 42, | ||
| PodLabels: map[string]string{ | ||
| "a": "1", | ||
| "b": "2", | ||
| }, | ||
| } | ||
|
|
||
| hashA := hashIntent(intentA) | ||
| hashB := hashIntent(intentB) | ||
| assert.Equal(t, hashA, hashB) | ||
| assert.Equal(t, util.HashStringSHA256Hex("podName=pod|podID=pod-id|nodeID=node-id|k8sNamespace=default|commandRegex=nginx|priority=1|executionTime=42|podLabels=a=1,b=2"), hashA) | ||
| } | ||
|
|
||
| func TestTraverseIntentMerkleTreeConcurrentReadWrite(t *testing.T) { | ||
| svc := &Service{ | ||
| intentMerkleRoot: util.BuildMerkleTree([]string{util.HashStringSHA256Hex("initial")}), | ||
| } | ||
|
|
||
| const workers = 4 | ||
| const iterations = 200 | ||
|
|
||
| errCh := make(chan error, workers*iterations) | ||
| var wg sync.WaitGroup | ||
|
|
||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for i := 0; i < iterations; i++ { | ||
| root := util.BuildMerkleTree([]string{util.HashStringSHA256Hex(fmt.Sprintf("leaf-%d", i))}) | ||
| svc.intentCacheMu.Lock() | ||
| svc.intentMerkleRoot = root | ||
| svc.intentCacheMu.Unlock() | ||
| } | ||
| }() | ||
|
|
||
| for i := 0; i < workers; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < iterations; j++ { | ||
| resp, err := svc.TraverseIntentMerkleTree(context.Background(), &TraverseIntentMerkleTreeOptions{Depth: 0}) | ||
| if err != nil { | ||
| errCh <- err | ||
| return | ||
| } | ||
| if resp == nil || resp.RootNode == nil || resp.RootNode.Hash == "" { | ||
| errCh <- fmt.Errorf("unexpected nil/empty root node: %+v", resp) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
| close(errCh) | ||
| for err := range errCh { | ||
| require.NoError(t, err) | ||
| } | ||
| } |
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.
New field
IntentIDadded but never used. This field is added to the Intent struct but is not referenced anywhere in the codebase (neither in hashing logic, serialization, nor any other operations). If this field is intended for future use, consider adding a comment to clarify its purpose. If it should be included in the hash computation for intent comparison, it must be added to thehashIntentfunction.