Skip to content

Commit

Permalink
Change config file to be YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
damyan committed Sep 19, 2024
1 parent ce3f78f commit 0ef1521
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,12 @@ As for in-band, a kubernetes namespace shall be passed as a parameter. Further,
The Metal plugin acts as a connection link between DHCP and the IronCore metal stack. It creates an `EndPoint` object for each machine with leased IP address. Those endpoints are then consumed by the metal operator, who then creates the corresponding `Machine` objects.

### Configuration
Path to an inventory json shall be passed as a string. It represents a list of machines as follows:
```bash
[
{
"name": "Server-01",
"macAddress": "00:1A:2B:3C:4D:5E"
},
{
"name": "Server-02",
"macAddress": "00:1A:2B:3C:4D:5F"
},
]
Path to an inventory yaml shall be passed as a string. It represents a list of machines as follows:
```yaml
- name: server-01
macAddress: 00:1A:2B:3C:4D:5E
- name: server-02
macAddress: 00:1A:2B:3C:4D:5F
```
### Notes
- supports both IPv4 and IPv6
Expand Down
3 changes: 2 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ server6:
- dns: 2001:4860:4860::6464 2001:4860:4860::64
# implement (i)PXE boot
- pxeboot: tftp://[2001:db8::1]/ipxe/x86_64/ipxe http://[2001:db8::1]/ipxe/boot6
- metal: machines.json
# create Endpoint objects in kubernetes
- metal: inventory.yaml
4 changes: 2 additions & 2 deletions internal/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
package api

type Inventory struct {
Name string `json:"name"`
MacAddress string `json:"macAddress"`
Name string `yaml:"name"`
MacAddress string `yaml:"macAddress"`
}
5 changes: 3 additions & 2 deletions plugins/metal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ package metal

import (
"context"
"encoding/json"
"fmt"
"net"
"net/netip"
"os"
"strings"

"gopkg.in/yaml.v2"

"github.com/coredhcp/coredhcp/handler"
"github.com/coredhcp/coredhcp/logger"
"github.com/coredhcp/coredhcp/plugins"
Expand Down Expand Up @@ -69,7 +70,7 @@ func loadConfig(args ...string) (map[string]string, error) {
}

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

Expand Down
15 changes: 8 additions & 7 deletions plugins/metal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
package metal

import (
"encoding/json"
"net"
"os"
"strings"

"gopkg.in/yaml.v2"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv6"
"github.com/ironcore-dev/fedhcp/internal/api"
Expand Down Expand Up @@ -97,7 +98,7 @@ var _ = Describe("Endpoint", func() {
})

It("Setup6 should return error if config file does not exist", func() {
_, err := setup6("does-not-exist.json")
_, err := setup6("does-not-exist.yaml")
Expect(err).NotTo(BeNil())
})

Expand All @@ -112,19 +113,19 @@ var _ = Describe("Endpoint", func() {
})

It("Setup4 should return error if config file does not exist", func() {
_, err := setup4("does-not-exist.json")
_, err := setup4("does-not-exist.yaml")
Expect(err).NotTo(BeNil())
})

It("Should return empty inventory list if the config file is malformed", func() {
configFile := "config.json"
configFile := "config.yaml"
data := []map[string]string{
{
"foo": "compute-1",
"bar": "aa:bb:cc:dd:ee:ff",
},
}
configData, err := json.Marshal(data)
configData, err := yaml.Marshal(data)
Expect(err).NotTo(HaveOccurred())

file, err := os.CreateTemp(GinkgoT().TempDir(), configFile)
Expand All @@ -140,14 +141,14 @@ var _ = Describe("Endpoint", func() {
})

It("Should return a valid inventory list for a valid config", func() {
configFile := "config.json"
configFile := "config.yaml"
data := []api.Inventory{
{
Name: "compute-1",
MacAddress: "aa:bb:cc:dd:ee:ff",
},
}
configData, err := json.Marshal(data)
configData, err := yaml.Marshal(data)
Expect(err).NotTo(HaveOccurred())

file, err := os.CreateTemp(GinkgoT().TempDir(), configFile)
Expand Down

0 comments on commit 0ef1521

Please sign in to comment.