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: simplify auto cluster configs #165

Merged
merged 3 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ekka.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, ekka,
[{description, "Autocluster for EMQ X Broker"},
{vsn, "0.12.9"},
{vsn, "0.13.0"},
{mod, {ekka_app,[]}},
{registered,
[ekka_sup,
Expand Down
2 changes: 1 addition & 1 deletion src/ekka.appup.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%% -*-: erlang -*-

{<<"0.12.9">>,
{<<"0.13.0">>,
[
{<<".*">>, []}
],
Expand Down
22 changes: 12 additions & 10 deletions src/ekka_cluster_dns.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@
]).

discover(Options) ->
Name = proplists:get_value(name, Options),
App = proplists:get_value(app, Options),
DomainName = proplists:get_value(name, Options),
NodeName = proplists:get_value(app, Options, undefined),
Type = proplists:get_value(type, Options, a),
{ok, [node_name(App, Host) || Host <- resolve_hosts(Name, Type)]}.
{ok, [node_name(NodeName, Host) || Host <- resolve_hosts(DomainName, Type)]}.

resolve_hosts(Name, a) ->
[inet:ntoa(IP) || IP <- inet_res:lookup(Name, in, a)];
resolve_hosts(Name, srv) ->
Records = inet_res:lookup(Name, in, srv),
resolve_hosts(DomainName, a) ->
[inet:ntoa(IP) || IP <- inet_res:lookup(DomainName, in, a)];
resolve_hosts(DomainName, srv) ->
Records = inet_res:lookup(DomainName, in, srv),
lists:usort(lists:map(fun({_, _, _, Host}) -> Host end, Records)).

node_name(App, Host) ->
list_to_atom(lists:concat([App, "@", Host])).
node_name(undefined, Host) ->
[Name | _] = string:tokens(atom_to_list(node()), "@"),
node_name(Name, Host);
node_name(NodeName, Host) ->
list_to_atom(lists:concat([NodeName, "@", Host])).

lock(_Options) ->
ignore.
Expand All @@ -52,4 +55,3 @@ register(_Options) ->

unregister(_Options) ->
ignore.

8 changes: 7 additions & 1 deletion src/ekka_cluster_k8s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
discover(Options) ->
Server = get_value(apiserver, Options),
Service = get_value(service_name, Options),
App = get_value(app_name, Options, "ekka"),
App = resolve_name(get_value(app_name, Options, undefined)),
AddrType = get_value(address_type, Options, ip),
Namespace = get_value(namespace, Options, "default"),
Suffix = get_value(suffix, Options, ""),
Expand All @@ -51,6 +51,12 @@ discover(Options) ->
{error, Reason}
end.

resolve_name(undefined) ->
[Name | _] = string:tokens(atom_to_list(node()), "@"),
Name;
resolve_name(Name) ->
Name.

node_name(App, Addr, Service, hostname, Namespace, Suffix) when length(Suffix) > 0 ->
list_to_atom(lists:concat([App, "@", binary_to_list(Addr), ".", Service, ".", Namespace, ".", Suffix]));

Expand Down
5 changes: 5 additions & 0 deletions test/ekka_cluster_dns_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ end_per_testcase(_, Config) -> Config.

all() -> ekka_ct:all(?MODULE).

%% This test case relies on DNS resolution from localhost to 127.0.0.1
t_discover(_) ->
Options1 = [{name, "localhost"}, {app, "ekka"}],
{ok, ['ekka@127.0.0.1']} = ekka_cluster_dns:discover(Options1),
Expand All @@ -64,6 +65,10 @@ t_discover(_) ->
'ekka@emqx-1.emqx.default.svc.cluster.local',
'ekka@emqx-2.emqx.default.svc.cluster.local'
]} = ekka_cluster_dns:discover(Options2),

Options3 = [{name, "localhost"}],
%% below test relies on rebar3 ct is run with '--name ct@127.0.0.1'
{ok, ['ct@127.0.0.1']} = ekka_cluster_dns:discover(Options3),
ok.

t_lock(_) ->
Expand Down
5 changes: 3 additions & 2 deletions test/ekka_cluster_k8s_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
{namespace, "default"},
{service_name, "ekka"},
{address_type, ip},
{app_name, "ekka"},
{suffix, ""}
]).

Expand All @@ -37,7 +36,9 @@ t_discover(_) ->
ok = meck:expect(httpc, request, fun(get, _Req, _Opts, _) ->
{ok, {{"HTTP/1.1", 200, "OK"}, [], Json}}
end),
{ok, ['ekka@192.168.10.10']} = ekka_cluster_k8s:discover(?OPTIONS),
{ok, ['ekka@192.168.10.10']} = ekka_cluster_k8s:discover([{app_name, "ekka"} | ?OPTIONS]),
%% below test relies on rebar3 ct is run with '--name ct@127.0.0.1'
{ok, ['ct@192.168.10.10']} = ekka_cluster_k8s:discover(?OPTIONS),
ok = meck:unload(httpc).

t_lock(_) ->
Expand Down