Skip to content

Commit 736a064

Browse files
Felix Wischke (65278)wikkyk
authored andcommitted
proxmox_webhook: check routing policy tables (netplan lied more)
1 parent 76f1061 commit 736a064

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

api/v1alpha1/proxmoxmachine_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ type RoutingPolicySpec struct {
281281

282282
// Table is the routing table ID.
283283
// +optional
284-
Table uint32 `json:"table,omitempty"`
284+
Table *uint32 `json:"table,omitempty"`
285285

286286
// Priority is the position in the ip rule FIB table.
287287
// +kubebuilder:validation:Maximum=4294967295

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/service/vmservice/bootstrap.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ func getRoutingPolicyData(rules []infrav1alpha1.RoutingPolicySpec) *[]cloudinit.
168168
ruleSpec.To = rule.To
169169
ruleSpec.From = rule.From
170170
ruleSpec.Priority = rule.Priority
171-
ruleSpec.Table = rule.Table
171+
if rule.Table != nil {
172+
ruleSpec.Table = *rule.Table
173+
}
172174
routingPolicyData = append(routingPolicyData, ruleSpec)
173175
}
174176

internal/service/vmservice/bootstrap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ func TestGetCommonInterfaceConfig(t *testing.T) {
205205
{To: "172.24.16.0/24", Via: "192.168.178.1", Table: 100},
206206
},
207207
RoutingPolicy: []infrav1alpha1.RoutingPolicySpec{
208-
{To: "10.10.10.0/24", Table: 100},
209-
{From: "172.24.16.0/24", Table: 100},
208+
{To: "10.10.10.0/24", Table: ptr.To(uint32(100))},
209+
{From: "172.24.16.0/24", Table: ptr.To(uint32(100))},
210210
},
211211
},
212212
},

internal/webhook/proxmoxmachine_webhook.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,53 @@ func validateNetworks(machine *infrav1.ProxmoxMachine) error {
120120
field.NewPath("spec", "network", "additionalDevices", fmt.Sprint(i), "linkMtu"), machine.Spec.Network.AdditionalDevices[i], err.Error()),
121121
})
122122
}
123+
err = validateRoutingPolicy(&machine.Spec.Network.AdditionalDevices[i].InterfaceConfig.RoutingPolicy)
124+
if err != nil {
125+
return apierrors.NewInvalid(
126+
gk,
127+
name,
128+
field.ErrorList{
129+
field.Invalid(
130+
field.NewPath("spec", "network", "additionalDevices", fmt.Sprint(i), "routingPolicy"), machine.Spec.Network.AdditionalDevices[i], err.Error()),
131+
})
132+
}
133+
}
134+
135+
for i := range machine.Spec.Network.VirtualNetworkDevices.VRFs {
136+
err := validateVRFConfigRoutingPolicy(&machine.Spec.Network.VirtualNetworkDevices.VRFs[i])
137+
if err != nil {
138+
return apierrors.NewInvalid(
139+
gk,
140+
name,
141+
field.ErrorList{
142+
field.Invalid(
143+
field.NewPath("spec", "network", "VirtualNetworkDevices", "VRFs", fmt.Sprint(i), "Table"), machine.Spec.Network.VirtualNetworkDevices.VRFs[i], err.Error()),
144+
})
145+
}
146+
}
147+
148+
return nil
149+
}
150+
151+
func validateRoutingPolicy(policies *[]infrav1.RoutingPolicySpec) error {
152+
for i, policy := range *policies {
153+
if policy.Table == nil {
154+
return fmt.Errorf("routing policy [%d] requires a table", i)
155+
}
123156
}
157+
return nil
158+
}
124159

160+
func validateVRFConfigRoutingPolicy(vrf *infrav1.VRFDevice) error {
161+
for _, policy := range vrf.Routing.RoutingPolicy {
162+
// Netplan will not accept rules not matching the l3mdev table, although
163+
// there is no technical reason for this limitation.
164+
if policy.Table != nil {
165+
if *policy.Table != vrf.Table {
166+
return fmt.Errorf("VRF %s: device/rule routing table mismatch %d != %d", vrf.Name, vrf.Table, *policy.Table)
167+
}
168+
}
169+
}
125170
return nil
126171
}
127172

internal/webhook/proxmoxmachine_webhook_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ var _ = Describe("Controller Test", func() {
6565
machine.Spec.Network.AdditionalDevices[0].LinkMTU = ptr.To(uint16(1000))
6666
g.Expect(k8sClient.Create(testEnv.GetContext(), &machine)).To(MatchError(ContainSubstring("mtu must be at least 1280, but was 1000")))
6767
})
68+
69+
It("should disallow conflicting l3mdev/routing policy table", func() {
70+
machine := validProxmoxMachine("test-machine")
71+
*machine.Spec.Network.VirtualNetworkDevices.VRFs[0].Routing.RoutingPolicy[0].Table = 667
72+
g.Expect(k8sClient.Create(testEnv.GetContext(), &machine)).To(MatchError(ContainSubstring("VRF vrf-green: device/rule routing table mismatch 665 != 667")))
73+
})
74+
75+
It("should disallow routing policy without table", func() {
76+
machine := validProxmoxMachine("test-machine")
77+
machine.Spec.Network.AdditionalDevices[0].InterfaceConfig.Routing.RoutingPolicy[0].Table = nil
78+
g.Expect(k8sClient.Create(testEnv.GetContext(), &machine)).To(MatchError(ContainSubstring("routing policy [0] requires a table")))
79+
})
6880
})
6981

7082
Context("update proxmox cluster", func() {
@@ -131,9 +143,25 @@ func validProxmoxMachine(name string) infrav1.ProxmoxMachine {
131143
Kind: "InClusterIPPool",
132144
APIGroup: ptr.To("ipam.cluster.x-k8s.io"),
133145
},
146+
Routing: infrav1.Routing{
147+
RoutingPolicy: []infrav1.RoutingPolicySpec{{
148+
Table: ptr.To(uint32(665)),
149+
}},
150+
},
134151
},
135152
},
136153
},
154+
VirtualNetworkDevices: infrav1.VirtualNetworkDevices{
155+
VRFs: []infrav1.VRFDevice{{
156+
Table: 665,
157+
Name: "vrf-green",
158+
Routing: infrav1.Routing{
159+
RoutingPolicy: []infrav1.RoutingPolicySpec{{
160+
Table: ptr.To(uint32(665)),
161+
}},
162+
}},
163+
},
164+
},
137165
},
138166
},
139167
}

0 commit comments

Comments
 (0)