Skip to content

: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.

fix(flags): point the jumphost pool at the golden-chart namespace (#96258)

Gustavo H. Strassburger87a9d66b5d4d
diff --git a/bin/rust-jumphost b/bin/rust-jumphost
index bd6ce2f03498..658da9763843 100755
--- a/bin/rust-jumphost
+++ b/bin/rust-jumphost
@@ -5,9 +5,14 @@
# 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.
+#
# Prerequisites:
# - kubectl context pointing at the target cluster, or pass
-# KUBE_CONTEXT=… (e.g. KUBE_CONTEXT=posthog-dev).
+# KUBE_CONTEXT=… (e.g. KUBE_CONTEXT=dev-eks).
#
# Usage:
# bin/rust-jumphost # claim + connect
diff --git a/hogli.yaml b/hogli.yaml
index e2ed731735b0..477386f69b93 100644
--- a/hogli.yaml
+++ b/hogli.yaml
@@ -1168,7 +1168,7 @@ environment:
hidden: true
flags:jumphost:
bin_script: rust-jumphost
- description: 'Shell into the flags-cache-jumphost pod (uses $KUBE_NAMESPACE, defaults to posthog)'
+ description: 'Shell into the flags-cache-jumphost pod (namespace follows the environment: flags-cache-jumphost on dev, posthog elsewhere; $KUBE_NAMESPACE overrides)'
hidden: true
wait:for:postgres:tables:
bin_script: wait-for-postgres-tables
diff --git a/tools/infra-scripts/clitools/test_toolbox.py b/tools/infra-scripts/clitools/test_toolbox.py
index e8aa782950fe..ef81db5afa65 100644
--- a/tools/infra-scripts/clitools/test_toolbox.py
+++ b/tools/infra-scripts/clitools/test_toolbox.py
@@ -1026,6 +1026,7 @@ class TestToolbox(unittest.TestCase):
toolbox_script.POOLS["flags-cache-jumphost"],
{
"default_namespace": "posthog",
+ "namespace_by_environment": {"dev": "flags-cache-jumphost"},
"app_label": "flags-cache-jumphost",
"claimed_label_key": "flags-jumphost-claimed",
},
@@ -1338,28 +1339,33 @@ class TestToolbox(unittest.TestCase):
patches["delete_pod"],
patches["select_context"],
patches["validate_context"],
+ patch.object(toolbox_script, "ensure_context_access", return_value=True),
patch.object(toolbox_script.sys, "argv", ["toolbox.py", "--pool", "flags-cache-jumphost"]),
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.
+ os.environ["KUBE_CONTEXT"] = "dev-eks"
with self.assertRaises(SystemExit) as ctx:
toolbox_script.main()
self.assertEqual(ctx.exception.code, 0)
- m_user.assert_called_once_with(claimed_label_key="flags-jumphost-claimed", context="posthog-dev")
+ m_user.assert_called_once_with(claimed_label_key="flags-jumphost-claimed", context="dev-eks")
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="posthog",
- context="posthog-dev",
+ namespace="flags-cache-jumphost",
+ context="dev-eks",
extra_selector=None,
)
# claim_pod gets namespace, context, and resource_version from get_toolbox_pod's return.
self.assertEqual(
m_claim.call_args.kwargs,
- {"namespace": "posthog", "context": "posthog-dev", "resource_version": "12345"},
+ {"namespace": "flags-cache-jumphost", "context": "dev-eks", "resource_version": "12345"},
)
def test_main_default_pool_dispatches_toolbox_django_kwargs(self):
diff --git a/tools/infra-scripts/clitools/toolbox.py b/tools/infra-scripts/clitools/toolbox.py
index 8de103f7dcbb..b072ac031e5b 100755
--- a/tools/infra-scripts/clitools/toolbox.py
+++ b/tools/infra-scripts/clitools/toolbox.py
@@ -14,7 +14,7 @@ from datetime import datetime, timedelta
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Import functions from the modular package
-from toolbox.kubernetes import ensure_context_access, select_context, validate_context
+from toolbox.kubernetes import TOOLBOX_ENVIRONMENTS, ensure_context_access, select_context, validate_context
from toolbox.pod import ClaimRaceError, claim_pod, connect_to_pod, delete_pod, get_toolbox_pod
from toolbox.tailscale import ensure_tailscale_connected
from toolbox.telemetry import capture_invocation, prompt_for_reason
@@ -40,7 +40,14 @@ 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"},
"app_label": "flags-cache-jumphost",
"claimed_label_key": "flags-jumphost-claimed",
},
@@ -50,6 +57,31 @@ 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.
@@ -107,19 +139,11 @@ 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; `KUBE_NAMESPACE`
+ # 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 = os.environ.get("KUBE_NAMESPACE", pool["default_namespace"])
- # 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.
- extra_selector = pool.get("extra_selectors_by_namespace", {}).get(namespace)
-
- print(f"🛠️ Connecting to {args.pool} pool in namespace {namespace}...") # noqa: T201
-
+ namespace = resolve_namespace(pool, os.environ.get("KUBE_CONTEXT"))
# 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.
@@ -132,8 +156,22 @@ def main():
selected_context = kube_context
else:
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.
+ extra_selector = pool.get("extra_selectors_by_namespace", {}).get(namespace)
+
+ print(f"🛠️ Connecting to {args.pool} pool in namespace {namespace}...") # noqa: T201
+
# Get current user labels
user_labels = get_current_user(claimed_label_key=claimed_label_key, context=selected_context)
print(f"👤 Current user labels: {user_labels}") # noqa: T201