-
Notifications
You must be signed in to change notification settings - Fork 47
MK8S-184 - Add restart script to react when control plane ingress change #4822
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
Changes from all commits
6821603
52eb865
2a4c95e
092df26
42b899c
bcc8970
09d35e8
edbd810
3b1e212
ab2f406
60f0f10
377a07a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #!/usr/bin/env python3 | ||
| import hashlib | ||
| import os | ||
| import sys | ||
| from datetime import datetime, timezone | ||
| from pathlib import Path | ||
|
|
||
| import requests | ||
|
|
||
| HASH_FILE_NAME = ".ca-hash-previous" | ||
|
|
||
| SA_TOKEN = Path("/var/run/secrets/kubernetes.io/serviceaccount/token") | ||
| SA_CA = Path("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt") | ||
| K8S_API = "https://kubernetes.default.svc" | ||
|
|
||
|
|
||
| def hash_file(file_path: Path) -> str: | ||
| h = hashlib.sha256() | ||
| h.update(file_path.read_bytes()) | ||
| return h.hexdigest() | ||
|
|
||
|
|
||
| def trigger_restart(namespace: str, deployment: str) -> None: | ||
| token = SA_TOKEN.read_text() | ||
| timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") | ||
| body = { | ||
| "spec": { | ||
| "template": { | ||
| "metadata": { | ||
| "annotations": {"kubectl.kubernetes.io/restartedAt": timestamp} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| url = f"{K8S_API}/apis/apps/v1/namespaces/{namespace}/deployments/{deployment}" | ||
| response = requests.patch( | ||
| url, | ||
| json=body, | ||
| headers={ | ||
| "Authorization": f"Bearer {token}", | ||
| "Content-Type": "application/strategic-merge-patch+json", | ||
| }, | ||
| verify=SA_CA, | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
|
|
||
| def main() -> None: | ||
| ca_dir = Path(os.environ["CA_DIR"]) | ||
| ca_file = ca_dir / os.environ["CA_FILE_NAME"] | ||
| hash_file_path = ca_dir / HASH_FILE_NAME | ||
|
|
||
| if not ca_file.exists(): | ||
| print(f"CA file {ca_file} does not exist, skipping") | ||
| return | ||
|
|
||
| current_hash = hash_file(ca_file) | ||
|
|
||
| if not hash_file_path.exists(): | ||
| hash_file_path.write_text(current_hash) | ||
| print("Initial CA load, skipping restart") | ||
| return | ||
|
|
||
| previous_hash = hash_file_path.read_text().strip() | ||
|
|
||
| if current_hash == previous_hash: | ||
| return | ||
|
|
||
| namespace = os.environ["DEPLOYMENT_NAMESPACE"] | ||
| deployment = os.environ["DEPLOYMENT_NAME"] | ||
|
|
||
| try: | ||
| trigger_restart(namespace, deployment) | ||
| except requests.RequestException as e: | ||
| print( | ||
| f"Failed to trigger restart for {deployment}: {e}", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| # Persist hash only after successful restart | ||
| hash_file_path.write_text(current_hash) | ||
| print(f"Rolling restart triggered for {deployment}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
TeddyAndrieux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,9 +60,22 @@ Create oauth2-proxy-alertmanager Deployment: | |
| value: secret | ||
| - name: UNIQUE_FILENAMES | ||
| value: "true" | ||
| - name: SCRIPT | ||
| value: /scripts/restart-on-ca-change.py | ||
| - name: DEPLOYMENT_NAMESPACE | ||
| value: metalk8s-monitoring | ||
| - name: DEPLOYMENT_NAME | ||
| value: oauth2-proxy-alertmanager | ||
| - name: CA_DIR | ||
| value: /tmp/secrets | ||
| - name: CA_FILE_NAME | ||
| value: {{ ca_file }} | ||
| volumeMounts: | ||
| - name: secrets-volume | ||
| mountPath: /tmp/secrets | ||
| - name: restart-script | ||
| mountPath: /scripts | ||
| readOnly: true | ||
| containers: | ||
| - name: oauth2-proxy | ||
| image: {{ build_image_name("oauth2-proxy") }} | ||
|
|
@@ -93,6 +106,10 @@ Create oauth2-proxy-alertmanager Deployment: | |
| volumes: | ||
| - name: secrets-volume | ||
| emptyDir: {} | ||
| - name: restart-script | ||
| configMap: | ||
| name: oidc-proxy-restart-script | ||
| defaultMode: "0555" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue: defaultMode should be an integer, not a string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same — Claude Code |
||
|
|
||
| Create oauth2-proxy-alertmanager Service: | ||
| metalk8s_kubernetes.object_present: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,9 +59,22 @@ Create oauth2-proxy-prometheus Deployment: | |
| value: secret | ||
| - name: UNIQUE_FILENAMES | ||
| value: "true" | ||
| - name: SCRIPT | ||
| value: /scripts/restart-on-ca-change.py | ||
| - name: DEPLOYMENT_NAMESPACE | ||
| value: metalk8s-monitoring | ||
| - name: DEPLOYMENT_NAME | ||
| value: oauth2-proxy-prometheus | ||
| - name: CA_DIR | ||
| value: /tmp/secrets | ||
| - name: CA_FILE_NAME | ||
| value: {{ ca_file }} | ||
| volumeMounts: | ||
| - name: secrets-volume | ||
| mountPath: /tmp/secrets | ||
| - name: restart-script | ||
| mountPath: /scripts | ||
| readOnly: true | ||
| containers: | ||
| - name: oauth2-proxy | ||
| image: {{ build_image_name("oauth2-proxy") }} | ||
|
|
@@ -92,6 +105,10 @@ Create oauth2-proxy-prometheus Deployment: | |
| volumes: | ||
| - name: secrets-volume | ||
| emptyDir: {} | ||
| - name: restart-script | ||
| configMap: | ||
| name: oidc-proxy-restart-script | ||
| defaultMode: "0555" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defaultMode in Kubernetes expects an integer, not a string. 0555 quoted is a YAML string and may be passed as-is to the API, where it could be rejected or misinterpreted. Use the unquoted octal literal 0555 (YAML interprets this as decimal 365) or just 365. The existing thanos-chart.sls uses defaultMode: 420 (integer) as a reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
— Claude Code |
||
|
|
||
| Create oauth2-proxy-prometheus Service: | ||
| metalk8s_kubernetes.object_present: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| {%- set prometheus_defaults = salt.slsutil.renderer( | ||
| 'salt://metalk8s/addons/prometheus-operator/config/prometheus.yaml', | ||
| saltenv=saltenv | ||
| ) | ||
| %} | ||
|
|
||
| {%- set prometheus = salt.metalk8s_service_configuration.get_service_conf( | ||
| 'metalk8s-monitoring', 'metalk8s-prometheus-config', prometheus_defaults | ||
| ) | ||
| %} | ||
|
|
||
| {%- set alertmanager_defaults = salt.slsutil.renderer( | ||
| 'salt://metalk8s/addons/prometheus-operator/config/alertmanager.yaml', | ||
| saltenv=saltenv | ||
| ) | ||
| %} | ||
|
|
||
| {%- set alertmanager = salt.metalk8s_service_configuration.get_service_conf( | ||
| 'metalk8s-monitoring', 'metalk8s-alertmanager-config', alertmanager_defaults | ||
| ) | ||
| %} | ||
|
|
||
| {%- set prometheus_oidc_enabled = prometheus.spec.get('config', {}).get('enable_oidc_authentication', False) %} | ||
| {%- set alertmanager_oidc_enabled = alertmanager.spec.get('config', {}).get('enable_oidc_authentication', False) %} | ||
|
|
||
| {%- if prometheus_oidc_enabled or alertmanager_oidc_enabled %} | ||
|
|
||
| {%- set script_content = salt['cp.get_file_str']( | ||
| 'salt://metalk8s/addons/prometheus-operator/deployed/files/restart-on-ca-change.py', | ||
| saltenv=saltenv | ||
| ) | ||
| %} | ||
|
|
||
| Create oidc-proxy-restart-script ConfigMap: | ||
| metalk8s_kubernetes.object_present: | ||
| - manifest: | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: oidc-proxy-restart-script | ||
| namespace: metalk8s-monitoring | ||
| labels: | ||
| app.kubernetes.io/managed-by: salt | ||
| app.kubernetes.io/part-of: metalk8s | ||
| heritage: metalk8s | ||
| data: | ||
| restart-on-ca-change.py: |- | ||
| {{ script_content | indent(12) }} | ||
|
|
||
| {%- else %} | ||
|
|
||
| Ensure oidc-proxy-restart-script ConfigMap does not exist: | ||
| metalk8s_kubernetes.object_absent: | ||
| - name: oidc-proxy-restart-script | ||
| - namespace: metalk8s-monitoring | ||
| - kind: ConfigMap | ||
| - apiVersion: v1 | ||
|
|
||
| {%- endif %} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.