Skip to content

Commit

Permalink
Added wrr support with weights. (#33)
Browse files Browse the repository at this point in the history
* Added wrr support with weights.

Added tests for functionality.

Signed-off-by: Ali Rizwan <[email protected]>

* Fix tests for ubuntu14.04

Signed-off-by: Ali Rizwan <[email protected]>
  • Loading branch information
aleeriz authored and ahelal committed Sep 27, 2017
1 parent 8266b7d commit 8bd49c6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
9 changes: 9 additions & 0 deletions templates/consul-service.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{% if 'weight' in consul_services[item] %}
{% set weight_value = consul_services[item].pop('weight') %}
{% if 'tags' in consul_services[item] %}
{% set _ = consul_services[item]['tags'].append("WEIGHT:" ~ weight_value ) %}
{% else %}
{% set _ = consul_services[item].update({'tags': ['WEIGHT:' ~ weight_value]}) %}
{% endif %}
{% endif %}

{ {{ '' if consul_services[item].pop('haproxy', '') else '' }}
"service": {{ consul_services[item] | to_nice_json }}
}
18 changes: 16 additions & 2 deletions templates/haproxy.ctmp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@ listen stats
backend {{ consul_services[service_name].name }}
mode {{ haproxy_setting.service_mode | default('http') }}
<% range service "{{ consul_services[service_name].name }}{{ consul_template_service_options_str }}" %>
<% scratch.Set "weightValue" "100" -%>
<% range .Tags -%>
<% if . | contains "WEIGHT:" -%>
<% $weightValue := . | split ":" -%>
<% $weightValue := index $weightValue 1 -%>
<% scratch.Set "weightValue" $weightValue -%>
<% end -%><% end -%>
{% if tags_contains %}<%if .Tags | contains "{{ tags_contains}}"%> # Match tags_contains {{ tags_contains }}{% endif %}

{% if tag_regex %}<%if .Tags | join " " | regexMatch "{{ tag_regex }}"%> # Match tag_regex {{ tag_regex }}{% endif %}

server <%.ID%>_<%.Address%>:<%.Port%> <%.Address%>:<%.Port%> {{ haproxy_setting.server_options | default(consul_haproxy_default_server_options) }}
server <%.ID%>_<%.Address%>:<%.Port%> <%.Address%>:<%.Port%> {% if consul_haproxy_default_balance == 'roundrobin' %}weight <% scratch.Get "weightValue" %> {% endif %} {{ haproxy_setting.server_options | default(consul_haproxy_default_server_options) }}
{% if tag_regex %}<%else%> # Did not match tag_regex {{ tag_regex }}<%end%>{% endif %}
{% if tags_contains %}<%else%> # Did not match tags_contains {{ tags_contains }}<%end%>{% endif %}

Expand All @@ -77,7 +84,14 @@ backend {{ consul_services[service_name].name }}
backend {{ consul_services[service_name].name }}
mode {{ haproxy_setting.service_mode | default('http') }}
<% range service "{{ consul_services[service_name].name }}{{ consul_template_service_options_str }}" %>
server <%.ID%>_<%.Address%>:<%.Port%> <%.Address%>:<%.Port%> {{ haproxy_setting.server_options | default(consul_haproxy_default_server_options) }}<%end%>
<% scratch.Set "weightValue" "100" -%>
<% range .Tags -%>
<% if . | contains "WEIGHT:" -%>
<% $weightValue := . | split ":" -%>
<% $weightValue := index $weightValue 1 -%>
<% scratch.Set "weightValue" $weightValue -%>
<% end -%><% end -%>
server <%.ID%>_<%.Address%>:<%.Port%> <%.Address%>:<%.Port%> {% if consul_haproxy_default_balance == 'roundrobin' %}weight <% scratch.Get "weightValue" %> {% endif %} {{ haproxy_setting.server_options | default(consul_haproxy_default_server_options) }}<%end%>
{% endif %}

frontend {{ consul_services[service_name].name }}
Expand Down
1 change: 1 addition & 0 deletions test/integration/basic-agent/agent_vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ consul_services :
name : "superssh-different-name"
tags :
- "test"
weight : 77
port : 22
local_port : 2222
check :
Expand Down
14 changes: 13 additions & 1 deletion test/integration/basic-agent/serverspec/consul_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe command 'curl -s -v http://127.0.0.1:8500/v1/catalog/service/superssh-different-name' do
its(:exit_status) { should eq 0 }
its(:stdout) { should contain '"ServiceName":"superssh-different-name"' }
its(:stdout) { should contain '"ServiceTags":\["test"]' }
its(:stdout) { should contain '"ServiceTags":\["test","WEIGHT:77"]' }
its(:stdout) { should contain '"ServicePort":22' }
end
end
Expand Down Expand Up @@ -115,6 +115,18 @@
end
end

describe 'hellofresh backend should have default weight' do
describe command 'echo "get weight hellofresh/`cat /etc/haproxy/haproxy.cfg | grep "server hellofresh" | awk \'{print $2}\'`" | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep 100' do
its(:stdout) { should contain '100 \(initial 100\)'}
end
end

describe 'superssh-different-name backend should have set weight' do
describe command 'echo "get weight superssh-different-name/`cat /etc/haproxy/haproxy.cfg | grep "server superssh-different-name" | awk \'{print $2}\'`" | socat unix-connect:/var/lib/haproxy/stats.sock stdio | grep 77' do
its(:stdout) { should contain '77 \(initial 77\)'}
end
end

describe 'Curl hellofresh upstream service is working on 80' do
describe command 'curl http://127.0.0.1:80' do
its(:exit_status) { should eq 0 }
Expand Down
2 changes: 1 addition & 1 deletion test/integration/tags/serverspec/consul_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe command 'curl -s -v http://127.0.0.1:8500/v1/health/service/hellofresh' do
its(:exit_status) { should eq 0 }
its(:stdout) { should contain '"ServiceName":"hellofresh"' }
its(:stdout) { should contain '"Tags":\["test","v1.1.5"]' }
its(:stdout) { should contain '"Tags":\["test","v1.1.5","WEIGHT:77"]' }
its(:stdout) { should contain '"Port":80' }
its(:stdout) { should contain '"Status":"passing"' }
end
Expand Down
1 change: 1 addition & 0 deletions test/integration/tags/tags_vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ consul_services :
- "v1.1.5"
port : 80
address : "hellofresh.com"
weight : "77"
check :
script : "curl https://www.hellofresh.com > /dev/null"
interval : "120s"
Expand Down

0 comments on commit 8bd49c6

Please sign in to comment.