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: use luasocket instead of curl in etcd.lua #2965

Merged
merged 7 commits into from
Jan 7, 2021

Conversation

starsz
Copy link
Contributor

@starsz starsz commented Dec 4, 2020

What this PR does / why we need it:

fix: #2818
fix: #2718

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

prefix: "/apisix"
' > conf/config.yaml

make init &>/tmp/apisix_temp &
Copy link
Member

Choose a reason for hiding this comment

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

Why need to run in the background?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the exit code of make init is 1. It will terminate the shell.

Copy link
Member

Choose a reason for hiding this comment

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

@starsz
You can try this way:

out=$(make init 2>&1 || true)


export ETCDCTL_API=3
etcdctl version
etcdctl --endpoints=127.0.0.1:2379 user add "root:apache-api6"
Copy link
Member

Choose a reason for hiding this comment

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

Need to clean the user/role after running the test

@@ -106,9 +111,6 @@ function _M.init(env, show_output)

local etcd_conf = yaml_conf.etcd

local timeout = etcd_conf.timeout or 3
Copy link
Member

Choose a reason for hiding this comment

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

Should allow configuring timeout in the new way


local body, _, err = dkjson.decode(res)
if err then
local res, err = http.request(version_url)
Copy link
Member

Choose a reason for hiding this comment

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

Look like this library doesn't support max-timeout? Only operation level timeout is supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. The library doesn't support max-timeout.

if err then
local res, err = http.request(version_url)
if err and type(err) == "string" then
errmsg = str_format("request etcd endpoint \'%s\' error, %s\n", host, err)
Copy link
Member

Choose a reason for hiding this comment

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

Better to log the full uri


-- err is string type
if err and type(err) == "string" then
errmsg = str_format("request etcd endpoint \"%s\" error, %s\n", host, err)
Copy link
Member

Choose a reason for hiding this comment

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

Better to log the full uri

sink = ltn12.sink.table(response_body),
headers = headers}
if err and type(err) == "string" then
errmsg = str_format("request etcd endpoint \"%s\" error, %s\n", host, err)
Copy link
Member

Choose a reason for hiding this comment

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

Better to log the full uri

@starsz
Copy link
Contributor Author

starsz commented Dec 23, 2020

Hi, @spacewander, please have a look when you are free.

sink = ltn12.sink.table(response_body),
headers = {["Content-Length"] = #post_json_auth}}
-- err is string type
if err and type(err) == "string" then
Copy link
Member

Choose a reason for hiding this comment

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

Better to add more info about the err handle, so that people can know what you are doing without looking into luasocket's doc.

Copy link
Contributor Author

@starsz starsz Dec 24, 2020

Choose a reason for hiding this comment

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

@spacewander Sorry, I can't get your point.
That we need to distinguish the connection refused and timeout

Copy link
Member

Choose a reason for hiding this comment

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

I mean, why we need to check the type of err? We need to clarify it in the comment, so that people can know it without searching luasocket's doc.

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 got it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

Copy link
Member

@spacewander spacewander Dec 25, 2020

Choose a reason for hiding this comment

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

I think it would be better to use if not res then handle err? Since the first argument is nil when error happened. This way is more normal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well. Agree with you.

@@ -179,14 +179,14 @@ function _M.init(env, show_output)

local post_json_auth = dkjson.encode(json_auth)
local response_body = {}
local _, err = http.request{url = auth_url, method = "POST",
local res, err = http.request{url = auth_url, method = "POST",
source = ltn12.source.string(post_json_auth),
sink = ltn12.sink.table(response_body),
headers = {["Content-Length"] = #post_json_auth}}
-- In case of failure, request returns nil followed by an error message.
-- Else the first return value is just the number 1
Copy link
Member

Choose a reason for hiding this comment

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

The first return value isn't the response body?

Copy link
Contributor Author

@starsz starsz Dec 25, 2020

Choose a reason for hiding this comment

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

Yep. It has two forms.
You can see more details about it.

In case of failure, the function returns nil followed by an error message. If successful, the simple form returns the response body as a string, followed by the response status code, the response headers and the response status line. The generic function returns the same information, except the first return value is just the number 1 (the body goes to the sink).

http://w3.impa.br/~diego/software/luasocket/http.html

Copy link
Member

Choose a reason for hiding this comment

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

Got it

@spacewander spacewander added this to the 2.3 milestone Dec 25, 2020
headers["Authorization"] = auth_token
end

local res, err = http.request{url = put_url, method = "POST",
Copy link
Member

Choose a reason for hiding this comment

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

style: I prefer http.request({...}), it is easy to read

@tokers tokers merged commit 7b5d2ea into apache:master Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants