Skip to content

Commit 9285f43

Browse files
authored
Merge pull request #1 from darix/add-support-for-named-defaults
Add support for named defaults
2 parents ccf8394 + 33a7627 commit 9285f43

2 files changed

Lines changed: 78 additions & 17 deletions

File tree

haproxy/templates/haproxy.jinja

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,68 @@ defaults
170170
{%- endfor %}
171171
{%- endif %}
172172
{%- endif %}
173+
174+
{%- for named_defaults_name, named_defaults in salt['pillar.get']('haproxy:named_defaults', {}) %}
175+
defaults {{ named_defaults_name }}{%- if 'inherit_defaults' in named_defaults %} from {{ named_defaults.inherit_defaults }}{%- endif }
176+
{%- if 'mode' in named_defaults) %}
177+
log {{ named_defaults['log'] }}
178+
{%- endif %}
179+
{%- if 'mode' in named_defaults %}
180+
mode {{ named_defaults['mode'] }}
181+
{%- endif %}
182+
{%- if 'retries' in named_defaults %}
183+
retries {{ named_defaults['retries'] }}
184+
{%- endif %}
185+
{%- if 'balance' in named_defaults %}
186+
balance {{ named_defaults['balance'] }}
187+
{%- endif %}
188+
{%- if 'monitoruri' in named_defaults %}
189+
monitor-uri {{ named_defaults['monitoruri'] }}
190+
{%- endif %}
191+
{%- if 'hashtype' in named_defaults %}
192+
hash-type {{ named_defaults['hashtype'] }}
193+
{%- endif %}
194+
{%- if 'options' in named_defaults -%}
195+
{{- render_list_of_dictionaries('option', named_defaults['options']) }}
196+
{%- endif %}
197+
{%- if 'logformat' in named_defaults %}
198+
log-format {{ named_defaults['logformat'] }}
199+
{%- endif %}
200+
{%- if 'maxconn' in named_defaults %}
201+
maxconn {{ named_defaults['maxconn'] }}
202+
{%- endif %}
203+
{%- if 'http_reuse' in named_defaults %}
204+
http-reuse {{ named_defaults['http_reuse'] }}
205+
{%- endif %}
206+
{%- if 'timeouts' in named_defaults %}
207+
{%- for timeout_type, timeout_value in named_defaults['timeouts'].items() %}
208+
timeout {{ timeout_type }} {{ timeout_value }}
209+
{%- endfor %}
210+
{%- else %}
211+
timeout client 1m
212+
timeout connect 10s
213+
timeout server 1m
214+
{%- endif %}
215+
{%- if 'stats' in named_defaults -%}
216+
{{ render_list_of_dictionaries('stats', named_defaults['stats']) }}
217+
{%- endif %}
218+
{%- if 'extra' in named_defaults %}
219+
{%- if named_defaults.get('extra', {}) is string %}
220+
{{ named_defaults['extra'] }}
221+
{%- else %}
222+
{%- for line in named_defaults['extra'] %}
223+
{{ line }}
224+
{%- endfor %}
225+
{%- endif %}
226+
{%- endif %}
227+
{%- endfor %}
228+
173229
{%- if 'errorfiles' in salt['pillar.get']('haproxy:defaults', {}) %}
174230
{%- for errorfile_name, errorfile in salt['pillar.get']('haproxy:defaults:errorfiles')|dictsort %}
175231
errorfile {{ errorfile_name }} {{ errorfile }}
176232
{%- endfor %}
177233
{% endif %}
234+
178235
{%- if salt['pillar.get']('haproxy:resolvers') %}
179236

180237

@@ -196,7 +253,7 @@ resolvers {{ resolver_name }}
196253
# listen instances
197254
#------------------
198255
{%- for listener_name, listener in salt['pillar.get']('haproxy:listens', {})|dictsort %}
199-
listen {{ listener.get('name', listener_name) }}
256+
listen {{ listener.get('name', listener_name) }}{%- if 'inherit_defaults' in listener %} from {{ listener.inherit_defaults }}{%- endif }
200257
{%- if 'bind' in listener %}
201258
{%- if listener.bind is string %}
202259
bind {{ listener.bind }}
@@ -429,7 +486,7 @@ listen {{ listener.get('name', listener_name) }}
429486
# frontend instances
430487
#------------------
431488
{%- for frontend_name, frontend in salt['pillar.get']('haproxy:frontends', {})|dictsort %}
432-
frontend {{ frontend.get('name', frontend_name) }}
489+
frontend {{ frontend.get('name', frontend_name) }}{%- if 'inherit_defaults' in frontend %} from {{ frontend.inherit_defaults }}{%- endif }
433490
{%- if 'bind' in frontend %}
434491
{{- render_list_of_dictionaries('bind', frontend.bind) }}
435492
{%- endif %}
@@ -530,7 +587,7 @@ frontend {{ frontend.get('name', frontend_name) }}
530587
# backend instances
531588
#------------------
532589
{%- for backend_name, backend in salt['pillar.get']('haproxy:backends', {})|dictsort %}
533-
backend {{ backend.get('name', backend_name) }}
590+
backend {{ backend.get('name', backend_name) }}{%- if 'inherit_defaults' in backend %} from {{ backend.inherit_defaults }}{%- endif }
534591
{%- if 'mode' in backend %}
535592
mode {{ backend.mode }}
536593
{%- endif %}

pillar.example

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ haproxy:
8181
- realm: 'Haproxy\ Statistics'
8282
- auth: 'admin1:AdMiN123'
8383

84+
named_defaults:
85+
patroni:
86+
mode: tcp
87+
balance: roundrobin
88+
options:
89+
- tcplog
90+
- httpchk
91+
httpcheck:
92+
- connect default ssl
93+
- send meth GET uri / ver HTTP/1.1 hdr Host localhost hdr User-Agent haproxy/patroni-clustercheck hdr Accept */*
94+
- expect status 200-399
95+
timeouts:
96+
connect: 10s
97+
client: 3600s
98+
server: 3600s
99+
defaultserver: check maxconn 1000 port 8008 inter 2000 rise 3 fall 3 crt /etc/step/certs/generic.user.full.pem ca-file /usr/share/pki/trust/anchors/step-ca-ibs.crt.pem on-marked-down shutdown-sessions
100+
84101
# the same can be set in a per backend/listen as well
85102
errorfiles:
86103
400: /etc/haproxy/errors/400.http
@@ -165,22 +182,9 @@ haproxy:
165182
check: check
166183
extra: port 6379 inter 1s backup
167184
patroni:
185+
inherit_defaults: patroni
168186
bind:
169187
- :25432
170-
mode: tcp
171-
balance: roundrobin
172-
options:
173-
- tcplog
174-
- httpchk
175-
httpcheck:
176-
- connect default ssl
177-
- send meth GET uri / ver HTTP/1.1 hdr Host localhost hdr User-Agent haproxy/patroni-clustercheck hdr Accept */*
178-
- expect status 200-399
179-
timeouts:
180-
connect: 10s
181-
client: 3600s
182-
server: 3600s
183-
defaultserver: check maxconn 1000 port 8008 inter 2000 rise 3 fall 3 crt /etc/step/certs/generic.user.full.pem ca-file /usr/share/pki/trust/anchors/step-ca-ibs.crt.pem on-marked-down shutdown-sessions
184188
servers:
185189
# for each node matching the target it will count up the loop index and append that to the server name
186190
postgresql:

0 commit comments

Comments
 (0)