feature/webtail

This commit is contained in:
Luckysideburn
2022-10-23 12:52:57 +00:00
parent 4f9eacf2ba
commit 2ed91b41e6
2 changed files with 20 additions and 11 deletions
+18 -11
View File
@@ -15,11 +15,15 @@ import time
import re
file = pathlib.Path('/tmp/redis.sock')
if file.exists():
r = redis.Redis(unix_socket_path='/tmp/redis.sock', charset="utf-8", decode_responses=True)
else:
r = redis.Redis("127.0.0.1", charset="utf-8", decode_responses=True)
if os.environ.get("DEV"):
r.set("log_pod_regex", ".*")
# create logger
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
logging.info('Starting script for KubeInvaders programming mode')
@@ -43,37 +47,40 @@ namespace = "kubeinvaders"
# r.delete(key)
while True:
pods_items = []
webtail_pods = []
final_pod_list = []
if r.exists("log_pod_regex"):
logging.info("Found regex log_pod_regex in Redis. Logs from all pods should be collected")
log_pod_regex = r.get("log_pod_regex")
try:
api_response = api_instance.list_pod_for_all_namespaces()
except ApiException as e:
logging.info(e)
logging.info(f"Going to search pod compliant with the regex on {len(api_response.items)} pods")
for pod in api_response.items:
if re.search(log_pod_regex, pod.metadata.name):
pods_items.append(pod)
if re.search(f"{log_pod_regex}", pod.metadata.name):
webtail_pods.append(pod)
logging.info(f"Taking log of {pod.metadata.name} because it is compliant with the regex {log_pod_regex}")
try:
api_response = api_instance.list_namespaced_pod(namespace="kubeinvaders")
except ApiException as e:
logging.info(e)
pods_items = pods_items + api_response.items
final_pod_list = webtail_pods + api_response.items
for pod in api_response.items:
if pod.metadata.labels.get('approle') != None and pod.metadata.labels['approle'] == 'chaosnode' and pod.status.phase != "Pending":
for pod in final_pod_list:
if ((pod.metadata.name in webtail_pods) or (pod.metadata.labels.get('approle') != None and pod.metadata.labels['approle'] == 'chaosnode' and pod.status.phase != "Pending")):
try:
logging.info(f"Reading logs of {pod.metadata.name}")
api_response = api_instance.read_namespaced_pod_log(name=pod.metadata.name, namespace='kubeinvaders')
logging.info(f"Reading logs of {pod.metadata.name} on {pod.metadata.namespace}")
api_response = api_instance.read_namespaced_pod_log(name=pod.metadata.name, namespace=pod.metadata.namespace)
logrow = f"<div class='row'><div style='margin-top: 2%; background-color:#400075; color: #e9f0ef; text-align: center;' class='alert' role='alert'>Pod Name: {pod.metadata.name} (Log TTL: 30sec)</div><div style='margin-top: 1.5px; background-color:#000000; color: #e9f0ef; font-size: 12px; font-family: Courier New, Courier, monospace;' class='alert' role='alert'>{api_response}</div></div>"
r.set(f"log:{pod.metadata.name}", logrow)
r.expire(f"log:{pod.metadata.name}", 30)
logging.info(f"Phase of {pod.metadata.name} is {pod.status.phase}")
if pod.status.phase == "Succeeded":
if pod.status.phase == "Succeeded" and pod.metadata.labels['approle'] == 'chaosnode':
try:
api_response = api_instance.delete_namespaced_pod(pod.metadata.name, namespace='kubeinvaders')
api_response = api_instance.delete_namespaced_pod(pod.metadata.name, namespace = pod.metadata.namespace)
logging.info(f"Deleted pod {pod.metadata.name}")
except ApiException as e:
logging.info(e)
+2
View File
@@ -3,4 +3,6 @@ secret=$(kubectl get secret -n kubeinvaders | grep 'service-account-token' | gre
token=$(kubectl describe secret $secret -n kubeinvaders | grep 'token:' | awk '{ print $2}')
ip=$(ip address show eth0 | grep inet | grep -v inet6 | awk '{ print $2 }' | awk -F/ '{ print $1 }')
export TOKEN=$token
export DEV=true
export PYTHONWARNINGS="ignore:Unverified HTTPS request"
python3 start.py https://$ip:6443