Skip to content

Commit c2f930f

Browse files
authored
Merge pull request #8675 from chrischdi/pr-capd-lb-logs
🌱 test/framework: add functions to collect infrastructure logs in tests
2 parents f66e30f + 55a88df commit c2f930f

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

docs/book/src/developer/providers/migrations/v1.4-to-v1.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ maintainers of providers and consumers of our Go API.
3131

3232
- clusterctl move is adding the new annotation `clusterctl.cluster.x-k8s.io/delete-for-move` before object deletion.
3333
- Providers running CAPI release-0.3 clusterctl upgrade tests should set `WorkloadKubernetesVersion` field to the maximum workload cluster kubernetes version supported by the old providers in `ClusterctlUpgradeSpecInput`. For more information, please see: https://github.com/kubernetes-sigs/cluster-api/pull/8518#issuecomment-1508064859
34+
- Introduced function `CollectInfrastructureLogs` at the `ClusterLogCollector` interface in `test/framework/cluster_proxy.go` to allow collecting infrastructure related logs during tests.
3435

3536
### Suggested changes for providers
3637

test/framework/cluster_proxy.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type ClusterProxy interface {
9393
// GetWorkloadCluster returns a proxy to a workload cluster defined in the Kubernetes cluster.
9494
GetWorkloadCluster(ctx context.Context, namespace, name string) ClusterProxy
9595

96-
// CollectWorkloadClusterLogs collects machines logs from the workload cluster.
96+
// CollectWorkloadClusterLogs collects machines and infrastructure logs from the workload cluster.
9797
CollectWorkloadClusterLogs(ctx context.Context, namespace, name, outputPath string)
9898

9999
// Dispose proxy's internal resources (the operation does not affects the Kubernetes cluster).
@@ -107,6 +107,8 @@ type ClusterLogCollector interface {
107107
// TODO: describe output folder struct
108108
CollectMachineLog(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, outputPath string) error
109109
CollectMachinePoolLog(ctx context.Context, managementClusterClient client.Client, m *expv1.MachinePool, outputPath string) error
110+
// CollectInfrastructureLogs collects log from the infrastructure.
111+
CollectInfrastructureLogs(ctx context.Context, managementClusterClient client.Client, c *clusterv1.Cluster, outputPath string) error
110112
}
111113

112114
// Option is a configuration option supplied to NewClusterProxy.
@@ -280,7 +282,7 @@ func (p *clusterProxy) GetWorkloadCluster(ctx context.Context, namespace, name s
280282
return newFromAPIConfig(name, config, p.scheme)
281283
}
282284

283-
// CollectWorkloadClusterLogs collects machines logs from the workload cluster.
285+
// CollectWorkloadClusterLogs collects machines and infrastructure logs and from the workload cluster.
284286
func (p *clusterProxy) CollectWorkloadClusterLogs(ctx context.Context, namespace, name, outputPath string) {
285287
if p.logCollector == nil {
286288
return
@@ -317,6 +319,23 @@ func (p *clusterProxy) CollectWorkloadClusterLogs(ctx context.Context, namespace
317319
fmt.Printf("Failed to get logs for MachinePool %s, Cluster %s: %v\n", mp.GetName(), klog.KRef(namespace, name), err)
318320
}
319321
}
322+
323+
cluster := &clusterv1.Cluster{}
324+
Eventually(func() error {
325+
key := client.ObjectKey{
326+
Namespace: namespace,
327+
Name: name,
328+
}
329+
return client.IgnoreNotFound(p.GetClient().Get(ctx, key, cluster))
330+
}, retryableOperationTimeout, retryableOperationInterval).Should(Succeed(), "Failed to get Cluster %s", klog.KRef(namespace, name))
331+
332+
if cluster != nil {
333+
err := p.logCollector.CollectInfrastructureLogs(ctx, p.GetClient(), cluster, path.Join(outputPath, "infrastructure"))
334+
if err != nil {
335+
// NB. we are treating failures in collecting logs as a non blocking operation (best effort)
336+
fmt.Printf("Failed to get infrastructure logs for Cluster %s: %v\n", klog.KRef(namespace, name), err)
337+
}
338+
}
320339
}
321340

322341
func getMachinesInCluster(ctx context.Context, c client.Client, namespace, name string) (*clusterv1.MachineList, error) {

test/framework/docker_logcollector.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ func (k DockerLogCollector) CollectMachinePoolLog(ctx context.Context, _ client.
7676
return kerrors.NewAggregate(errs)
7777
}
7878

79+
func (k DockerLogCollector) CollectInfrastructureLogs(ctx context.Context, _ client.Client, c *clusterv1.Cluster, outputPath string) error {
80+
containerRuntime, err := container.NewDockerClient()
81+
if err != nil {
82+
return err
83+
}
84+
ctx = container.RuntimeInto(ctx, containerRuntime)
85+
86+
lbContainerName := fmt.Sprintf("%s-lb", c.GetName())
87+
88+
f, err := fileOnHost(filepath.Join(outputPath, fmt.Sprintf("%s.log", lbContainerName)))
89+
if err != nil {
90+
return err
91+
}
92+
93+
defer f.Close()
94+
95+
return containerRuntime.ContainerDebugInfo(ctx, lbContainerName, f)
96+
}
97+
7998
func (k DockerLogCollector) collectLogsFromNode(ctx context.Context, outputPath string, containerName string) error {
8099
containerRuntime, err := container.RuntimeFrom(ctx)
81100
if err != nil {

test/infrastructure/container/docker.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"bytes"
2323
"context"
2424
"encoding/csv"
25+
"encoding/json"
2526
"fmt"
2627
"io"
2728
"os"
@@ -331,8 +332,13 @@ func (d *dockerRuntime) ContainerDebugInfo(ctx context.Context, containerName st
331332
return errors.Wrapf(err, "failed to inspect container %q", containerName)
332333
}
333334

335+
rawJSON, err := json.Marshal(containerInfo)
336+
if err != nil {
337+
return errors.Wrapf(err, "failed to marshal container info to json")
338+
}
339+
334340
fmt.Fprintln(w, "Inspected the container:")
335-
fmt.Fprintf(w, "%+v\n", containerInfo)
341+
fmt.Fprintf(w, "%s\n", rawJSON)
336342

337343
options := types.ContainerLogsOptions{
338344
ShowStdout: true,

0 commit comments

Comments
 (0)