From 099e540c03d195c7077c6ce4465980e8e3a3053a Mon Sep 17 00:00:00 2001 From: Michele Costa Date: Thu, 31 Aug 2023 14:20:34 +0100 Subject: [PATCH] Remove the log subcommand in favour of the logs collector --- README.md | 7 ++--- pkg/cmd/logs.go | 44 -------------------------- pkg/grablogs/grab_logs.go | 66 --------------------------------------- 3 files changed, 2 insertions(+), 115 deletions(-) delete mode 100644 pkg/cmd/logs.go delete mode 100644 pkg/grablogs/grab_logs.go diff --git a/README.md b/README.md index af73f1b1..f0522842 100644 --- a/README.md +++ b/README.md @@ -57,11 +57,8 @@ Run the following command (check help string for more details): ``` ### Fetching logs -Run the following command (check help string for more details): - -```shell -./vse-sync-collection-tools logs --kubeconfig="${KUBECONFIG}" -``` +The log subcommand has been removed. Instead we have implimented at collector which is enabled by default. +If possible you should use a log aggregator. You can control the collectors running using the `--collector` flag. ## Running tests diff --git a/pkg/cmd/logs.go b/pkg/cmd/logs.go deleted file mode 100644 index 7e1733c4..00000000 --- a/pkg/cmd/logs.go +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package cmd - -import ( - "github.com/spf13/cobra" - - "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/grablogs" -) - -var ( - outputDirPath string - logWindow string - - // logsCmd represents the logs command - logsCmd = &cobra.Command{ - Use: "logs", - Short: "collect container logs", - Long: `collect container logs`, - Run: func(cmd *cobra.Command, args []string) { - grablogs.GrabLogs(kubeConfig, logWindow, outputDirPath) - }, - } -) - -func init() { - rootCmd.AddCommand(logsCmd) - AddKubeconfigFlag(logsCmd) - - logsCmd.PersistentFlags().StringVarP( - &outputDirPath, - "output-dir", - "o", - ".", - "Optional. Specify the output directory. Target must exist. Defaults to working directory.", - ) - - logsCmd.PersistentFlags().StringVar( - &logWindow, - "since", - "", - "Optional. Only get logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs if omitted.", - ) -} diff --git a/pkg/grablogs/grab_logs.go b/pkg/grablogs/grab_logs.go deleted file mode 100644 index 2d2ec54a..00000000 --- a/pkg/grablogs/grab_logs.go +++ /dev/null @@ -1,66 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package grablogs - -import ( - "fmt" - "os" - "os/exec" - "strings" - "sync" - "time" - - log "github.com/sirupsen/logrus" - - "github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils" -) - -var ( - ptpContainers = []string{"linuxptp-daemon-container"} -) - -func buildCommand(containerName, kubeconfigPath, logWindow string) *exec.Cmd { - params := []string{"logs", "--kubeconfig", kubeconfigPath, "--namespace=openshift-ptp", "daemonset/linuxptp-daemon", "-c", containerName} //nolint:lll //easier to read unwrapped - if logWindow != "" { - params = append(params, "--since", logWindow) - } - - cmd := exec.Command("oc", params...) - return cmd -} - -func buildFilename(outputPath, name string, timestamp time.Time) string { - return fmt.Sprintf("%s/%s-%s", outputPath, name, timestamp.Format("060102T150405")) -} - -func getLogsForContainer(cmd *exec.Cmd, filename string) { - log.Infof("Running command: %s", cmd) - outputfile, err := os.Create(filename) - utils.IfErrorExitOrPanic(err) - log.Infof("Outputting to file: %v", outputfile.Name()) - defer outputfile.Close() - cmd.Stdout = outputfile - - var stderr strings.Builder - cmd.Stderr = &stderr - - err = cmd.Run() - utils.IfErrorExitOrPanic(err) -} - -func GrabLogs(kubeconfigPath, logWindow, outputPath string) { - var wg sync.WaitGroup //nolint:varnamelen // `wg` is a common abbreviation for waitgroup. - wg.Add(len(ptpContainers)) - - now := time.Now() - for i, containerName := range ptpContainers { - log.Infof("Starting goroutine %d for %s", i, containerName) - go func(containerName string) { - defer wg.Done() - cmd := buildCommand(containerName, kubeconfigPath, logWindow) - filename := buildFilename(outputPath, containerName, now) - getLogsForContainer(cmd, filename) - }(containerName) - } - wg.Wait() -}