Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lifecycle hooks for cloudmap (de)registration operations on vtgate pods #3934

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion paasta_tools/vitesscluster_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class GatewayConfigDict(TypedDict, total=False):
extraEnv: List[Union[KVEnvVar, KVEnvVarValueFrom]]
extraFlags: Dict[str, str]
extraLabels: Dict[str, str]
lifecycle: Dict[str, Dict[str, Dict[str, List[str]]]]
replicas: int
resources: Dict[str, Any]
annotations: Mapping[str, Any]
Expand Down Expand Up @@ -236,6 +237,7 @@ def get_cell_config(
labels: Dict[str, str],
node_affinity: dict,
annotations: Mapping[str, Any],
aws_region: str,
) -> CellConfigDict:
"""
get vtgate config
Expand All @@ -256,6 +258,26 @@ def get_cell_config(
config = CellConfigDict(
name=cell,
gateway=GatewayConfigDict(
lifecycle={
"preStop": {
"exec": {
"command": [
"/bin/sh",
"-c",
f"/cloudmap/scripts/deregister_from_cloudmap.sh vtgate-{cell} {aws_region}",
]
}
},
"postStart": {
"exec": {
"command": [
"/bin/sh",
"-c",
f"/cloudmap/scripts/register_to_cloudmap.sh vtgate-{cell} {aws_region}",
]
}
},
},
affinity={"nodeAffinity": node_affinity},
extraEnv=updated_vtgate_extra_env,
extraFlags={
Expand Down Expand Up @@ -523,7 +545,10 @@ def get_keyspaces_config(
tablet_types = load_system_paasta_config().get_vitess_tablet_types()
for tablet_type in tablet_types:
ecosystem = region.split("-")[-1]
host = f"mysql-{cluster}-{tablet_type}.dre-{ecosystem}"
if tablet_type == "primary":
host = f"mysql-{cluster}.dre-{ecosystem}"
else:
host = f"mysql-{cluster}-{tablet_type}.dre-{ecosystem}"

# We use migration_replication delay for migration tablets and read_replication_delay for everything else
# Also throttling threshold for refresh and sanitized primaries is set at 30 seconds and everything else at 3 seconds
Expand Down Expand Up @@ -712,6 +737,10 @@ def get_region(self) -> str:
sys.exit(1)
return region

def get_aws_region(self) -> str:
region = self.get_region()
return f"{region[:2]}-{region[2:6]}-{region[6:7]}"
Comment on lines +740 to +742
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a tad brittle as this slicing won't work for all AWS regions - it's probably fine for where vitess will be deployed initially, but perhaps we can have the region identification happen inside the script itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can have the region identification happen inside the script itself?
Can you explain this? Or do you mean pass it from yelpsoa?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VinaySagarGonabavi my bad, i meant that the register/deregister script could read /nail/etc/habitat and figure out the region from there - in general, it's probably better to have the script be somewhat independent of how paasta is configuring pods since it's easier/faster to roll out an image change for your service than to do anything else (e.g., you can avoid having to go through a paasta release if the script is called with no or minimal arguments)


def get_global_lock_server(self) -> Dict[str, Dict[str, str]]:
zk_address = self.config_dict.get("zk_address")
return {
Expand All @@ -725,6 +754,7 @@ def get_global_lock_server(self) -> Dict[str, Dict[str, str]]:
def get_cells(self) -> List[CellConfigDict]:
cells = self.config_dict.get("cells")
region = self.get_region()
aws_region = self.get_aws_region()
vtgate_resources = self.config_dict.get("vtgate_resources")

formatted_env = self.get_env_variables()
Expand All @@ -741,6 +771,7 @@ def get_cells(self) -> List[CellConfigDict]:
labels,
node_affinity,
annotations,
aws_region,
)
for cell in cells
]
Expand Down
22 changes: 21 additions & 1 deletion tests/test_vitesscluster_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@
"mysql_auth_vault_ttl": "60s",
},
"extraLabels": {"tablet_type": "fake_keyspaces_migration"},
"lifecycle": {
"postStart": {
"exec": {
"command": [
"/bin/sh",
"-c",
"/cloudmap/scripts/register_to_cloudmap.sh vtgate-fake_cell mo-ck_r-e",
]
}
},
"preStop": {
"exec": {
"command": [
"/bin/sh",
"-c",
"/cloudmap/scripts/deregister_from_cloudmap.sh vtgate-fake_cell mo-ck_r-e",
]
}
},
},
"replicas": 1,
"resources": {
"limits": {"cpu": "100m", "memory": "256Mi"},
Expand Down Expand Up @@ -189,7 +209,7 @@
"volumeName": "vttablet-fake-credentials",
},
"database": "fake_keyspaces",
"host": "mysql-fake_cluster-primary.dre-devc",
"host": "mysql-fake_cluster.dre-devc",
"port": 3306,
"user": "vt_app",
},
Expand Down
Loading