:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(toolbox): default the jumphost pool to the golden-chart namespace (#104076)
Gustavo H. Strassburger717689a6f2e7
diff --git a/bin/rust-jumphost b/bin/rust-jumphostindex 658da9763843..e52b08220e33 100755--- a/bin/rust-jumphost+++ b/bin/rust-jumphost@@ -5,10 +5,8 @@# tools/infra-scripts/clitools/toolbox.py with --pool flags-cache-jumphost# and --auto-delete; all the orphan-relabel-claim logic lives there.#-# The namespace follows the environment while the migration to the posthog-app-# golden chart is only partly done (PostHog/charts#14843): dev serves the pool-# from `flags-cache-jumphost`, prod-us and prod-eu still from `posthog`.-# KUBE_NAMESPACE overrides both.+# The pool runs on the posthog-app golden chart, in namespace+# `flags-cache-jumphost`, in every environment. KUBE_NAMESPACE overrides it.## Prerequisites:# - kubectl context pointing at the target cluster, or pass@@ -16,8 +14,8 @@## Usage:# bin/rust-jumphost # claim + connect-# KUBE_CONTEXT=posthog-dev bin/rust-jumphost-# KUBE_NAMESPACE=posthog bin/rust-jumphost+# KUBE_CONTEXT=dev-eks bin/rust-jumphost+# KUBE_NAMESPACE=<namespace> bin/rust-jumphost # override the pool namespace# bin/rust-jumphost --claim-duration 4 # 4h claim window## Inside the pod:diff --git a/hogli.yaml b/hogli.yamlindex 9b1f8494e9de..c273fb1c7efd 100644--- a/hogli.yaml+++ b/hogli.yaml@@ -1221,7 +1221,7 @@ environment:hidden: trueflags:jumphost:bin_script: rust-jumphost- description: 'Shell into the flags-cache-jumphost pod (namespace follows the environment: flags-cache-jumphost on dev, posthog elsewhere; $KUBE_NAMESPACE overrides)'+ description: 'Shell into the flags-cache-jumphost pod (namespace flags-cache-jumphost in every environment; $KUBE_NAMESPACE overrides)'hidden: truewait:for:postgres:tables:bin_script: wait-for-postgres-tablesdiff --git a/tools/infra-scripts/clitools/test_toolbox.py b/tools/infra-scripts/clitools/test_toolbox.pyindex 5b979aeb0a79..7c8c7a005e10 100644--- a/tools/infra-scripts/clitools/test_toolbox.py+++ b/tools/infra-scripts/clitools/test_toolbox.py@@ -1025,54 +1025,12 @@ class TestToolbox(unittest.TestCase):self.assertEqual(toolbox_script.POOLS["flags-cache-jumphost"],{- "default_namespace": "posthog",- "namespace_by_environment": {- "dev": "flags-cache-jumphost",- "prod-eu": "flags-cache-jumphost",- "prod-us": "flags-cache-jumphost",- },+ "default_namespace": "flags-cache-jumphost","app_label": "flags-cache-jumphost","claimed_label_key": "flags-jumphost-claimed",},)- @patch.dict(os.environ, {}, clear=False)- def test_resolve_namespace_uses_golden_namespace_for_migrated_environments(self):- """Every environment now runs the jumphost on the golden chart, in its own namespace."""- os.environ.pop("KUBE_NAMESPACE", None)- pool = toolbox_script.POOLS["flags-cache-jumphost"]-- for context in (- "dev-eks",- "dev-admin",- "prod-eu-eks",- "prod-eu-admin",- "prod-us-eks",- "prod-us-admin",- "prod-eu",- "dev",- "prod-us",- ):- with self.subTest(context=context):- self.assertEqual(toolbox_script.resolve_namespace(pool, context), "flags-cache-jumphost")-- @patch.dict(os.environ, {}, clear=False)- def test_resolve_namespace_keeps_unmigrated_environments_on_default(self):- """A context that names no migrated environment falls back to the default namespace."""- os.environ.pop("KUBE_NAMESPACE", None)- pool = toolbox_script.POOLS["flags-cache-jumphost"]-- for context in (None, "unknown-eks"):- with self.subTest(context=context):- self.assertEqual(toolbox_script.resolve_namespace(pool, context), "posthog")-- @patch.dict(os.environ, {"KUBE_NAMESPACE": "posthog"})- def test_resolve_namespace_honours_kube_namespace_override(self):- """KUBE_NAMESPACE wins over the per-environment mapping, which is the rollback path."""- pool = toolbox_script.POOLS["flags-cache-jumphost"]-- self.assertEqual(toolbox_script.resolve_namespace(pool, "prod-eu-eks"), "posthog")-def test_exit_for_signal_raises_systemexit_for_sigterm(self):"""SIGTERM handler routes through sys.exit so atexit-registered cleanup fires."""with self.assertRaises(SystemExit) as ctx:@@ -1385,9 +1343,7 @@ class TestToolbox(unittest.TestCase):patch.dict(os.environ, {}, clear=False),):self._clean_env()- # The pool is mid-migration, so its namespace depends on the- # environment. Managed contexts are `<environment>-<access suffix>`,- # and dev is the environment already on the golden chart.+ # Managed contexts are `<environment>-<access suffix>`.os.environ["KUBE_CONTEXT"] = "dev-eks"with self.assertRaises(SystemExit) as ctx:toolbox_script.main()@@ -1409,6 +1365,46 @@ class TestToolbox(unittest.TestCase):{"namespace": "flags-cache-jumphost", "context": "dev-eks", "resource_version": "12345"},)+ def test_main_jumphost_pool_without_context_uses_the_pool_namespace(self):+ """With no KUBE_CONTEXT the jumphost namespace is known before the context is chosen.++ The pool has one fixed namespace, so select_context and the pod lookup both receive it.+ """+ patches = self._patch_main_collaborators()+ patches["get_current_user"] = patch.object(+ toolbox_script,+ "get_current_user",+ return_value={"flags-jumphost-claimed": "user_at_posthog.com"},+ )+ patches["select_context"] = patch.object(toolbox_script, "select_context", return_value="prod-eu-eks")++ with (+ patches["get_current_user"],+ patches["get_toolbox_pod"] as m_get_pod,+ patches["claim_pod"],+ patches["connect_to_pod"],+ patches["delete_pod"],+ patches["select_context"] as m_select,+ patches["validate_context"],+ patch.object(toolbox_script.sys, "argv", ["toolbox.py", "--pool", "flags-cache-jumphost"]),+ patch.dict(os.environ, {}, clear=False),+ ):+ self._clean_env()+ with self.assertRaises(SystemExit) as ctx:+ toolbox_script.main()+ self.assertEqual(ctx.exception.code, 0)++ m_select.assert_called_once_with("flags-cache-jumphost")+ m_get_pod.assert_called_once_with(+ "user_at_posthog.com",+ check_claimed=True,+ app_label="flags-cache-jumphost",+ claimed_label_key="flags-jumphost-claimed",+ namespace="flags-cache-jumphost",+ context="prod-eu-eks",+ extra_selector=None,+ )+def test_main_default_pool_dispatches_toolbox_django_kwargs(self):"""--pool toolbox-django (the default) threads the right kwargs per active namespace.diff --git a/tools/infra-scripts/clitools/toolbox.py b/tools/infra-scripts/clitools/toolbox.pyindex 313488c7719e..f2039745e6d5 100755--- a/tools/infra-scripts/clitools/toolbox.py+++ b/tools/infra-scripts/clitools/toolbox.py@@ -14,7 +14,7 @@ from datetime import datetime, timedeltasys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))# Import functions from the modular package-from toolbox.kubernetes import TOOLBOX_ENVIRONMENTS, ensure_context_access, select_context, validate_context+from toolbox.kubernetes import ensure_context_access, select_context, validate_contextfrom toolbox.pod import ClaimRaceError, claim_pod, connect_to_pod, delete_pod, get_toolbox_podfrom toolbox.tailscale import ensure_tailscale_connectedfrom toolbox.telemetry import capture_invocation, prompt_for_reason@@ -40,18 +40,8 @@ POOLS = {},},"flags-cache-jumphost": {- # `namespace_by_environment` names the namespace for each environment- # that has moved to the posthog-app golden chart; `default_namespace`- # covers the rest, and `KUBE_NAMESPACE` overrides both. Add an entry as- # each environment migrates, and collapse this back to a single- # `default_namespace` once every environment is listed- # (PostHog/charts#14843).- "default_namespace": "posthog",- "namespace_by_environment": {- "dev": "flags-cache-jumphost",- "prod-eu": "flags-cache-jumphost",- "prod-us": "flags-cache-jumphost",- },+ # Golden-chart deployment in the per-app namespace, in every environment.+ "default_namespace": "flags-cache-jumphost","app_label": "flags-cache-jumphost","claimed_label_key": "flags-jumphost-claimed",},@@ -61,31 +51,6 @@ POOLS = {MAX_CLAIM_RETRIES = 3-def resolve_namespace(pool: dict, kube_context: str | None) -> str:- """Pick the namespace a pool lives in, honouring per-environment overrides.-- `KUBE_NAMESPACE` wins outright. Otherwise a pool may map an environment to- its own namespace, which is what a partly-finished migration looks like: one- environment on the golden chart in a per-app namespace, the rest still in- `posthog`. Contexts are named `<environment>-<access suffix>`, so the- environment comes from the context when one is set.-- With no context set the caller is prompted for an environment later, so- there is nothing to key on yet. Return the pool's own default, which is the- unmigrated namespace, and let `KUBE_NAMESPACE` cover the rest.- """- if override := os.environ.get("KUBE_NAMESPACE"):- return override-- by_environment = pool.get("namespace_by_environment") or {}- if by_environment and kube_context:- for environment in TOOLBOX_ENVIRONMENTS:- if kube_context.startswith(f"{environment}-") or kube_context == environment:- return by_environment.get(environment, pool["default_namespace"])-- return pool["default_namespace"]--def _exit_for_signal(signum, _frame):"""SIGTERM/SIGHUP handler that routes through sys.exit so atexit-registered cleanup runs.@@ -143,11 +108,10 @@ def main():pool = POOLS[args.pool]app_label = pool["app_label"]claimed_label_key = pool["claimed_label_key"]- # Each pool advertises its own default namespace, and may override it per- # environment while a migration is only partly done. `KUBE_NAMESPACE`- # remains the escape hatch (e.g. to point the toolbox-django pool back- # at the legacy `posthog` namespace during migration).- namespace = resolve_namespace(pool, os.environ.get("KUBE_CONTEXT"))+ # Each pool advertises its own default namespace. `KUBE_NAMESPACE` remains+ # the escape hatch (e.g. to point the toolbox-django pool back at the+ # legacy `posthog` namespace during migration).+ namespace = os.environ.get("KUBE_NAMESPACE") or pool["default_namespace"]# Resolve which kubernetes context to use without ever calling# `kubectl config use-context`. Switching kubeconfig globally would persist past# this script and silently redirect later operational commands.@@ -160,18 +124,13 @@ def main():selected_context = kube_contextelse:selected_context = select_context(namespace)- # The environment was only chosen inside select_context, so a pool- # with per-environment namespaces has to be resolved again now that- # the answer exists.- namespace = resolve_namespace(pool, selected_context)print(f"🔄 Using kubernetes context: {selected_context}") # noqa: T201# The base selector is `app.kubernetes.io/name=<app_label>`. Some# namespaces also host other workloads that share that label (e.g. the# golden chart deploys a per-app pgbouncer under the same name in the# `posthog-toolbox-django` namespace), so we may need a further- # discriminator to pick only the main pool pods. Resolved after the- # context, because the namespace can depend on the chosen environment.+ # discriminator to pick only the main pool pods.extra_selector = pool.get("extra_selectors_by_namespace", {}).get(namespace)print(f"🛠️ Connecting to {args.pool} pool in namespace {namespace}...") # noqa: T201