-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visualization tool to debug image sources
- Loading branch information
1 parent
025ae72
commit f857e58
Showing
7 changed files
with
496 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
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,138 @@ | ||
package visualize | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/netip" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type EventStore interface { | ||
Record(id string, peer netip.Addr, status int, mirror bool) | ||
FilterById(include []string) EventStore | ||
FilterByDirection(rootIsSource bool) EventStore | ||
Dot() string | ||
} | ||
|
||
// TODO: Include blob or manifest | ||
type edge struct { | ||
node string | ||
id string | ||
status int | ||
rootIsSource bool | ||
} | ||
|
||
var _ EventStore = &MemoryStore{} | ||
|
||
type MemoryStore struct { | ||
mx sync.RWMutex | ||
edges []edge | ||
edgeIndex map[string]int | ||
} | ||
|
||
func NewMemoryStore() *MemoryStore { | ||
return &MemoryStore{ | ||
edges: []edge{}, | ||
edgeIndex: map[string]int{}, | ||
} | ||
} | ||
|
||
func (m *MemoryStore) Record(id string, peer netip.Addr, status int, mirror bool) { | ||
m.mx.Lock() | ||
defer m.mx.Unlock() | ||
|
||
e := edge{ | ||
node: peer.String(), | ||
id: id, | ||
status: status, | ||
rootIsSource: mirror, | ||
} | ||
m.edges = append(m.edges, e) | ||
m.edgeIndex[id] = len(m.edges) - 1 | ||
} | ||
|
||
func (m *MemoryStore) FilterById(include []string) EventStore { | ||
m.mx.RLock() | ||
defer m.mx.RUnlock() | ||
|
||
f := NewMemoryStore() | ||
for _, v := range include { | ||
idx, ok := m.edgeIndex[v] | ||
if !ok { | ||
continue | ||
} | ||
edge := m.edges[idx] | ||
f.edges = append(f.edges, edge) | ||
f.edgeIndex[v] = len(f.edges) - 1 | ||
} | ||
return f | ||
} | ||
|
||
func (m *MemoryStore) FilterByDirection(rootIsSource bool) EventStore { | ||
m.mx.RLock() | ||
defer m.mx.RUnlock() | ||
|
||
f := NewMemoryStore() | ||
for _, edge := range m.edges { | ||
if edge.rootIsSource != rootIsSource { | ||
continue | ||
} | ||
f.edges = append(f.edges, edge) | ||
f.edgeIndex[edge.id] = len(f.edges) - 1 | ||
} | ||
return f | ||
} | ||
|
||
func (m *MemoryStore) Dot() string { | ||
m.mx.RLock() | ||
defer m.mx.RUnlock() | ||
|
||
nodeIndex := map[string]interface{}{} | ||
dotNodes := []string{} | ||
dotEdges := []string{} | ||
for _, edge := range m.edges { | ||
color := "" | ||
switch edge.status { | ||
case 0: | ||
color = "yellow" | ||
case http.StatusOK: | ||
color = "green" | ||
default: | ||
color = "red" | ||
} | ||
|
||
src := "n0" | ||
dest := edge.node | ||
if !edge.rootIsSource { | ||
src = edge.node | ||
dest = "n0" | ||
} | ||
|
||
id := edge.id | ||
if strings.HasPrefix(id, "sha256:") { | ||
id = id[:14] | ||
} | ||
dotEdge := fmt.Sprintf(`%q -> %q[color=%s tooltip=%q]`, src, dest, color, id) | ||
dotEdges = append(dotEdges, dotEdge) | ||
|
||
if _, ok := nodeIndex[edge.node]; ok { | ||
continue | ||
} | ||
nodeIndex[edge.node] = nil | ||
dotNode := fmt.Sprintf(`%q[label="%s"];`, edge.node, edge.node) | ||
dotNodes = append(dotNodes, dotNode) | ||
} | ||
|
||
dot := fmt.Sprintf(`digraph { | ||
layout="circo"; | ||
root="n0"; | ||
n0[label="host"] | ||
%s | ||
%s | ||
}`, strings.Join(dotNodes, "\n\t"), strings.Join(dotEdges, "\n\t")) | ||
return strings.TrimSpace(dot) | ||
} |
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,30 @@ | ||
package visualize | ||
|
||
import ( | ||
"net/http" | ||
"net/netip" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMemoryStore(t *testing.T) { | ||
store := NewMemoryStore() | ||
store.Record("one", netip.MustParseAddr("127.0.0.1"), http.StatusOK, true) | ||
store.Record("two", netip.MustParseAddr("127.0.0.1"), http.StatusOK, true) | ||
store.Record("three", netip.MustParseAddr("0.0.0.0"), http.StatusOK, true) | ||
|
||
dot := store.Dot() | ||
b, err := os.ReadFile("./testdata/all.dot") | ||
require.NoError(t, err) | ||
expected := strings.TrimSpace(string(b)) | ||
require.Equal(t, expected, dot) | ||
|
||
dot = store.Filter([]string{"two", "three"}).Dot() | ||
Check failure on line 25 in pkg/visualize/store_test.go GitHub Actions / unit
|
||
b, err = os.ReadFile("./testdata/filter.dot") | ||
require.NoError(t, err) | ||
expected = strings.TrimSpace(string(b)) | ||
require.Equal(t, expected, dot) | ||
} |
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,13 @@ | ||
digraph { | ||
layout="circo"; | ||
root="n0"; | ||
|
||
n0[label="host"] | ||
|
||
"127.0.0.1"[label="127.0.0.1"]; | ||
"0.0.0.0"[label="0.0.0.0"]; | ||
|
||
"n0" -> "127.0.0.1"[color=green tooltip="one"] | ||
"n0" -> "127.0.0.1"[color=green tooltip="two"] | ||
"n0" -> "0.0.0.0"[color=green tooltip="three"] | ||
} |
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,12 @@ | ||
digraph { | ||
layout="circo"; | ||
root="n0"; | ||
|
||
n0[label="host"] | ||
|
||
"127.0.0.1"[label="127.0.0.1"]; | ||
"0.0.0.0"[label="0.0.0.0"]; | ||
|
||
"n0" -> "127.0.0.1"[color=green tooltip="two"] | ||
"n0" -> "0.0.0.0"[color=green tooltip="three"] | ||
} |
Oops, something went wrong.