Skip to content

Commit

Permalink
Merge pull request #3505 from VitalyGushin/include-docker-logs
Browse files Browse the repository at this point in the history
Include docker logs.
  • Loading branch information
denis-tingaikin authored Mar 7, 2024
2 parents cfea1f4 + 4ed1cdf commit 2fa1987
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions extensions/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
ctx context.Context
kubeConfigs []string
matchRegex *regexp.Regexp
dockerRegex *regexp.Regexp
runner *bash.Bash
clusterDumpSingleOperation *singleOperation
)
Expand All @@ -54,6 +55,7 @@ type Config struct {
WorkerCount int `default:"8" desc:"Number of log collector workers" split_words:"true"`
MaxKubeConfigs int `default:"3" desc:"Number of used kubeconfigs" split_words:"true"`
AllowedNamespaces string `default:"(ns-.*)|(nsm-system)|(spire)|(observability)" desc:"Regex of allowed namespaces" split_words:"true"`
AllowedContainers string `default:"(nsc-.*)|(nse-.*)" desc:"Regexp of allowed docker containers" split_words:"true"`
LogCollectionEnabled bool `default:"true" desc:"Boolean variable which enables log collection" split_words:"true"`
}

Expand All @@ -68,6 +70,7 @@ func initialize() {
}

matchRegex = regexp.MustCompile(config.AllowedNamespaces)
dockerRegex = regexp.MustCompile(config.AllowedContainers)

var singleClusterKubeConfig = os.Getenv("KUBECONFIG")

Expand Down Expand Up @@ -125,6 +128,25 @@ func ClusterDump(suiteName, testName string) {
if err != nil {
logrus.Errorf("An error while getting cluster dump. Error: %s", err.Error())
}

allContainers, _, _, err := runner.Run(`docker ps --format '{{.Names}}'`)
if err != nil {
logrus.Errorf("An error while getting docker containers. Error: %s", err.Error())
continue
}
containerList := strings.Split(strings.TrimSpace(allContainers), "\n")
containers := filterContainers(containerList)

for _, container := range containers {
_, _, dExitCode, dErr := runner.Run(fmt.Sprintf("docker logs %s > %s/%s.log", container, suitedir, container))

if dExitCode != 0 {
logrus.Errorf("An error while getting docker logs. Exit Code: %v", dExitCode)
}
if dErr != nil {
logrus.Errorf("An error while getting docker logs. Error: %s", dErr.Error())
}
}
}
})
}
Expand All @@ -140,3 +162,15 @@ func filterNamespaces(nsList []string) []string {

return result
}

func filterContainers(containerList []string) []string {
result := make([]string, 0)

for i := range containerList {
if dockerRegex.MatchString(containerList[i]) {
result = append(result, containerList[i])
}
}

return result
}

0 comments on commit 2fa1987

Please sign in to comment.