Skip to content

Commit 0ada870

Browse files
kvapsclaude
andcommitted
feat(kg): add --internal-cidr flag to filter IP auto-detection
Add a new --internal-cidr flag that allows users to specify which CIDRs should be considered during internal IP auto-detection. This flag can be specified multiple times to allow multiple CIDRs. When set, only IPs within these CIDRs will be selected as the internal IP, preventing incorrect IP selection (e.g., when CiliumInternalIP is chosen over the real node IP due to lexicographic sorting). Example usage: --internal-cidr=192.168.0.0/16 --internal-cidr=$(NODE_IP)/32 Also update manifests for cilium and flannel deployments to use this flag with NODE_IP from Kubernetes Downward API. Co-Authored-By: Claude <[email protected]> Signed-off-by: Andrei Kvapil <[email protected]>
1 parent 66b81d5 commit 0ada870

File tree

10 files changed

+65
-4
lines changed

10 files changed

+65
-4
lines changed

cmd/kg/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ var (
120120
topologyLabel string
121121
port int
122122
serviceCIDRsRaw []string
123+
internalCIDRsRaw []string
123124
subnet string
124125
resyncPeriod time.Duration
125126
iptablesForwardRule bool
@@ -152,6 +153,7 @@ func init() {
152153
cmd.Flags().StringVar(&topologyLabel, "topology-label", k8s.RegionLabelKey, "Kubernetes node label used to group nodes into logical locations.")
153154
cmd.Flags().IntVar(&port, "port", mesh.DefaultKiloPort, "The port over which WireGuard peers should communicate.")
154155
cmd.Flags().StringSliceVar(&serviceCIDRsRaw, "service-cidr", nil, "The service CIDR for the Kubernetes cluster. Can be provided optionally to avoid masquerading packets sent to service IPs. Can be specified multiple times.")
156+
cmd.Flags().StringSliceVar(&internalCIDRsRaw, "internal-cidr", nil, "CIDRs to consider for internal IP auto-detection. If specified, only IPs within these CIDRs will be used. Can be specified multiple times.")
155157
cmd.Flags().StringVar(&subnet, "subnet", mesh.DefaultKiloSubnet.String(), "CIDR from which to allocate addresses for WireGuard interfaces.")
156158
cmd.Flags().DurationVar(&resyncPeriod, "resync-period", 30*time.Second, "How often should the Kilo controllers reconcile?")
157159
cmd.Flags().BoolVar(&iptablesForwardRule, "iptables-forward-rules", false, "Add default accept rules to the FORWARD chain in iptables. Warning: this may break firewalls with a deny all policy and is potentially insecure!")
@@ -266,7 +268,16 @@ func runRoot(_ *cobra.Command, _ []string) error {
266268
serviceCIDRs = append(serviceCIDRs, s)
267269
}
268270

269-
m, err := mesh.New(b, enc, gr, hostname, port, s, local, cni, cniPath, iface, cleanUp, cleanUpIface, createIface, mtu, resyncPeriod, prioritisePrivateAddr, iptablesForwardRule, serviceCIDRs, log.With(logger, "component", "kilo"), registry)
271+
var internalCIDRs []*net.IPNet
272+
for _, internalCIDR := range internalCIDRsRaw {
273+
_, s, err := net.ParseCIDR(internalCIDR)
274+
if err != nil {
275+
return fmt.Errorf("failed to parse %q as CIDR: %v", internalCIDR, err)
276+
}
277+
internalCIDRs = append(internalCIDRs, s)
278+
}
279+
280+
m, err := mesh.New(b, enc, gr, hostname, port, s, local, cni, cniPath, iface, cleanUp, cleanUpIface, createIface, mtu, resyncPeriod, prioritisePrivateAddr, iptablesForwardRule, internalCIDRs, serviceCIDRs, log.With(logger, "component", "kilo"), registry)
270281
if err != nil {
271282
return fmt.Errorf("failed to create Kilo mesh: %v", err)
272283
}

manifests/kilo-bootkube-flannel.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ spec:
7474
- --cni=false
7575
- --compatibility=flannel
7676
- --local=false
77+
- --internal-cidr=$(NODE_IP)/32
7778
env:
7879
- name: NODE_NAME
7980
valueFrom:
8081
fieldRef:
8182
fieldPath: spec.nodeName
83+
- name: NODE_IP
84+
valueFrom:
85+
fieldRef:
86+
fieldPath: status.hostIP
8287
ports:
8388
- containerPort: 1107
8489
name: metrics

manifests/kilo-k3s-cilium.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,16 @@ spec:
106106
- --encapsulate=crosssubnet
107107
- --clean-up-interface=true
108108
- --log-level=all
109+
- --internal-cidr=$(NODE_IP)/32
109110
env:
110111
- name: NODE_NAME
111112
valueFrom:
112113
fieldRef:
113114
fieldPath: spec.nodeName
115+
- name: NODE_IP
116+
valueFrom:
117+
fieldRef:
118+
fieldPath: status.hostIP
114119
ports:
115120
- containerPort: 1107
116121
name: metrics

manifests/kilo-k3s-flannel.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,16 @@ spec:
103103
- --cni=false
104104
- --compatibility=flannel
105105
- --local=false
106+
- --internal-cidr=$(NODE_IP)/32
106107
env:
107108
- name: NODE_NAME
108109
valueFrom:
109110
fieldRef:
110111
fieldPath: spec.nodeName
112+
- name: NODE_IP
113+
valueFrom:
114+
fieldRef:
115+
fieldPath: status.hostIP
111116
ports:
112117
- containerPort: 1107
113118
name: metrics

manifests/kilo-kubeadm-cilium.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,16 @@ spec:
7979
- --clean-up-interface=true
8080
- --subnet=172.31.254.0/24
8181
- --log-level=all
82+
- --internal-cidr=$(NODE_IP)/32
8283
env:
8384
- name: NODE_NAME
8485
valueFrom:
8586
fieldRef:
8687
fieldPath: spec.nodeName
88+
- name: NODE_IP
89+
valueFrom:
90+
fieldRef:
91+
fieldPath: status.hostIP
8792
ports:
8893
- containerPort: 1107
8994
name: metrics

manifests/kilo-kubeadm-flannel-userspace.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ spec:
8888
- --cni=false
8989
- --compatibility=flannel
9090
- --local=false
91+
- --internal-cidr=$(NODE_IP)/32
9192
env:
9293
- name: NODE_NAME
9394
valueFrom:
9495
fieldRef:
9596
fieldPath: spec.nodeName
97+
- name: NODE_IP
98+
valueFrom:
99+
fieldRef:
100+
fieldPath: status.hostIP
96101
ports:
97102
- containerPort: 1107
98103
name: metrics

manifests/kilo-kubeadm-flannel.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ spec:
7474
- --cni=false
7575
- --compatibility=flannel
7676
- --local=false
77+
- --internal-cidr=$(NODE_IP)/32
7778
env:
7879
- name: NODE_NAME
7980
valueFrom:
8081
fieldRef:
8182
fieldPath: spec.nodeName
83+
- name: NODE_IP
84+
valueFrom:
85+
fieldRef:
86+
fieldPath: status.hostIP
8287
ports:
8388
- containerPort: 1107
8489
name: metrics

manifests/kilo-typhoon-flannel.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ spec:
7474
- --cni=false
7575
- --compatibility=flannel
7676
- --local=false
77+
- --internal-cidr=$(NODE_IP)/32
7778
env:
7879
- name: NODE_NAME
7980
valueFrom:
8081
fieldRef:
8182
fieldPath: spec.nodeName
83+
- name: NODE_IP
84+
valueFrom:
85+
fieldRef:
86+
fieldPath: status.hostIP
8287
ports:
8388
- containerPort: 1107
8489
name: metrics

pkg/mesh/discoverips.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import (
4040
// - private IP assigned to interface of default route
4141
// - private IP assigned to local interface
4242
// - if no IP was found, return nil and an error.
43-
func getIP(hostname string, ignoreIfaces ...int) (*net.IPNet, *net.IPNet, error) {
43+
// If allowedCIDRs is not empty, only IPs within these CIDRs will be considered for private IP selection.
44+
func getIP(hostname string, allowedCIDRs []*net.IPNet, ignoreIfaces ...int) (*net.IPNet, *net.IPNet, error) {
4445
ignore := make(map[string]struct{})
4546
for i := range ignoreIfaces {
4647
if ignoreIfaces[i] == 0 {
@@ -144,6 +145,10 @@ func getIP(hostname string, ignoreIfaces ...int) (*net.IPNet, *net.IPNet, error)
144145
if _, ok := ignore[tmpPriv[i].String()]; ok {
145146
continue
146147
}
148+
// If allowedCIDRs is specified, filter private IPs by these CIDRs.
149+
if len(allowedCIDRs) > 0 && !isInCIDRs(tmpPriv[i].IP, allowedCIDRs) {
150+
continue
151+
}
147152
priv = append(priv, tmpPriv[i])
148153
}
149154
for i := range tmpPub {
@@ -290,3 +295,13 @@ func defaultInterface() (*net.Interface, error) {
290295

291296
return nil, errors.New("failed to find default route")
292297
}
298+
299+
// isInCIDRs checks if the given IP is within any of the provided CIDRs.
300+
func isInCIDRs(ip net.IP, cidrs []*net.IPNet) bool {
301+
for _, cidr := range cidrs {
302+
if cidr.Contains(ip) {
303+
return true
304+
}
305+
}
306+
return false
307+
}

pkg/mesh/mesh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type Mesh struct {
8989
}
9090

9191
// New returns a new Mesh instance.
92-
func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularity, hostname string, port int, subnet *net.IPNet, local, cni bool, cniPath, iface string, cleanup bool, cleanUpIface bool, createIface bool, mtu uint, resyncPeriod time.Duration, prioritisePrivateAddr, iptablesForwardRule bool, serviceCIDRs []*net.IPNet, logger log.Logger, registerer prometheus.Registerer) (*Mesh, error) {
92+
func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularity, hostname string, port int, subnet *net.IPNet, local, cni bool, cniPath, iface string, cleanup bool, cleanUpIface bool, createIface bool, mtu uint, resyncPeriod time.Duration, prioritisePrivateAddr, iptablesForwardRule bool, allowedInternalCIDRs []*net.IPNet, serviceCIDRs []*net.IPNet, logger log.Logger, registerer prometheus.Registerer) (*Mesh, error) {
9393
if err := os.MkdirAll(kiloPath, 0700); err != nil {
9494
return nil, fmt.Errorf("failed to create directory to store configuration: %v", err)
9595
}
@@ -134,7 +134,7 @@ func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularit
134134
}
135135
kiloIface = link.Attrs().Index
136136
}
137-
privateIP, publicIP, err := getIP(hostname, kiloIface, enc.Index(), cniIndex)
137+
privateIP, publicIP, err := getIP(hostname, allowedInternalCIDRs, kiloIface, enc.Index(), cniIndex)
138138
if err != nil {
139139
return nil, fmt.Errorf("failed to find public IP: %v", err)
140140
}

0 commit comments

Comments
 (0)