-
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6821603
MK8S-184 - Add restart script to react when control plane ingress change
ChengYanJin 52eb865
MK8S-184 - Add label to control plane ingress secret
ChengYanJin 2a4c95e
MK8S-184 - Add a mock for Salt cp.get_file_str
ChengYanJin 092df26
MK8S-184 - Add the new sls file in the salt tree
ChengYanJin 42b899c
MK8S-184 - Add test for restart script
ChengYanJin bcc8970
MK8S-197 - improve the mock
ChengYanJin 09d35e8
MK8S-184 - Improve the Python script
ChengYanJin edbd810
MK8S-184 - Rename POD_NAMESPACE to DEPLOYMENT_NAMESPACE
ChengYanJin 3b1e212
MK8S-184 - Remove shell wrapper, execute Python script directly via s…
ChengYanJin ab2f406
MK8S-184 - Move the test to a scripts folder
ChengYanJin 60f0f10
MK8S-184 - Use modern Python library pathlib.Path instead of os.path
ChengYanJin 377a07a
MK8S-184 - Add __init__ in the unit/script folder
ChengYanJin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
salt/metalk8s/addons/prometheus-operator/deployed/files/restart-on-ca-change.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env python3 | ||
| import hashlib | ||
| import json | ||
| import os | ||
| import ssl | ||
| import sys | ||
| import urllib.error | ||
| import urllib.request | ||
| from datetime import datetime, timezone | ||
|
|
||
| CA_DIR = "/tmp/secrets" | ||
| HASH_FILE = os.path.join(CA_DIR, ".ca-hash-previous") | ||
|
|
||
|
|
||
| def hash_dir(path): | ||
| h = hashlib.sha256() | ||
| for fname in sorted(os.listdir(path)): | ||
| if fname.startswith("."): | ||
| continue | ||
| fpath = os.path.join(path, fname) | ||
| if os.path.isfile(fpath): | ||
| with open(fpath, "rb") as f: | ||
| h.update(fname.encode()) | ||
| h.update(f.read()) | ||
| return h.hexdigest() | ||
|
|
||
|
|
||
| def trigger_restart(namespace, deployment): | ||
| with open("/var/run/secrets/kubernetes.io/serviceaccount/token") as f: | ||
| token = f.read() | ||
| timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") | ||
| body = json.dumps( | ||
| { | ||
| "spec": { | ||
| "template": { | ||
| "metadata": { | ||
| "annotations": {"kubectl.kubernetes.io/restartedAt": timestamp} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ).encode() | ||
| ctx = ssl.create_default_context( | ||
| cafile="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" | ||
| ) | ||
| url = ( | ||
| f"https://kubernetes.default.svc/apis/apps/v1" | ||
| f"/namespaces/{namespace}/deployments/{deployment}" | ||
| ) | ||
| req = urllib.request.Request( | ||
| url, | ||
| data=body, | ||
| method="PATCH", | ||
| headers={ | ||
| "Authorization": "Bearer " + token, | ||
| "Content-Type": "application/strategic-merge-patch+json", | ||
| }, | ||
| ) | ||
| urllib.request.urlopen(req, context=ctx) | ||
|
|
||
|
|
||
| def main(): | ||
| if not [f for f in os.listdir(CA_DIR) if not f.startswith(".")]: | ||
| print("CA directory empty, skipping") | ||
| return | ||
|
|
||
| current_hash = hash_dir(CA_DIR) | ||
|
|
||
| if not os.path.exists(HASH_FILE): | ||
| with open(HASH_FILE, "w") as f: | ||
| f.write(current_hash) | ||
| print("Initial CA load, skipping restart") | ||
| return | ||
|
|
||
| with open(HASH_FILE) as f: | ||
| previous_hash = f.read().strip() | ||
|
|
||
| if current_hash == previous_hash: | ||
| return | ||
|
|
||
| namespace = os.environ["POD_NAMESPACE"] | ||
| deployment = os.environ["DEPLOYMENT_NAME"] | ||
|
|
||
| try: | ||
| trigger_restart(namespace, deployment) | ||
| except urllib.error.URLError as e: | ||
| print( | ||
| f"Failed to trigger restart for {deployment}: {e}", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| # Persist hash only after successful restart | ||
| with open(HASH_FILE, "w") as f: | ||
| f.write(current_hash) | ||
| print(f"Rolling restart triggered for {deployment}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
TeddyAndrieux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
salt/metalk8s/addons/prometheus-operator/deployed/oidc-proxy-restart-script.sls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| {%- 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, first=True) }} | ||
TeddyAndrieux marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| restart-on-ca-change.sh: | | ||
| python3 /scripts/restart-on-ca-change.py | ||
TeddyAndrieux marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| {%- 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 %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.