From 10a350e0aa72941193d2de6c1773f4ba0128e194 Mon Sep 17 00:00:00 2001 From: Dan Kenigsberg Date: Tue, 8 Aug 2023 11:11:36 +0300 Subject: [PATCH] Add a default A record for each VM make vm.namespace. resolve to one of the VM-reported IP addresses. This may make it a bit easier to find the VM even if the user does not recall its interface names. Signed-off-by: Dan Kenigsberg --- .../zone-file-cache/zone_file_cache.go | 12 +++++-- .../zone-file-cache/zone_file_cache_test.go | 35 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/pkg/zonemgr/internal/zone-file-cache/zone_file_cache.go b/pkg/zonemgr/internal/zone-file-cache/zone_file_cache.go index af37625..4efbc1f 100644 --- a/pkg/zonemgr/internal/zone-file-cache/zone_file_cache.go +++ b/pkg/zonemgr/internal/zone-file-cache/zone_file_cache.go @@ -114,20 +114,28 @@ func buildARecordsArr(name string, namespace string, interfaces []v1.VirtualMach IPs := iface.IPs for _, IP := range IPs { if net.IsIPv4String(IP) { - recordsArr = append(recordsArr, generateARecord(name, namespace, iface.Name, IP)) + recordsArr = append(recordsArr, generateIfaceARecord(name, namespace, iface.Name, IP)) break } } } sort.Strings(recordsArr) + if len(recordsArr) > 0 { + recordsArr = append(recordsArr, generateDefaultARecord(name, namespace, recordsArr[0])) + } return recordsArr } -func generateARecord(name string, namespace string, ifaceName string, ifaceIP string) string { +func generateIfaceARecord(name string, namespace string, ifaceName string, ifaceIP string) string { fqdn := fmt.Sprintf("%s.%s.%s", ifaceName, name, namespace) return fmt.Sprintf("%s IN A %s\n", fqdn, ifaceIP) } +func generateDefaultARecord(name string, namespace string, ifaceIP string) string { + fqdn := fmt.Sprintf("%s.%s", name, namespace) + return fmt.Sprintf("%s IN A %s\n", fqdn, ifaceIP) +} + func (zoneFileCache ZoneFileCache) generateARecords() string { aRecords := "" for _, recordsArr := range zoneFileCache.vmiRecordsMap { diff --git a/pkg/zonemgr/internal/zone-file-cache/zone_file_cache_test.go b/pkg/zonemgr/internal/zone-file-cache/zone_file_cache_test.go index 7a41002..db03f68 100644 --- a/pkg/zonemgr/internal/zone-file-cache/zone_file_cache_test.go +++ b/pkg/zonemgr/internal/zone-file-cache/zone_file_cache_test.go @@ -63,7 +63,8 @@ var _ = Describe("cached zone file content maintenance", func() { nic4IP = "13.14.15.16" IPv6 = "fe80::74c8:f2ff:fe5f:ff2b" - aRecordFmt = "%s.%s.%s IN A %s\n" + aRecordFmt = "%s.%s.%s IN A %s\n" + defaultARecordFmt = "%s.%s IN A %s\n" updated = true notUpdated = false @@ -81,6 +82,11 @@ var _ = Describe("cached zone file content maintenance", func() { aRecord_nic3_vm1_ns2 = fmt.Sprintf(aRecordFmt, nic3Name, vmi1Name, namespace2, nic3IP) aRecord_nic4_vm1_ns2 = fmt.Sprintf(aRecordFmt, nic4Name, vmi1Name, namespace2, nic4IP) + + defARecord_nic1_vm1_ns1 = fmt.Sprintf(defARecordFmt, vmi1Name, namespace1, nic1IP) + defARecord_nic2_vm1_ns1 = fmt.Sprintf(defARecordFmt, vmi1Name, namespace1, nic2IP) + defARecord_nic1_vm2_ns1 = fmt.Sprintf(defARecordFmt, vmi2Name, namespace1, nic1IP) + defARecord_nic3_vm2_ns1 = fmt.Sprintf(defARecordFmt, vmi2Name, namespace1, nic3IP) ) validateUpdateFunc := func(vmiName, vmiNamespace string, newInterfaces []v1.VirtualMachineInstanceNetworkInterface, @@ -102,7 +108,7 @@ var _ = Describe("cached zone file content maintenance", func() { namespace1, []v1.VirtualMachineInstanceNetworkInterface{{IPs: []string{nic1IP}, Name: nic1Name}}, updated, - aRecord_nic1_vm1_ns1, + aRecord_nic1_vm1_ns1+defARecord_nic1_vm1_ns1, 1, ), Entry("when non existing vmi is deleted", @@ -142,8 +148,10 @@ var _ = Describe("cached zone file content maintenance", func() { updated, aRecord_nic1_vm1_ns1+ aRecord_nic2_vm1_ns1+ + defARecord_nic1_vm1_ns1+ aRecord_nic3_vm2_ns1+ - aRecord_nic4_vm2_ns1, + aRecord_nic4_vm2_ns1+ + defARecord_nic3_vm2_ns1, 2, ), Entry("when existing vmi is deleted", @@ -160,7 +168,8 @@ var _ = Describe("cached zone file content maintenance", func() { []v1.VirtualMachineInstanceNetworkInterface{{IPs: []string{nic1IP}, Name: nic1Name}, {IPs: []string{nic4IP}, Name: nic4Name}}, updated, aRecord_nic1_vm1_ns1+ - aRecord_nic4_vm1_ns1, + aRecord_nic4_vm1_ns1+ + defARecord_nic1_vm1_ns1, 2, ), Entry("when existing vmi is not changed but its interfaces order is changed", @@ -169,7 +178,8 @@ var _ = Describe("cached zone file content maintenance", func() { []v1.VirtualMachineInstanceNetworkInterface{{IPs: []string{nic2IP}, Name: nic2Name}, {IPs: []string{nic1IP}, Name: nic1Name}}, notUpdated, aRecord_nic1_vm1_ns1+ - aRecord_nic2_vm1_ns1, + aRecord_nic2_vm1_ns1+ + defARecord_nic1_vm1_ns1, 1, ), ) @@ -197,7 +207,9 @@ var _ = Describe("cached zone file content maintenance", func() { aRecord_nic1_vm2_ns1+ aRecord_nic2_vm2_ns1+ aRecord_nic3_vm1_ns2+ - aRecord_nic4_vm1_ns2, + aRecord_nic4_vm1_ns2+ + defARecord_nic2_vm1_ns1+ + defARecord_nic1_vm2_ns1, 3, ), Entry("when existing vmi is deleted", @@ -216,8 +228,10 @@ var _ = Describe("cached zone file content maintenance", func() { updated, aRecord_nic1_vm1_ns1+ aRecord_nic4_vm1_ns1+ + defARecord_nic1_vm1_ns1+ aRecord_nic1_vm2_ns1+ - aRecord_nic2_vm2_ns1, + aRecord_nic2_vm2_ns1+ + defARecord_nic1_vm2_ns1, 3, ), Entry("when existing vmi is not changed but its interfaces order is changed", @@ -227,8 +241,10 @@ var _ = Describe("cached zone file content maintenance", func() { notUpdated, aRecord_nic2_vm1_ns1+ aRecord_nic1_vm1_ns1+ + defARecord_nic1_vm1_ns1+ aRecord_nic1_vm2_ns1+ - aRecord_nic2_vm2_ns1, + aRecord_nic2_vm2_ns1+ + defARecord_nic2_vm2_ns1, 2, ), ) @@ -246,7 +262,8 @@ var _ = Describe("cached zone file content maintenance", func() { []v1.VirtualMachineInstanceNetworkInterface{{IPs: []string{nic1IP, IPv6}, Name: nic1Name}, {IPs: []string{nic2IP, IPv6}, Name: nic2Name}}, updated, aRecord_nic1_vm1_ns1+ - aRecord_nic2_vm1_ns1, + aRecord_nic2_vm1_ns1+ + defARecord_nic1_vm1_ns1, 1, ), Entry("vmi interfaces contain IPv6 only",