Skip to content

Commit 8a9fd10

Browse files
author
Cecile Robert-Michon
committed
Add MachinePool to e2e framework log collector
1 parent 5858391 commit 8a9fd10

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
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

+28-5
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,29 +228,50 @@ 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) {
238-
if name == "" {
239-
return nil, nil
240-
}
252+
Expect(name).NotTo(BeEmpty(), "cluster name should not be empty")
241253

242254
machineList := &clusterv1.MachineList{}
243255
labels := map[string]string{clusterv1.ClusterLabelName: name}
244-
245256
if err := c.List(ctx, machineList, client.InNamespace(namespace), client.MatchingLabels(labels)); err != nil {
246257
return nil, err
247258
}
248259

249260
return machineList, nil
250261
}
251262

263+
func getMachinePoolsInCluster(ctx context.Context, c client.Client, namespace, name string) (*expv1.MachinePoolList, error) {
264+
Expect(name).NotTo(BeEmpty(), "cluster name should not be empty")
265+
266+
machinePoolList := &expv1.MachinePoolList{}
267+
labels := map[string]string{clusterv1.ClusterLabelName: name}
268+
if err := c.List(ctx, machinePoolList, client.InNamespace(namespace), client.MatchingLabels(labels)); err != nil {
269+
return nil, err
270+
}
271+
272+
return machinePoolList, nil
273+
}
274+
252275
func (p *clusterProxy) getKubeconfig(ctx context.Context, namespace string, name string) *api.Config {
253276
cl := p.GetClient()
254277

test/framework/docker_logcollector.go

+19
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package framework
1919
import (
2020
"context"
2121
"fmt"
22+
kerrors "k8s.io/apimachinery/pkg/util/errors"
2223
"os"
2324
osExec "os/exec"
2425
"path/filepath"
26+
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4"
2527
"strings"
2628

2729
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
@@ -45,6 +47,23 @@ func machineContainerName(cluster, machine string) string {
4547

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

0 commit comments

Comments
 (0)