Skip to content

Commit dd35c95

Browse files
authored
feat: add discovery k8s dump data interface (#11111)
1 parent 59b50b6 commit dd35c95

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

apisix/discovery/kubernetes/init.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,46 @@ function _M.init_worker()
615615
end
616616
end
617617

618+
619+
function _M.dump_data()
620+
621+
local eps = {}
622+
for _, conf in ipairs(local_conf.discovery.kubernetes) do
623+
624+
local id = conf.id
625+
local endpoint_dict = get_endpoint_dict(id)
626+
local keys, err = endpoint_dict:get_keys()
627+
if err then
628+
error(err)
629+
break
630+
end
631+
632+
if keys then
633+
local k8s = {}
634+
for i = 1, #keys do
635+
636+
local key = keys[i]
637+
--skip key with suffix #version
638+
if key:sub(-#"#version") ~= "#version" then
639+
local value = endpoint_dict:get(key)
640+
641+
core.table.insert(k8s, {
642+
name = key,
643+
value = value
644+
})
645+
end
646+
end
647+
648+
core.table.insert(eps, {
649+
id = conf.id,
650+
endpoints = k8s
651+
})
652+
653+
end
654+
end
655+
656+
return {config = local_conf.discovery.kubernetes, endpoints = eps}
657+
end
658+
659+
618660
return _M

docs/en/latest/discovery/kubernetes.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,56 @@ A: Assume your [_ServiceAccount_](https://kubernetes.io/docs/tasks/configure-pod
351351
```shell
352352
kubectl -n apisix get secret kubernetes-discovery-token-c64cv -o jsonpath={.data.token} | base64 -d
353353
```
354+
355+
## Debugging API
356+
357+
It also offers control api for debugging.
358+
359+
### Memory Dump API
360+
361+
To query/list the nodes discoverd by kubernetes discovery, you can query the /v1/discovery/kubernetes/dump control API endpoint like so:
362+
363+
```shell
364+
GET /v1/discovery/kubernetes/dump
365+
```
366+
367+
Which will yield the following response:
368+
369+
```
370+
{
371+
"endpoints": [
372+
{
373+
"endpoints": [
374+
{
375+
"value": "{\"https\":[{\"host\":\"172.18.164.170\",\"port\":6443,\"weight\":50},{\"host\":\"172.18.164.171\",\"port\":6443,\"weight\":50},{\"host\":\"172.18.164.172\",\"port\":6443,\"weight\":50}]}",
376+
"name": "default/kubernetes"
377+
},
378+
{
379+
"value": "{\"metrics\":[{\"host\":\"172.18.164.170\",\"port\":2379,\"weight\":50},{\"host\":\"172.18.164.171\",\"port\":2379,\"weight\":50},{\"host\":\"172.18.164.172\",\"port\":2379,\"weight\":50}]}",
380+
"name": "kube-system/etcd"
381+
},
382+
{
383+
"value": "{\"http-85\":[{\"host\":\"172.64.89.2\",\"port\":85,\"weight\":50}]}",
384+
"name": "test-ws/testing"
385+
}
386+
],
387+
"id": "first"
388+
}
389+
],
390+
"config": [
391+
{
392+
"default_weight": 50,
393+
"id": "first",
394+
"client": {
395+
"token": "xxx"
396+
},
397+
"service": {
398+
"host": "172.18.164.170",
399+
"port": "6443",
400+
"schema": "https"
401+
},
402+
"shared_size": "1m"
403+
}
404+
]
405+
}
406+
```

docs/zh/latest/discovery/kubernetes.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,55 @@ A: 假定你指定的 [_ServiceAccount_](https://kubernetes.io/docs/tasks/config
349349
```shell
350350
kubectl -n apisix get secret kubernetes-discovery-token-c64cv -o jsonpath={.data.token} | base64 -d
351351
```
352+
353+
## 调试 API
354+
355+
它还提供了用于调试的控制 api。
356+
357+
### 内存 Dump API
358+
359+
```shell
360+
GET /v1/discovery/kubernetes/dump
361+
```
362+
363+
例子
364+
365+
```shell
366+
# curl http://127.0.0.1:9090/v1/discovery/kubernetes/dump | jq
367+
{
368+
"endpoints": [
369+
{
370+
"endpoints": [
371+
{
372+
"value": "{\"https\":[{\"host\":\"172.18.164.170\",\"port\":6443,\"weight\":50},{\"host\":\"172.18.164.171\",\"port\":6443,\"weight\":50},{\"host\":\"172.18.164.172\",\"port\":6443,\"weight\":50}]}",
373+
"name": "default/kubernetes"
374+
},
375+
{
376+
"value": "{\"metrics\":[{\"host\":\"172.18.164.170\",\"port\":2379,\"weight\":50},{\"host\":\"172.18.164.171\",\"port\":2379,\"weight\":50},{\"host\":\"172.18.164.172\",\"port\":2379,\"weight\":50}]}",
377+
"name": "kube-system/etcd"
378+
},
379+
{
380+
"value": "{\"http-85\":[{\"host\":\"172.64.89.2\",\"port\":85,\"weight\":50}]}",
381+
"name": "test-ws/testing"
382+
}
383+
],
384+
"id": "first"
385+
}
386+
],
387+
"config": [
388+
{
389+
"default_weight": 50,
390+
"id": "first",
391+
"client": {
392+
"token": "xxx"
393+
},
394+
"service": {
395+
"host": "172.18.164.170",
396+
"port": "6443",
397+
"schema": "https"
398+
},
399+
"shared_size": "1m"
400+
}
401+
]
402+
}
403+
```

t/kubernetes/discovery/kubernetes3.t

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ _EOC_
187187
core.log.err("operator k8s cluster error: ", err)
188188
return 500
189189
end
190+
191+
ngx.sleep(1)
192+
193+
local k8s = require("apisix.discovery.kubernetes")
194+
local data = k8s.dump_data()
195+
ngx.say(core.json.encode(data,true))
196+
190197
if res.status ~= 200 and res.status ~= 201 and res.status ~= 409 then
191198
return res.status
192199
end
@@ -195,6 +202,29 @@ _EOC_
195202
}
196203
}
197204
205+
location /dump {
206+
content_by_lua_block {
207+
local json_decode = require("toolkit.json").decode
208+
local core = require("apisix.core")
209+
local http = require "resty.http"
210+
local httpc = http.new()
211+
212+
ngx.sleep(1)
213+
214+
local dump_uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/v1/discovery/kubernetes/dump"
215+
local res, err = httpc:request_uri(dump_uri, { method = "GET"})
216+
if err then
217+
ngx.log(ngx.ERR, err)
218+
ngx.status = res.status
219+
return
220+
end
221+
222+
local body = json_decode(res.body)
223+
local endpoints = body.endpoints
224+
ngx.say(core.json.encode(endpoints,true))
225+
}
226+
}
227+
198228
_EOC_
199229

200230
$block->set_value("config", $config);
@@ -332,6 +362,8 @@ POST /operators
332362
]
333363
--- more_headers
334364
Content-type: application/json
365+
--- response_body_like
366+
.*"name":"default/kubernetes".*
335367
336368
337369
@@ -386,3 +418,12 @@ GET /queries
386418
Content-type: application/json
387419
--- response_body eval
388420
qr{ 0 0 2 2 0 0 0 0 2 2 0 0 }
421+
422+
423+
424+
=== TEST 4: test dump
425+
--- yaml_config eval: $::yaml_config
426+
--- request
427+
GET /dump
428+
--- response_body_like
429+
.*"name":"default/kubernetes".*

0 commit comments

Comments
 (0)