MCPcopy Index your code
hub / github.com/api7/etcd-adapter

github.com/api7/etcd-adapter @v0.2.5

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2.5 ↗ · + Follow
160 symbols 526 edges 27 files 40 documented · 25%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

ETCD Adapter

ETCD Adapter mimics the ETCD V3 APIs best effort. It incorporates the kine as the Server side implementation, and it develops a totally in-memory watchable backend.

Not all features in ETCD V3 APIs supported, this is designed for Apache APISIX, so it's inherently not a generic solution.

How to use it

It's easy to create an etcd adapter instance. The use of this instance contains two parts, the first thing is you should prepare a network listener and let it run. ETCD adapter launches a gRPC service (with a built-in gRPC Gateway, so that HTTP Restful requests can also be handled), you can try to access this service with the etcdctl tool.

The another thing is you should feed some events to this instance, so that data changes can be applied to the instance and ultimately it affects the end-user of this gRPC service.

package main

import (
        "context"
        "fmt"
        "math/rand"
        "net"
        "time"

        adapter "github.com/api7/etcd-adapter"
)

func init() {
        rand.Seed(time.Now().UnixNano())
}

func main() {
        a := adapter.NewEtcdAdapter(nil)
        ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Hour)
        defer cancel()
        go produceEvents(ctx, a)

        ln, err := net.Listen("tcp", "127.0.0.1:12379")
        if err != nil {
                panic(err)
        }
        go func() {
                if err := a.Serve(context.Background(), ln); err != nil {
                        panic(err)
                }
        }()
        <-ctx.Done()
        if err := a.Shutdown(context.TODO()); err != nil {
                panic(err)
        }
}

func produceEvents(ctx context.Context, a adapter.Adapter) {
        ticker := time.NewTicker(time.Second)
        for {
                select {
                case <-ctx.Done():
                        return
                case <-ticker.C:
                        break
                }
                event := &adapter.Event{
                        Type: adapter.EventType(rand.Intn(3) + 1),
                        Key:  fmt.Sprintf("/key/part/%d", rand.Int()),
                }
                if event.Type != adapter.EventDelete {
                        event.Value = []byte(fmt.Sprintf("value-%d", rand.Int()))
                }
                a.EventCh() <- []*adapter.Event{event}
        }
}

The above example shows a simple usage about the etcd adapter.

Note, get keys by prefix constrained strictly as the key format has to be path-like, for instance, keys can be /apisix/routes/1, apisix/upstreams/2, and you can get them with the prefix /apisix, or /apisix/routes, /apisix/upstreams perspective.

Extension points exported contracts — how you extend this code

Item (Interface)
Item will be used as the key and value type of the backends.
pkg/backends/cache.go
Adapter (Interface)
(no doc) [1 implementers]
pkg/adapter/etcd.go
Revisioner (Interface)
Revisioner is the revision manager, revision is a int64 typed integer.
pkg/backends/cache.go
EtcdServerRegister (Interface)
(no doc) [1 implementers]
pkg/adapter/etcd.go

Core symbols most depended-on inside this repo

Equal
called by 99
pkg/backends/btree/tree_index.go
String
called by 32
pkg/backends/btree/key_index.go
Get
called by 31
pkg/backends/btree/tree_index.go
Create
called by 20
pkg/backends/btree/btree.go
Len
called by 14
pkg/backends/btree/revision.go
NewBTreeCache
called by 12
pkg/backends/btree/btree.go
isEmpty
called by 11
pkg/backends/btree/key_index.go
Delete
called by 11
pkg/backends/btree/btree.go

Shape

Method 93
Function 36
Struct 21
Interface 5
TypeAlias 5

Languages

Go100%

Modules by API surface

pkg/backends/btree/tree_index.go36 symbols
pkg/backends/btree/btree.go20 symbols
pkg/backends/btree/key_index.go18 symbols
pkg/adapter/etcd.go18 symbols
pkg/backends/btree/btree_test.go12 symbols
pkg/etcdserver/watch.go11 symbols
pkg/config/struct.go7 symbols
pkg/backends/cache.go6 symbols
pkg/backends/btree/revision.go6 symbols
pkg/backends/mysql/mysql.go4 symbols
pkg/adapter/server.go4 symbols
pkg/etcdserver/server.go3 symbols

For agents

$ claude mcp add etcd-adapter \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact