Post-Deploy Automation Must Treat Already Configured as Success
Kubernetes makes pods exist, but many applications inside them still need setup through their own web UI or API: an initial admin, a hostname, links between apps. A Python script that calls those APIs after the pods are healthy closes the gap, provided a second run doesn't fail on what the first run already did.
Bridging Declarative Pods and Imperative Apps
The cluster is declarative; the app's setup endpoint is imperative: "configure this" works once, then answers with an error. Python fits here better than Bash because requests handles HTTP and JSON natively and the script can branch on the response.
Read What the App Generated
Some apps create their API key on first boot and store it in their own config file, so it can't be put in a Secret beforehand. The script reads it from the running pod:
import subprocess
import xml.etree.ElementTree as ET
def get_api_key(namespace: str, app: str) -> str:
result = subprocess.run(
["kubectl", "exec", "-n", namespace, f"deploy/{app}", "--",
"cat", "/config/config.xml"],
capture_output=True, text=True, check=True,
)
return ET.fromstring(result.stdout).findtext("ApiKey")
Make the Setup Call Idempotent
response = requests.post(f"{app_url}/api/v1/setup", json=payload, timeout=10)
if response.status_code == 500 and "already configured" in response.text:
print("Already configured") # expected on every run after the first
else:
response.raise_for_status()
References
- Kubernetes MOC - hub for Kubernetes notes.
- Idempotent Scripts Check the Current State Before Changing It - the same rule applied to Bash.
- Declarative Tools Converge on a Desired State, Imperative Scripts Replay Steps - why the pods and the apps behave differently.
- Python documentation: subprocess security considerations - why
shell=Trueis risky. - Requests documentation: Quickstart - JSON bodies, status codes and timeouts.
Review cards
Flashcard Why must a post-deploy setup script treat the app's 'already configured' answer as success?
The setup call works once and errors afterwards. Without that rule, every run after the first fails, and the script can't run on each deploy.
Flashcard Why does the setup script read the app's API key from the running pod instead of a Secret?
The app generates the key on first boot and stores it in its own config file, so the key doesn't exist beforehand. The script reads it with kubectl exec.