I am new to golang, I have a task to collect the application logs, application is running as deployment in k8s cluster, there are 4 pods in total. As part of test automation, I need to collect the application logs (only for the duration of my test operation) in parallel while I perform some operations on the application and write the logs to a file, and move to the next operation and do the same.
Finally I iterate through the log files one-by-one and filter for certain keywords corresponding to my operation and validate the logs are correct/not.
I am thinking to get the pod logs using kubectl command directly, instead of using the go-sdk as I am facing missing log entries which I couldn't triage with many attempts.
kubectl logs -f -l app=my-app -n dev > /usr/mylogs.txt
I found a way to run this command using exec.Command
command := exec.Command("/bin/sh", "-c", "kubectl logs -f -l app=my-app -n dev > /usr/mylogs.txt")
err := command.Start()
Now I need to do this in golang,
func myTest(t *testing.T){
go collectApplicationLogs("test1")
// do application operation
// run test
stopLogsCollection () -------> how to achieve this?
}
func collectApplicationLogs(fileName string){
// command to collect the logs to a file
// kubectl logs -f -l app=my-app -n dev > /usr/{fileName}
}