Skip to content

Commit

Permalink
Export logs from knative-eventing namespace (#3687)
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>
  • Loading branch information
pierDipi authored Jul 23, 2020
1 parent 82399b8 commit ce4fa36
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions test/conformance/main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build e2e

/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,6 +26,7 @@ import (

"knative.dev/eventing/test"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/resources"
)

var channelTestRunner testlib.ComponentsTestRunner
Expand All @@ -50,6 +53,7 @@ func TestMain(m *testing.M) {
// place that cleans it up. If an individual test calls this instead, then it will break other
// tests that need the tracing in place.
defer zipkin.CleanupZipkinTracingSetup(log.Printf)
defer testlib.ExportLogs(testlib.SystemLogsDir, resources.SystemNamespace)

return m.Run()
}())
Expand Down
2 changes: 2 additions & 0 deletions test/conformance/source_crd_metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build e2e

/*
Copyright 2020 The Knative Authors
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build e2e

/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,6 +23,7 @@ import (

"knative.dev/eventing/test"
testlib "knative.dev/eventing/test/lib"
"knative.dev/eventing/test/lib/resources"
)

var setup = testlib.Setup
Expand All @@ -35,5 +38,10 @@ func TestMain(m *testing.M) {
ComponentsToTest: test.EventingFlags.Channels,
}
brokerClass = test.EventingFlags.BrokerClass
os.Exit(m.Run())

exit := m.Run()

testlib.ExportLogs(testlib.SystemLogsDir, resources.SystemNamespace)

os.Exit(exit)
}
2 changes: 2 additions & 0 deletions test/lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const (
// The interval and timeout used for polling pod logs.
interval = 1 * time.Second
timeout = 4 * time.Minute

SystemLogsDir = "knative-eventing-logs"
)

// InterestingHeaders is used by logging pods to decide interesting HTTP headers to log
Expand Down
38 changes: 32 additions & 6 deletions test/lib/logexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,44 @@ package lib

import (
"fmt"
"log"
"os"
"path/filepath"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/test"
pkgtest "knative.dev/pkg/test"
"knative.dev/pkg/test/helpers"
"knative.dev/pkg/test/prow"
)

func (c *Client) ExportLogs(dir string) error {
return exportLogs(c.Kube, c.Namespace, dir, c.T.Logf)
}

func exportLogs(kubeClient *test.KubeClient, namespace, dir string, logFunc func(format string, args ...interface{})) error {

// Create a directory for the namespace.
logPath := filepath.Join(dir, c.Namespace)
logPath := filepath.Join(dir, namespace)
if err := helpers.CreateDir(logPath); err != nil {
return fmt.Errorf("error creating directory %q: %w", c.Namespace, err)
return fmt.Errorf("error creating directory %q: %w", namespace, err)
}

pods, err := c.Kube.Kube.CoreV1().Pods(c.Namespace).List(metav1.ListOptions{})
pods, err := kubeClient.Kube.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing pods in namespace %q: %w", c.Namespace, err)
return fmt.Errorf("error listing pods in namespace %q: %w", namespace, err)
}

var errs []error
for _, pod := range pods.Items {
for _, ct := range pod.Spec.Containers {
fn := filepath.Join(logPath, fmt.Sprintf("%s-%s.log", pod.Name, ct.Name))
c.T.Logf("Exporting logs in pod %q container %q to %q", pod.Name, ct.Name, fn)
logFunc("Exporting logs in pod %q container %q to %q", pod.Name, ct.Name, fn)
f, err := os.Create(fn)
if err != nil {
errs = append(errs, fmt.Errorf("error creating file %q: %w", fn, err))
}
log, err := c.Kube.PodLogs(pod.Name, ct.Name, c.Namespace)
log, err := kubeClient.PodLogs(pod.Name, ct.Name, pod.Namespace)
if err != nil {
errs = append(errs, fmt.Errorf("error getting logs for pod %q container %q: %w", pod.Name, ct.Name, err))
}
Expand All @@ -61,3 +70,20 @@ func (c *Client) ExportLogs(dir string) error {

return helpers.CombineErrors(errs)
}

func ExportLogs(systemLogsDir, systemNamespace string) {

// If the test is run by CI, export the pod logs in the namespace to the artifacts directory,
// which will then be uploaded to GCS after the test job finishes.
if prow.IsCI() {
kubeClient, err := pkgtest.NewKubeClient(pkgtest.Flags.Kubeconfig, pkgtest.Flags.Cluster)
if err != nil {
log.Printf("Failed to create Kube client: %v\n", err)
}

dir := filepath.Join(prow.GetLocalArtifactsDir(), systemLogsDir)
if err := exportLogs(kubeClient, systemNamespace, dir, log.Printf); err != nil {
log.Printf("Error in exporting logs: %v", err)
}
}
}

0 comments on commit ce4fa36

Please sign in to comment.