nic-6443 commented on code in PR #13201:
URL: https://github.com/apache/apisix/pull/13201#discussion_r3067583441


##########
apisix/discovery/kubernetes/init.lua:
##########
@@ -563,71 +74,26 @@ local function single_mode_init(conf)
         return
     end
 
-    local apiserver, err = get_apiserver(conf)
-    if err then
-        error(err)
-        return
-    end
-
-    local default_weight = conf.default_weight
-    local endpoints_informer, err
-    if conf.watch_endpoint_slices then
-        endpoints_informer, err = informer_factory.new("discovery.k8s.io", 
"v1",
-                                                       "EndpointSlice", 
"endpointslices", "")
-    else
-        endpoints_informer, err = informer_factory.new("", "v1", "Endpoints", 
"endpoints", "")
-    end
+    local handle, err = k8s_core.create_handle(conf, {
+        endpoint_dict = endpoint_dict,
+    })
     if err then
         error(err)
         return
     end
 
-    setup_namespace_selector(conf, endpoints_informer)
-    setup_label_selector(conf, endpoints_informer)
-
-    if conf.watch_endpoint_slices then
-        endpoints_informer.on_added = on_endpoint_slices_modified
-        endpoints_informer.on_modified = on_endpoint_slices_modified
-        endpoints_informer.on_deleted = on_endpoint_slices_deleted
-        endpoints_informer.endpoint_slices_cache = {}
-    else
-        endpoints_informer.on_added = on_endpoint_modified
-        endpoints_informer.on_modified = on_endpoint_modified
-        endpoints_informer.on_deleted = on_endpoint_deleted
-    end
-
-    endpoints_informer.pre_list = pre_list
-    endpoints_informer.post_list = post_list
-
-    ctx = setmetatable({
-        endpoint_dict = endpoint_dict,
-        apiserver = apiserver,
-        default_weight = default_weight
-    }, { __index = endpoints_informer })
-
-    start_fetch(ctx)
+    ctx = handle
+    k8s_core.start_fetch(ctx)
 end
 
 
 local function single_mode_nodes(service_name)
-    local pattern = "^(.*):(.*)$" -- namespace/name:port_name
-    local match = ngx.re.match(service_name, pattern, "jo")
-    if not match then
-        core.log.error("get unexpected upstream service_name: ", service_name)
-        return nil
-    end
-
-    local endpoint_dict = ctx
-    local endpoint_key = match[1]
-    local endpoint_port = match[2]
-    local endpoint_version = endpoint_dict:get(endpoint_key .. "#version")
-    if not endpoint_version then
-        core.log.info("get empty endpoint version from discovery DICT ", 
endpoint_key)
-        return nil
-    end
-
-    return endpoint_lrucache(service_name, endpoint_version,
-            create_endpoint_lrucache, endpoint_dict, endpoint_key, 
endpoint_port)
+    return k8s_core.resolve_nodes(
+        endpoint_lrucache, service_name,
+        "^(.*):(.*)$",   -- namespace/name:port_name
+        function(match)
+            return ctx, match[1], match[2]

Review Comment:
   False positive — `nodes()` only runs in worker processes, not the privileged 
agent. See line 72-74: workers set `ctx = endpoint_dict` (the shared dict) and 
return early, never touching the handle. The privileged agent sets `ctx = 
handle` (line 85) but only calls `start_fetch()`, never `nodes()`.



##########
apisix/discovery/kubernetes/init.lua:
##########
@@ -671,81 +137,36 @@ local function multiple_mode_init(confs)
                     "please check your APISIX version")
         end
 
-        local apiserver, err = get_apiserver(conf)
-        if err then
-            error(err)
-            return
-        end
-
-        local default_weight = conf.default_weight
-
-        local endpoints_informer, err
-        if conf.watch_endpoint_slices then
-            endpoints_informer, err = informer_factory.new("discovery.k8s.io", 
"v1",
-                                                           "EndpointSlice", 
"endpointslices", "")
-        else
-            endpoints_informer, err = informer_factory.new("", "v1", 
"Endpoints", "endpoints", "")
-        end
+        local handle, err = k8s_core.create_handle(conf, {
+            endpoint_dict = endpoint_dict,
+        })
         if err then
             error(err)
             return
         end
 
-        setup_namespace_selector(conf, endpoints_informer)
-        setup_label_selector(conf, endpoints_informer)
-
-        if conf.watch_endpoint_slices then
-            endpoints_informer.on_added = on_endpoint_slices_modified
-            endpoints_informer.on_modified = on_endpoint_slices_modified
-            endpoints_informer.on_deleted = on_endpoint_slices_deleted
-            endpoints_informer.endpoint_slices_cache = {}
-        else
-            endpoints_informer.on_added = on_endpoint_modified
-            endpoints_informer.on_modified = on_endpoint_modified
-            endpoints_informer.on_deleted = on_endpoint_deleted
-        end
-
-        endpoints_informer.pre_list = pre_list
-        endpoints_informer.post_list = post_list
-
-        ctx[id] = setmetatable({
-            endpoint_dict = endpoint_dict,
-            apiserver = apiserver,
-            default_weight = default_weight
-        }, { __index = endpoints_informer })
+        ctx[id] = handle
     end
 
     for _, item in pairs(ctx) do
-        start_fetch(item)
+        k8s_core.start_fetch(item)
     end
 end
 
 
 local function multiple_mode_nodes(service_name)
-    local pattern = "^(.*)/(.*/.*):(.*)$" -- id/namespace/name:port_name
-    local match = ngx.re.match(service_name, pattern, "jo")
-    if not match then
-        core.log.error("get unexpected upstream service_name: ", service_name)
-        return nil
-    end
-
-    local id = match[1]
-    local endpoint_dict = ctx[id]
-    if not endpoint_dict then
-        core.log.error("id not exist")
-        return nil
-    end
-
-    local endpoint_key = match[2]
-    local endpoint_port = match[3]
-    local endpoint_version = endpoint_dict:get(endpoint_key .. "#version")
-    if not endpoint_version then
-        core.log.info("get empty endpoint version from discovery DICT ", 
endpoint_key)
-        return nil
-    end
-
-    return endpoint_lrucache(service_name, endpoint_version,
-            create_endpoint_lrucache, endpoint_dict, endpoint_key, 
endpoint_port)
+    return k8s_core.resolve_nodes(
+        endpoint_lrucache, service_name,
+        "^(.*)/(.*/.*):(.*)$",   -- id/namespace/name:port_name
+        function(match)
+            local id = match[1]
+            local endpoint_dict = ctx[id]
+            if not endpoint_dict then
+                core.log.error("id not exist")
+                return nil
+            end

Review Comment:
   Same as above — workers set `ctx[id] = endpoint_dict` at line 114 (via 
`multiple_mode_worker_init`), privileged agent sets `ctx[id] = handle` at line 
148. `nodes()` only runs in workers.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to