From 8bd49c619b97ac0adf06621fac979e39cadd039d Mon Sep 17 00:00:00 2001 From: Ali Rizwan Date: Wed, 27 Sep 2017 13:57:30 +0200 Subject: [PATCH] Added wrr support with weights. (#33) * Added wrr support with weights. Added tests for functionality. Signed-off-by: Ali Rizwan * Fix tests for ubuntu14.04 Signed-off-by: Ali Rizwan --- templates/consul-service.j2 | 9 +++++++++ templates/haproxy.ctmp.j2 | 18 ++++++++++++++++-- test/integration/basic-agent/agent_vars.yml | 1 + .../serverspec/consul_service_spec.rb | 14 +++++++++++++- .../tags/serverspec/consul_service_spec.rb | 2 +- test/integration/tags/tags_vars.yml | 1 + 6 files changed, 41 insertions(+), 4 deletions(-) diff --git a/templates/consul-service.j2 b/templates/consul-service.j2 index 8b0275a..aea0885 100644 --- a/templates/consul-service.j2 +++ b/templates/consul-service.j2 @@ -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 }} } diff --git a/templates/haproxy.ctmp.j2 b/templates/haproxy.ctmp.j2 index 4a6ee04..348619b 100644 --- a/templates/haproxy.ctmp.j2 +++ b/templates/haproxy.ctmp.j2 @@ -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 %} @@ -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 }} diff --git a/test/integration/basic-agent/agent_vars.yml b/test/integration/basic-agent/agent_vars.yml index 01df652..0f97578 100644 --- a/test/integration/basic-agent/agent_vars.yml +++ b/test/integration/basic-agent/agent_vars.yml @@ -23,6 +23,7 @@ consul_services : name : "superssh-different-name" tags : - "test" + weight : 77 port : 22 local_port : 2222 check : diff --git a/test/integration/basic-agent/serverspec/consul_service_spec.rb b/test/integration/basic-agent/serverspec/consul_service_spec.rb index 943d365..4c13b36 100644 --- a/test/integration/basic-agent/serverspec/consul_service_spec.rb +++ b/test/integration/basic-agent/serverspec/consul_service_spec.rb @@ -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 @@ -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 } diff --git a/test/integration/tags/serverspec/consul_service_spec.rb b/test/integration/tags/serverspec/consul_service_spec.rb index f4d45d3..0a3f107 100644 --- a/test/integration/tags/serverspec/consul_service_spec.rb +++ b/test/integration/tags/serverspec/consul_service_spec.rb @@ -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 diff --git a/test/integration/tags/tags_vars.yml b/test/integration/tags/tags_vars.yml index 0381176..ec4934e 100644 --- a/test/integration/tags/tags_vars.yml +++ b/test/integration/tags/tags_vars.yml @@ -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"