spacewander commented on a change in pull request #2884:
URL: https://github.com/apache/apisix/pull/2884#discussion_r533111811



##########
File path: apisix/server_info.lua
##########
@@ -0,0 +1,168 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require = require
+local core = require("apisix.core")
+
+local type = type
+local ngx_time = ngx.time
+
+local boot_time = os.time()
+local internal_status = ngx.shared.internal_status
+
+local _M = {}
+
+if not internal_status then
+    error("lua_shared_dict \"internal_status\" not configured")
+end
+
+
+local function is_privileged()
+    local process_type = require("ngx.process").type()
+    return process_type == "privileged agent" or process_type == "single"
+end
+
+-- server information will be saved into shared memory only if the key
+-- "server_info" not exist if excl is true.
+local function save(data, excl)
+    local handler = excl and internal_status.add or internal_status.set
+
+    local ok, err = handler(internal_status, "server_info", data)
+    if not ok then
+        if excl and err == "exists" then
+            return true
+        end
+
+        return nil, err
+    end
+
+    return true
+end
+
+
+local function encode_and_save(server_info, excl)
+    local data, err = core.json.encode(server_info)
+    if not data then
+        return nil, err
+    end
+
+    return save(data, excl)
+end
+
+
+local function report()
+    local server_info, err = _M.get()
+    if not server_info then
+        core.log.error("failed to get server_info: ", err)
+        return nil, err
+    end
+
+    if server_info.etcd_version == "unknown" then
+        local res, err = core.etcd.server_version()
+        if not res then
+            core.log.error("failed to fetch etcd version: ", err)
+            return nil, err
+
+        elseif type(res.body) ~= "table" then
+            core.log.error("failed to fetch etcd version: bad version info")
+            return nil, "bad etcd version info"
+        else
+            server_info.etcd_version = res.body.etcdcluster
+        end
+    end
+
+    server_info.last_report_time = ngx_time()
+
+    local data, err = core.json.encode(server_info)
+    if not data then
+        core.log.error("failed to encode server_info: ", err)
+        return nil, err
+    end
+
+    local key = "/data_plane/server_info/" .. server_info.id

Review comment:
       @membphis @tokers 
   I think `/data_plane/` is better.
   It would be better to limit the write of DP to certain prefix, so that we 
can grant the privilege strictly.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to