Skip to content

Commit 2b91b77

Browse files
committed
Switch to awk for zone and node name extraction
With the move to AlmaLinux 10 base image the rev util is not present anymore.
1 parent ae141f2 commit 2b91b77

File tree

3 files changed

+5
-20
lines changed

3 files changed

+5
-20
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Unreleased
88
stopped gracefully.
99
* Change nginx ``proxy-body-size`` annotation value as it has stricter validations now
1010
* Bump ``sql_exporter`` to ``0.17.0`` and fix ``cluster_last_user_activity`` NULL warning.
11+
* Switch to ``awk`` for zone and node name extraction.
1112

1213
2.43.1 (2025-01-08)
1314
-------------------

crate/operator/create.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -437,23 +437,7 @@ def get_statefulset_crate_command(
437437
settings = {
438438
"-Cstats.enabled": "true",
439439
"-Ccluster.name": cluster_name,
440-
# This is a clever way of doing string split in SH and picking the last
441-
# item. Here's how it works:
442-
#
443-
# `hostname` is e.g. `crate-data-hot-11111111-1111-1111-1111-111111111111-12`
444-
# for a StatefulSet that has at least 13 replicas (hence the 12 a the
445-
# end). What we want now is to get the `12` from the end. In Bash, one
446-
# would do `${$(hostname)##*-}` to do a greedy prefix removal. However,
447-
# such string manipulations don't exist in SH.
448-
# We can, however, make use of the `cut` command that allows splitting
449-
# a string at an arbitrary delimiter and allows picking a field.
450-
# However, fields can only be picked from the beginning; there's no
451-
# negative indexing to get the last field.
452-
# Now, by reversing the hostname, then taking the first field, we get
453-
# `21`. We can again reverse that to get what we want.
454-
#
455-
# https://stackoverflow.com/a/9125818
456-
"-Cnode.name": f"{crate_node_name_prefix}$(hostname | rev | cut -d- -f1 | rev)",
440+
"-Cnode.name": f"{crate_node_name_prefix}$(hostname | awk -F- '{{print $NF}}')",
457441
"-Ccluster.initial_master_nodes": ",".join(master_nodes),
458442
"-Cdiscovery.seed_providers": "srv",
459443
"-Cdiscovery.srv.query": f"_cluster._tcp.crate-discovery-{name}.{namespace}.svc.cluster.local", # noqa
@@ -520,7 +504,7 @@ def get_statefulset_crate_command(
520504
# projects/<account-id>/zones/us-central1-a
521505
settings[
522506
"-Cnode.attr.zone"
523-
] = f"$(curl -s '{url}' -H 'Metadata-Flavor: Google' | rev | cut -d '/' -f 1 | rev)" # noqa
507+
] = f"$(curl -s '{url}' -H 'Metadata-Flavor: Google' | awk -F'/' '{{print $NF}}')" # noqa
524508

525509
if cluster_settings:
526510
for k, v in cluster_settings.items():

tests/test_create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def test_node_name(self, random_string):
430430
crate_version="4.6.3",
431431
)
432432
assert (
433-
f"-Cnode.name={crate_node_name_prefix}$(hostname | rev | cut -d- -f1 | rev)"
433+
f"-Cnode.name={crate_node_name_prefix}$(hostname | awk -F- '{{print $NF}}')"
434434
in cmd
435435
)
436436
assert f"-Cnode.attr.node_name={node_name}" in cmd
@@ -661,7 +661,7 @@ def test_node_and_cluster_settings_may_override(self):
661661
(
662662
CloudProvider.GCP,
663663
"'http://169.254.169.254/computeMetadata/v1/instance/zone'",
664-
" -H 'Metadata-Flavor: Google' | rev | cut -d '/' -f 1 | rev",
664+
" -H 'Metadata-Flavor: Google' | awk -F'/' '{print $NF}'",
665665
),
666666
],
667667
)

0 commit comments

Comments
 (0)