Skip to content

Commit 14a9201

Browse files
committed
feat(agent): use group CIDR from network status when VM has spec.networkGroup set
1 parent 8a071c7 commit 14a9201

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

internal/agent/firecracker_driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func (d *FirecrackerDriver) setupNetwork(ctx context.Context, vm *impdevv1alpha1
507507
tapName := network.TAPName(vKey)
508508
macAddr := network.MACAddr(vKey)
509509

510-
allocSubnet, err := resolveAllocationSubnet(ctx, d.Client, &impNet)
510+
allocSubnet, err := resolveAllocationSubnet(ctx, d.Client, &impNet, vm.Spec.NetworkGroup)
511511
if err != nil {
512512
return nil, fmt.Errorf("resolve allocation subnet: %w", err)
513513
}

internal/agent/ipam_resolver.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ func ciliumPoolGVK(version string) schema.GroupVersionKind {
2222
}
2323

2424
func resolveAllocationSubnet(
25-
ctx context.Context, c ctrlclient.Client, impNet *impdevv1alpha1.ImpNetwork,
25+
ctx context.Context, c ctrlclient.Client, impNet *impdevv1alpha1.ImpNetwork, networkGroup string,
2626
) (string, error) {
27+
// If VM is in a named group, return the group's allocated CIDR from network status.
28+
if networkGroup != "" {
29+
for _, gc := range impNet.Status.GroupCIDRs {
30+
if gc.Name == networkGroup {
31+
return gc.CIDR, nil
32+
}
33+
}
34+
return "", fmt.Errorf("network group %q has no allocated CIDR yet (network controller may not have reconciled)", networkGroup)
35+
}
36+
2737
if impNet.Spec.IPAM == nil || impNet.Spec.IPAM.Provider == "" || impNet.Spec.IPAM.Provider == "internal" {
2838
return impNet.Spec.Subnet, nil
2939
}

internal/agent/ipam_resolver_test.go

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestResolveAllocationSubnet_DefaultsToImpSubnet(t *testing.T) {
2727
},
2828
}
2929

30-
got, err := resolveAllocationSubnet(context.Background(), c, impNet)
30+
got, err := resolveAllocationSubnet(context.Background(), c, impNet, "")
3131
if err != nil {
3232
t.Fatalf("resolveAllocationSubnet: %v", err)
3333
}
@@ -63,7 +63,7 @@ func TestResolveAllocationSubnet_UsesCiliumPoolCIDR(t *testing.T) {
6363
},
6464
}
6565

66-
got, err := resolveAllocationSubnet(context.Background(), c, impNet)
66+
got, err := resolveAllocationSubnet(context.Background(), c, impNet, "")
6767
if err != nil {
6868
t.Fatalf("resolveAllocationSubnet: %v", err)
6969
}
@@ -87,7 +87,7 @@ func TestResolveAllocationSubnet_CiliumWithOverrideCidr(t *testing.T) {
8787
}
8888
// When Cidr override is set, should use it without fetching pool from API.
8989
// Pass nil client to prove it doesn't touch the API.
90-
subnet, err := resolveAllocationSubnet(context.Background(), nil, net)
90+
subnet, err := resolveAllocationSubnet(context.Background(), nil, net, "")
9191
if err != nil {
9292
t.Fatalf("resolveAllocationSubnet: %v", err)
9393
}
@@ -96,6 +96,56 @@ func TestResolveAllocationSubnet_CiliumWithOverrideCidr(t *testing.T) {
9696
}
9797
}
9898

99+
func TestResolveAllocationSubnet_UsesGroupCIDRFromStatus(t *testing.T) {
100+
scheme := runtime.NewScheme()
101+
if err := impdevv1alpha1.AddToScheme(scheme); err != nil {
102+
t.Fatalf("scheme: %v", err)
103+
}
104+
c := fake.NewClientBuilder().WithScheme(scheme).Build()
105+
106+
impNet := &impdevv1alpha1.ImpNetwork{
107+
ObjectMeta: metav1.ObjectMeta{Name: "net1", Namespace: "default"},
108+
Spec: impdevv1alpha1.ImpNetworkSpec{
109+
Subnet: "10.44.0.0/24",
110+
Groups: []impdevv1alpha1.NetworkGroupSpec{
111+
{Name: "workers", ExpectedSize: 14},
112+
},
113+
},
114+
}
115+
impNet.Status.GroupCIDRs = []impdevv1alpha1.GroupCIDR{
116+
{Name: "workers", CIDR: "10.44.0.0/28"},
117+
}
118+
119+
got, err := resolveAllocationSubnet(context.Background(), c, impNet, "workers")
120+
if err != nil {
121+
t.Fatalf("resolveAllocationSubnet: %v", err)
122+
}
123+
if got != "10.44.0.0/28" {
124+
t.Fatalf("got %q, want %q", got, "10.44.0.0/28")
125+
}
126+
}
127+
128+
func TestResolveAllocationSubnet_GroupNotAllocatedYetReturnsError(t *testing.T) {
129+
scheme := runtime.NewScheme()
130+
if err := impdevv1alpha1.AddToScheme(scheme); err != nil {
131+
t.Fatalf("scheme: %v", err)
132+
}
133+
c := fake.NewClientBuilder().WithScheme(scheme).Build()
134+
135+
impNet := &impdevv1alpha1.ImpNetwork{
136+
ObjectMeta: metav1.ObjectMeta{Name: "net1", Namespace: "default"},
137+
Spec: impdevv1alpha1.ImpNetworkSpec{
138+
Subnet: "10.44.0.0/24",
139+
},
140+
// Status.GroupCIDRs is empty — controller hasn't reconciled yet.
141+
}
142+
143+
_, err := resolveAllocationSubnet(context.Background(), c, impNet, "workers")
144+
if err == nil {
145+
t.Fatal("expected error for unallocated group CIDR")
146+
}
147+
}
148+
99149
func TestResolveAllocationSubnet_CiliumPoolMissingReturnsError(t *testing.T) {
100150
scheme := runtime.NewScheme()
101151
if err := impdevv1alpha1.AddToScheme(scheme); err != nil {
@@ -114,7 +164,7 @@ func TestResolveAllocationSubnet_CiliumPoolMissingReturnsError(t *testing.T) {
114164
},
115165
}
116166

117-
_, err := resolveAllocationSubnet(context.Background(), c, impNet)
167+
_, err := resolveAllocationSubnet(context.Background(), c, impNet, "")
118168
if err == nil {
119169
t.Fatal("expected error for missing CiliumPodIPPool")
120170
}

0 commit comments

Comments
 (0)