Skip to content

Commit 00486bb

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 00486bb

File tree

2 files changed

+3
-19
lines changed

2 files changed

+3
-19
lines changed

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: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)