154 Commits

Author SHA1 Message Date
Manuel Rüger
99e0011527 chore: fix gofmt, perfsprint, and staticcheck lint issues
- Fix import ordering in lballoc.go (gofmt)
- Replace static fmt.Errorf with errors.New across multiple files (perfsprint)
- Replace fmt.Sprintf string-only calls with concatenation (perfsprint)
- Replace fmt.Sprint(int) with strconv.Itoa/FormatUint (perfsprint)
- Replace fmt.Sprintf("%x") with hex.EncodeToString (perfsprint)
- Lowercase capitalized error strings to follow Go conventions (staticcheck ST1005)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-26 16:01:52 -05:00
Manuel Rüger
5f0361188f fact: modernize Go primitives to use 1.13+ stdlib features
- Replace fmt.Errorf %s/%v + err.Error() with %w for proper error
  wrapping and errors.Is/As chain support across all packages
- Replace errors.New("msg" + err.Error()) with fmt.Errorf("msg: %w", err)
- Replace strings.Contains(err.Error(), ...) with errors.Is(err,
  syscall.EEXIST) and errors.Is(err, syscall.ESRCH) in linux_networking.go
- Remove now-unused IfaceHasAddr and IpvsServerExists string constants
- Replace sort.Strings with slices.Sort in bgp_policies.go, ipset.go,
  and testhelpers
- Replace sort.SliceStable with slices.SortStableFunc in bgp_policies.go
- Replace reflect.DeepEqual on []string with slices.Equal in bgp_policies.go
  (also fixes bug: was comparing map to slice instead of slice to slice)
- Replace reflect.DeepEqual on []*gobgpapi.Prefix with slices.EqualFunc
  comparing exported fields to avoid protobuf internal state comparison
- Replace strings.Index + manual slicing with strings.Cut in docker.go
  and classify.go
- Update cni_test.go to use assert.EqualError instead of assert.Equal
  for wrapped error comparison

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-26 16:01:52 -05:00
Aaron U'Ren
a1f0b2eea3
fix: validate external IPs and LB IPs against configured ranges
Moves all Service VIP range configurations into pkg/svcip this is where
validation and querying of ranges goes rather than passing each range to
each controller.

It also centralizes the validation logic since NRC and NSC need
basically equivalent logic. It additionally adds a RangeQuerier
interface for the NPC and LBC controllers which require knowing the
literal ranges.
2026-03-15 20:46:54 -05:00
Aaron U'Ren
070d9565db feat(lint): add basic typos checker to ensure less spelling mistakes in the future 2026-03-15 12:29:17 -07:00
Roman Kuzmitskii
39efb9230c feat: add support for SCTP
includes workaround for musl hardcoded protocol table that
  is missing SCTP support by using protocol name to
  numeric value mapping in ipset entries

closes: https://github.com/cloudnativelabs/kube-router/issues/1019
Signed-off-by: Roman Kuzmitskii <roman@damex.org>
2026-03-09 19:42:08 -10:00
Aaron U'Ren
f44598bcb1 test(ipset): add unit tests for ipset regression testing 2025-10-27 21:25:33 -05:00
Aaron U'Ren
6c44013bc5 fix(ipset): ignore non-kube-router ipsets
Attempt to filter out sets that we are not authoritative for to avoid
race conditions with other operators (like Istio) that might be
attempting to modify ipsets at the same time.
2025-10-04 18:30:28 -05:00
Aaron U'Ren
94e72aa8b3 fix(NPC): allow bi-directional ipv6 network discovery
This commit allows ICMP traffic always, not just on the case that
network policy is not applied in a particular direction, as was
originally the intention for KUBE-NWPLCY-DEFAULT.

