Skip to content

Commit 4fab620

Browse files
Add code explanation doc for gwctl
1 parent 5c757be commit 4fab620

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

gwctl/CODE_ORGANIZATION.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Code Organization
2+
3+
* **cmd/main.go:**
4+
* Entry point for the application.
5+
* Uses the widely adopted Cobra: [https://github.com/spf13/cobra](https://github.com/spf13/cobra) library to manage subcommands and flags.
6+
7+
* **pkg/cmd/(describe, get):**
8+
* Contain the starting points for individual subcommands.
9+
* Tasks:
10+
* Parse command-line arguments into internal structures.
11+
* Gather necessary data.
12+
* Output the data to the console.
13+
14+
* **Conceptual Separation (Model-View):**
15+
* **pkg/resourcediscovery:** Handles data model definition and construction.
16+
* **pkg/printer:** Manages formatting and printing the data model to the command line (currently the sole view available).
17+
18+
* **pkg/policymanager:**
19+
* Provides logic to work with Policies, Policy CRDs, and facilitates policy merging based on type and hierarchy.
20+
21+
* **pkg/resourcediscovery:**
22+
* **ResourceModel (pkg/resourcediscovery/resourcemodel.go):**
23+
* Employs a graph-like structure.
24+
```
25+
+------------+
26+
| Policy |
27+
+------+-----+
28+
|
29+
|
30+
|
31+
+------------+ +--------------+ TargetRef is Namespace
32+
| Policy | +----> GatewayClass <-------+ |
33+
+----+-------+ | +--------------+ | |
34+
| | | +---------+ |
35+
| | | +-------->|Namespace|<--------+
36+
| Parent GatewayClass | | +---------+
37+
| | | |
38+
| | | |
39+
| | | |
40+
| +-----+-----+ +-----+--+--+
41+
TargetRef is HTTPRoute +-------> Gateway <---+ | Gateway <--+
42+
| | +-----------+ | +-----------+ |
43+
| | | |
44+
| | | |
45+
| Attached to Attached to Attached to
46+
| | | |
47+
| | | |
48+
| +----+------+ +--+--------+ +----+------+
49+
+---------->| HTTPRoute | | HTTPRoute | | HTTPRoute |
50+
+----+------+ +----+------+ +-----+-----+
51+
| | |
52+
| | |
53+
Routes to Routes to Routes to
54+
| | |
55+
| +---------+ | +----v----+
56+
+-------> Backend <-------+ | Backend |
57+
+---------+ +---------+
58+
```
59+
* Nodes: Resources from the Gateway API (Gateways, HTTPRoutes, etc.).
60+
* Edges: Connections between resources (e.g., HTTPRoutes linked to Backends).
61+
* **Discoverer (pkg/resourcediscovery/discoverer.go):**
62+
* Builds the `ResourceModel`.
63+
* **Example (`gwctl describe httproutes -l key1=value1`):**
64+
1. `DiscoverResourcesForHTTPRoute(filter)`:
65+
* Identifies HTTPRoutes with the `key1=value1` label.
66+
* Inserts them as source nodes into the `ResourceModel`.
67+
2. **Graph Traversal:**
68+
* Uses a BFS-like algorithm that begins from the source HTTPRoute nodes.
69+
* Finds associated Gateways, GatewayClasses, Namespaces, Policies, and Backends
70+
71+
* **pkg/printer:**
72+
* Receives the `ResourceModel` as input.
73+
* Extracts relevant information, arranges it into a printable format, and sends it to the command line.
74+
75+
76+
### Additional Notes:
77+
78+
* The code partially adheres to the Model-View pattern, with `resourcediscovery` managing data representation and `printer` controlling output.
79+
* The `Discoverer` constructs the `ResourceModel` graph, performing BFS-like graph traversals to locate connected resources.
80+
* Future improvements could add alternative output views (e.g., Graphviz visualizations, interactive web interfaces).

0 commit comments

Comments
 (0)