Skip to content

Conversation

joamaki
Copy link
Contributor

@joamaki joamaki commented Oct 30, 2023

This implements support for remotely querying the contents of StateDB tables over the REST API. It is implemented in terms of raw queries, e.g. the table, index names and the key []byte is transferred to the server which then performs the query against the given table and index. This allows reuse of the Index[Obj, Key] types and thus provides the same experience as server side:

    table := statedb.NewRemoteTable[*tables.Device](client, "devices")

    // Query devices by revision, least recently changed objects first.
    iter, err := table.LowerBound(ctx, statedb.ByRevision[*tables.Device](0))
    for device, revision, ok := iter.Next(); ok; device, revision, ok = iter.Next { ... }

    // Find devices by name: all devices that start with "e" (or higher):
    iter, err = table.LowerBound(ctx, tables.DeviceNameIndex("e"))

    // Exact matches:
    iter, err = table.Get(ctx, tables.DeviceNameIndex("eth0"))
    if device, revision, ok := iter.Next(); ok { ... }

On top of this API a generic utility is implemented in cilium-dbg/cmd/statedb.go to allow adding the ability to pretty-print the contents of any table whose object implements 'statedb.TabWritable':

	// "cilium-dbg statedb devices"
	statedbTableCommand[*tables.Device]("devices")

Example output:

  root@kind-worker:/home/cilium# cilium statedb devices
  Name              Index   Selected   Type     MTU     HWAddr              Flags                    Addresses
  lxc4446c55b3291   8       false      veth     1500    46:b9:9b:85:c2:e2   up|broadcast|multicast   fe80::44b9:9bff:fe85:c2e2
  lo                1       false      device   65536                       up|loopback              127.0.0.1, ::1
  lxc_health        71      false      veth     1500    06:d8:63:ff:20:3b   up|broadcast|multicast   fe80::4d8:63ff:feff:203b
  cilium_host       3       false      veth     1500    66:e0:0e:19:a6:9f   up|broadcast|multicast   10.244.1.186

Since go-swagger does not provide an easy way to implement proper streaming APIs this does not add support for delete events (DeleteTracker). Implementing this is more involved and would require registering a DeleteTracker and keeping it alive with some sort of keepalive mechanism.

cilium-dbg: Add statedb query support and commands to inspect statedb tables devices, routes and l2-announce. 

@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Oct 30, 2023
@joamaki joamaki added release-note/minor This PR changes functionality that users may find relevant to operating Cilium. release-note/misc This PR makes changes that have no direct user impact. labels Oct 30, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot removed dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. labels Oct 30, 2023
@joamaki joamaki added dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. and removed release-note/misc This PR makes changes that have no direct user impact. labels Oct 30, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot removed dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. labels Oct 30, 2023
@joamaki joamaki force-pushed the pr/joamaki/statedb-restapi-queries branch 3 times, most recently from 72d86e4 to 3ad7e57 Compare October 30, 2023 16:30
@joamaki
Copy link
Contributor Author

joamaki commented Oct 30, 2023

/test

@joamaki joamaki marked this pull request as ready for review October 30, 2023 16:33
@joamaki joamaki requested review from a team as code owners October 30, 2023 16:33
@joamaki
Copy link
Contributor Author

joamaki commented Oct 30, 2023

I was planning to originally build this on top of #28729 with gRPC so there'd be proper streaming RPC goodness. I decided in the end to split this off from that since we need to have proper discussions on if/how/when to introduce competition to go-swagger/openapi and how we'd migrate.

@joamaki joamaki force-pushed the pr/joamaki/statedb-restapi-queries branch from 3ad7e57 to f63258f Compare October 31, 2023 07:34
@joamaki joamaki requested a review from tommyp1ckles October 31, 2023 07:35
Copy link
Member

@qmonnet qmonnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc changes look good. I took a brief look at the rest, and it didn't raise any red flag to me. Nice addition, thanks!

@joamaki
Copy link
Contributor Author