This commit also consolidates common matching logic for established /
related & invalid traffic flows which hopefully reduces how much
iptables rules we have to make by a significant factor.
2025-09-20 13:58:02 -05:00
Manuel Rüger
6a1d15c24c Use golangci-lint 2.0.2 2025-04-23 22:56:24 +02:00
Aaron U'Ren
d8430e21c0 fix(lint): remove nolint for error messages
It looks like they fixed goconst upstream and it no longer checks this
2025-02-14 14:18:26 -06:00
Aaron U'Ren
760fcd5c85 fix(lint): remove non-constant format string (govet) 2025-02-14 14:18:26 -06:00
Aaron U'Ren
48b631c4ea fix(lint): remove unnecessary variable initializations (copyloopvar) 2025-02-14 14:18:26 -06:00
Roberto Bonafiglia
d1982eb29c Added ipset.Save at the start of syncNetworkPolicyChains
Signed-off-by: Roberto Bonafiglia <roberto.bonafiglia@suse.com>
2025-02-12 19:59:12 -06:00
Aaron U'Ren
0ac15b273e fact(healthcontroller): make more robust
Make the health controller more robust and extensible by adding in
constants for heart beats instead of 3 character random strings that are
easy to get wrong.
2024-11-21 15:24:09 +01:00
Aaron U'Ren
5e4db3fa33 test(krnode): add unit tests for new functionality 2024-09-29 17:53:36 -05:00
Aaron U'Ren
e4fa335acb fix(krnode): apply suggestions from code review
Co-authored-by: Tom Wieczorek <twz123@users.noreply.github.com>
2024-09-29 17:53:36 -05:00
Aaron U'Ren
9fd46cc86d fact(krnode): add node struct abstraction
This prepares the way for broader refactors in the way that we handle
nodes by:

* Separating frequently used node logic from the controller creation
  steps
* Keeping reused code DRY-er
* Adding interface abstractions for key groups of node data and starting
  to rely on those more rather than concrete types
* Separating node data from the rest of the controller data structure so
  that it smaller definitions of data can be passed around to functions
  that need it rather than always passing the entire controller which
  contains more data / surface area than most functions need.
2024-09-29 17:53:36 -05:00
Aaron U'Ren
a0442e5abd fix: allow basic ICMPv6 neighbor discovery
This fixes the problem where if network policy is applied before any
communication between two pods, all subsequent communication fails
because the two pods aren't able to discovery each other.
2024-08-03 14:55:47 -05:00
Aaron U'Ren
7abe95389e fix(ipset): reset ipset handler before use
At the very end of a NPC full sync we call ipset.Save() during the ipset
cleanup stage. This causes all of the current IPv4 and IPv6 sets that
are defined on the system (ours or not) to enter into the handler's
state.

