Skip to content

Commit

Permalink
Dynamically calculate rack ID in machine registration (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwindower authored Mar 29, 2021
1 parent 30de7eb commit e843d25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
12 changes: 9 additions & 3 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package service
import (
"context"
"fmt"
s3server "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/s3client"
"net"
"net/http"
"strconv"
"strings"
"time"

s3server "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/s3client"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/grpc"
"github.com/pkg/errors"

Expand Down Expand Up @@ -591,7 +592,6 @@ func (r machineResource) registerMachine(request *restful.Request, response *res
Allocation: nil,
SizeID: size.ID,
PartitionID: partition.ID,
RackID: requestPayload.RackID,
Hardware: machineHardware,
BIOS: metal.BIOS{
Version: requestPayload.BIOS.Version,
Expand Down Expand Up @@ -621,7 +621,6 @@ func (r machineResource) registerMachine(request *restful.Request, response *res

m.SizeID = size.ID
m.PartitionID = partition.ID
m.RackID = requestPayload.RackID
m.Hardware = machineHardware
m.BIOS.Version = requestPayload.BIOS.Version
m.BIOS.Vendor = requestPayload.BIOS.Vendor
Expand Down Expand Up @@ -653,10 +652,17 @@ func (r machineResource) registerMachine(request *restful.Request, response *res
}
}

old := *m
err = connectMachineWithSwitches(r.ds, m)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}

err = r.ds.UpdateMachine(&old, m)
if checkError(request, response, utils.CurrentFuncName(), err) {
return
}

err = response.WriteHeaderAndEntity(returnCode, makeMachineResponse(m, r.ds, utils.Logger(request).Sugar()))
if err != nil {
zapup.MustRootLogger().Error("Failed to send response", zap.Error(err))
Expand Down
8 changes: 7 additions & 1 deletion cmd/metal-api/internal/service/switch-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func setVrf(s *metal.Switch, mid, vrf string) {
}

func connectMachineWithSwitches(ds *datastore.RethinkStore, m *metal.Machine) error {
switches, err := ds.SearchSwitches(m.RackID, nil)
switches, err := ds.SearchSwitches("", nil)
if err != nil {
return err
}
Expand All @@ -574,6 +574,12 @@ func connectMachineWithSwitches(ds *datastore.RethinkStore, m *metal.Machine) er
return connectionMapError
}

if s1.RackID != s2.RackID {
return fmt.Errorf("connected switches of a machine must reside in the same rack, rack of switch %s: %s, rack of switch %s: %s, machine: %s", s1.Name, s1.RackID, s2.Name, s2.RackID, m.ID)
}
// We detect the rackID of a machine by connections to leaf switches
m.RackID = s1.RackID

byNicName, err := s2.MachineConnections.ByNicName()
if err != nil {
return err
Expand Down
15 changes: 8 additions & 7 deletions cmd/metal-api/internal/service/v1/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,14 @@ type MachineFru struct {
}

type MachineRegisterRequest struct {
UUID string `json:"uuid" description:"the product uuid of the machine to register"`
PartitionID string `json:"partitionid" description:"the partition id to register this machine with"`
RackID string `json:"rackid" description:"the rack id where this machine is connected to"`
Hardware MachineHardwareExtended `json:"hardware" description:"the hardware of this machine"`
BIOS MachineBIOS `json:"bios" description:"bios information of this machine"`
IPMI MachineIPMI `json:"ipmi" description:"the ipmi access infos"`
Tags []string `json:"tags" description:"tags for this machine"`
UUID string `json:"uuid" description:"the product uuid of the machine to register"`
PartitionID string `json:"partitionid" description:"the partition id to register this machine with"`
// Deprecated: RackID is not used any longer, it is calculated by the switch connections of a machine. A metal-core instance might respond to pxe requests from all racks
RackID string `json:"rackid" description:"the rack id where this machine is connected to"`
Hardware MachineHardwareExtended `json:"hardware" description:"the hardware of this machine"`
BIOS MachineBIOS `json:"bios" description:"bios information of this machine"`
IPMI MachineIPMI `json:"ipmi" description:"the ipmi access infos"`
Tags []string `json:"tags" description:"tags for this machine"`
}

type MachineAllocateRequest struct {
Expand Down

0 comments on commit e843d25

Please sign in to comment.