-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from rundeck-plugins/debug-ephemeral-container
RUN-1525: Add plugin for Ephemeral Debug Container
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,78 @@ | ||
#!/usr/bin/env python -u | ||
import logging | ||
import sys | ||
import os | ||
import common | ||
import json | ||
|
||
from kubernetes import client | ||
from kubernetes.client.rest import ApiException | ||
from kubernetes import watch | ||
|
||
logging.basicConfig(stream=sys.stdout, level=logging.INFO, | ||
format='%(message)s') | ||
log = logging.getLogger('kubernetes-model-source') | ||
|
||
if os.environ.get('RD_JOB_LOGLEVEL') == 'DEBUG': | ||
log.setLevel(logging.DEBUG) | ||
|
||
def main(): | ||
|
||
common.connect() | ||
|
||
container_name = os.environ.get("RD_CONFIG_CONTAINER_NAME") | ||
container_image = os.environ.get("RD_CONFIG_CONTAINER_IMAGE") | ||
|
||
if os.environ.get("RD_CONFIG_TARGET_CONTAINER"): | ||
target_container = os.environ.get("RD_CONFIG_TARGET_CONTAINER") | ||
|
||
try: | ||
v1 = client.CoreV1Api() | ||
|
||
[name, namespace, container] = common.get_core_node_parameter_list() | ||
|
||
if not container: | ||
core_v1 = client.CoreV1Api() | ||
response = core_v1.read_namespaced_pod_status( | ||
name=name, | ||
namespace=namespace, | ||
pretty="True" | ||
) | ||
container = response.spec.containers[0].name | ||
|
||
common.log_pod_parameters(log, {'name': name, 'namespace': namespace, 'container_name': container}) | ||
common.verify_pod_exists(name, namespace) | ||
|
||
# add a debug container to it | ||
body = { | ||
"spec": { | ||
"ephemeralContainers": [ | ||
{ | ||
"name": container_name, | ||
"image": container_image, | ||
"targetContainerName": target_container, | ||
"stdin": True, | ||
"tty": True | ||
} | ||
] | ||
} | ||
} | ||
|
||
response = v1.patch_namespaced_pod_ephemeralcontainers( | ||
name, | ||
namespace, | ||
body, | ||
_preload_content=False) | ||
|
||
if os.environ.get("RD_CONFIG_PRINT_POD_SPEC") == "true": | ||
json_data = json.loads(response.data.decode("utf-8")) | ||
print(json.dumps(json_data, indent=2)) | ||
else: | ||
print("Ephemeral container " + container_name + " successfully added to pod " + name) | ||
|
||
except ApiException: | ||
log.exception("Exception error creating:") | ||
sys.exit(1) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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