Skip to content

Commit 1be45f1

Browse files
authored
Fix consul engine bug when adding the key with slash prefix (#283)
1 parent 8b0eda8 commit 1be45f1

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

config/config-consul.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
# under the License.
1717
#
1818

19-
addr: "127.0.0.1:8500"
20-
19+
addr: "127.0.0.1:9379"
2120

2221
# Which store engine should be used by controller
2322
# options: etcd, zookeeper, raft, consul
@@ -40,12 +39,11 @@ controller:
4039
failover:
4140
ping_interval_seconds: 3
4241
min_alive_size: 5
43-
4442
# Uncomment this part to save logs to filename instead of stdout
4543
#log:
4644
# level: info
4745
# filename: /data/logs/kvctl.log
4846
# max_backups: 10
4947
# max_age: 7
5048
# max_size: 100
51-
# compress: false
49+
# compress: false

store/engine/consul/consul.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func (c *Consul) IsReady(ctx context.Context) bool {
162162
}
163163

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

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

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

195198
func (c *Consul) Delete(ctx context.Context, key string) error {
199+
key = sanitizeKey(key)
196200
_, err := c.client.KV().Delete(key, nil)
197201
return err
198202
}
199203

200204
func (c *Consul) List(ctx context.Context, prefix string) ([]engine.Entry, error) {
205+
prefix = sanitizeKey(prefix)
201206
rsp, _, err := c.client.KV().List(prefix, nil)
202207
if err != nil {
203208
return nil, err
@@ -209,7 +214,7 @@ func (c *Consul) List(ctx context.Context, prefix string) ([]engine.Entry, error
209214
if string(kv.Key) == prefix {
210215
continue
211216
}
212-
key := strings.TrimLeft(string(kv.Key[prefixLen+1]), "/")
217+
key := strings.TrimLeft(string(kv.Key[prefixLen+1:]), "/")
213218
if strings.ContainsRune(key, '/') {
214219
continue
215220
}
@@ -236,7 +241,6 @@ func (c *Consul) electLoop() {
236241
TTL: fmt.Sprintf("%v", sessionTTL),
237242
LockDelay: lockDelay,
238243
}, nil)
239-
240244
if err != nil {
241245
logger.Get().With(
242246
zap.Error(err),
@@ -312,3 +316,10 @@ func (c *Consul) Close() error {
312316
c.client = nil
313317
return nil
314318
}
319+
320+
func sanitizeKey(key string) string {
321+
if len(key) > 0 && key[0] == '/' {
322+
key = strings.TrimPrefix(key, "/")
323+
}
324+
return key
325+
}

store/engine/consul/consul_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ func TestBasicOperations(t *testing.T) {
4747
}()
4848

4949
ctx := context.Background()
50-
keys := []string{"a/b/c0", "a/b/c1", "a/b/c2"}
50+
keys := []string{"/a/b/c0", "/a/b/c1", "/a/b/c2"}
5151
value := []byte("v")
5252
for _, key := range keys {
5353
require.NoError(t, persist.Set(ctx, key, value))
5454
gotValue, err := persist.Get(ctx, key)
5555
require.NoError(t, err)
5656
require.Equal(t, value, gotValue)
5757
}
58-
entries, err := persist.List(ctx, "a/b")
58+
entries, err := persist.List(ctx, "/a/b")
5959
require.NoError(t, err)
6060
require.Equal(t, len(keys), len(entries))
6161
for _, key := range keys {

0 commit comments

Comments
 (0)