@@ -18,6 +18,7 @@ import (
1818 fakeTypes "github.com/cilium/cilium/pkg/datapath/fake/types"
1919 ipamOption "github.com/cilium/cilium/pkg/ipam/option"
2020 ipamTypes "github.com/cilium/cilium/pkg/ipam/types"
21+ "github.com/cilium/cilium/pkg/ipmasq"
2122 ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
2223 "github.com/cilium/cilium/pkg/node"
2324 "github.com/cilium/cilium/pkg/option"
@@ -95,12 +96,12 @@ func TestMarkForReleaseNoAllocate(t *testing.T) {
9596
9697 fakeAddressing := fakeTypes .NewNodeAddressing ()
9798 conf := testConfigurationCRD
98- initNodeStore .Do (func () {
99- sharedNodeStore = newFakeNodeStore (conf , t )
100- sharedNodeStore .ownNode = cn
101- })
99+ initNodeStore .Do (func () {}) // Ensure the real initNodeStore is not called
100+ sharedNodeStore = newFakeNodeStore (conf , t )
101+ sharedNodeStore .ownNode = cn
102+
102103 localNodeStore := node .NewTestLocalNodeStore (node.LocalNode {})
103- ipam := NewIPAM (fakeAddressing , conf , & ownerMock {}, localNodeStore , & ownerMock {}, & resourceMock {}, & mtuMock , nil , nil , nil )
104+ ipam := NewIPAM (fakeAddressing , conf , & ownerMock {}, localNodeStore , & ownerMock {}, & resourceMock {}, & mtuMock , nil , nil , nil , nil )
104105 ipam .ConfigureAllocator ()
105106 sharedNodeStore .updateLocalNodeResource (cn )
106107
@@ -127,6 +128,79 @@ func TestMarkForReleaseNoAllocate(t *testing.T) {
127128 require .Equal (t , ipamOption .IPAMDoNotRelease , string (cn .Status .IPAM .ReleaseIPs ["1.1.1.3" ]))
128129}
129130
131+ type ipMasqMapDummy struct {}
132+
133+ func (m ipMasqMapDummy ) Update (netip.Prefix ) error { return nil }
134+ func (m ipMasqMapDummy ) Delete (netip.Prefix ) error { return nil }
135+ func (m ipMasqMapDummy ) Dump () ([]netip.Prefix , error ) { return []netip.Prefix {}, nil }
136+
137+ func TestIPMasq (t * testing.T ) {
138+ cn := newCiliumNode ("node1" , 4 , 4 , 0 )
139+ dummyResource := ipamTypes.AllocationIP {Resource : "eni-1" }
140+ cn .Spec .IPAM .Pool ["10.1.1.226" ] = dummyResource
141+ cn .Status .ENI .ENIs = map [string ]eniTypes.ENI {
142+ "eni-1" : {
143+ ID : "eni-1" ,
144+ Addresses : []string {
145+ "10.1.1.226" ,
146+ "10.1.1.229" ,
147+ },
148+ VPC : eniTypes.AwsVPC {
149+ ID : "vpc-1" ,
150+ PrimaryCIDR : "10.1.0.0/16" ,
151+ CIDRs : []string {
152+ "10.2.0.0/16" ,
153+ },
154+ },
155+ },
156+ }
157+
158+ fakeAddressing := fakeTypes .NewNodeAddressing ()
159+ conf := testConfigurationCRD
160+ conf .IPAM = ipamOption .IPAMENI
161+ conf .EnableIPMasqAgent = true
162+ ipMasqAgent := ipmasq .NewIPMasqAgent ("" , ipMasqMapDummy {})
163+ err := ipMasqAgent .Start ()
164+ require .NoError (t , err )
165+
166+ initNodeStore .Do (func () {}) // Ensure the real initNodeStore is not called
167+ sharedNodeStore = newFakeNodeStore (conf , t )
168+ sharedNodeStore .ownNode = cn
169+
170+ localNodeStore := node .NewTestLocalNodeStore (node.LocalNode {})
171+ ipam := NewIPAM (fakeAddressing , conf , & ownerMock {}, localNodeStore , & ownerMock {}, & resourceMock {}, & mtuMock , nil , nil , nil , ipMasqAgent )
172+ ipam .ConfigureAllocator ()
173+
174+ epipv4 := netip .MustParseAddr ("10.1.1.226" )
175+ result , err := ipam .IPv4Allocator .Allocate (epipv4 .AsSlice (), "test1" , PoolDefault ())
176+ require .NoError (t , err )
177+ // The resulting CIDRs should contain the VPC CIDRs and the default ip-masq-agent CIDRs from pkg/ipmasq/ipmasq.go
178+ require .ElementsMatch (
179+ t ,
180+ []string {
181+ // VPC CIDRs
182+ "10.1.0.0/16" ,
183+ "10.2.0.0/16" ,
184+ // Default ip-masq-agent CIDRs
185+ "10.0.0.0/8" ,
186+ "172.16.0.0/12" ,
187+ "192.168.0.0/16" ,
188+ "100.64.0.0/10" ,
189+ "192.0.0.0/24" ,
190+ "192.0.2.0/24" ,
191+ "192.88.99.0/24" ,
192+ "198.18.0.0/15" ,
193+ "198.51.100.0/24" ,
194+ "203.0.113.0/24" ,
195+ "240.0.0.0/4" ,
196+ "169.254.0.0/16" ,
197+ },
198+ result .CIDRs ,
199+ )
200+
201+ ipMasqAgent .Stop ()
202+ }
203+
130204func Test_validateENIConfig (t * testing.T ) {
131205 type args struct {
132206 node * ciliumv2.CiliumNode
0 commit comments