Skip to content

Commit

Permalink
Add MachinePool to e2e framework log collector
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecile Robert-Michon committed May 18, 2021
1 parent 18b2741 commit 177ce4f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterPr
Byf("Dumping logs from the %q workload cluster", cluster.Name)

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

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

Expand Down
31 changes: 30 additions & 1 deletion test/framework/cluster_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path"
goruntime "runtime"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4"
"strings"

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

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

for i := range machines.Items {
m := &machines.Items[i]
err := p.logCollector.CollectMachineLog(ctx, p.GetClient(), m, path.Join(outputPath, m.GetName()))
err := p.logCollector.CollectMachineLog(ctx, p.GetClient(), m, path.Join(outputPath, "machines", m.GetName()))
if err != nil {
// NB. we are treating failures in collecting logs as a non blocking operation (best effort)
fmt.Printf("Failed to get logs for machine %s, cluster %s/%s: %v\n", m.GetName(), namespace, name, err)
}
}

machinePools, err := getMachinePoolsInCluster(ctx, p.GetClient(), namespace, name)
Expect(err).ToNot(HaveOccurred(), "Failed to get machine pools for the %s/%s cluster", namespace, name)

for i := range machinePools.Items {
mp := &machinePools.Items[i]
err := p.logCollector.CollectMachinePoolLog(ctx, p.GetClient(), mp, path.Join(outputPath, "machine-pools", mp.GetName()))
if err != nil {
// NB. we are treating failures in collecting logs as a non blocking operation (best effort)
fmt.Printf("Failed to get logs for machine pool %s, cluster %s/%s: %v\n", mp.GetName(), namespace, name, err)
}
}
}

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

func getMachinePoolsInCluster(ctx context.Context, c client.Client, namespace, name string) (*expv1.MachinePoolList, error) {
if name == "" {
return nil, nil
}

machinePoolList := &expv1.MachinePoolList{}
labels := map[string]string{clusterv1.ClusterLabelName: name}

if err := c.List(ctx, machinePoolList, client.InNamespace(namespace), client.MatchingLabels(labels)); err != nil {
return nil, err
}

return machinePoolList, nil
}

func (p *clusterProxy) getKubeconfig(ctx context.Context, namespace string, name string) *api.Config {
cl := p.GetClient()

Expand Down
17 changes: 17 additions & 0 deletions test/framework/docker_logcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
osExec "os/exec"
"path/filepath"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha4"
"strings"

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

func (k DockerLogCollector) CollectMachineLog(ctx context.Context, managementClusterClient client.Client, m *clusterv1.Machine, outputPath string) error {
containerName := machineContainerName(m.Spec.ClusterName, m.Name)
return k.collectLogsFromNode(outputPath, containerName)
}

func (k DockerLogCollector) CollectMachinePoolLog(ctx context.Context, managementClusterClient client.Client, m *expv1.MachinePool, outputPath string) error {
var reterr error
for _, instance := range m.Status.NodeRefs {
containerName := machineContainerName(m.Spec.ClusterName, instance.Name)
if err := k.collectLogsFromNode(filepath.Join(outputPath, instance.Name), containerName); err != nil {
// collecting logs is best effort so we proceed to the next instance even if we encounter an error.
reterr = err
}
}
return reterr
}

func (k DockerLogCollector) collectLogsFromNode(outputPath string, containerName string) error {
execToPathFn := func(outputFileName, command string, args ...string) func() error {
return func() error {
f, err := fileOnHost(filepath.Join(outputPath, outputFileName))
Expand Down

0 comments on commit 177ce4f

Please sign in to comment.