Skip to content

feat: use luasocket instead of curl in etcd.lua#2965

Merged
tokers merged 7 commits into
apache:masterfrom
starsz:feat_2818
Jan 7, 2021
Merged

feat: use luasocket instead of curl in etcd.lua#2965
tokers merged 7 commits into
apache:masterfrom
starsz:feat_2818

Conversation

@starsz

@starsz starsz commented Dec 4, 2020

Copy link
Copy Markdown
Contributor

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

Comment thread .travis/apisix_cli_test.sh Outdated
prefix: "/apisix"
' > conf/config.yaml

make init &>/tmp/apisix_temp &

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Comment thread apisix/cli/etcd.lua

local etcd_conf = yaml_conf.etcd

local timeout = etcd_conf.timeout or 3

Copy link
Copy Markdown
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

Comment thread apisix/cli/etcd.lua

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

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread apisix/cli/etcd.lua Outdated
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
Copy Markdown
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

Comment thread apisix/cli/etcd.lua Outdated

-- 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
Copy Markdown
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

Comment thread apisix/cli/etcd.lua Outdated
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
Copy Markdown
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

starsz commented Dec 23, 2020

Copy link
Copy Markdown
Contributor Author

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

Comment thread apisix/cli/etcd.lua Outdated
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
Copy Markdown
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.

@starsz starsz Dec 24, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added.

@spacewander spacewander Dec 25, 2020

Copy link
Copy Markdown
Member

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

Comment thread apisix/cli/etcd.lua
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
Copy Markdown
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?

@starsz starsz Dec 25, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Comment thread apisix/cli/etcd.lua
headers["Authorization"] = auth_token
end

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

Copy link
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

[DISCUSS]: Use luasocket lib instead of curl in "bin/apisix" [DISCUSS] The error message "got malformed version message from etcd" is ambiguous

4 participants