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: stream subsystem support kubernetes service discovery #8640

Merged

Conversation

ronething
Copy link
Contributor

@ronething ronething commented Jan 9, 2023

Description

Fixes #7779

because of kubernetes service discovery use lua_shared_dict, so i add discovery_shared_dicts with stream suffix to stream subsystem in ngx_tpl.lua.

for test case, i use extra_stream_config to get nodes and return response.

because of openresty version 1.19.3.2 do not implemented ngx.process API for stream subsystem, so ci failed. see https://github.com/apache/apisix/actions/runs/3874260736/jobs/6605244559

i change openresty version from 1.19.3.2 to 1.19.9.1 to solve this problem, refer: https://openresty.org/en/changelog-1019009.html

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@ronething ronething marked this pull request as ready for review January 10, 2023 04:08
@@ -434,6 +442,9 @@ local function multiple_mode_init(confs)
end

local endpoint_dict = ngx.shared["kubernetes-" .. id]
if not is_http then
endpoint_dict = ngx.shared["kubernetes-" .. id .. "-stream"]
end
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to refactor these three changes into a function, instead of three if-else blocks.

@@ -17,5 +17,5 @@
#


export OPENRESTY_VERSION=1.19.3.2
export OPENRESTY_VERSION=1.19.9.1
Copy link
Member

Choose a reason for hiding this comment

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

We have promised to support the latest three versions of OpenResty, so we still need to cover 1.19.3.2.
What about updating the test case and only running it when the version is matched, like https://github.com/apache/apisix/blob/release/2.14/t/plugin/ext-plugin/sanity-openresty-1-19.t?

It would be better to refuse to start when the OpenResty is too old and k8s discovery is used in the stream.

Copy link
Contributor Author

@ronething ronething Jan 10, 2023

Choose a reason for hiding this comment

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

We have promised to support the latest three versions of OpenResty, so we still need to cover 1.19.3.2.
What about updating the test case and only running it when the version is matched, like https://github.com/apache/apisix/blob/release/2.14/t/plugin/ext-plugin/sanity-openresty-1-19.t?

ok, i will try it later.

It would be better to refuse to start when the OpenResty is too old and k8s discovery is used in the stream.

Is it a good choice if we add a boolean field like enable_stream(default is false) into apisix/discovery/kubernetes/schema.lua as a property and check OpenResty version when enable_stream is true?

Can you give me some hints? thanks.

Copy link
Member

Choose a reason for hiding this comment

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

something like

if conf.pass_host == "node" and conf.nodes and

Copy link
Contributor Author

@ronething ronething Jan 12, 2023

Choose a reason for hiding this comment

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

@spacewander

  • first, it will cause err like below when start apisix with openresty 1.19.3.2, because local discovery = require("apisix.discovery.init").discovery in apisix/upstream.lua:19 will import discovery/kubernetes/init.lua.
$ ./bin/apisix start
/usr/local/openresty-debug//luajit/bin/luajit ./apisix/cli/apisix.lua start
nginx: [error] init_by_lua error: /usr/local/openresty-debug/lualib/ngx/process.lua:5: unsupported subsystem: stream
stack traceback:
        [C]: in function 'error'
        /usr/local/openresty-debug/lualib/resty/core/base.lua:223: in function 'allows_subsystem'
        /usr/local/openresty-debug/lualib/ngx/process.lua:5: in main chunk
        [C]: in function 'require'
        ...ng/Documents/apisix/apisix/discovery/kubernetes/init.lua:29: in main chunk
        [C]: in function 'require'
        /home/ronething/Documents/apisix/apisix/discovery/init.lua:28: in main chunk
        [C]: in function 'require'
        /home/ronething/Documents/apisix/apisix/upstream.lua:19: in main chunk
        [C]: in function 'require'
        /home/ronething/Documents/apisix/apisix/http/service.lua:18: in main chunk
        [C]: in function 'require'
        /home/ronething/Documents/apisix/apisix/init.lua:35: in main chunk
        [C]: in function 'require'
        init_by_lua:3: in main chunk
  • then, something like if conf.pass_host == "node" and conf.nodes and in check_upstream_conf function may only print error log when check upstream if in_dp is true, or cause err when create/modify upstream or other resource which need to check upstream_conf both in http and stream subsystem. But we need to refuse to start apisix when the OpenResty is too old and k8s discovery is used in the stream?

so maybe we should consider other solutions? if something i was wrong, please tell me, thanks.

Copy link
Member

Choose a reason for hiding this comment

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

But we need to refuse to start apisix when the OpenResty is too old and k8s discovery is used in the stream?

Yes. Unlike the upstream conf which is a runtime conf, the wrong discovery conf in the yaml can't be corrected without restarting APISIX.

And we need to provide a clear error message so people don't need to debug it.

@@ -331,9 +332,18 @@ local function start_fetch(handle)
ngx.timer.at(0, timer_runner)
end

local function get_endpoint_dict(shm)
Copy link
Member

Choose a reason for hiding this comment

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

We can pass the id as the argument, and put the constant kubernetes in this function? There is no need to repeat it.

@@ -520,6 +520,12 @@ end


function _M.init_worker()
if not support_process then
core.log.error("kubernetes discovery not support in subsystem: "
.. ngx.config.subsystem
Copy link
Member

Choose a reason for hiding this comment

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

We can use ',' here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@spacewander do you mean something like below? .. ngx.config.subsystem .. ", "

if not support_process then
    core.log.error("kubernetes discovery not support in subsystem: "
                   .. ngx.config.subsystem .. ", "
                   .. "please check if your openresty version >= 1.19.9.1 or not")
    return
end

Copy link
Member

Choose a reason for hiding this comment

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

Err. What I mean is that we can use ',' instead of '..'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, i see. I will change it later.

@ronething
Copy link
Contributor Author

@tokers @soulbird Could you please take a look, thanks.

@spacewander spacewander merged commit 9c899b8 into apache:master Jan 28, 2023
@ronething ronething deleted the feat/stream_route_discovery_kubernetes branch January 28, 2023 08:29
hongbinhsu added a commit to fitphp/apix that referenced this pull request Jan 30, 2023
* upstream/master: (67 commits)
  fix: grpc-transcode plugin: fix map data population (apache#8731)
  change(jwt-auth): unify apisix/core/vault.lua and apisix/secret/vault.lua (apache#8660)
  feat: stream subsystem support consul_kv service discovery (apache#8633)
  fix(proxy-mirror): use with uri rewrite (apache#8718)
  ci: move helper script to the right dir (apache#8691)
  refactor(pubsub): simpify the get_cmd implementation (apache#8608)
  feat: stream subsystem support kubernetes service discovery (apache#8640)
  docs: fix deployment links (apache#8714)
  fix: remove backslash before slash when encoding (apache#8684)
  ci: kafka should register port in the zookeeper same as exposed (apache#8672)
  docs: fix plugin config naming (apache#8701)
  docs: fix code block (apache#8700)
  docs: rename kms to secret (apache#8697)
  docs: replace transparent logos with white background logos (apache#8689)
  fix: upgrade lua-resty-etcd to 1.10.3 (apache#8668)
  fix: upgrade casbin to 1.41.3 to improve performance (apache#8676)
  chore: make send_stream_request more clear (apache#8627)
  feat: stream subsystem support nacos service discovery (apache#8584)
  feat: stream subsystem support dns service discovery (apache#8593)
  refactor(admin): refactor resource routes (apache#8611)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: As a user, I want to support service discovery in the stream subsystem
4 participants