pycodestyle fixes: scenarios/openshift/post_action_regex.py

Signed-off-by: Sandro Bonazzola <sbonazzo@redhat.com>
This commit is contained in:
Sandro Bonazzola
2022-09-07 16:48:58 +02:00
committed by Sandro Bonazzola
parent ec807e3b3a
commit 1a1a9c9bfe
+24 -10
View File
@@ -1,14 +1,17 @@
#!/usr/bin/env python3
import subprocess
import logging
import re
import subprocess
import sys
from kubernetes import client, config
from kubernetes.client.rest import ApiException
import logging
# List all namespaces
def list_namespaces():
"""
List all namespaces
"""
namespaces = []
try:
config.load_kube_config()
@@ -16,17 +19,18 @@ def list_namespaces():
ret = cli.list_namespace(pretty=True)
except ApiException as e:
logging.error(
"Exception when calling \
CoreV1Api->list_namespaced_pod: %s\n"
% e
"Exception when calling CoreV1Api->list_namespaced_pod: %s\n",
e
)
for namespace in ret.items:
namespaces.append(namespace.metadata.name)
return namespaces
# Check if all the watch_namespaces are valid
def check_namespaces(namespaces):
"""
Check if all the watch_namespaces are valid
"""
try:
valid_namespaces = list_namespaces()
regex_namespaces = set(namespaces) - set(valid_namespaces)
@@ -41,7 +45,11 @@ def check_namespaces(namespaces):
break
invalid_namespaces = regex_namespaces - valid_regex
if invalid_namespaces:
raise Exception("There exists no namespaces matching: %s" % (invalid_namespaces))
raise Exception(
"There exists no namespaces matching: %s" % (
invalid_namespaces
)
)
return list(final_namespaces)
except Exception as e:
logging.error("%s" % (e))
@@ -51,7 +59,11 @@ def check_namespaces(namespaces):
def run(cmd):
try:
output = subprocess.Popen(
cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
cmd,
shell=True,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
(out, err) = output.communicate()
except Exception as e:
@@ -63,7 +75,9 @@ regex_namespace = ["openshift-.*"]
namespaces = check_namespaces(regex_namespace)
pods_running = 0
for namespace in namespaces:
new_pods_running = run("oc get pods -n " + namespace + " | grep -c Running").rstrip()
new_pods_running = run(
"oc get pods -n " + namespace + " | grep -c Running"
).rstrip()
try:
pods_running += int(new_pods_running)
except Exception: