From 0ef1521c71783c904ba2100f7f4adfe0860a132d Mon Sep 17 00:00:00 2001 From: Damyan Yordanov Date: Thu, 19 Sep 2024 16:33:07 +0200 Subject: [PATCH] Change config file to be YAML --- README.md | 18 ++++++------------ config.yaml | 3 ++- internal/api/config.go | 4 ++-- plugins/metal/plugin.go | 5 +++-- plugins/metal/plugin_test.go | 15 ++++++++------- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index d8f0459..f216d21 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config.yaml b/config.yaml index 4ec2968..94abca9 100644 --- a/config.yaml +++ b/config.yaml @@ -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 \ No newline at end of file + # create Endpoint objects in kubernetes + - metal: inventory.yaml \ No newline at end of file diff --git a/internal/api/config.go b/internal/api/config.go index 6c8aedc..1bda670 100644 --- a/internal/api/config.go +++ b/internal/api/config.go @@ -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"` } diff --git a/plugins/metal/plugin.go b/plugins/metal/plugin.go index 15e2a5e..94e6635 100644 --- a/plugins/metal/plugin.go +++ b/plugins/metal/plugin.go @@ -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" @@ -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) } diff --git a/plugins/metal/plugin_test.go b/plugins/metal/plugin_test.go index e027059..49fa01d 100644 --- a/plugins/metal/plugin_test.go +++ b/plugins/metal/plugin_test.go @@ -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" @@ -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()) }) @@ -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) @@ -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)