From 20b5fb9dcb04a449b9397e9dd2127b19da617cbc Mon Sep 17 00:00:00 2001 From: Mikhail Avramenko Date: Mon, 22 Nov 2021 13:38:46 +0700 Subject: [PATCH 1/2] improved describe method Signed-off-by: Mikhail Avramenko --- extensions/logs/logs.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/extensions/logs/logs.go b/extensions/logs/logs.go index bced454b1f..54d17e3c5a 100644 --- a/extensions/logs/logs.go +++ b/extensions/logs/logs.go @@ -25,6 +25,7 @@ import ( "os/signal" "path/filepath" "regexp" + "strings" "sync" "syscall" "time" @@ -205,12 +206,11 @@ func capture(name string) context.CancelFunc { } } -//nolint func describePods(name string) { getCtx, cancel := context.WithTimeout(ctx, config.Timeout) defer cancel() - _, err := kubeClient.CoreV1().Namespaces().Get(getCtx, "nsm-system", metav1.GetOptions{}) + nsList, err := kubeClient.CoreV1().Namespaces().List(getCtx, metav1.ListOptions{}) if err != nil { return } @@ -220,7 +220,24 @@ func describePods(name string) { return } - runner.Run("kubectl describe pods -n nsm-system >" + filepath.Join(config.ArtifactsDir, name, "describe.log")) + for _, ns := range getNamespaces(nsList) { + _, _, exitCode, err := runner.Run("kubectl describe pods -n " + ns + ">" + filepath.Join(config.ArtifactsDir, name, "describe-"+ns+".log")) + if exitCode != 0 || err != nil { + logrus.Errorf("An error while retrieving describe for namespace: %v", ns) + } + } +} + +func getNamespaces(nsList *corev1.NamespaceList) []string { + var rv []string + + for i := 0; i < len(nsList.Items); i++ { + if (nsList.Items[i].Name == "spire" || nsList.Items[i].Name == "nsm-system" || strings.HasPrefix(nsList.Items[i].Name, "ns-")) && nsList.Items[i].Status.Phase == corev1.NamespaceActive { + rv = append(rv, nsList.Items[i].Name) + } + } + + return rv } // Capture returns a function that saves logs since Capture function has been called. From 6780ef367d4ed31f8bdc954c1d7444ffea17f70c Mon Sep 17 00:00:00 2001 From: Mikhail Avramenko Date: Tue, 30 Nov 2021 17:11:33 +0700 Subject: [PATCH 2/2] review refactoring Signed-off-by: Mikhail Avramenko --- extensions/logs/logs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/logs/logs.go b/extensions/logs/logs.go index 54d17e3c5a..b5f6e973a1 100644 --- a/extensions/logs/logs.go +++ b/extensions/logs/logs.go @@ -220,7 +220,7 @@ func describePods(name string) { return } - for _, ns := range getNamespaces(nsList) { + for _, ns := range filterNamespaces(nsList) { _, _, exitCode, err := runner.Run("kubectl describe pods -n " + ns + ">" + filepath.Join(config.ArtifactsDir, name, "describe-"+ns+".log")) if exitCode != 0 || err != nil { logrus.Errorf("An error while retrieving describe for namespace: %v", ns) @@ -228,7 +228,7 @@ func describePods(name string) { } } -func getNamespaces(nsList *corev1.NamespaceList) []string { +func filterNamespaces(nsList *corev1.NamespaceList) []string { var rv []string for i := 0; i < len(nsList.Items); i++ {