From bb702e3edf58842b07e33495e1c903d73d3f41b4 Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Wed, 5 Jan 2022 15:36:17 +0100 Subject: [PATCH] collect kind logs when e2e bootstrap fails --- test/e2e/e2e_suite_test.go | 1 + test/framework/bootstrap/kind_provider.go | 22 ++++++++++++++++++++-- test/framework/bootstrap/kind_util.go | 6 ++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index 968911fb68b9..4c82431dfd82 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -218,6 +218,7 @@ func setupBootstrapCluster(config *clusterctl.E2EConfig, scheme *runtime.Scheme, RequiresDockerSock: config.HasDockerProvider(), Images: config.Images, IPFamily: config.GetVariable(IPFamily), + LogFolder: filepath.Join(artifactFolder, "kind"), }) Expect(clusterProvider).ToNot(BeNil(), "Failed to create a bootstrap cluster") diff --git a/test/framework/bootstrap/kind_provider.go b/test/framework/bootstrap/kind_provider.go index b8e4377dfe23..cddf262c1b41 100644 --- a/test/framework/bootstrap/kind_provider.go +++ b/test/framework/bootstrap/kind_provider.go @@ -72,6 +72,13 @@ func WithIPv6Family() KindClusterOption { }) } +// LogFolder implements a New Option that instruct the kindClusterProvider to dump bootstrap logs in a folder in case of errors. +func LogFolder(path string) KindClusterOption { + return kindClusterOptionAdapter(func(k *KindClusterProvider) { + k.logFolder = path + }) +} + // NewKindClusterProvider returns a ClusterProvider that can create a kind cluster. func NewKindClusterProvider(name string, options ...KindClusterOption) *KindClusterProvider { Expect(name).ToNot(BeEmpty(), "name is required for NewKindClusterProvider") @@ -92,6 +99,7 @@ type KindClusterProvider struct { kubeconfigPath string nodeImage string ipFamily clusterv1.ClusterIPFamily + logFolder string } // Create a Kubernetes cluster using kind. @@ -139,8 +147,18 @@ func (k *KindClusterProvider) createKindCluster() { nodeImage = k.nodeImage } kindCreateOptions = append(kindCreateOptions, kind.CreateWithNodeImage(nodeImage)) - - err := kind.NewProvider(kind.ProviderWithLogger(cmd.NewLogger())).Create(k.name, kindCreateOptions...) + kindCreateOptions = append(kindCreateOptions, kind.CreateWithRetain(true)) + + provider := kind.NewProvider(kind.ProviderWithLogger(cmd.NewLogger())) + err := provider.Create(k.name, kindCreateOptions...) + if err != nil { + if err := os.MkdirAll(k.logFolder, 0750); err != nil { + log.Logf("Failed to create folder for kind collect logs: %v", err) + } + if err := provider.CollectLogs(k.name, k.logFolder); err != nil { + log.Logf("Failed to collect logs from kind: %v", err) + } + } Expect(err).ToNot(HaveOccurred(), "Failed to create the kind cluster") } diff --git a/test/framework/bootstrap/kind_util.go b/test/framework/bootstrap/kind_util.go index 886a18664a99..97903ded1678 100644 --- a/test/framework/bootstrap/kind_util.go +++ b/test/framework/bootstrap/kind_util.go @@ -49,6 +49,9 @@ type CreateKindBootstrapClusterAndLoadImagesInput struct { // IPFamily is either ipv4 or ipv6. Default is ipv4. IPFamily string + + // LogFolder where to dump logs in case of errors + LogFolder string } // CreateKindBootstrapClusterAndLoadImages returns a new Kubernetes cluster with pre-loaded images. @@ -68,6 +71,9 @@ func CreateKindBootstrapClusterAndLoadImages(ctx context.Context, input CreateKi if input.IPFamily == "IPv6" { options = append(options, WithIPv6Family()) } + if input.LogFolder != "" { + options = append(options, LogFolder(input.LogFolder)) + } clusterProvider := NewKindClusterProvider(input.Name, options...) Expect(clusterProvider).ToNot(BeNil(), "Failed to create a kind cluster")