Skip to content

Commit

Permalink
fixed kubectl command in Popen
Browse files Browse the repository at this point in the history
Signed-off-by: vivekr-splunk <94569031+vivekr-splunk@users.noreply.github.com>
  • Loading branch information
vivekr-splunk committed Jul 24, 2023
1 parent 785365a commit b61b5e2
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions tools/k8s_collectors/k8s-splunk-collector-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import sys, getopt
import subprocess

def executeShellCommand(command):
cmdlist = shlex.split(command)
stream = subprocess.Popen(cmdlist).wait()
def executeKubectlCommand(command):
stream = subprocess.Popen(["kubectl", command]).wait()
output = stream.read()
return output


def runAndCollectDiag(collectDir, podDiagsDir, pod):
output = executeShellCommand("kubectl exec --stdin %s -- /opt/splunk/bin/splunk diag;" % pod)
output = executeKubectlCommand("exec --stdin %s -- /opt/splunk/bin/splunk diag;" % pod)
for line in output.splitlines():
words = line.split()
if len(words) > 4 and "Splunk diagnosis file created:" in line:
Expand All @@ -24,10 +23,10 @@ def runAndCollectDiag(collectDir, podDiagsDir, pod):
diagFile = dirs[3]

#Copy the diag over
executeShellCommand("kubectl cp %s:%s %s/%s/%s" % (pod, diagFileFullPath, collectDir, podDiagsDir, diagFile))
executeKubectlCommand("cp %s:%s %s/%s/%s" % (pod, diagFileFullPath, collectDir, podDiagsDir, diagFile))

#Delete the diag
executeShellCommand("kubectl exec --stdin %s -- rm -rf %s" % (pod, diagFileFullPath))
executeKubectlCommand("exec --stdin %s -- rm -rf %s" % (pod, diagFileFullPath))

def main(argv):
#Define required variables
Expand All @@ -51,10 +50,10 @@ def main(argv):
collectDir = arg

# Collect logs from the operator
output = executeShellCommand("kubectl logs deployment/splunk-operator-controller-manager manager > %s/%s/operator.log" % (collectDir, podLogsDir))
output = executeShellCommand("kubectl logs -l app.kubernetes.io/managed-by=splunk-operator --tail -1 > %s/%s/splunkEnterprisePods.log" % (collectDir, podLogsDir))
output = executeKubectlCommand("logs deployment/splunk-operator-controller-manager manager > %s/%s/operator.log" % (collectDir, podLogsDir))
output = executeKubectlCommand("logs -l app.kubernetes.io/managed-by=splunk-operator --tail -1 > %s/%s/splunkEnterprisePods.log" % (collectDir, podLogsDir))

output = executeShellCommand("kubectl get pods")
output = executeKubectlCommand("kubectl get pods")
for line in output.splitlines():
words = line.split()
if "splunk" in words[0]:
Expand All @@ -63,11 +62,11 @@ def main(argv):
#ensure container is specified for the operator
if "operator" in pod:
opPod = pod + " -c manager"
executeShellCommand("kubectl logs %s > %s/%s/%s.log" % (opPod, collectDir, podLogsDir, pod))
executeKubectlCommand("logs %s > %s/%s/%s.log" % (opPod, collectDir, podLogsDir, pod))
continue

# Collect logs from pod
executeShellCommand("kubectl logs %s > %s/%s/%s.log" % (pod, collectDir, podLogsDir, pod))
executeKubectlCommand("logs %s > %s/%s/%s.log" % (pod, collectDir, podLogsDir, pod))

# Collect diag and save diag from all Splunk Instances
if collectDiag == "true":
Expand Down

0 comments on commit b61b5e2

Please sign in to comment.