diff --git a/lib/kontena/templates/haproxy/main.text.erb b/lib/kontena/templates/haproxy/main.text.erb index 63f40cc..285b250 100644 --- a/lib/kontena/templates/haproxy/main.text.erb +++ b/lib/kontena/templates/haproxy/main.text.erb @@ -29,7 +29,7 @@ defaults <% end %> <% end %> -<% if services.size > 0 %> +<% if services.size > 0 || ssl? || health_uri? %> <%= http_in %> <% end %> diff --git a/lib/kontena/views/common.rb b/lib/kontena/views/common.rb new file mode 100644 index 0000000..edca77f --- /dev/null +++ b/lib/kontena/views/common.rb @@ -0,0 +1,12 @@ +module Kontena::Views + module Common + + def ssl? + ENV['SSL_CERTS'] || ENV.any? { |env, value| env.start_with? 'SSL_CERT_' } + end + + def health_uri? + ENV['KONTENA_LB_HEALTH_URI'] + end + end +end diff --git a/lib/kontena/views/haproxy.rb b/lib/kontena/views/haproxy.rb index 6fbb48d..a13335a 100644 --- a/lib/kontena/views/haproxy.rb +++ b/lib/kontena/views/haproxy.rb @@ -1,4 +1,5 @@ require 'erb' +require_relative 'common' require_relative 'http_in' require_relative 'http_backends' require_relative 'tcp_proxies' @@ -6,6 +7,7 @@ module Kontena::Views class Haproxy include Hanami::View + include Kontena::Views::Common format :text template 'haproxy/main' diff --git a/lib/kontena/views/http_in.rb b/lib/kontena/views/http_in.rb index d6275b1..0ec1f99 100644 --- a/lib/kontena/views/http_in.rb +++ b/lib/kontena/views/http_in.rb @@ -1,12 +1,11 @@ +require_relative 'common' + module Kontena::Views class HttpIn include Hanami::View + include Kontena::Views::Common format :text template 'haproxy/http_in' - - def ssl? - ENV['SSL_CERTS'] || ENV.any? { |env, value| env.start_with? 'SSL_CERT_' } - end end end diff --git a/spec/kontena/views/haproxy_spec.rb b/spec/kontena/views/haproxy_spec.rb index 62d938e..ac0c39e 100644 --- a/spec/kontena/views/haproxy_spec.rb +++ b/spec/kontena/views/haproxy_spec.rb @@ -1,7 +1,39 @@ describe Kontena::Views::Haproxy do - describe '.render' do - context 'stats' do + describe '.render' do + context 'http-in' do + it 'does not configure http-in by default' do + output = described_class.render( + format: :text, + services: [], + tcp_services: [] + ) + expect(output.match(/listen http-in/)).to be_falsey + end + + it 'configures http-in if health uri is defined' do + allow(ENV).to receive(:[]) + allow(ENV).to receive(:[]).with('KONTENA_LB_HEALTH_URI').and_return('/__health') + output = described_class.render( + format: :text, + services: [], + tcp_services: [] + ) + expect(output.match(/listen http-in/)).to be_truthy + end + + it 'configures http-in if ssl certs' do + allow(ENV).to receive(:[]) + allow(ENV).to receive(:[]).with('SSL_CERTS').and_return('cert...') + output = described_class.render( + format: :text, + services: [], + tcp_services: [] + ) + expect(output.match(/listen http-in/)).to be_truthy + end + end + context 'stats' do it 'configures stats auth' do allow(ENV).to receive(:[]) allow(ENV).to receive(:[]).with('STATS_PASSWORD').and_return('secrettzzz')