Skip to content

Commit e39b878

Browse files
authored
victoriametrics: support args as list (#736)
1 parent 3a85e11 commit e39b878

5 files changed

Lines changed: 78 additions & 37 deletions

File tree

victoriametrics/_setup.sls

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,59 @@
6464
6565
{%- set service_name = kind if vm_name == "main" else kind ~ "-" ~ vm_name %}
6666
67-
{%- if ( kind in ["vmserver", "vmalert"] ) and vm_data.get("nginx", {}) and vm_data.get("nginx",{}).get("enabled", True) %}
68-
{%- include "victoriametrics/nginx/init.sls" %}
69-
{%- endif %}
67+
{%- set vmargskeys = [] %}
68+
{%- set vmargslist = [] %}
7069
71-
{%- do defaults[kind]["args"].update(vm_data.get("args", {})) %}
72-
{%- do vm_data.setdefault("args", {}).update(defaults[kind]["args"]) %}
73-
{%- if kind != "vmalert" %}
74-
{%- set arg_storage = defaults[kind]["arg_storage"] %}
75-
{%- set data_dir = vm_data.get(arg_storage, defaults[kind]["args"][arg_storage]).format(vm_name=vm_name) %}
76-
{%- do vm_data["args"].update({arg_storage: data_dir}) %}
70+
{%- set vmargs = [] %}
71+
{%- set vm_data_args = vm_data.get("args", []) %}
72+
{%- if vm_data_args is mapping %}
73+
{% do vmargs.append(vm_data_args) %}
74+
{%- else %}
75+
{% do vmargs.extend(vm_data_args) %}
7776
{%- endif %}
7877
79-
{%- set vmargslist = [] %}
80-
{%- for k, v in vm_data.get("args", {}).items() %}
81-
{%- do vmargslist.append("-" ~ k ~ "=" ~ v) %}
78+
{# Populate args from pillar #}
79+
{%- for arg in vmargs %}
80+
{%- for k, v in arg.items() %}
81+
{%- do vmargskeys.append(k) %}
82+
{% if v is string %}
83+
{%- do vmargslist.append({k: v.format(vm_name=vm_name)}) %}
84+
{%- else %}
85+
{%- do vmargslist.append({k: v}) %}
86+
{%- endif %}
87+
{%- endfor %}
88+
{%- endfor %}
89+
90+
{# Populate args from defaults if not set in pillar #}
91+
{%- for arg in defaults[kind]["args"] %}
92+
{%- for k, v in arg.items() %}
93+
{% if k not in vmargskeys %}
94+
{%- do vmargslist.append({k: v.format(vm_name=vm_name)}) %}
95+
{% endif %}
96+
{%- endfor %}
8297
{%- endfor %}
8398
99+
{%- do vm_data.update({"settings":{}}) %}
100+
{%- for arg in vmargslist %}
101+
{%- for k, v in arg.items() %}
102+
{%- if k == defaults[kind]["arg_storage"] %}
103+
{%- do vm_data["settings"].update({"storage_dir": v}) %}
104+
{%- endif %}
105+
{%- if k == "httpListenAddr" %}
106+
{%- do vm_data["settings"].update({k: v}) %}
107+
{%- endif %}
108+
{% endfor %}
109+
{% endfor %}
110+
111+
112+
{%- if ( kind in ["vmserver", "vmalert"] ) and vm_data.get("nginx", {}) and vm_data.get("nginx",{}).get("enabled", True) %}
113+
{%- include "victoriametrics/nginx/init.sls" %}
114+
{%- endif %}
115+
84116
{%- if kind != "vmalert" %}
85117
{{ kind }}_{{ vm_name }}_storage_dir:
86118
file.directory:
87-
- name: {{ vm_data["args"][arg_storage] }}
119+
- name: {{ vm_data["settings"]["storage_dir"] }}
88120
- makedirs: True
89121
- user: root
90122
- group: root
@@ -96,6 +128,14 @@
96128
- source: {{ service_target.rsplit("/", maxsplit=1)[0] }}/{{ defaults[kind]["original_name"] }}
97129
- force: True
98130
131+
{# Transform in joinable list #}
132+
{%- set vmargslistjoin = [] %}
133+
{%- for arg in vmargslist %}
134+
{%- for k, v in arg.items() %}
135+
{%- do vmargslistjoin.append("-" ~ k ~ "=" ~ v) %}
136+
{% endfor %}
137+
{% endfor %}
138+
99139
{{ kind }}_{{ vm_name }}_systemd_unit:
100140
file.managed:
101141
- name: /etc/systemd/system/{{ service_name }}.service
@@ -112,7 +152,7 @@
112152
StartLimitInterval=0
113153
Restart=on-failure
114154
RestartSec=1
115-
ExecStart={{ service_target }} {{ " ".join(vmargslist) }}
155+
ExecStart={{ service_target }} {{ " ".join(vmargslistjoin) }}
116156
117157
[Install]
118158
WantedBy=multi-user.target

victoriametrics/defaults.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ vmagent:
2626
target: /usr/local/bin/vmagent
2727
arg_storage: remoteWrite.tmpDataPath
2828
args:
29-
remoteWrite.tmpDataPath: /var/lib/vmagent/{vm_name}/remotewrite-data
29+
- remoteWrite.tmpDataPath: /var/lib/vmagent/{vm_name}/remotewrite-data
3030

3131
vmserver:
3232
name: victoria-metrics
3333
original_name: victoria-metrics-prod
3434
target: /usr/local/bin/vmserver
3535
arg_storage: storageDataPath
3636
args:
37-
httpListenAddr: 127.0.0.1:8428
38-
storageDataPath: /var/lib/victoriametrics/{vm_name}
39-
retentionPeriod: 1
37+
- httpListenAddr: 127.0.0.1:8428
38+
- storageDataPath: /var/lib/victoriametrics/{vm_name}
39+
- retentionPeriod: 1
4040

4141
vmalert:
4242
name: vmutils
4343
original_name: vmalert-prod
4444
target: /usr/local/bin/vmalert
4545
args:
46-
rule: /etc/victoriametrics/vmalert/rules/*.yaml
47-
datasource.url: http://localhost:8428
48-
httpListenAddr: 127.0.0.1:8880
49-
notifier.url: http://localhost:9093
50-
evaluationInterval: 10s
46+
- rule: /etc/victoriametrics/vmalert/rules/*.yaml
47+
- datasource.url: http://localhost:8428
48+
- httpListenAddr: 127.0.0.1:8880
49+
- notifier.url: http://localhost:9093
50+
- evaluationInterval: 10s
5151

victoriametrics/nginx/init.sls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
3030
{%- endfor %}
3131
32+
3233
{{ kind }}_{{ vm_name }}_nginx_files_1:
3334
file.managed:
3435
- name: /etc/nginx/sites-available/{{ service_name }}.conf

victoriametrics/nginx/vhost.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ server {
1111
}
1212

1313
upstream {{ service_name }} {
14-
server {{ vm_data["args"]["httpListenAddr"] }};
14+
server {{ vm_data["settings"]["httpListenAddr"] }};
1515
keepalive 10;
1616
}
1717

victoriametrics/pillar.example

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ vmserver:
5555
version: v1.101.0 # optional, will not overwrite the binary files, if version matches
5656

5757
args: # https://docs.victoriametrics.com/single-server-victoriametrics/#list-of-command-line-flags
58-
httpListenAddr: 0.0.0.0:8428 # optional, default
59-
storageDataPath: /var/lib/victoriametrics/__VM_NAME__ # optional, default
60-
retentionPeriod: 1 # If suffix isn't set, then the duration is counted in months (default 1)
58+
- httpListenAddr: 0.0.0.0:8428 # optional, default
59+
- storageDataPath: /var/lib/victoriametrics/__VM_NAME__ # optional, default
60+
- retentionPeriod: 1 # If suffix isn't set, then the duration is counted in months (default 1)
6161
# following optional suffixes are supported: s (second), m (minute), h (hour), d (day), w (week), y (year)
6262
files: # optional, see https://github.com/microdevops-com/microdevops-formula/tree/master/_include/file_manage
6363

@@ -75,11 +75,11 @@ vmagent: # https://docs.victoriametrics.com/vmagent/#features
7575
# will not redownload the archive, if hash matches
7676
version: v1.101.0 # optional, will not overwrite the binary files, if version matches
7777
args: # https://docs.victoriametrics.com/vmagent/#advanced-usage
78-
remoteWrite.tmpDataPath: /var/lib/vmagent/__VM_NAME__/remotewrite-data # optional, default
79-
remoteWrite.url: https://vm.tsdb.example.com/api/v1/write
80-
remoteWrite.basicAuth.username: user1
81-
remoteWrite.basicAuth.password: password1
82-
promscrape.config: /etc/victoriametrics/promscrape.yaml
78+
- remoteWrite.tmpDataPath: /var/lib/vmagent/__VM_NAME__/remotewrite-data # optional, default
79+
- remoteWrite.url: https://vm.tsdb.example.com/api/v1/write
80+
- remoteWrite.basicAuth.username: user1
81+
- remoteWrite.basicAuth.password: password1
82+
- promscrape.config: /etc/victoriametrics/promscrape.yaml
8383
files:
8484
managed:
8585
promscrape:
@@ -143,11 +143,11 @@ vmalert:
143143
version: v1.101.0 # optional, will not overwrite the binary files, if version matches
144144

145145
args: # https://docs.victoriametrics.com/single-server-victoriametrics/#list-of-command-line-flags
146-
httpListenAddr: 127.0.0.1:8880 # optional, default
147-
datasource.url: http://localhost:8428 # optional, default
148-
rule: /etc/victoriametrics/vmalert/rules/*yaml # optional, default
149-
notifier.url: http://localhost:9093 # optional, default
150-
evaluationInterval: 10s # optional, default
146+
- httpListenAddr: 127.0.0.1:8880 # optional, default
147+
- datasource.url: http://localhost:8428 # optional, default
148+
- rule: /etc/victoriametrics/vmalert/rules/*yaml # optional, default
149+
- notifier.url: http://localhost:9093 # optional, default
150+
- evaluationInterval: 10s # optional, default
151151
files: # optional, see https://github.com/microdevops-com/microdevops-formula/tree/master/_include/file_manage
152152
managed:
153153
vmalert_rules_disk_alert:

0 commit comments

Comments
 (0)