Skip to content

Commit 9b13f7f

Browse files
committed
Add dnsServer to IPPool and IPAddress
This commit adds the dnsServer field to the IPPool object, both on a per pool and global basis within that object. It also adds it in the IPAddress object, populated based on the value in the IPPool
1 parent 82a1839 commit 9b13f7f

File tree

7 files changed

+142
-23
lines changed

7 files changed

+142
-23
lines changed

api/v1alpha1/ipaddress_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ type IPAddressSpec struct {
4545

4646
// Address contains the IP address
4747
Address IPAddressStr `json:"address"`
48+
49+
// DNSServers is the list of dns servers
50+
DNSServers []IPAddressStr `json:"dnsServers,omitempty"`
4851
}
4952

5053
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

api/v1alpha1/ippool_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type Pool struct {
4848

4949
// Gateway is the gateway ip address
5050
Gateway *IPAddressStr `json:"gateway,omitempty"`
51+
52+
// DNSServers is the list of dns servers
53+
DNSServers []IPAddressStr `json:"dnsServers,omitempty"`
5154
}
5255

5356
// IPPoolSpec defines the desired state of IPPool.
@@ -69,6 +72,9 @@ type IPPoolSpec struct {
6972
// Gateway is the gateway ip address
7073
Gateway *IPAddressStr `json:"gateway,omitempty"`
7174

75+
// DNSServers is the list of dns servers
76+
DNSServers []IPAddressStr `json:"dnsServers,omitempty"`
77+
7278
// +kubebuilder:validation:MinLength=1
7379
// namePrefix is the prefix used to generate the IPAddress object names
7480
NamePrefix string `json:"namePrefix"`

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/ipam.metal3.io_ipaddresses.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ spec:
8282
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
8383
type: string
8484
type: object
85+
dnsServers:
86+
description: DNSServers is the list of dns servers
87+
items:
88+
description: IPAddress is used for validation of an IP address
89+
pattern: ((^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$)|(^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$))
90+
type: string
91+
type: array
8592
gateway:
8693
description: Gateway is the gateway ip address
8794
pattern: ((^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$)|(^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$))

config/crd/bases/ipam.metal3.io_ippools.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ spec:
5050
description: ClusterName is the name of the Cluster this object belongs
5151
to.
5252
type: string
53+
dnsServers:
54+
description: DNSServers is the list of dns servers
55+
items:
56+
description: IPAddress is used for validation of an IP address
57+
pattern: ((^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$)|(^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$))
58+
type: string
59+
type: array
5360
gateway:
5461
description: Gateway is the gateway ip address
5562
pattern: ((^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$)|(^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$))
@@ -65,6 +72,13 @@ spec:
6572
description: MetaDataIPAddress contains the info to render th ip
6673
address. It is IP-version agnostic
6774
properties:
75+
dnsServers:
76+
description: DNSServers is the list of dns servers
77+
items:
78+
description: IPAddress is used for validation of an IP address
79+
pattern: ((^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$)|(^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$))
80+
type: string
81+
type: array
6882
end:
6983
description: End is the last IP address that can be rendered.
7084
It is used as a validation that the rendered IP is in bound.

ipam/ippool_manager.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,20 @@ func (m *IPPoolManager) updateAddress(ctx context.Context,
230230

231231
func (m *IPPoolManager) allocateAddress(addressClaim *ipamv1.IPClaim,
232232
addresses map[ipamv1.IPAddressStr]string,
233-
) (ipamv1.IPAddressStr, int, *ipamv1.IPAddressStr, error) {
233+
) (ipamv1.IPAddressStr, int, *ipamv1.IPAddressStr, []ipamv1.IPAddressStr, error) {
234234
var err error
235235

236236
// Get pre-allocated addresses
237237
allocatedAddress, ipAllocated := m.IPPool.Spec.PreAllocations[addressClaim.Name]
238238
// If the IP is pre-allocated, the default prefix and gateway are used
239239
prefix := m.IPPool.Spec.Prefix
240240
gateway := m.IPPool.Spec.Gateway
241+
dnsServers := m.IPPool.Spec.DNSServers
241242

242243
for _, pool := range m.IPPool.Spec.Pools {
243244
if ipAllocated {
244245
break
245246
}
246-
if pool.Prefix != 0 {
247-
prefix = pool.Prefix
248-
}
249-
if pool.Gateway != nil {
250-
gateway = pool.Gateway
251-
}
252247
index := 0
253248
for !ipAllocated {
254249
allocatedAddress, err = getIPAddress(pool, index)
@@ -258,14 +253,23 @@ func (m *IPPoolManager) allocateAddress(addressClaim *ipamv1.IPClaim,
258253
index++
259254
if _, ok := addresses[allocatedAddress]; !ok && allocatedAddress != "" {
260255
ipAllocated = true
256+
if pool.Prefix != 0 {
257+
prefix = pool.Prefix
258+
}
259+
if pool.Gateway != nil {
260+
gateway = pool.Gateway
261+
}
262+
if len(pool.DNSServers) != 0 {
263+
dnsServers = pool.DNSServers
264+
}
261265
}
262266
}
263267
}
264268
if !ipAllocated {
265269
addressClaim.Status.ErrorMessage = pointer.StringPtr("Exhausted IP Pools")
266-
return "", 0, nil, errors.New("Exhausted IP Pools")
270+
return "", 0, nil, []ipamv1.IPAddressStr{}, errors.New("Exhausted IP Pools")
267271
}
268-
return allocatedAddress, prefix, gateway, nil
272+
return allocatedAddress, prefix, gateway, dnsServers, nil
269273
}
270274

271275
func (m *IPPoolManager) createAddress(ctx context.Context,
@@ -288,7 +292,7 @@ func (m *IPPoolManager) createAddress(ctx context.Context,
288292
// Get a new index for this machine
289293
m.Log.Info("Getting address", "Claim", addressClaim.Name)
290294
// Get a new IP for this owner
291-
allocatedAddress, prefix, gateway, err := m.allocateAddress(addressClaim, addresses)
295+
allocatedAddress, prefix, gateway, dnsServers, err := m.allocateAddress(addressClaim, addresses)
292296
if err != nil {
293297
return addresses, err
294298
}
@@ -337,8 +341,9 @@ func (m *IPPoolManager) createAddress(ctx context.Context,
337341
Name: addressClaim.Name,
338342
Namespace: m.IPPool.Namespace,
339343
},
340-
Prefix: prefix,
341-
Gateway: gateway,
344+
Prefix: prefix,
345+
Gateway: gateway,
346+
DNSServers: dnsServers,
342347
},
343348
}
344349

ipam/ippool_manager_test.go

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,14 @@ var _ = Describe("IPPool manager", func() {
679679
)
680680

681681
type testCaseAllocateAddress struct {
682-
ipPool *ipamv1.IPPool
683-
ipClaim *ipamv1.IPClaim
684-
addresses map[ipamv1.IPAddressStr]string
685-
expectedAddress ipamv1.IPAddressStr
686-
expectedPrefix int
687-
expectedGateway *ipamv1.IPAddressStr
688-
expectError bool
682+
ipPool *ipamv1.IPPool
683+
ipClaim *ipamv1.IPClaim
684+
addresses map[ipamv1.IPAddressStr]string
685+
expectedAddress ipamv1.IPAddressStr
686+
expectedPrefix int
687+
expectedGateway *ipamv1.IPAddressStr
688+
expectedDNSServers []ipamv1.IPAddressStr
689+
expectError bool
689690
}
690691

691692
DescribeTable("Test AllocateAddress",
@@ -694,7 +695,7 @@ var _ = Describe("IPPool manager", func() {
694695
klogr.New(),
695696
)
696697
Expect(err).NotTo(HaveOccurred())
697-
allocatedAddress, prefix, gateway, err := ipPoolMgr.allocateAddress(
698+
allocatedAddress, prefix, gateway, dnsServers, err := ipPoolMgr.allocateAddress(
698699
tc.ipClaim, tc.addresses,
699700
)
700701
if tc.expectError {
@@ -706,6 +707,7 @@ var _ = Describe("IPPool manager", func() {
706707
Expect(allocatedAddress).To(Equal(tc.expectedAddress))
707708
Expect(prefix).To(Equal(tc.expectedPrefix))
708709
Expect(*gateway).To(Equal(*tc.expectedGateway))
710+
Expect(dnsServers).To(Equal(tc.expectedDNSServers))
709711
},
710712
Entry("Empty pools", testCaseAllocateAddress{
711713
ipPool: &ipamv1.IPPool{
@@ -727,13 +729,19 @@ var _ = Describe("IPPool manager", func() {
727729
End: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.20")),
728730
Prefix: 26,
729731
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.1.1")),
732+
DNSServers: []ipamv1.IPAddressStr{
733+
ipamv1.IPAddressStr("8.8.8.8"),
734+
},
730735
},
731736
},
732737
PreAllocations: map[string]ipamv1.IPAddressStr{
733738
"TestRef": ipamv1.IPAddressStr("192.168.0.15"),
734739
},
735740
Prefix: 24,
736741
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.1")),
742+
DNSServers: []ipamv1.IPAddressStr{
743+
ipamv1.IPAddressStr("8.8.4.4"),
744+
},
737745
},
738746
},
739747
ipClaim: &ipamv1.IPClaim{
@@ -743,7 +751,10 @@ var _ = Describe("IPPool manager", func() {
743751
},
744752
expectedAddress: ipamv1.IPAddressStr("192.168.0.15"),
745753
expectedGateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.1")),
746-
expectedPrefix: 24,
754+
expectedDNSServers: []ipamv1.IPAddressStr{
755+
ipamv1.IPAddressStr("8.8.4.4"),
756+
},
757+
expectedPrefix: 24,
747758
}),
748759
Entry("One pool, with start and existing address", testCaseAllocateAddress{
749760
ipPool: &ipamv1.IPPool{
@@ -780,10 +791,16 @@ var _ = Describe("IPPool manager", func() {
780791
End: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.20")),
781792
Prefix: 24,
782793
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.1")),
794+
DNSServers: []ipamv1.IPAddressStr{
795+
ipamv1.IPAddressStr("8.8.8.8"),
796+
},
783797
},
784798
},
785799
Prefix: 26,
786800
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.1.1")),
801+
DNSServers: []ipamv1.IPAddressStr{
802+
ipamv1.IPAddressStr("8.8.4.4"),
803+
},
787804
},
788805
},
789806
ipClaim: &ipamv1.IPClaim{
@@ -797,7 +814,50 @@ var _ = Describe("IPPool manager", func() {
797814
},
798815
expectedAddress: ipamv1.IPAddressStr("192.168.0.13"),
799816
expectedGateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.1")),
800-
expectedPrefix: 24,
817+
expectedDNSServers: []ipamv1.IPAddressStr{
818+
ipamv1.IPAddressStr("8.8.8.8"),
819+
},
820+
expectedPrefix: 24,
821+
}),
822+
Entry("two pools, with subnet and override prefix in first", testCaseAllocateAddress{
823+
ipPool: &ipamv1.IPPool{
824+
Spec: ipamv1.IPPoolSpec{
825+
Pools: []ipamv1.Pool{
826+
ipamv1.Pool{
827+
Start: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.10")),
828+
End: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.0.10")),
829+
Prefix: 24,
830+
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.1.1")),
831+
DNSServers: []ipamv1.IPAddressStr{
832+
ipamv1.IPAddressStr("8.8.8.8"),
833+
},
834+
},
835+
ipamv1.Pool{
836+
Subnet: (*ipamv1.IPSubnetStr)(pointer.StringPtr("192.168.1.10/24")),
837+
},
838+
},
839+
Prefix: 26,
840+
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.2.1")),
841+
DNSServers: []ipamv1.IPAddressStr{
842+
ipamv1.IPAddressStr("8.8.4.4"),
843+
},
844+
},
845+
},
846+
ipClaim: &ipamv1.IPClaim{
847+
ObjectMeta: metav1.ObjectMeta{
848+
Name: "TestRef",
849+
},
850+
},
851+
addresses: map[ipamv1.IPAddressStr]string{
852+
ipamv1.IPAddressStr("192.168.1.11"): "bcde",
853+
ipamv1.IPAddressStr("192.168.0.10"): "abcd",
854+
},
855+
expectedAddress: ipamv1.IPAddressStr("192.168.1.12"),
856+
expectedGateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.2.1")),
857+
expectedDNSServers: []ipamv1.IPAddressStr{
858+
ipamv1.IPAddressStr("8.8.4.4"),
859+
},
860+
expectedPrefix: 26,
801861
}),
802862
Entry("two pools, with subnet and override prefix", testCaseAllocateAddress{
803863
ipPool: &ipamv1.IPPool{
@@ -811,10 +871,16 @@ var _ = Describe("IPPool manager", func() {
811871
Subnet: (*ipamv1.IPSubnetStr)(pointer.StringPtr("192.168.1.10/24")),
812872
Prefix: 24,
813873
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.1.1")),
874+
DNSServers: []ipamv1.IPAddressStr{
875+
ipamv1.IPAddressStr("8.8.8.8"),
876+
},
814877
},
815878
},
816879
Prefix: 26,
817880
Gateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.2.1")),
881+
DNSServers: []ipamv1.IPAddressStr{
882+
ipamv1.IPAddressStr("8.8.4.4"),
883+
},
818884
},
819885
},
820886
ipClaim: &ipamv1.IPClaim{
@@ -828,7 +894,10 @@ var _ = Describe("IPPool manager", func() {
828894
},
829895
expectedAddress: ipamv1.IPAddressStr("192.168.1.12"),
830896
expectedGateway: (*ipamv1.IPAddressStr)(pointer.StringPtr("192.168.1.1")),
831-
expectedPrefix: 24,
897+
expectedDNSServers: []ipamv1.IPAddressStr{
898+
ipamv1.IPAddressStr("8.8.8.8"),
899+
},
900+
expectedPrefix: 24,
832901
}),
833902
Entry("Exhausted pools start", testCaseAllocateAddress{
834903
ipPool: &ipamv1.IPPool{

0 commit comments

Comments
 (0)