Skip to content

Commit

Permalink
collect kind logs when e2e bootstrap fails
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Jan 5, 2022
1 parent 4bddd69 commit bb702e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
22 changes: 20 additions & 2 deletions test/framework/bootstrap/kind_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -92,6 +99,7 @@ type KindClusterProvider struct {
kubeconfigPath string
nodeImage string
ipFamily clusterv1.ClusterIPFamily
logFolder string
}

// Create a Kubernetes cluster using kind.
Expand Down Expand Up @@ -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")
}

Expand Down
6 changes: 6 additions & 0 deletions test/framework/bootstrap/kind_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down

0 comments on commit bb702e3

Please sign in to comment.