Skip to content

Commit

Permalink
Switch to awk for zone and node name extraction
Browse files Browse the repository at this point in the history
With the move to AlmaLinux 10 base image the rev util is not present
anymore.
  • Loading branch information
goat-ssh committed Feb 11, 2025
1 parent ae141f2 commit 2b91b77
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Unreleased
stopped gracefully.
* Change nginx ``proxy-body-size`` annotation value as it has stricter validations now
* Bump ``sql_exporter`` to ``0.17.0`` and fix ``cluster_last_user_activity`` NULL warning.
* Switch to ``awk`` for zone and node name extraction.

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

if cluster_settings:
for k, v in cluster_settings.items():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def test_node_name(self, random_string):
crate_version="4.6.3",
)
assert (
f"-Cnode.name={crate_node_name_prefix}$(hostname | rev | cut -d- -f1 | rev)"
f"-Cnode.name={crate_node_name_prefix}$(hostname | awk -F- '{{print $NF}}')"
in cmd
)
assert f"-Cnode.attr.node_name={node_name}" in cmd
Expand Down Expand Up @@ -661,7 +661,7 @@ def test_node_and_cluster_settings_may_override(self):
(
CloudProvider.GCP,
"'http://169.254.169.254/computeMetadata/v1/instance/zone'",
" -H 'Metadata-Flavor: Google' | rev | cut -d '/' -f 1 | rev",
" -H 'Metadata-Flavor: Google' | awk -F'/' '{print $NF}'",
),
],
)
Expand Down

0 comments on commit 2b91b77

Please sign in to comment.