You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/01/27 09:56:43 UTC

[GitHub] [apisix] zhixiongdu027 commented on a change in pull request #4880: feat: add kubernetes discovery module

zhixiongdu027 commented on a change in pull request #4880:
URL: https://github.com/apache/apisix/pull/4880#discussion_r793438419



##########
File path: apisix/kubernetes.lua
##########
@@ -0,0 +1,330 @@
+--
+-- 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 ngx = ngx
+local ipairs = ipairs
+local string = string
+local math = math
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local _constants = {
+    ErrorEvent = "ERROR",
+    AddedEvent = "ADDED",
+    ModifiedEvent = "MODIFIED",
+    DeletedEvent = "DELETED",
+    BookmarkEvent = "BOOKMARK",
+    ListDrive = "list",
+    WatchDrive = "watch",
+    ErrorGone = 410,
+}
+
+local _apiserver = {
+    schema = "",
+    host = "",
+    port = "",
+    token = ""
+}
+
+local empty_table = {}
+
+local function list(httpc, informer)
+    local res, err = httpc:request({
+        path = informer.path,
+        query = informer:list_query(),
+        headers = {
+            ["Host"] = _apiserver.host .. ":" .. _apiserver.port,
+            ["Authorization"] = "Bearer " .. _apiserver.token,
+            ["Accept"] = "application/json",
+            ["Connection"] = "keep-alive"
+        }
+    })
+
+    core.log.info("--raw=" .. informer.path .. "?" .. informer:list_query())
+
+    if not res then
+        return false, "RequestError", err or ""
+    end
+
+    if res.status ~= 200 then
+        return false, res.reason, res:read_body() or ""
+    end
+    local body, err = res:read_body()
+    if err then
+        return false, "ReadBodyError", err
+    end
+
+    local data, _ = core.json.decode(body)
+    if not data or data.kind ~= informer.list_kind then
+        return false, "UnexpectedBody", body
+    end
+
+    informer.version = data.metadata.resourceVersion
+
+    if informer.on_added ~= nil then
+        for _, item in ipairs(data.items or empty_table) do
+            informer:on_added(item, _constants.ListDrive)
+        end
+    end
+
+    informer.continue = data.metadata.continue
+    if informer.continue ~= nil and informer.continue ~= "" then
+        list(httpc, informer)
+    end
+
+    return true, "Success", ""
+end
+
+local function watch(httpc, informer)
+    local max_watch_times = 5
+    for _ = 0, max_watch_times do
+        local watch_seconds = 1800 + math.random(9, 999)
+        informer.overtime = watch_seconds
+        local http_seconds = watch_seconds + 120
+        httpc:set_timeouts(2000, 3000, http_seconds * 1000)
+
+        local res, err = httpc:request({
+            path = informer.path,
+            query = informer:watch_query(),
+            headers = {
+                ["Host"] = _apiserver.host .. ":" .. _apiserver.port,
+                ["Authorization"] = "Bearer " .. _apiserver.token,
+                ["Accept"] = "application/json",
+                ["Connection"] = "keep-alive"
+            }
+        })
+
+        core.log.info("--raw=" .. informer.path .. "?" .. informer:watch_query())
+
+        if err then
+            return false, "RequestError", err
+        end
+
+        if res.status ~= 200 then
+            return false, res.reason, res:read_body() or ""
+        end
+
+        local remainder_body = ""
+        local body
+        local reader = res.body_reader
+        local gmatch_iterator
+        local captures
+        local captured_size = 0
+
+        while true do
+            body, err = reader()
+            if err then
+                return false, "ReadBodyError", err
+            end
+
+            if not body then
+                break
+            end
+
+            if #remainder_body ~= 0 then
+                body = remainder_body .. body
+            end
+
+            gmatch_iterator, err = ngx.re.gmatch(body, "{\"type\":.*}\n", "jao")
+            if not gmatch_iterator then
+                return false, "GmatchError", err
+            end
+
+            captures, err = gmatch_iterator()

Review comment:
       TKS review , But gmatch_iterator() is already in a loop.




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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