Skip to content

Commit ec65c57

Browse files
LiteSunYour Name
authored andcommitted
feat: add warning for data plane writing to etcd (apache#12241)
1 parent 1557cd2 commit ec65c57

File tree

2 files changed

+1218
-0
lines changed

2 files changed

+1218
-0
lines changed

apisix/core/etcd.lua

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
local require = require
2323
local fetch_local_conf = require("apisix.core.config_local").local_conf
2424
local array_mt = require("apisix.core.json").array_mt
25+
local log = require("apisix.core.log")
26+
local try_read_attr = require("apisix.core.table").try_read_attr
2527
local v3_adapter = require("apisix.admin.v3_adapter")
2628
local etcd = require("resty.etcd")
2729
local clone_tab = require("table.clone")
@@ -37,6 +39,81 @@ local ngx_get_phase = ngx.get_phase
3739
local _M = {}
3840

3941

42+
local NOT_ALLOW_WRITE_ETCD_WARN = 'Data plane role should not write to etcd. ' ..
43+
'This operation will be deprecated in future releases.'
44+
45+
local function is_data_plane()
46+
local local_conf, err = fetch_local_conf()
47+
if not local_conf then
48+
return nil, err
49+
end
50+
51+
local role = try_read_attr(local_conf, "deployment", "role")
52+
if role == "data_plane" then
53+
return true
54+
end
55+
56+
return false
57+
end
58+
59+
60+
61+
local function disable_write_if_data_plane()
62+
local data_plane, err = is_data_plane()
63+
if err then
64+
log.error("failed to check data plane role: ", err)
65+
return true, err
66+
end
67+
68+
if data_plane then
69+
-- current only warn, will be return false in future releases
70+
-- to block etcd write
71+
log.warn(NOT_ALLOW_WRITE_ETCD_WARN)
72+
return false
73+
end
74+
75+
return false, nil
76+
end
77+
78+
79+
local function wrap_etcd_client(etcd_cli)
80+
-- note: methods txn can read and write, don't use txn to write when data plane role
81+
local methods_to_wrap = {
82+
"set",
83+
"setnx",
84+
"setx",
85+
"delete",
86+
"rmdir",
87+
"grant",
88+
"revoke",
89+
"keepalive"
90+
}
91+
92+
local original_methods = {}
93+
for _, method in ipairs(methods_to_wrap) do
94+
if not etcd_cli[method] then
95+
log.error("method ", method, " not found in etcd client")
96+
return nil, "method " .. method .. " not found in etcd client"
97+
end
98+
99+
original_methods[method] = etcd_cli[method]
100+
end
101+
102+
for _, method in ipairs(methods_to_wrap) do
103+
etcd_cli[method] = function(self, ...)
104+
local disable, err = disable_write_if_data_plane()
105+
if disable then
106+
return nil, err
107+
end
108+
109+
return original_methods[method](self, ...)
110+
end
111+
end
112+
113+
return etcd_cli
114+
end
115+
116+
40117
local function _new(etcd_conf)
41118
local prefix = etcd_conf.prefix
42119
etcd_conf.http_host = etcd_conf.host
@@ -67,6 +144,8 @@ local function _new(etcd_conf)
67144
return nil, nil, err
68145
end
69146

147+
etcd_cli = wrap_etcd_client(etcd_cli)
148+
70149
return etcd_cli, prefix
71150
end
72151

@@ -337,6 +416,12 @@ end
337416

338417

339418
local function set(key, value, ttl)
419+
local disable, err = disable_write_if_data_plane()
420+
if disable then
421+
return nil, err
422+
end
423+
424+
340425
local etcd_cli, prefix, err = get_etcd_cli()
341426
if not etcd_cli then
342427
return nil, err
@@ -386,6 +471,11 @@ _M.set = set
386471

387472

388473
function _M.atomic_set(key, value, ttl, mod_revision)
474+
local disable, err = disable_write_if_data_plane()
475+
if disable then
476+
return nil, err
477+
end
478+
389479
local etcd_cli, prefix, err = get_etcd_cli()
390480
if not etcd_cli then
391481
return nil, err
@@ -443,7 +533,13 @@ function _M.atomic_set(key, value, ttl, mod_revision)
443533
end
444534

445535

536+
446537
function _M.push(key, value, ttl)
538+
local disable, err = disable_write_if_data_plane()
539+
if disable then
540+
return nil, err
541+
end
542+
447543
local etcd_cli, _, err = get_etcd_cli()
448544
if not etcd_cli then
449545
return nil, err
@@ -476,6 +572,11 @@ end
476572

477573

478574
function _M.delete(key)
575+
local disable, err = disable_write_if_data_plane()
576+
if disable then
577+
return nil, err
578+
end
579+
479580
local etcd_cli, prefix, err = get_etcd_cli()
480581
if not etcd_cli then
481582
return nil, err
@@ -502,6 +603,11 @@ function _M.delete(key)
502603
end
503604

504605
function _M.rmdir(key, opts)
606+
local disable, err = disable_write_if_data_plane()
607+
if disable then
608+
return nil, err
609+
end
610+
505611
local etcd_cli, prefix, err = get_etcd_cli()
506612
if not etcd_cli then
507613
return nil, err
@@ -548,6 +654,11 @@ end
548654

549655

550656
function _M.keepalive(id)
657+
local disable, err = disable_write_if_data_plane()
658+
if disable then
659+
return nil, err
660+
end
661+
551662
local etcd_cli, _, err = get_etcd_cli()
552663
if not etcd_cli then
553664
return nil, err

0 commit comments

Comments
 (0)