Skip to content

Commit 82035ed

Browse files
author
Roey Prat
committed
RED-32089 Removing globals from python code.
This makes the flow of data more predictable and readable. (cherry picked from commit 6a57b4e)
1 parent bb1c54a commit 82035ed

File tree

1 file changed

+34
-57
lines changed

1 file changed

+34
-57
lines changed

log_collector.py

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
logger = logging.getLogger("log collector")
2121

22-
output_dir = ""
23-
namespace = ""
2422
TIME_FORMAT = time.strftime("%Y%m%d-%H%M%S")
2523
dir_name = "redis_enterprise_k8s_debug_info_{}".format(TIME_FORMAT)
2624

@@ -37,55 +35,45 @@
3735

3836
def make_dir(directory):
3937
if not os.path.exists(directory):
38+
# noinspection PyBroadException
4039
try:
4140
os.mkdir(directory)
4241
except:
4342
logger.exception("Could not create directory %s - exiting", directory)
4443
sys.exit()
4544

4645

47-
def run(configured_namespace, configured_output_path):
48-
global output_dir, namespace
49-
50-
namespace = ""
51-
if configured_namespace:
52-
namespace = configured_namespace
53-
else:
46+
def run(namespace, output_dir):
47+
if not namespace:
5448
namespace = get_namespace_from_config()
5549

56-
global TIME_FORMAT
57-
global dir_name
58-
59-
if configured_output_path:
60-
output_dir = os.path.join(configured_output_path, dir_name)
61-
else:
50+
if not output_dir:
6251
output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), dir_name)
6352

6453
make_dir(output_dir)
6554

66-
get_redis_enterprise_debug_info()
67-
collect_cluster_info()
68-
collect_resources_list()
69-
collect_events()
70-
collect_api_resources()
71-
collect_pods_logs()
72-
archive_files()
55+
get_redis_enterprise_debug_info(namespace, output_dir)
56+
collect_cluster_info(output_dir)
57+
collect_resources_list(output_dir)
58+
collect_events(namespace, output_dir)
59+
collect_api_resources(namespace, output_dir)
60+
collect_pods_logs(namespace, output_dir)
61+
archive_files(output_dir)
7362
logger.info("Finished Redis Enterprise log collector")
7463

7564

76-
def get_redis_enterprise_debug_info():
65+
def get_redis_enterprise_debug_info(namespace, output_dir):
7766
"""
7867
Connects to an RS cluster node, creates and copies debug info package from the pod
7968
"""
80-
pod_names = get_pod_names(selector='redis.io/role=node')
69+
pod_names = get_pod_names(namespace, selector='redis.io/role=node')
8170
if not pod_names:
8271
logger.warning("Cannot find redis enterprise pod")
8372
return
8473

8574
pod_name = pod_names[0]
8675

87-
cmd = "kubectl {} exec {} /opt/redislabs/bin/rladmin cluster debug_info path /tmp".format(
88-
get_namespace_argument(), pod_name)
76+
cmd = "kubectl -n {} exec {} /opt/redislabs/bin/rladmin cluster debug_info path /tmp".format(namespace, pod_name)
8977
rc, out = run_shell_command(cmd)
9078
if "Downloading complete" not in out:
9179
logger.warning("Failed running rladmin command in pod: {}".format(out))
@@ -103,7 +91,7 @@ def get_redis_enterprise_debug_info():
10391
return
10492

