Skip to content

Commit

Permalink
test: ensure we can collect machine logs
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 8, 2024
1 parent 82e85a3 commit 4682706
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions test/framework/log/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/services/vmoperator"
)

const (
Expand All @@ -54,8 +57,8 @@ func (c *MachineLogCollector) CollectMachinePoolLog(_ context.Context, _ client.
return nil
}

func (c *MachineLogCollector) CollectMachineLog(ctx context.Context, _ client.Client, m *clusterv1.Machine, outputPath string) error {
machineIPAddresses, err := c.machineIPAddresses(ctx, m)
func (c *MachineLogCollector) CollectMachineLog(ctx context.Context, ctrlClient client.Client, m *clusterv1.Machine, outputPath string) error {
machineIPAddresses, err := c.machineIPAddresses(ctx, ctrlClient, m)
if err != nil {
return err
}
Expand Down Expand Up @@ -100,15 +103,35 @@ func (c *MachineLogCollector) CollectInfrastructureLogs(_ context.Context, _ cli
return nil
}

func (c *MachineLogCollector) machineIPAddresses(ctx context.Context, m *clusterv1.Machine) ([]string, error) {
func (c *MachineLogCollector) machineIPAddresses(ctx context.Context, ctrlClient client.Client, m *clusterv1.Machine) ([]string, error) {
for _, address := range m.Status.Addresses {
if address.Type != clusterv1.MachineExternalIP {
continue
}
return []string{address.Address}, nil
}

vmObj, err := c.Finder.VirtualMachine(ctx, m.GetName())
vmName := m.GetName()

// For supervisor mode it could be the case that the name of the virtual machine differs from the machine's name.
if m.Spec.InfrastructureRef.GroupVersionKind().Group == vmwarev1.GroupVersion.Group {
vsphereMachine := &vmwarev1.VSphereMachine{}
if err := ctrlClient.Get(ctx, client.ObjectKey{Namespace: m.Spec.InfrastructureRef.Namespace, Name: m.Spec.InfrastructureRef.Name}, vsphereMachine); err != nil {
return nil, errors.Wrapf(err, "getting vmwarev1.VSphereMachine %s/%s", m.Spec.InfrastructureRef.Namespace, m.Spec.InfrastructureRef.Name)
}

if vsphereMachine.Status.IPAddr != "" {
return []string{vsphereMachine.Status.IPAddr}, nil
}

var err error
vmName, err = vmoperator.GenerateVirtualMachineName(m.Name, vsphereMachine.Spec.NamingStrategy)
if err != nil {
return nil, errors.Wrapf(err, "generating VirtualMachine name for Machine %s/%s", m.Namespace, m.Name)
}
}

vmObj, err := c.Finder.VirtualMachine(ctx, vmName)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4682706

Please sign in to comment.