Skip to content

Commit

Permalink
[PLAT-16268] Adding exported instance environment variable in k8s_par…
Browse files Browse the repository at this point in the history
…ent.py

Summary:
[PLAT-16268] Adding exported instance environment variable in k8s_parent.py
Following this we will add the chart changes to use this environment variable

Test Plan: phabricator

Reviewers: sanketh, amalyshev, vkumar

Reviewed By: sanketh, amalyshev, vkumar

Differential Revision: https://phorge.dev.yugabyte.com/D41794
  • Loading branch information
amannijhawan committed Feb 19, 2025
1 parent f6bf364 commit ba02465
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions scripts/k8s_parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,39 @@ def is_master(command):
return False


def parse_zone_from_flags():
infile = GFLAGS_MASTER_TEMPLATE_FILE if is_master(command) else GFLAGS_TSERVER_TEMPLATE_FILE
with open(infile, 'r') as instream:
content = instream.readlines()
for line in content:
if "--placement_zone" in line:
zone = line.split("=")[-1].strip()
return zone


def set_exported_instance_env(command):
try:
if is_master(command):
pod_type = "yb-master"
else:
pod_type = "yb-tserver"
ordinal_index = os.getenv("HOSTNAME").split("-")[-1]
availability_zone = parse_zone_from_flags()
# if we cannot find zone for some reason, we should just call it yb-tserver-0
# in multi-zone deployments, we will have to format it like yb-tserver-0_us-west1-ae
if availability_zone.strip():
# format yb-tserver-0_us-west1-a"
os.environ["EXPORTED_INSTANCE"] = "%s-%s_%s" % (
pod_type, ordinal_index, availability_zone)
else:
os.environ["EXPORTED_INSTANCE"] = "%s-%s" % (pod_type, ordinal_index)
except Exception as e:
logging.error("Error while setting exported instance env: {}, traceback: {}".format(
e, traceback.format_exc()))
# set it back to hostname in case there is an exception
os.environ["EXPORTED_INSTANCE"] = os.getenv("HOSTNAME")


if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s [%(levelname)s] %(filename)s: %(message)s",
Expand All @@ -272,6 +305,8 @@ def is_master(command):
core_collection_interval = 30 # Seconds
subs_env_gflags_interval = 20 # Seconds
command = sys.argv[1:]
set_exported_instance_env(command)

if len(command) < 1:
logging.critical("No command to run")
sys.exit(1)
Expand Down

0 comments on commit ba02465

Please sign in to comment.