10593
# copy package from RS pod
106-
cmd = "kubectl {} cp {}:{} {}".format(get_namespace_argument(), pod_name, debug_file, output_dir)
94+
cmd = "kubectl -n {} cp {}:{} {}".format(namespace, pod_name, debug_file, output_dir)
10795
rc, out = run_shell_command(cmd)
10896
if rc:
10997
logger.warning(
@@ -113,66 +101,64 @@ def get_redis_enterprise_debug_info():
113101
logger.info("Collected Redis Enterprise cluster debug package")
114102

115103

116-
def collect_resources_list():
104+
def collect_resources_list(output_dir):
117105
"""
118106
Prints the output of kubectl get all to a file
119107
"""
120-
collect_helper(cmd="kubectl get all", file_name="resources_list", resource_name="resources list")
108+
collect_helper(output_dir, cmd="kubectl get all", file_name="resources_list", resource_name="resources list")
121109

122110

123-
def collect_cluster_info():
111+
def collect_cluster_info(output_dir):
124112
"""
125113
Prints the output of kubectl cluster-info to a file
126114
"""
127-
collect_helper(cmd="kubectl cluster-info", file_name="cluster_info", resource_name="cluster-info")
115+
collect_helper(output_dir, cmd="kubectl cluster-info", file_name="cluster_info", resource_name="cluster-info")
128116

129117

130-
def collect_events():
118+
def collect_events(namespace, output_dir):
131119
"""
132120
Prints the output of kubectl cluster-info to a file
133121
"""
134-
global output_dir
135122
# events need -n parameter in kubectl
136123
if not namespace:
137124
logger.warning("Cannot collect events without namespace - skipping events collection")
138125
return
139-
cmd = "kubectl get events {}".format(get_namespace_argument())
140-
collect_helper(cmd=cmd, file_name="events", resource_name="events")
126+
cmd = "kubectl get events -n {}".format(namespace)
127+
collect_helper(output_dir, cmd=cmd, file_name="events", resource_name="events")
141128

142129

143-
def collect_api_resources():
130+
def collect_api_resources(namespace, output_dir):
144131
"""
145132
Creates file for each of the API resources with the output of kubectl get <resource> -o yaml
146133
"""
147134
logger.info("Collecting API resources:")
148135
resources_out = OrderedDict()
149136
for resource in api_resources:
150-
output = run_kubectl_get(resource)
137+
output = run_kubectl_get(namespace, resource)
151138
if output:
152-
resources_out[resource] = run_kubectl_get(resource)
139+
resources_out[resource] = run_kubectl_get(namespace, resource)
153140
logger.info(" + {}".format(resource))
154141

155142
for entry, out in resources_out.iteritems():
156143
with open(os.path.join(output_dir, "{}.yaml".format(entry)), "w+") as fp:
157144
fp.write(out)
158145

159146

160-
def collect_pods_logs():
147+
def collect_pods_logs(namespace, output_dir):
161148
"""
162149
Collects all the pods logs from given namespace
163150
"""
164-
global output_dir
165151
logger.info("Collecting pods' logs:")
166152
logs_dir = os.path.join(output_dir, "pods")
167153
make_dir(logs_dir)
168154

169-
pods = get_pod_names()
155+
pods = get_pod_names(namespace)
170156
if not pods:
171157
logger.warning("Could not get pods list - skipping pods logs collection")
172158
return
173159

174160
for pod in pods:
175-
cmd = "kubectl logs {} {}".format(get_namespace_argument(), pod)
161+
cmd = "kubectl logs -n {} {}".format(namespace, pod)
176162
with open(os.path.join(logs_dir, "{}.log".format(pod)), "w+") as fp:
177163
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
178164
while True:
@@ -185,8 +171,7 @@ def collect_pods_logs():
185171
logger.info(" + {}".format(pod))
186172

187173

188-
def archive_files():
189-
global dir_name
174+
def archive_files(output_dir):
190175
file_name = output_dir + ".tar.gz"
191176

192177
with tarfile.open(file_name, "w|gz") as tar:
@@ -199,13 +184,13 @@ def archive_files():
199184
logger.warning("Failed to delete directory after archiving: %s", e)
200185

201186

202-
def get_pod_names(selector=""):
187+
def get_pod_names(namespace, selector=""):
203188
"""
204189
Returns list of pods names
205190
"""
206191
if selector:
207192
selector = '--selector="{}"'.format(selector)
208-
cmd = 'kubectl get pod {} {} -o json '.format(get_namespace_argument(), selector)
193+
cmd = 'kubectl get pod -n {} {} -o json '.format(namespace, selector)
209194
rc, out = run_shell_command(cmd)
210195
if rc:
211196
logger.warning("Failed to get pod names: {}".format(out))
@@ -215,13 +200,6 @@ def get_pod_names(selector=""):
215200
return [pod['metadata']['name'] for pod in pods_json['items']]
216201

217202

218-
def get_namespace_argument():
219-
global namespace
220-
if namespace:
221-
return "-n {}".format(namespace)
222-
return ""
223-
224-
225203
def get_namespace_from_config():
226204
"""
227205
Returns the namespace from current context if one is set OW None
@@ -244,11 +222,10 @@ def get_namespace_from_config():
244222
break
245223

246224

247-
def collect_helper(cmd, file_name, resource_name):
225+
def collect_helper(output_dir, cmd, file_name, resource_name):
248226
"""
249227
Runs command, write output to file_name, logs the resource_name
250228
"""
251-
global output_dir
252229
rc, out = run_shell_command(cmd)
253230
if rc:
254231
logger.warning("Error when running {}: {}".format(cmd, out))
@@ -279,11 +256,11 @@ def run_shell_command(cmd):
279256
return 0, native_string(output)
280257

281258

282-
def run_kubectl_get(resource_type):
259+
def run_kubectl_get(namespace, resource_type):
283260
"""
284261
Runs kubectl get command
285262
"""
286-
cmd = "kubectl get {} {} -o yaml".format(resource_type, get_namespace_argument())
263+
cmd = "kubectl get -n {} {} -o yaml".format(namespace, resource_type)
287264
rc, out = run_shell_command(cmd)
288265
if rc == 0:
289266
return out

0 commit comments

Comments
 (0)