Since `ipset restore` is not implicitly destructive (e.g. it doesn't
remove sets that aren't defined like iptables-restore does) we don't
really need this previous state, and in some ways it may come back to
cause bugs if the state isn't purged.

So this is a fail safe to clean them out to ensure that they don't end
up building up cruft. It also makes the restores go faster as
kube-router is only defining it's own rules rather than defining all
rules.
2024-05-13 12:18:05 -05:00
Aaron U'Ren
f5167732dc fact(ipset): simplify cleanup code by reducing family complexity 2024-05-13 12:18:05 -05:00
Aaron U'Ren
ecaad2c6e4 fix(cleanup): add missing handlers for cleanup
kube-router v2.X introduced the idea of iptables and ipset handlers that
allow kube-router to be dual-stack capable. However, the cleanup logic
for the various controllers was not properly ported when this happened.
When the cleanup functions run, they often have not had their
controllers fully initialized as cleanup should not be dependant on
kube-router being able to reach a kube-apiserver.

As such, they were missing these handlers. And as such they either
silently ended up doing noops or worse, they would run into nil pointer
failures.

This corrects that, so that kube-router no longer fails this way and
cleans up as it had in v1.X.
2024-04-26 14:16:09 -05:00
Aaron U'Ren
7755b4a67f fix(node.go): improve logic for GetNodeObject
Before the logic ran like the following in terms of preference:

1. Prefer environment var NODE_NAME
2. `Use os.Hostname()`
3. Fallback to `--hostname-override` passed by user

This didn't make a whole lot of sense, as `--hostname-override` is
directly, and supposedly intentionally set by the user, therefore it
should be the MOST preferred, not the least preferred. Additionally,
none of the errors encountered were passed back to the user so that
future conditions could be considered, so if there was an error at the
API level, that error was swallowed. Now the logic looks like:

1. Prefer `--hostname-override` if it is set. If it is set and we
   weren't able to resolve to a node object, return the error
2. Use environment var NODE_NAME if it is set. If it is set and we
   weren't able to resolve to a node object, return the error
3. Fallback to `os.Hostname()`. If we weren't able to resolve to a node
   object then return the error and give the user options
2024-04-26 14:16:09 -05:00
Aaron U'Ren
d12f422f65 fix(policy): generate ipv6 names correctly
Use ipSetName utility method to ensure that ipset names are generated
correctly when they are formulated. This feeds into the activeIPSets map
later on, so it is important that we get the name right from the start.
2024-04-26 13:55:30 -05:00
Aaron U'Ren
2c7151b516 fix(policy.go): use new utility method ipSetName 2024-04-26 13:55:30 -05:00
Aaron U'Ren
c762eaf2e5 feat(ipset): add more name utilities
Naming ipsets with the advent of IPv6 gets tricky because IPv6 ipsets
have to be prefixed with inet6:. This commit adds additional utilities
that help users find the correct name of ipsets.
2024-04-26 13:55:30 -05:00
xujunjie-cover
ada3179c39 fix: wrong ipset name used by ip6tables.
ipset name has prefix "inet6:" for ipv6. so ip6tables rule also need to
convert ipset name.

Signed-off-by: xujunjie-cover <xujunjie37@jd.com>
2024-04-26 13:55:30 -05:00
Aaron U'Ren
46a1b17903 feat(go): upgrade 1.20.13 -> 1.21.7 + dep update
Upgrades to Go 1.21.7 now that Go 1.20 is no longer being maintained.

It also, resolves the race conditions that we were seeing with BGP
server tests when we upgraded from 1.20 -> 1.21. This appears to be
because some efficiency changed in 1.21 that caused BGP to write to the
events at the same time that the test harness was trying to read from
them. Solved this in a coarse manner by adding surrounding mutexes to
the test code.

Additionally, upgraded dependencies.
2024-03-02 15:45:54 -06:00
Aaron U'Ren
47fe189fe6 feat(lint): update golangci-lint and fix lint errors 2024-03-02 15:45:54 -06:00
Aaron U'Ren
4c6e19f2e1 feat(ipset): consolidate ipset usage across controllers
Before this, we had 2 different ways to interact with ipsets, through
the handler interface which had the best handling for IPv6 because NPC
heavily utilizes it, and through the ipset struct which mostly repeated
the handler logic, but didn't handle some key things.

NPC utilized the handler functions and NSC / NRC mostly utilized the old
ipset struct functions. This caused a lot of duplication between the two
groups of functions and also caused issues with proper IPv6 handling.

This commit consolidates the two sets of usage into just the handler
interface. This greatly simplifies how the controllers interact with
ipsets and it also reduces the logic complexity on the ipset side.

This also fixes up some inconsistency with how we handled IPv6 ipset
names. ipset likes them to be prefixed with inet6:, but we weren't
always doing this in a way that made sense and was consistent across all
functions in the ipset struct.
2023-10-07 08:52:31 -05:00
Aaron U'Ren
f397a1f011 feat: increase log level for save/restore msgs 2023-10-07 08:52:31 -05:00
Aaron U'Ren
68a7d03bac fix: take family metrics out of defer
Deferring these will end up making the end times match for both families
as the variables aren't tracked separately. Since these are the same
metrics, it should be safe to emit them at time of generation.
2023-10-07 08:52:31 -05:00
Aaron U'Ren
301e856a92 fix(NPC): remove redundant assign 2023-10-07 08:52:31 -05:00
Brad Davidson
b06b4f05c3 Move ipset restore outside policy loop
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
2023-10-07 08:52:31 -05:00
Brad Davidson
e34ef29fe2 Add additional save/restore metrics
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
2023-10-07 08:52:31 -05:00
Brad Davidson
aa107d6376 Make metrics registerer/gathererer replacable
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
2023-10-07 08:52:31 -05:00
Erik Larsson
afdf553fa8 add loadbalancer address allocator
This adds a simple controller that will watch for services of type LoadBalancer
and try to allocated addresses from the specified IPv4 and/or IPv6 ranges.
It's assumed that kube-router (or another network controller) will announce the addresses.

As the controller uses leases for leader election and updates the service status new
RBAC permissions are required.
2023-10-07 08:52:31 -05:00
Aaron U'Ren
4861021797 fix(NPC): update IPBlocks to be ipFamily specific
Previously, IPBlocks (like srcIPBlocks) only contained a single IP
Family which meant that a len() > 0 would indicate that an IP block had
been defined in the NetworkPolicy. However, now the IPBlocks structs are
IP family specific which means that they will always contain 2 entries,
one for the IPv4 family and one of the IPv6 family. Which means that
this condition will evaluate to true for all NetworkPolicies and waste
system resources creating empty ipsets and bad iptables rules.
2023-10-07 08:52:31 -05:00
Boleyn Su
f0d7f1e17a netpol: Fix ipset only containing one IP when port name is used. 2023-10-07 08:52:31 -05:00
Aaron U'Ren
06f5f8babf feat(go): update package version to /v2
Do the necessary to update kube-router to a new major version following
upstream documentation: https://go.dev/doc/modules/major-version
2023-10-07 08:52:31 -05:00
Aaron U'Ren
e51ee3ae71 fix(NPC): add warning for unsupported family
Rather than just silently not adding policies for controllers that don't
support a given address family, emit a warning so that it is more
obvious in the logs that kube-router isn't able to add a policy for a
given family when the controller doesn't have that family enabled.
2023-10-07 08:52:31 -05:00
Aaron U'Ren
4e1679f03b fix(NPC): don't add chains for missing family
On dual-stack nodes there can still be pods that are single stack. When
this happens there won't be a pod IP for a given family and if
kube-router tries to add rules with a missing pod IP the iptables rules
won't be formatted correctly (because it won't have a valid source or
destination for that family).

