id: elasticsearch-overprovisioned-replica-yellow-guard
namespace: company.team
description: |
Detect Elasticsearch indices configured with more replicas than the
cluster can ever place, lower the replica count to the placeable floor
behind a dry run and an approval gate, then re-read health and settings
and fail if the targeted index still has unassigned replica shards.
inputs:
- id: elasticsearch_url
type: STRING
defaults: "http://localhost:9200"
description: Base URL of the Elasticsearch cluster, scheme host and port.
- id: elasticsearch_username
type: STRING
defaults: "{{ secret('ELASTICSEARCH_USERNAME') }}"
description: Username for basic auth. Clusters with security disabled ignore it.
- id: elasticsearch_password
type: STRING
defaults: "{{ secret('ELASTICSEARCH_PASSWORD') }}"
description: Password for basic auth. Clusters with security disabled ignore it.
- id: index_pattern
type: STRING
defaults: "*"
description: Index or pattern to inspect. Defaults to every index.
- id: include_system_indices
type: BOOL
defaults: false
description: >-
When true, dot-prefixed and hidden indices are inspected too. Off by
default because those belong to Elasticsearch and its plugins.
- id: min_data_nodes
type: INT
defaults: 1
description: >-
The flow refuses to lower any replica count unless the cluster reports at
least this many data nodes. Set it to your normal data node count so a
cluster mid-restart is never mistaken for an over-provisioned index.
- id: dry_run
type: BOOL
defaults: true
description: When true the flow only reports, it never changes a replica count.
- id: auto_remediate
type: BOOL
defaults: false
description: When true and dry_run is false the flow lowers the replica count
after approval.
triggers:
- id: daily
type: io.kestra.plugin.core.trigger.Schedule
description: Scan for unplaceable replicas every morning in safe report-only mode.
cron: "0 6 * * *"
disabled: true
inputs:
dry_run: true
auto_remediate: false
tasks:
- id: detect_health
type: io.kestra.plugin.elasticsearch.Request
description: Read cluster health for the overall status, the unassigned shard
count and the number of data nodes.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "_cluster/health"
- id: detect_nodes
type: io.kestra.plugin.elasticsearch.Request
description: Read the data node roster so the placeable floor is computed from
named nodes, not from a single counter.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "_nodes/data:true"
parameters:
filter_path: "nodes.*.name,nodes.*.roles"
- id: detect_indices
type: io.kestra.plugin.elasticsearch.Request
description: Read per-index health so each index exposes its replica count next
to its unassigned shard count.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "_cluster/health/{{ inputs.index_pattern }}"
parameters:
level: "indices"
expand_wildcards: "open,hidden"
- id: detect_settings
type: io.kestra.plugin.elasticsearch.Request
description: Read the configured replica count and the hidden flag for every
matching index.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "{{ inputs.index_pattern
}}/_settings/index.number_of_replicas,index.hidden"
parameters:
flat_settings: "true"
expand_wildcards: "open,hidden"
- id: check_reads
type: io.kestra.plugin.core.flow.If
description: >-
The Request task reports task success on any HTTP status, so every read is
gated on its status here. A 4xx would otherwise pass for a healthy
cluster.
condition: "{{ outputs.detect_health.status != 200 or
outputs.detect_nodes.status != 200 or outputs.detect_indices.status != 200
or outputs.detect_settings.status != 200 }}"
then:
- id: fail_reads
type: io.kestra.plugin.core.execution.Fail
description: Fail when the cluster could not be read so nothing downstream acts
on a partial picture.
errorMessage: >-
Could not read the cluster. health {{ outputs.detect_health.status }},
nodes {{ outputs.detect_nodes.status }}, index health {{
outputs.detect_indices.status }}, settings {{
outputs.detect_settings.status }}.
- id: plan
type: io.kestra.plugin.core.output.OutputValues
description: >-
Compute the placeable floor (data nodes minus one, never below zero) and
keep the indices whose configured replica count is above it while replica
shards sit unassigned.
values:
data_nodes: "{{ outputs.detect_health.response | jq('.number_of_data_nodes') |
first }}"
node_count: "{{ outputs.detect_nodes.response |
jq('[.nodes|to_entries[]|select([.value.roles[]|select(.==\"data\" or
startswith(\"data_\"))]|length>0)]|length') | first }}"
node_names: "{{ outputs.detect_nodes.response |
jq('[.nodes|to_entries[]|select([.value.roles[]|select(.==\"data\" or
startswith(\"data_\"))]|length>0)|.value.name]|sort|join(\", \")') |
first }}"
floor: "{{ outputs.detect_health.response | jq('if (.number_of_data_nodes - 1) <
0 then 0 else (.number_of_data_nodes - 1) end') | first }}"
cluster_status: "{{ outputs.detect_health.response | jq('.status') | first }}"
cluster_unassigned: "{{ outputs.detect_health.response | jq('.unassigned_shards') | first }}"
targets_json: >-
{{ {'h': outputs.detect_indices.response, 's':
outputs.detect_settings.response, 'inc': inputs.include_system_indices}
| toJson | jq('. as $r | (if ($r.h.number_of_data_nodes - 1) < 0 then 0
else ($r.h.number_of_data_nodes - 1) end) as $floor | [($r.h.indices //
{}) | to_entries[] | select(.value.number_of_replicas > $floor) |
select(.value.unassigned_shards > 0) | . as $e | {index: $e.key,
replicas: $e.value.number_of_replicas, shards:
$e.value.number_of_shards, unassigned: $e.value.unassigned_shards,
status: $e.value.status, floor: $floor, system: (($e.key |
startswith(".")) or ((($r.s[$e.key].settings["index.hidden"]) //
"false") == "true"))}] | map(select($r.inc or (.system == false)))') |
first | toJson }}
target_names: >-
{{ {'h': outputs.detect_indices.response, 's':
outputs.detect_settings.response, 'inc': inputs.include_system_indices}
| toJson | jq('. as $r | (if ($r.h.number_of_data_nodes - 1) < 0 then 0
else ($r.h.number_of_data_nodes - 1) end) as $floor | [($r.h.indices //
{}) | to_entries[] | select(.value.number_of_replicas > $floor) |
select(.value.unassigned_shards > 0) | . as $e | {index: $e.key,
replicas: $e.value.number_of_replicas, unassigned:
$e.value.unassigned_shards, floor: $floor, system: (($e.key |
startswith(".")) or ((($r.s[$e.key].settings["index.hidden"]) //
"false") == "true"))}] | map(select($r.inc or (.system == false))) |
map(.index + " (replicas " + (.replicas|tostring) + " to " +
(.floor|tostring) + ", " + (.unassigned|tostring) + " unassigned)") |
join(" | ")') | first }}
inspected: "{{ outputs.detect_indices.response | jq('(.indices // {}) | length')
| first }}"
- id: report
type: io.kestra.plugin.core.log.Log
description: Record the floor, the data node roster, the flagged indices and the mode.
message: |
Over-provisioned replica scan on pattern {{ inputs.index_pattern }}.
Data nodes: {{ outputs.plan.values.data_nodes }} reported by cluster health, {{ outputs.plan.values.node_count }} in the roster ({{ outputs.plan.values.node_names }}).
Placeable replica floor: {{ outputs.plan.values.floor }}. A replica is never placed on the node that already holds its primary.
Cluster status {{ outputs.plan.values.cluster_status }} with {{ outputs.plan.values.cluster_unassigned }} unassigned shards. Indices inspected: {{ outputs.plan.values.inspected }}.
Flagged: {{ outputs.plan.values.targets_json | jq('length') | first }}. {{ outputs.plan.values.target_names }}
Mode dry_run={{ inputs.dry_run }} auto_remediate={{ inputs.auto_remediate }} include_system_indices={{ inputs.include_system_indices }} min_data_nodes={{ inputs.min_data_nodes }}.
- id: alert_detected_gate
type: io.kestra.plugin.core.flow.If
description: Alert Slack the moment an unplaceable replica count is found,
before and apart from any remediation decision.
condition: "{{ (outputs.plan.values.targets_json | jq('length') | first) > 0 }}"
then:
- id: alert_detected
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Post the flagged indices, the floor and whether a change will run
on this pass.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "Elasticsearch over-provisioned replica guard: {{ outputs.plan.values.targets_json | jq('length') | first }} index(es) on pattern {{ inputs.index_pattern }} ask for more replicas than {{ outputs.plan.values.data_nodes }} data node(s) can place, so the cluster is stuck yellow with unassigned replica shards. Placeable floor {{ outputs.plan.values.floor }}. {{ outputs.plan.values.target_names }}. dry_run={{ inputs.dry_run }} auto_remediate={{ inputs.auto_remediate }}."}
- id: route
type: io.kestra.plugin.core.flow.If
description: Act only when an unplaceable replica count is present and the
operator asked for a real change.
condition: "{{ (outputs.plan.values.targets_json | jq('length') | first) > 0 and
inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: nodes_gate
type: io.kestra.plugin.core.flow.If
description: >-
Lower a replica count only when the cluster has at least
min_data_nodes data nodes. A node that is temporarily out for a
restart shrinks the floor, so acting on that would throw away
redundancy the topology does support.
condition: "{{ (outputs.plan.values.node_count | number) >=
inputs.min_data_nodes and (outputs.plan.values.data_nodes | number) >=
inputs.min_data_nodes }}"
then:
- id: approval_gate
type: io.kestra.plugin.core.flow.Pause
description: >-
Hold the run until a human approves lowering the replica counts.
The change never starts on its own.
pauseDuration: PT2H
behavior: FAIL
onResume:
- id: approved
type: BOOL
defaults: false
description: >-
Set true to lower the listed indices to the placeable floor.
Anything else leaves them untouched.
- id: apply
type: io.kestra.plugin.core.flow.If
description: Run the change only on an explicit approval. An unanswered gate
never reads as approval.
condition: "{{ outputs.approval_gate.onResume.approved ?? false }}"
then:
- id: remediate_replicas
type: io.kestra.plugin.core.flow.Loop
description: >-
Remediation loop. One PUT _settings per flagged index,
concurrencyLimit 1 so the updates land in turn and never race
on the cluster state. allowFailure so one rejected update does
not stop the pass and the re-read stays the real gate.
concurrencyLimit: 1
values: "{{ outputs.plan.values.targets_json }}"
tasks:
- id: set_replicas
type: io.kestra.plugin.elasticsearch.Request
description: Lower one index to the placeable replica floor.
allowFailure: true
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: PUT
endpoint: "{{ fromJson(item.value).index }}/_settings"
body: >-
{"index": {"number_of_replicas": {{
fromJson(item.value).floor }} } }
- id: set_log
type: io.kestra.plugin.core.log.Log
message: >-
PUT {{ fromJson(item.value).index }}/_settings
number_of_replicas {{ fromJson(item.value).replicas }} to
{{ fromJson(item.value).floor }} returned HTTP {{
outputs.set_replicas.status }}.
- id: reverify_health
type: io.kestra.plugin.elasticsearch.Request
description: Re-read per-index health so the result is confirmed against the
cluster itself.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "_cluster/health/{{ inputs.index_pattern }}"
parameters:
level: "indices"
expand_wildcards: "open,hidden"
wait_for_no_initializing_shards: "true"
timeout: "30s"
- id: reverify_settings
type: io.kestra.plugin.elasticsearch.Request
description: Re-read the configured replica counts so the new values are on the
record.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "{{ inputs.index_pattern
}}/_settings/index.number_of_replicas,index.hidden"
parameters:
flat_settings: "true"
expand_wildcards: "open,hidden"
- id: verify_plan
type: io.kestra.plugin.core.output.OutputValues
description: >-
Keep the flagged indices that still carry unassigned shards or
still sit above the floor. An index that rolled in during the
pass is excluded by design.
values:
still_json: >-
{{ {'t': outputs.plan.values.targets_json, 'h':
outputs.reverify_health.response} | toJson | jq('. as $r |
($r.t | fromjson) as $t | [$t[] | . as $x | (($r.h.indices
// {})[$x.index] // {}) as $a |
select((($a.unassigned_shards // 0) > 0) or
(($a.number_of_replicas // 0) > $x.floor)) | {index:
$x.index, replicas: ($a.number_of_replicas // -1),
unassigned: ($a.unassigned_shards // -1), status: ($a.status
// "gone")}]') | first | toJson }}
still_names: >-
{{ {'t': outputs.plan.values.targets_json, 'h':
outputs.reverify_health.response} | toJson | jq('. as $r |
($r.t | fromjson) as $t | [$t[] | . as $x | (($r.h.indices
// {})[$x.index] // {}) as $a |
select((($a.unassigned_shards // 0) > 0) or
(($a.number_of_replicas // 0) > $x.floor)) | ($x.index + "
(replicas " + (($a.number_of_replicas // -1)|tostring) + ",
" + (($a.unassigned_shards // -1)|tostring) + " unassigned,
" + (($a.status // "gone")|tostring) + ")")] | join(" | ")')
| first }}
after_replicas: >-
{{ {'t': outputs.plan.values.targets_json, 's':
outputs.reverify_settings.response} | toJson | jq('. as $r |
($r.t | fromjson) as $t | [$t[] | .index + "=" +
((($r.s[.index].settings["index.number_of_replicas"]) //
"gone")|tostring)] | join(" | ")') | first }}
- id: verify_report
type: io.kestra.plugin.core.log.Log
description: Record the state after the pass so the audit trail carries the
result.
message: |
After the pass on pattern {{ inputs.index_pattern }}.
Configured replicas now: {{ outputs.verify_plan.values.after_replicas }}.
Cluster status {{ outputs.reverify_health.response | jq('.status') | first }} with {{ outputs.reverify_health.response | jq('.unassigned_shards') | first }} unassigned shards.
Still unhealthy of the {{ outputs.plan.values.targets_json | jq('length') | first }} flagged: {{ outputs.verify_plan.values.still_json | jq('length') | first }}. {{ outputs.verify_plan.values.still_names }}
- id: verify_result
type: io.kestra.plugin.core.flow.If
description: Pass only when every flagged index sits at the floor with no
unassigned shards left, otherwise fail loudly.
condition: "{{ outputs.reverify_health.status != 200 or
outputs.reverify_settings.status != 200 or
(outputs.verify_plan.values.still_json | jq('length') | first)
> 0 }}"
then:
- id: remediation_failed
type: io.kestra.plugin.core.execution.Fail
description: Fail when an index is still short of healthy so a partial fix is
never reported as green.
errorMessage: >-
{{ outputs.verify_plan.values.still_json | jq('length') |
first }} of {{ outputs.plan.values.targets_json |
jq('length') | first }} flagged index(es) still carry
unassigned shards or sit above the floor: {{
outputs.verify_plan.values.still_names }}. Re-read
statuses health {{ outputs.reverify_health.status }}
settings {{ outputs.reverify_settings.status }}. An
allocation filter, a shard allocation setting or a
rejected update can hold an index back, lowering replicas
alone does not fix those.
else:
- id: remediated
type: io.kestra.plugin.core.log.Log
description: Record that every flagged index is at the floor and fully
allocated.
message: >-
Lowered and verified. {{ outputs.plan.values.targets_json
| jq('length') | first }} index(es) were above the
placeable floor of {{ outputs.plan.values.floor }}, 0 of
them after. Configured replicas now {{
outputs.verify_plan.values.after_replicas }}. Cluster
status {{ outputs.reverify_health.response | jq('.status')
| first }}.
else:
- id: not_approved
type: io.kestra.plugin.core.log.Log
description: Record that approval was withheld so nothing changed.
message: >-
Approval not granted, so no replica count was changed. {{
outputs.plan.values.targets_json | jq('length') | first }}
index(es) still ask for more replicas than the cluster can
place: {{ outputs.plan.values.target_names }}.
else:
- id: refuse_report
type: io.kestra.plugin.core.log.Log
description: Record that the flow refused to act because the cluster is short of
data nodes.
message: >-
Refusing to lower any replica count. The cluster reports {{
outputs.plan.values.data_nodes }} data node(s) ({{
outputs.plan.values.node_count }} in the roster) against
min_data_nodes {{ inputs.min_data_nodes }}. A missing node shrinks
the floor, so this looks like a restart rather than an
over-provisioned index.
- id: fail_min_nodes
type: io.kestra.plugin.core.execution.Fail
description: Fail so the alert fires, acting here would drop redundancy the real
topology supports.
errorMessage: >-
Only {{ outputs.plan.values.data_nodes }} of min_data_nodes {{
inputs.min_data_nodes }} data nodes are present, so the placeable
floor is not trustworthy. Bring the missing data node(s) back,
then re-run.
else:
- id: report_only
type: io.kestra.plugin.core.log.Log
description: Record a no-change outcome, either nothing is flagged or the run is
in report mode.
message: >-
No change made on pattern {{ inputs.index_pattern }}. Flagged {{
outputs.plan.values.targets_json | jq('length') | first }}, dry_run {{
inputs.dry_run }}, auto_remediate {{ inputs.auto_remediate }}.
errors:
- id: alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert Slack on any failure so a refused pass or a replica count
that stayed unplaceable is never silent.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "elasticsearch-overprovisioned-replica-yellow-guard failed in flow {{ flow.id }} (execution {{ execution.id }}) on pattern {{ inputs.index_pattern }}. One of these happened: the cluster could not be read, the data node count fell below min_data_nodes, a flagged index still carries unassigned shards after the pass. Check the execution logs."}