From 87afb1c2a4de6d133567f99db1da932192bca3db Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Mon, 26 Jun 2023 09:59:52 +0200 Subject: [PATCH] Ensure at least the machine is allocated in the expected project (#82) --- pkg/resources/metal/machine.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/resources/metal/machine.go b/pkg/resources/metal/machine.go index 5a79378..ec56d04 100644 --- a/pkg/resources/metal/machine.go +++ b/pkg/resources/metal/machine.go @@ -30,12 +30,22 @@ type MetalService struct { func New(client metalgo.Client, k8sclient clientset.Interface, projectID string) *MetalService { machineByUUIDCache := cache.New(time.Minute, func(ctx context.Context, id string) (*models.V1MachineResponse, error) { - machine, err := client.Machine().FindMachine(machine.NewFindMachineParams().WithContext(ctx).WithID(id), nil) + resp, err := client.Machine().FindMachine(machine.NewFindMachineParams().WithContext(ctx).WithID(id), nil) if err != nil { return nil, err } - return machine.Payload, nil + if resp.Payload.Allocation == nil { + return nil, fmt.Errorf("machine %q is not allocated", id) + } + if resp.Payload.Allocation.Project == nil { + return nil, fmt.Errorf("machine %q allocation does not have a project", id) + } + if *resp.Payload.Allocation.Project != projectID { + return nil, fmt.Errorf("machine %q is allocated in the wrong project: %q", id, projectID) + } + + return resp.Payload, nil }) machineByHostnameCache := cache.New(time.Minute, func(ctx context.Context, hostname string) (*models.V1MachineResponse, error) { resp, err := client.Machine().FindMachines(machine.NewFindMachinesParams().WithContext(ctx).WithBody(&models.V1MachineFindRequest{