So rather than breaking the whole iptables-restore we warn in the logs
and skip the pod policy chains for that family.
2023-10-07 08:52:31 -05:00
Aaron U'Ren
0ecb51de3e fix(NPC/pod): check drop policy on ipv4 & ipv6
Change return to continue so that both IPv4 and IPv6 are checked for
drop policy not just the first one.
2023-10-07 08:52:31 -05:00
Aaron U'Ren
b3e0768281 fix(options): make clusterIP specification similar to other options 2023-10-07 08:52:31 -05:00
Aaron U'Ren
a31511d987 fix(NPC): actually separate chain indices for ipv4 / ipv6 2023-10-07 08:52:31 -05:00
Aaron U'Ren
096da81f92 fact(NPC): pluralize newIPTablesHandler 2023-10-07 08:52:31 -05:00
Aaron U'Ren
ddb0e63c46 feat(NRC): make NRC dual stack 2023-10-07 08:52:31 -05:00
Aaron U'Ren
3db482be3b fix(NPC): separate chain indices for ipv4 / ipv6
Without this, kube-router would end up sharing the index between ipv4
and ipv6 which would cause it to error out when one incremented beyond
the number of rules that actually existed in the chain.
2023-10-07 08:52:31 -05:00
Thomas Ferrandiz
d7e2a146f3 fix golangci issues 2023-10-07 08:52:31 -05:00
Thomas Ferrandiz
12561f9faa fix test compilation error 2023-10-07 08:52:31 -05:00
Thomas Ferrandiz
76e5d20c5a use createGenericHashIPSet 2023-10-07 08:52:31 -05:00