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.
This commit is contained in:
James Shubin
2025-08-19 18:52:48 -04:00
parent 0031acbcbc
commit bed7e6be79
3 changed files with 16 additions and 2 deletions

View File

@@ -109,6 +109,8 @@ func NewClientFromNamespaceStr(client *etcd.Client, ns string) *Simple {
method: methodClient, // similar enough to this one to share it! method: methodClient, // similar enough to this one to share it!
wg: &sync.WaitGroup{}, wg: &sync.WaitGroup{},
namespace: ns, // XXX: should we add this here?
client: client, // store for GetClient() client: client, // store for GetClient()
kv: kv, kv: kv,
w: w, w: w,
@@ -146,6 +148,8 @@ func NewClientFromSimple(client interfaces.Client, ns string) *Simple {
method: methodNamespace, method: methodNamespace,
wg: &sync.WaitGroup{}, wg: &sync.WaitGroup{},
namespace: client.GetNamespace(), // XXX: should we add this here?
client: client.GetClient(), // store for GetClient() client: client.GetClient(), // store for GetClient()
kv: kv, kv: kv,
w: w, 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 // NewClientFromNamespace builds a new simple client by taking an existing set
// of interface API's that we might use. // 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{ return &Simple{
method: methodNamespace, method: methodNamespace,
wg: &sync.WaitGroup{}, wg: &sync.WaitGroup{},
namespace: ns, // in case someone wants to read what our ns is.
client: client, // store for GetClient() client: client, // store for GetClient()
kv: kv, kv: kv,
w: w, w: w,
@@ -293,6 +299,12 @@ func (obj *Simple) GetClient() *etcd.Client {
return obj.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 // Set runs a set operation. If you'd like more information about whether a
// value changed or not, use Txn instead. // value changed or not, use Txn instead.
func (obj *Simple) Set(ctx context.Context, key, value string, opts ...etcd.OpOption) error { func (obj *Simple) Set(ctx context.Context, key, value string, opts ...etcd.OpOption) error {

View File

@@ -1464,7 +1464,7 @@ func (obj *EmbdEtcd) MakeClient() (interfaces.Client, error) {
func (obj *EmbdEtcd) MakeClientFromNamespace(ns string) (interfaces.Client, error) { func (obj *EmbdEtcd) MakeClientFromNamespace(ns string) (interfaces.Client, error) {
kv := namespace.NewKV(obj.etcd.KV, ns) kv := namespace.NewKV(obj.etcd.KV, ns)
w := namespace.NewWatcher(obj.etcd.Watcher, 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 { if err := c.Init(); err != nil {
return nil, err return nil, err
} }

View File

@@ -66,6 +66,8 @@ type WatcherInfo struct {
// EmbdEtcd.MakeClient and client.Simple implement this. // EmbdEtcd.MakeClient and client.Simple implement this.
type Client interface { type Client interface {
GetClient() *etcd.Client GetClient() *etcd.Client
GetNamespace() string
Set(ctx context.Context, key, value string, opts ...etcd.OpOption) error Set(ctx context.Context, key, value string, opts ...etcd.OpOption) error
Get(ctx context.Context, path string, opts ...etcd.OpOption) (map[string]string, 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) Del(ctx context.Context, path string, opts ...etcd.OpOption) (int64, error)