From bed7e6be799fade4651781241e3708ac0b66eea7 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 19 Aug 2025 18:52:48 -0400 Subject: [PATCH] etcd: Pass through the namespace This is a bit tricky, and we should nuke and redo some of this API. The sneaky bit has to do with whether we've already added the namespace magic into our etcd client or not. --- etcd/client/simple.go | 14 +++++++++++++- etcd/etcd.go | 2 +- etcd/interfaces/client.go | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/etcd/client/simple.go b/etcd/client/simple.go index 67481c21..eb6da084 100644 --- a/etcd/client/simple.go +++ b/etcd/client/simple.go @@ -109,6 +109,8 @@ func NewClientFromNamespaceStr(client *etcd.Client, ns string) *Simple { method: methodClient, // similar enough to this one to share it! wg: &sync.WaitGroup{}, + namespace: ns, // XXX: should we add this here? + client: client, // store for GetClient() kv: kv, w: w, @@ -146,6 +148,8 @@ func NewClientFromSimple(client interfaces.Client, ns string) *Simple { method: methodNamespace, wg: &sync.WaitGroup{}, + namespace: client.GetNamespace(), // XXX: should we add this here? + client: client.GetClient(), // store for GetClient() kv: kv, w: w, @@ -154,11 +158,13 @@ func NewClientFromSimple(client interfaces.Client, ns string) *Simple { // NewClientFromNamespace builds a new simple client by taking an existing set // of interface API's that we might use. -func NewClientFromNamespace(client *etcd.Client, kv etcd.KV, w etcd.Watcher) *Simple { +func NewClientFromNamespace(client *etcd.Client, kv etcd.KV, w etcd.Watcher, ns string) *Simple { return &Simple{ method: methodNamespace, wg: &sync.WaitGroup{}, + namespace: ns, // in case someone wants to read what our ns is. + client: client, // store for GetClient() kv: kv, w: w, @@ -293,6 +299,12 @@ func (obj *Simple) GetClient() *etcd.Client { return obj.client } +// GetNamespace returns the namespace prefix we use for all keys. This is needed +// whenever we use the client manually. +func (obj *Simple) GetNamespace() string { + return obj.namespace +} + // Set runs a set operation. If you'd like more information about whether a // value changed or not, use Txn instead. func (obj *Simple) Set(ctx context.Context, key, value string, opts ...etcd.OpOption) error { diff --git a/etcd/etcd.go b/etcd/etcd.go index 84e0ac00..ccc43a77 100644 --- a/etcd/etcd.go +++ b/etcd/etcd.go @@ -1464,7 +1464,7 @@ func (obj *EmbdEtcd) MakeClient() (interfaces.Client, error) { func (obj *EmbdEtcd) MakeClientFromNamespace(ns string) (interfaces.Client, error) { kv := namespace.NewKV(obj.etcd.KV, ns) w := namespace.NewWatcher(obj.etcd.Watcher, ns) - c := client.NewClientFromNamespace(obj.etcd, kv, w) + c := client.NewClientFromNamespace(obj.etcd, kv, w, ns) if err := c.Init(); err != nil { return nil, err } diff --git a/etcd/interfaces/client.go b/etcd/interfaces/client.go index 5c0934d1..1fa7f485 100644 --- a/etcd/interfaces/client.go +++ b/etcd/interfaces/client.go @@ -66,6 +66,8 @@ type WatcherInfo struct { // EmbdEtcd.MakeClient and client.Simple implement this. type Client interface { GetClient() *etcd.Client + GetNamespace() string + Set(ctx context.Context, key, value string, opts ...etcd.OpOption) error Get(ctx context.Context, path string, opts ...etcd.OpOption) (map[string]string, error) Del(ctx context.Context, path string, opts ...etcd.OpOption) (int64, error)