Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions config/config-consul.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
# under the License.
#

addr: "127.0.0.1:8500"

addr: "127.0.0.1:9379"

# Which store engine should be used by controller
# options: etcd, zookeeper, raft, consul
Expand All @@ -40,12 +39,11 @@ controller:
failover:
ping_interval_seconds: 3
min_alive_size: 5

# Uncomment this part to save logs to filename instead of stdout
#log:
# level: info
# filename: /data/logs/kvctl.log
# max_backups: 10
# max_age: 7
# max_size: 100
# compress: false
# compress: false
15 changes: 13 additions & 2 deletions store/engine/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (c *Consul) IsReady(ctx context.Context) bool {
}

func (c *Consul) Get(ctx context.Context, key string) ([]byte, error) {
key = sanitizeKey(key)
rsp, _, err := c.client.KV().Get(key, nil)
if err != nil {
return nil, err
Expand All @@ -173,6 +174,7 @@ func (c *Consul) Get(ctx context.Context, key string) ([]byte, error) {
}

func (c *Consul) Exists(ctx context.Context, key string) (bool, error) {
key = sanitizeKey(key)
_, err := c.Get(ctx, key)
if err != nil {
if errors.Is(err, consts.ErrNotFound) {
Expand All @@ -184,6 +186,7 @@ func (c *Consul) Exists(ctx context.Context, key string) (bool, error) {
}

func (c *Consul) Set(ctx context.Context, key string, value []byte) error {
key = sanitizeKey(key)
kvPair := &api.KVPair{
Key: key,
Value: value,
Expand All @@ -193,11 +196,13 @@ func (c *Consul) Set(ctx context.Context, key string, value []byte) error {
}

func (c *Consul) Delete(ctx context.Context, key string) error {
key = sanitizeKey(key)
_, err := c.client.KV().Delete(key, nil)
return err
}

func (c *Consul) List(ctx context.Context, prefix string) ([]engine.Entry, error) {
prefix = sanitizeKey(prefix)
rsp, _, err := c.client.KV().List(prefix, nil)
if err != nil {
return nil, err
Expand All @@ -209,7 +214,7 @@ func (c *Consul) List(ctx context.Context, prefix string) ([]engine.Entry, error
if string(kv.Key) == prefix {
continue
}
key := strings.TrimLeft(string(kv.Key[prefixLen+1]), "/")
key := strings.TrimLeft(string(kv.Key[prefixLen+1:]), "/")
Copy link
Contributor Author

@bseto bseto Mar 19, 2025

Choose a reason for hiding this comment

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

Bug 2 fix. I noticed that this was missing the : which I could find in the etcd and other implementations

if strings.ContainsRune(key, '/') {
continue
}
Expand All @@ -236,7 +241,6 @@ func (c *Consul) electLoop() {
TTL: fmt.Sprintf("%v", sessionTTL),
LockDelay: lockDelay,
}, nil)

if err != nil {
logger.Get().With(
zap.Error(err),
Expand Down Expand Up @@ -312,3 +316,10 @@ func (c *Consul) Close() error {
c.client = nil
return nil
}

func sanitizeKey(key string) string {
if len(key) > 0 && key[0] == '/' {
key = strings.TrimPrefix(key, "/")
}
Comment on lines +321 to +323
Copy link
Contributor Author

Choose a reason for hiding this comment

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

trimming the leading / to avoid the error
https://github.com/hashicorp/consul/blob/main/api/kv.go#L208-L210

return key
}
4 changes: 2 additions & 2 deletions store/engine/consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ func TestBasicOperations(t *testing.T) {
}()

ctx := context.Background()
keys := []string{"a/b/c0", "a/b/c1", "a/b/c2"}
keys := []string{"/a/b/c0", "/a/b/c1", "/a/b/c2"}
Copy link
Contributor Author

@bseto bseto Mar 19, 2025

Choose a reason for hiding this comment

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

the error really happened because the nsPrefix has a leading /, but I think if we just keep the tests consistent with the other engines and it'll be ok?

value := []byte("v")
for _, key := range keys {
require.NoError(t, persist.Set(ctx, key, value))
gotValue, err := persist.Get(ctx, key)
require.NoError(t, err)
require.Equal(t, value, gotValue)
}
entries, err := persist.List(ctx, "a/b")
entries, err := persist.List(ctx, "/a/b")
require.NoError(t, err)
require.Equal(t, len(keys), len(entries))
for _, key := range keys {
Expand Down
Loading