joamaki commented Oct 31, 2023

/test

@joamaki joamaki force-pushed the pr/joamaki/statedb-restapi-queries branch from f63258f to 133f769 Compare October 31, 2023 13:54
Copy link
Contributor

@derailed derailed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joamaki Very cool. Nice work!

@joamaki joamaki requested a review from derailed November 2, 2023 09:35
@joamaki joamaki force-pushed the pr/joamaki/statedb-restapi-queries branch 4 times, most recently from 65e8844 to dc13ca1 Compare November 2, 2023 14:21
Copy link
Contributor

@tommyp1ckles tommyp1ckles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, looks good on my end 👍

@joamaki
Copy link
Contributor Author

joamaki commented Nov 3, 2023

/test

Copy link
Member

@pippolo84 pippolo84 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Copy link
Contributor

@derailed derailed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joamaki Thank you for the updates! This is a great addition!
I think the TabWriter may need some TLC ie what if the output warrants custom cols or different columns ordering, etc...
But we can cross that bridge if we ever get there...

@joamaki joamaki force-pushed the pr/joamaki/statedb-restapi-queries branch from dc13ca1 to 6bd1955 Compare November 7, 2023 07:16
@joamaki
Copy link
Contributor Author

joamaki commented Nov 7, 2023

/test

This implements support for remotely querying the contents of StateDB tables
over the REST API. It is implemented in terms of raw queries, e.g. the table,
index names and the key []byte is transferred to the server which then performs
the query against the given table and index. This allows reuse of the Index[Obj, Key]
types and thus provides the same experience as server side:

    table := statedb.NewRemoteTable[*tables.Device](client, "devices")

    // Query devices by revision, least recently changed objects first.
    iter, err := table.LowerBound(ctx, statedb.ByRevision[*tables.Device](0))
    for device, revision, ok := iter.Next(); ok; device, revision, ok = iter.Next { ... }

    // Find devices by name: all devices that start with "e":
    iter, err = table.LowerBound(ctx, tables.DeviceNameIndex("e"))

    // Exact matches:
    iter, err = table.Get(ctx, tables.DeviceNameIndex("eth0"))
    if device, revision, ok := iter.Next(); ok { ... }

On top of this API a generic utility is implemented in cilium-dbg/cmd/statedb.go
to allow adding the ability to pretty-print the contents of any table whose object
implements 'statedb.TableWritable':

	// "cilium-dbg statedb devices"
	statedbTableCommand[*tables.Device]("devices")

Example output:

  root@kind-worker:/home/cilium# cilium statedb devices
  Name              Index   Selected   Type     MTU     HWAddr              Flags                    Addresses
  lxc4446c55b3291   8       false      veth     1500    46:b9:9b:85:c2:e2   up|broadcast|multicast   fe80::44b9:9bff:fe85:c2e2
  lo                1       false      device   65536                       up|loopback              127.0.0.1, ::1
  lxc_health        71      false      veth     1500    06:d8:63:ff:20:3b   up|broadcast|multicast   fe80::4d8:63ff:feff:203b
  cilium_host       3       false      veth     1500    66:e0:0e:19:a6:9f   up|broadcast|multicast   10.244.1.186

Since go-swagger does not provide an easy way to implement proper streaming APIs
this does not add support for delete events (DeleteTracker). Implementing this
is more involved and would require registering a DeleteTracker and keeping it alive
with some sort of keepalive mechanism.

Signed-off-by: Jussi Maki <jussi@isovalent.com>
@joamaki joamaki force-pushed the pr/joamaki/statedb-restapi-queries branch from 6bd1955 to 4e81177 Compare November 7, 2023 11:40
@joamaki
Copy link
Contributor Author

joamaki commented Nov 7, 2023

/test

@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Nov 8, 2023
@nathanjsweet nathanjsweet merged commit 963b8e6 into cilium:main Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/minor This PR changes functionality that users may find relevant to operating Cilium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants