Skip to content

Commit

Permalink
Rename machine to inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
damyan committed Sep 19, 2024
1 parent 81f68f4 commit ce3f78f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion internal/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package api

type Machine struct {
type Inventory struct {
Name string `json:"name"`
MacAddress string `json:"macAddress"`
}
32 changes: 16 additions & 16 deletions plugins/metal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var Plugin = plugins.Plugin{
Setup4: setup4,
}

// map MAC address to machine name
var machineMap map[string]string
// map MAC address to inventory name
var inventoryMap map[string]string

// args[0] = path to configuration file
func parseArgs(args ...string) (string, error) {
Expand All @@ -47,7 +47,7 @@ func parseArgs(args ...string) (string, error) {

func setup6(args ...string) (handler.Handler6, error) {
var err error
machineMap, err = loadConfig(args...)
inventoryMap, err = loadConfig(args...)
if err != nil {
return nil, err
}
Expand All @@ -68,25 +68,25 @@ func loadConfig(args ...string) (map[string]string, error) {
return nil, fmt.Errorf("failed to read config file: %v", err)
}

var config []api.Machine
var config []api.Inventory
if err = json.Unmarshal(configData, &config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %v", err)
}

machines := make(map[string]string)
for _, m := range config {
if m.MacAddress != "" && m.Name != "" {
machines[m.MacAddress] = m.Name
inventories := make(map[string]string)
for _, i := range config {
if i.MacAddress != "" && i.Name != "" {
inventories[i.MacAddress] = i.Name
}
}

log.Info("Loaded metal config", "Machines", len(machines))
return machines, nil
log.Info("Loaded metal config", "Inventories", len(inventories))
return inventories, nil
}

func setup4(args ...string) (handler.Handler4, error) {
var err error
machineMap, err = loadConfig(args...)
inventoryMap, err = loadConfig(args...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,10 +133,10 @@ func handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
}

func applyEndpointForMACAddress(mac net.HardwareAddr, subnetFamily ipamv1alpha1.SubnetAddressType) error {
machineName, ok := machineMap[mac.String()]
inventoryName, ok := inventoryMap[mac.String()]
if !ok {
// done here, next plugin
return fmt.Errorf("unknown machine MAC address: %s", mac.String())
return fmt.Errorf("unknown inventory MAC address: %s", mac.String())
}

ip, err := GetIPForMACAddress(mac, subnetFamily)
Expand All @@ -145,8 +145,8 @@ func applyEndpointForMACAddress(mac net.HardwareAddr, subnetFamily ipamv1alpha1.
}

if ip != nil {
if err := ApplyEndpointForMachine(machineName, mac, ip); err != nil {
return fmt.Errorf("could not apply endpoint for machine: %s", err)
if err := ApplyEndpointForInventory(inventoryName, mac, ip); err != nil {
return fmt.Errorf("could not apply endpoint for inventory: %s", err)
}
} else {
log.Infof("Could not find IP for MAC address %s", mac.String())
Expand All @@ -155,7 +155,7 @@ func applyEndpointForMACAddress(mac net.HardwareAddr, subnetFamily ipamv1alpha1.
return nil
}

func ApplyEndpointForMachine(name string, mac net.HardwareAddr, ip *netip.Addr) error {
func ApplyEndpointForInventory(name string, mac net.HardwareAddr, ip *netip.Addr) error {
if ip == nil {
log.Info("No IP address specified. Skipping.")
return nil
Expand Down
14 changes: 7 additions & 7 deletions plugins/metal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var _ = Describe("Endpoint", func() {
Expect(err).NotTo(BeNil())
})

It("Should return empty machine list if the config file is malformed", func() {
It("Should return empty inventory list if the config file is malformed", func() {
configFile := "config.json"
data := []map[string]string{
{
Expand All @@ -134,14 +134,14 @@ var _ = Describe("Endpoint", func() {
}()
Expect(os.WriteFile(file.Name(), configData, 0644)).To(Succeed())

m, err := loadConfig(file.Name())
i, err := loadConfig(file.Name())
Expect(err).NotTo(HaveOccurred())
Expect(m).To(BeEmpty())
Expect(i).To(BeEmpty())
})

It("Should return a valid machines list for a valid config", func() {
It("Should return a valid inventory list for a valid config", func() {
configFile := "config.json"
data := []api.Machine{
data := []api.Inventory{
{
Name: "compute-1",
MacAddress: "aa:bb:cc:dd:ee:ff",
Expand All @@ -157,9 +157,9 @@ var _ = Describe("Endpoint", func() {
}()
Expect(os.WriteFile(file.Name(), configData, 0644)).To(Succeed())

m, err := loadConfig(file.Name())
i, err := loadConfig(file.Name())
Expect(err).NotTo(HaveOccurred())
Expect(m).To(HaveKeyWithValue("aa:bb:cc:dd:ee:ff", "compute-1"))
Expect(i).To(HaveKeyWithValue("aa:bb:cc:dd:ee:ff", "compute-1"))
})

It("Should create an endpoint for IPv6 DHCP request from a known machine with IP address", func(ctx SpecContext) {
Expand Down
8 changes: 4 additions & 4 deletions plugins/metal/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func SetupTest() *corev1.Namespace {
Expect(k8sClient.Create(ctx, ns)).To(Succeed(), "failed to create test namespace")
DeferCleanup(k8sClient.Delete, ns)

config := []api.Machine{
config := []api.Inventory{
{
Name: machineWithIPAddressName,
MacAddress: machineWithIPAddressMACAddress,
Expand All @@ -126,9 +126,9 @@ func SetupTest() *corev1.Namespace {
MacAddress: machineWithoutIPAddressMACAddress,
},
}
machineMap = make(map[string]string)
for _, m := range config {
machineMap[m.MacAddress] = m.Name
inventoryMap = make(map[string]string)
for _, i := range config {
inventoryMap[i.MacAddress] = i.Name
}
})

Expand Down

0 comments on commit ce3f78f

Please sign in to comment.