Skip to content

Commit 55f064d

Browse files
authored
Merge pull request #4038 from tcordeu/bootstrap_cluster_logs
🌱 Collect bootstrap cluster logs in E2E tests
2 parents 386a852 + 5b6d896 commit 55f064d

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

test/e2e/e2e_suite_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package e2e
2020

2121
import (
2222
"flag"
23+
"fmt"
2324
"os"
2425
"path/filepath"
2526
"strings"
@@ -29,7 +30,9 @@ import (
2930
. "github.com/onsi/gomega"
3031
ctrl "sigs.k8s.io/controller-runtime"
3132

33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3234
"k8s.io/apimachinery/pkg/runtime"
35+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
3336
"sigs.k8s.io/cluster-api/test/framework"
3437
"sigs.k8s.io/cluster-api/test/framework/bootstrap"
3538
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
@@ -142,6 +145,9 @@ var _ = SynchronizedAfterSuite(func() {
142145
}, func() {
143146
// After all ParallelNodes.
144147

148+
By("Dumping logs from the bootstrap cluster")
149+
dumpBootstrapClusterLogs(bootstrapClusterProxy)
150+
145151
By("Tearing down the management cluster")
146152
if !skipCleanup {
147153
tearDown(bootstrapClusterProvider, bootstrapClusterProxy)
@@ -210,6 +216,43 @@ func initBootstrapCluster(bootstrapClusterProxy framework.ClusterProxy, config *
210216
}, config.GetIntervals(bootstrapClusterProxy.GetName(), "wait-controllers")...)
211217
}
212218

219+
func dumpBootstrapClusterLogs(bootstrapClusterProxy framework.ClusterProxy) {
220+
if bootstrapClusterProxy == nil {
221+
return
222+
}
223+
224+
clusterLogCollector := bootstrapClusterProxy.GetLogCollector()
225+
if clusterLogCollector == nil {
226+
return
227+
}
228+
229+
nodes, err := bootstrapClusterProxy.GetClientSet().CoreV1().Nodes().List(ctx, metav1.ListOptions{})
230+
if err != nil {
231+
fmt.Printf("Failed to get nodes for the bootstrap cluster: %v\n", err)
232+
return
233+
}
234+
235+
for i := range nodes.Items {
236+
nodeName := nodes.Items[i].GetName()
237+
err = clusterLogCollector.CollectMachineLog(
238+
ctx,
239+
bootstrapClusterProxy.GetClient(),
240+
// The bootstrap cluster is not expected to be a CAPI cluster, so in order to re-use the logCollector,
241+
// we create a fake machine that wraps the node.
242+
// NOTE: This assumes a naming convention between machines and nodes, which e.g. applies to the bootstrap clusters generated with kind.
243+
// This might not work if you are using an existing bootstrap cluster provided by other means.
244+
&clusterv1.Machine{
245+
Spec: clusterv1.MachineSpec{ClusterName: nodeName},
246+
ObjectMeta: metav1.ObjectMeta{Name: nodeName},
247+
},
248+
filepath.Join(artifactFolder, "clusters", bootstrapClusterProxy.GetName(), "machines", nodeName),
249+
)
250+
if err != nil {
251+
fmt.Printf("Failed to get logs for the bootstrap cluster node %s: %v\n", nodeName, err)
252+
}
253+
}
254+
}
255+
213256
func tearDown(bootstrapClusterProvider bootstrap.ClusterProvider, bootstrapClusterProxy framework.ClusterProxy) {
214257
if bootstrapClusterProxy != nil {
215258
bootstrapClusterProxy.Dispose(ctx)

test/framework/cluster_proxy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type ClusterProxy interface {
6363
// GetRESTConfig returns the REST config for direct use with client-go if needed.
6464
GetRESTConfig() *rest.Config
6565

66+
// GetLogCollector returns the machine log collector for the Kubernetes cluster.
67+
GetLogCollector() ClusterLogCollector
68+
6669
// Apply to apply YAML to the Kubernetes cluster, `kubectl apply`.
6770
Apply(ctx context.Context, resources []byte, args ...string) error
6871

@@ -199,6 +202,10 @@ func (p *clusterProxy) GetRESTConfig() *rest.Config {
199202
return restConfig
200203
}
201204

205+
func (p *clusterProxy) GetLogCollector() ClusterLogCollector {
206+
return p.logCollector
207+
}
208+
202209
// GetWorkloadCluster returns ClusterProxy for the workload cluster.
203210
func (p *clusterProxy) GetWorkloadCluster(ctx context.Context, namespace, name string) ClusterProxy {
204211
Expect(ctx).NotTo(BeNil(), "ctx is required for GetWorkloadCluster")

0 commit comments

Comments
 (0)