Skip to content

Commit 177ce4f

Browse files
author
Cecile Robert-Michon
committed
Add MachinePool to e2e framework log collector
1 parent 18b2741 commit 177ce4f

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

test/e2e/common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterPr
6363
Byf("Dumping logs from the %q workload cluster", cluster.Name)
6464

6565
// Dump all the logs from the workload cluster before deleting them.
66-
clusterProxy.CollectWorkloadClusterLogs(ctx, cluster.Namespace, cluster.Name, filepath.Join(artifactFolder, "clusters", cluster.Name, "machines"))
66+
clusterProxy.CollectWorkloadClusterLogs(ctx, cluster.Namespace, cluster.Name, filepath.Join(artifactFolder, "clusters", cluster.Name))
6767

6868
Byf("Dumping all the Cluster API resources in the %q namespace", namespace.Name)
6969

test/framework/cluster_proxy.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path"
2525
goruntime "runtime"
26+
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4"
2627
"strings"
2728

2829
. "github.com/onsi/gomega"
@@ -81,6 +82,7 @@ type ClusterLogCollector interface {
8182
// CollectMachineLog collects log from a machine.
8283
// TODO: describe output folder struct
8384
CollectMachineLog(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, outputPath string) error
85+
CollectMachinePoolLog(ctx context.Context, managementClusterClient client.Client, m *expv1.MachinePool, outputPath string) error
8486
}
8587

8688
// Option is a configuration option supplied to NewClusterProxy.
@@ -226,12 +228,24 @@ func (p *clusterProxy) CollectWorkloadClusterLogs(ctx context.Context, namespace
226228

227229
for i := range machines.Items {
228230
m := &machines.Items[i]
229-
err := p.logCollector.CollectMachineLog(ctx, p.GetClient(), m, path.Join(outputPath, m.GetName()))
231+
err := p.logCollector.CollectMachineLog(ctx, p.GetClient(), m, path.Join(outputPath, "machines", m.GetName()))
230232
if err != nil {
231233
// NB. we are treating failures in collecting logs as a non blocking operation (best effort)
232234
fmt.Printf("Failed to get logs for machine %s, cluster %s/%s: %v\n", m.GetName(), namespace, name, err)
233235
}
234236
}
237+
238+
machinePools, err := getMachinePoolsInCluster(ctx, p.GetClient(), namespace, name)
239+
Expect(err).ToNot(HaveOccurred(), "Failed to get machine pools for the %s/%s cluster", namespace, name)
240+
241+
for i := range machinePools.Items {
242+
mp := &machinePools.Items[i]
243+
err := p.logCollector.CollectMachinePoolLog(ctx, p.GetClient(), mp, path.Join(outputPath, "machine-pools", mp.GetName()))
244+
if err != nil {
245+
// NB. we are treating failures in collecting logs as a non blocking operation (best effort)
246+
fmt.Printf("Failed to get logs for machine pool %s, cluster %s/%s: %v\n", mp.GetName(), namespace, name, err)
247+
}
248+
}
235249
}
236250

237251
func getMachinesInCluster(ctx context.Context, c client.Client, namespace, name string) (*clusterv1.MachineList, error) {
@@ -249,6 +263,21 @@ func getMachinesInCluster(ctx context.Context, c client.Client, namespace, name
249263
return machineList, nil
250264
}
251265

266+
func getMachinePoolsInCluster(ctx context.Context, c client.Client, namespace, name string) (*expv1.MachinePoolList, error) {
267+
if name == "" {
268+
return nil, nil
269+
}
270+
271+
machinePoolList := &expv1.MachinePoolList{}
272+
labels := map[string]string{clusterv1.ClusterLabelName: name}
273+
274+
if err := c.List(ctx, machinePoolList, client.InNamespace(namespace), client.MatchingLabels(labels)); err != nil {
275+
return nil, err
276+
}
277+
278+
return machinePoolList, nil
279+
}
280+
252281
func (p *clusterProxy) getKubeconfig(ctx context.Context, namespace string, name string) *api.Config {
253282
cl := p.GetClient()
254283

test/framework/docker_logcollector.go

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
osExec "os/exec"
2424
"path/filepath"
25+
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4"
2526
"strings"
2627

2728
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
@@ -45,6 +46,22 @@ func machineContainerName(cluster, machine string) string {
4546

4647
func (k DockerLogCollector) CollectMachineLog(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, outputPath string) error {
4748
containerName := machineContainerName(m.Spec.ClusterName, m.Name)
49+
return k.collectLogsFromNode(outputPath, containerName)
50+
}
51+
52+
func (k DockerLogCollector) CollectMachinePoolLog(ctx context.Context, managementClusterClient client.Client, m *expv1.MachinePool, outputPath string) error {
53+
var reterr error
54+
for _, instance := range m.Status.NodeRefs {
55+
containerName := machineContainerName(m.Spec.ClusterName, instance.Name)
56+
if err := k.collectLogsFromNode(filepath.Join(outputPath, instance.Name), containerName); err != nil {
57+
// collecting logs is best effort so we proceed to the next instance even if we encounter an error.
58+
reterr = err
59+
}
60+
}
61+
return reterr
62+
}
63+
64+
func (k DockerLogCollector) collectLogsFromNode(outputPath string, containerName string) error {
4865
execToPathFn := func(outputFileName, command string, args ...string) func() error {
4966
return func() error {
5067
f, err := fileOnHost(filepath.Join(outputPath, outputFileName))

0 commit comments

Comments
 (0)