Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add discovery k8s dump data interface #11111

Merged
merged 13 commits into from
Apr 18, 2024
42 changes: 42 additions & 0 deletions apisix/discovery/kubernetes/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,46 @@ function _M.init_worker()
end
end


function _M.dump_data()

local eps = {}
for _, conf in ipairs(local_conf.discovery.kubernetes) do

local id = conf.id
local endpoint_dict = get_endpoint_dict(id)
local keys, err = endpoint_dict:get_keys()
if err then
error(err)
break
end

if keys then
local k8s = {}
for i = 1, #keys do

local key = keys[i]
--skip key with suffix #version
shreemaan-abhishek marked this conversation as resolved.
Show resolved Hide resolved
if key:sub(-#"#version") ~= "#version" then
shreemaan-abhishek marked this conversation as resolved.
Show resolved Hide resolved
local value = endpoint_dict:get(key)

core.table.insert(k8s, {
name = key,
value = value
})
end
end

core.table.insert(eps, {
id = conf.id,
endpoints = k8s
})

end
end

return {config = local_conf.discovery.kubernetes, endpoints = eps}
end


return _M
54 changes: 54 additions & 0 deletions docs/en/latest/discovery/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,57 @@ A: Assume your [_ServiceAccount_](https://kubernetes.io/docs/tasks/configure-pod
```shell
kubectl -n apisix get secret kubernetes-discovery-token-c64cv -o jsonpath={.data.token} | base64 -d
```

## Debugging API

It also offers control api for debugging.

### Memory Dump API

shreemaan-abhishek marked this conversation as resolved.
Show resolved Hide resolved
To query/list the nodes discoverd by kubernetes discovery, you can query the /v1/discovery/kubernetes/dump control API endpoint like so:

```shell
GET /v1/discovery/kubernetes/dump
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
GET /v1/discovery/kubernetes/dump
curl http://127.0.0.1:9090/v1/discovery/kubernetes/dump | jq

```

Which will yield the following response:

```shell
# curl http://127.0.0.1:9090/v1/discovery/kubernetes/dump | jq
hanqingwu marked this conversation as resolved.
Show resolved Hide resolved
{
"endpoints": [
{
"endpoints": [
{
"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}]}",
"name": "default/kubernetes"
},
{
"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}]}",
"name": "kube-system/etcd"
},
{
"value": "{\"http-85\":[{\"host\":\"172.64.89.2\",\"port\":85,\"weight\":50}]}",
"name": "test-ws/testing"
}
],
"id": "first"
}
],
"config": [
{
"default_weight": 50,
"id": "first",
"client": {
"token": "xxx"
},
"service": {
"host": "172.18.164.170",
"port": "6443",
"schema": "https"
},
"shared_size": "1m"
}
]
}
```
52 changes: 52 additions & 0 deletions docs/zh/latest/discovery/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,55 @@ A: 假定你指定的 [_ServiceAccount_](https://kubernetes.io/docs/tasks/config
```shell
kubectl -n apisix get secret kubernetes-discovery-token-c64cv -o jsonpath={.data.token} | base64 -d
```

## 调试 API

它还提供了用于调试的控制 api。

### 内存 Dump API

```shell
GET /v1/discovery/kubernetes/dump
```

例子

```shell
# curl http://127.0.0.1:9090/v1/discovery/kubernetes/dump | jq
{
"endpoints": [
{
"endpoints": [
{
"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}]}",
"name": "default/kubernetes"
},
{
"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}]}",
"name": "kube-system/etcd"
},
{
"value": "{\"http-85\":[{\"host\":\"172.64.89.2\",\"port\":85,\"weight\":50}]}",
"name": "test-ws/testing"
}
],
"id": "first"
}
],
"config": [
{
"default_weight": 50,
"id": "first",
"client": {
"token": "xxx"
},
"service": {
"host": "172.18.164.170",
"port": "6443",
"schema": "https"
},
"shared_size": "1m"
}
]
}
```
41 changes: 41 additions & 0 deletions t/kubernetes/discovery/kubernetes3.t
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ _EOC_
core.log.err("operator k8s cluster error: ", err)
return 500
end

ngx.sleep(1)

local k8s = require("apisix.discovery.kubernetes")
local data = k8s.dump_data()
ngx.say(core.json.encode(data,true))

hanqingwu marked this conversation as resolved.
Show resolved Hide resolved
if res.status ~= 200 and res.status ~= 201 and res.status ~= 409 then
return res.status
end
Expand All @@ -195,6 +202,29 @@ _EOC_
}
}

location /dump {
content_by_lua_block {
local json_decode = require("toolkit.json").decode
local core = require("apisix.core")
local http = require "resty.http"
local httpc = http.new()

ngx.sleep(1)

local dump_uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/v1/discovery/kubernetes/dump"
local res, err = httpc:request_uri(dump_uri, { method = "GET"})
if err then
ngx.log(ngx.ERR, err)
ngx.status = res.status
return
end

local body = json_decode(res.body)
local endpoints = body.endpoints
ngx.say(core.json.encode(endpoints,true))
}
}

_EOC_

$block->set_value("config", $config);
Expand Down Expand Up @@ -332,6 +362,8 @@ POST /operators
]
--- more_headers
Content-type: application/json
--- response_body_like
.*"name":"default/kubernetes".*



Expand Down Expand Up @@ -386,3 +418,12 @@ GET /queries
Content-type: application/json
--- response_body eval
qr{ 0 0 2 2 0 0 0 0 2 2 0 0 }



=== TEST 4: test dump
--- yaml_config eval: $::yaml_config
--- request
GET /dump
--- response_body_like
.*"name":"default/kubernetes".*
Loading