19
19
20
20
logger = logging .getLogger ("log collector" )
21
21
22
- output_dir = ""
23
- namespace = ""
24
22
TIME_FORMAT = time .strftime ("%Y%m%d-%H%M%S" )
25
23
dir_name = "redis_enterprise_k8s_debug_info_{}" .format (TIME_FORMAT )
26
24
37
35
38
36
def make_dir (directory ):
39
37
if not os .path .exists (directory ):
38
+ # noinspection PyBroadException
40
39
try :
41
40
os .mkdir (directory )
42
41
except :
43
42
logger .exception ("Could not create directory %s - exiting" , directory )
44
43
sys .exit ()
45
44
46
45
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 :
54
48
namespace = get_namespace_from_config ()
55
49
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 :
62
51
output_dir = os .path .join (os .path .dirname (os .path .abspath (__file__ )), dir_name )
63
52
64
53
make_dir (output_dir )
65
54
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 )
73
62
logger .info ("Finished Redis Enterprise log collector" )
74
63
75
64
76
- def get_redis_enterprise_debug_info ():
65
+ def get_redis_enterprise_debug_info (namespace , output_dir ):
77
66
"""
78
67
Connects to an RS cluster node, creates and copies debug info package from the pod
79
68
"""
80
- pod_names = get_pod_names (selector = 'redis.io/role=node' )
69
+ pod_names = get_pod_names (namespace , selector = 'redis.io/role=node' )
81
70
if not pod_names :
82
71
logger .warning ("Cannot find redis enterprise pod" )
83
72
return
84
73
85
74
pod_name = pod_names [0 ]
86
75
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 )
89
77
rc , out = run_shell_command (cmd )
90
78
if "Downloading complete" not in out :
91
79
logger .warning ("Failed running rladmin command in pod: {}" .format (out ))
@@ -103,7 +91,7 @@ def get_redis_enterprise_debug_info():
103
91
return
104
92
105
93
# 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 )
107
95
rc , out = run_shell_command (cmd )
108
96
if rc :
109
97
logger .warning (
@@ -113,66 +101,64 @@ def get_redis_enterprise_debug_info():
113
101
logger .info ("Collected Redis Enterprise cluster debug package" )
114
102
115
103
116
- def collect_resources_list ():
104
+ def collect_resources_list (output_dir ):
117
105
"""
118
106
Prints the output of kubectl get all to a file
119
107
"""
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" )
121
109
122
110
123
- def collect_cluster_info ():
111
+ def collect_cluster_info (output_dir ):
124
112
"""
125
113
Prints the output of kubectl cluster-info to a file
126
114
"""
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" )
128
116
129
117
130
- def collect_events ():
118
+ def collect_events (namespace , output_dir ):
131
119
"""
132
120
Prints the output of kubectl cluster-info to a file
133
121
"""
134
- global output_dir
135
122
# events need -n parameter in kubectl
136
123
if not namespace :
137
124
logger .warning ("Cannot collect events without namespace - skipping events collection" )
138
125
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" )
141
128
142
129
143
- def collect_api_resources ():
130
+ def collect_api_resources (namespace , output_dir ):
144
131
"""
145
132
Creates file for each of the API resources with the output of kubectl get <resource> -o yaml
146
133
"""
147
134
logger .info ("Collecting API resources:" )
148
135
resources_out = OrderedDict ()
149
136
for resource in api_resources :
150
- output = run_kubectl_get (resource )
137
+ output = run_kubectl_get (namespace , resource )
151
138
if output :
152
- resources_out [resource ] = run_kubectl_get (resource )
139
+ resources_out [resource ] = run_kubectl_get (namespace , resource )
153
140
logger .info (" + {}" .format (resource ))
154
141
155
142
for entry , out in resources_out .iteritems ():
156
143
with open (os .path .join (output_dir , "{}.yaml" .format (entry )), "w+" ) as fp :
157
144
fp .write (out )
158
145
159
146
160
- def collect_pods_logs ():
147
+ def collect_pods_logs (namespace , output_dir ):
161
148
"""
162
149
Collects all the pods logs from given namespace
163
150
"""
164
- global output_dir
165
151
logger .info ("Collecting pods' logs:" )
166
152
logs_dir = os .path .join (output_dir , "pods" )
167
153
make_dir (logs_dir )
168
154
169
- pods = get_pod_names ()
155
+ pods = get_pod_names (namespace )
170
156
if not pods :
171
157
logger .warning ("Could not get pods list - skipping pods logs collection" )
172
158
return
173
159
174
160
for pod in pods :
175
- cmd = "kubectl logs {} {}" .format (get_namespace_argument () , pod )
161
+ cmd = "kubectl logs -n {} {}" .format (namespace , pod )
176
162
with open (os .path .join (logs_dir , "{}.log" .format (pod )), "w+" ) as fp :
177
163
p = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
178
164
while True :
@@ -185,8 +171,7 @@ def collect_pods_logs():
185
171
logger .info (" + {}" .format (pod ))
186
172
187
173
188
- def archive_files ():
189
- global dir_name
174
+ def archive_files (output_dir ):
190
175
file_name = output_dir + ".tar.gz"
191
176
192
177
with tarfile .open (file_name , "w|gz" ) as tar :
@@ -199,13 +184,13 @@ def archive_files():
199
184
logger .warning ("Failed to delete directory after archiving: %s" , e )
200
185
201
186
202
- def get_pod_names (selector = "" ):
187
+ def get_pod_names (namespace , selector = "" ):
203
188
"""
204
189
Returns list of pods names
205
190
"""
206
191
if selector :
207
192
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 )
209
194
rc , out = run_shell_command (cmd )
210
195
if rc :
211
196
logger .warning ("Failed to get pod names: {}" .format (out ))
@@ -215,13 +200,6 @@ def get_pod_names(selector=""):
215
200
return [pod ['metadata' ]['name' ] for pod in pods_json ['items' ]]
216
201
217
202
218
- def get_namespace_argument ():
219
- global namespace
220
- if namespace :
221
- return "-n {}" .format (namespace )
222
- return ""
223
-
224
-
225
203
def get_namespace_from_config ():
226
204
"""
227
205
Returns the namespace from current context if one is set OW None
@@ -244,11 +222,10 @@ def get_namespace_from_config():
244
222
break
245
223
246
224
247
- def collect_helper (cmd , file_name , resource_name ):
225
+ def collect_helper (output_dir , cmd , file_name , resource_name ):
248
226
"""
249
227
Runs command, write output to file_name, logs the resource_name
250
228
"""
251
- global output_dir
252
229
rc , out = run_shell_command (cmd )
253
230
if rc :
254
231
logger .warning ("Error when running {}: {}" .format (cmd , out ))
@@ -279,11 +256,11 @@ def run_shell_command(cmd):
279
256
return 0 , native_string (output )
280
257
281
258
282
- def run_kubectl_get (resource_type ):
259
+ def run_kubectl_get (namespace , resource_type ):
283
260
"""
284
261
Runs kubectl get command
285
262
"""
286
- cmd = "kubectl get {} {} -o yaml" .format (resource_type , get_namespace_argument () )
263
+ cmd = "kubectl get -n {} {} -o yaml" .format (namespace , resource_type )
287
264
rc , out = run_shell_command (cmd )
288
265
if rc == 0 :
289
266
return out
0 commit comments