Commit Graph

90 Commits

Author SHA1 Message Date
Thomas Ferrandiz
daa12ca505 Update code generator script
The script we were using is fully deprecated so we need to upgrade
to a new code generator.
2024-12-16 16:05:40 +00:00
Igor Velichkovich
93fd190c28 use WHEREABOUTS_NAMESPACE for nodeslicepools like it does for ippools 2024-07-08 10:41:38 -07:00
Igor Velichkovich
9a9a3a01af fast node slice initial implementation 2024-07-08 10:40:53 -07:00
Marcelo Guerrero
61df92c1b0 Align api calls timeouts cronjob ip reconciler
Parent timeout context of 30s was removed. All listing operations
used by the cronjob reconciler has 30s as timeout.

Fixes https://github.com/k8snetworkplumbingwg/whereabouts/issues/389

Signed-off-by: Marcelo Guerrero <marguerr@redhat.com>
2024-06-20 16:25:43 +02:00
Marcelo Guerrero
b3a0025056 Return previous allocation for add cmd
This allows the cni to return a previous allocation
for a pod with the same podRef and interface name. This
is needed on networks with limited IPs.

Signed-off-by: Marcelo Guerrero <marguerr@redhat.com>
2024-06-04 17:01:41 +02:00
Marcelo Guerrero
22c22deed9 Add ifName on operations
Support the new Interface property on IP and cluster wide
allocations.

Signed-off-by: Marcelo Guerrero <marguerr@redhat.com>
2024-06-04 17:01:37 +02:00
Marcelo Guerrero
72f2695cea Handle missing allocation in del cmd
Del command should not fail when allocation is not found

Signed-off-by: Marcelo Guerrero <marguerr@redhat.com>
2024-05-03 17:05:59 +02:00
Manuel Buil
8825da9f90 Update net-attach version to v1.7
Signed-off-by: Manuel Buil <mbuil@suse.com>
2024-04-19 11:16:05 +02:00
Manuel Buil
25b500bf36 Use containernetworking/cni/pkg/types/100
Signed-off-by: Manuel Buil <mbuil@suse.com>
2024-02-29 14:33:18 +01:00
Manuel Buil
72d455d694 Update containernetworking deps
Signed-off-by: Manuel Buil <mbuil@suse.com>
2024-02-22 12:23:35 +01:00
Miguel Duarte Barroso
58ba24c348 reconciler: add a configuration watcher pkg
Move everything related to watching / updating the configuration to a
separate pkg. That makes the code easier to follow, and allows us to
properly unit test watching the configuration.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2023-12-22 17:21:06 +01:00
Miguel Duarte Barroso
da358de050 reconciler: only read from flatfile if config is not present
This change will allow us to unit test the configuration updates without
having to create the CNI configuration files (they're unrelated to this
unit).

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2023-12-22 12:20:00 +01:00
Miguel Duarte Barroso
f67b5ceba8 reconciler: sort out the reconciler error codes
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2023-12-20 17:04:38 +01:00
Miguel Duarte Barroso
e0625d9991 reconciler: react to cron expr updates
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2023-12-20 17:04:37 +01:00
Miguel Duarte Barroso
41366842f5 build: update gocron to v2
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2023-12-20 16:47:09 +01:00
nicklesimba
6684139632 Extended support for customizing whereabouts ip-reconciler cron schedule
Signed-off-by: nicklesimba <simha.nikhil@gmail.com>
2023-12-20 16:42:03 +01:00
Doug Smith
475be371e9
Merge pull request #322 from andreaskaris/improve-iterate-for-assignment-separate-commits2
Improve iterate for assignment
2023-05-19 16:12:49 -04:00
Jorik Jonker
55b24e2899 fix: refactor tests to have a config file
It is required per my previous commit, which seemed to break a couple of
tests.

Signed-off-by: Jorik Jonker <jorik.jonker@eu.equinix.com>
2023-05-03 13:43:23 +02:00
Andreas Karis
f875495a1c Refactor IterateForAssignment and GetIPRange
Refactor both IterateForAssignment and GetIPRange.
Make sure that IterateForAssignment adheres to a consistent set of
rules for both IPv4 and IPv6:
* Valid IPs are contained within the ipnet, excluding the network and
  broadcast address
* If rangeStart is specified, it is respected if it lies within the
  ipnet
* If rangeEnd is specified, it is respected if it lies within the
  ipnet and if it is >= rangeStart
*  reserveList holds a list of reserved IPs.
*  excludeRanges holds a list of subnets to be excluded (meaning the
   full subnet, including the network and broadcast IP)

Add more unit tests to make sure the above conditions are met.

Signed-off-by: Andreas Karis <ak.karis@gmail.com>
2023-04-25 11:47:42 +02:00
Andreas Karis
344bad5797 Move IsIPv4 to iphelpers package
Signed-off-by: Andreas Karis <ak.karis@gmail.com>
2023-04-25 11:47:42 +02:00
Doug Smith
6a7b22ccc4
Merge pull request #256 from genesiscloud/implement-network-name
Implement network name
2023-03-27 12:48:54 -04:00
Andreas Karis
ef409bbf9c Use downward API to pass current spec.nodeName to pod
The podInformerFactory uses filter key spec.nodeName to filter the pods
that it should monitor. Up until now, this filter was set to the value
of HOSTNAME. However, this is not reliable, as spec.nodeName can be
overridden in kubernetes with --hostname-override and thus HOSTNAME and
spec.nodeName do not necessarily always match. Instead, rely on a new
custom environment variable NODENAME which is populated by the downward
API.

Signed-off-by: Andreas Karis <ak.karis@gmail.com>
2023-03-16 18:52:45 +01:00
Philipp Riederer
ce0c911d43
Allow specifying a network_name in the IPAM config
this name is prepended to the name of the IP pool to allow different L2 networks with the same network ranges
2023-03-14 16:59:38 +01:00
Paulo Gomes
f1dce84e98 Remove usage of deprecated io/ioutil
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
2023-02-03 14:24:28 -06:00
nicklesimba
3de4e9a566 Added tests for ipv6 ranges that end in zeroes
Signed-off-by: nicklesimba <simha.nikhil@gmail.com>
2023-02-03 14:22:27 -06:00
Ayush Patel
1dac1cbf72 Assign multiple IPs to pods based on IPRanges
Update 'allocate.AssignIP' to use 'RangeConfiguration' instead of
'IPAMConfig'. It makes no change in the IP assignment logic.

Update Kubernetes' IP Management to handle multiple IP ranges present in
'IPAM.IPRanges' and return a list for new IPs instead of single new IP.

Update whereabouts' logic to assign multiple new IPs to a pod based on a
list of new IPs returned by IP Management module rather than a single
new IP address.

Update 'net.IPNet' to '[]net.IPNet' in return parameters of
'garbageCollector' to match with the changes in definition of
'wbclient.IPManagement'.

Removed old "Range" from all the places and use IPRanges' 1st element
instead of it

Signed-off-by: Ayush Patel <patel.ayush08@gmail.com>
2022-11-03 21:45:53 +09:00
Miguel Duarte Barroso
ae659d30c9 build, tests: update unit tests to work with injected K8s clients
Some tests that didn't make sense anymore were deleted, while some
were ported to the `config` pkg unit tests.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-08-19 14:27:58 +02:00
Miguel Duarte Barroso
4dccb1dc4a build: inject the K8s client from the main app / unit tests
We will now rely on the fake kubernetes clients to hold the state of the
pools / pods / etc, instead of the testing `controller-runtime` objects.

Thus, we need to perform dependency injection to inject the "correct"
K8s client to the CNI cmd ADD / DEL / CHECK functions.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-08-19 13:33:03 +02:00
Miguel Duarte Barroso
c26343d2c1 storage, etcd: remove backend
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-07-18 13:53:13 +02:00
nicklesimba
545f05525c Moved IP Reconciler code into IP Control Loop
Signed-off-by: nicklesimba <simha.nikhil@gmail.com>
2022-07-11 15:43:24 -05:00
Miguel Duarte Barroso
30f92b0011 reconciler, shutdown: export shutdown into a func
This prepares the ground for a coordinated shutdown of the reconciler
worker queue triggered from the main reconciler func.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-07-05 13:17:25 +02:00
Miguel Duarte Barroso
f4b1d44635 tests: copy the testEnv kubeconfig
Kubebuilder's API server is now serving via an https endpoint; rather
than figuring out how to build a suitable kubeconfig for that, copy the
one the `testEnv` is generating.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-06-10 16:40:57 +02:00
Miguel Duarte Barroso
0c152e0a94 build, tests: api server listens in https
With the bump to k8s-1.23, kube-api-server listens to an https endpoint,
rather than http.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-06-10 16:40:57 +02:00
Miguel Duarte Barroso
59f1052972
IP control loop (#185)
* build: generate ip pool clientSet/informers/listers

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* vendor: update vendor stuff

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* build: vendor net-attach-def-client types

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* config: look for the whereabouts config file in multiple places

The reconciler controller will have access to the whereabouts
configuration via a mount point. As such, we need a way to specify its
path.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* reconcile-loop: requires the IP ranges in normalized format

The IP reconcile loop also requires the IP ranges in a normalized
format; as such, we export it into a function, which will be used in a
follow-up commit.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* config: allow IPAM config parsing from a NetConfList

Currently whereabouts is only able to parse network configurations in
the strict [0] format - i.e. **do not accept** a plugin list - [1].

The `ip-control-loop` must recover the full plugin configuration, which
may be in the network configuration format.

This commit allows whereabouts to now understand both formats.

Furthermore, the current CNI release - v1.0.Z - removed the support for
[0], meaning that only the configuration list format is now supported
[2].

[0] - https://github.com/containernetworking/cni/blob/v0.8.1/SPEC.md#network-configuration
[1] - https://github.com/containernetworking/cni/blob/v0.8.1/SPEC.md#network-configuration-lists
[2] - https://github.com/containernetworking/cni/blob/master/SPEC.md#released-versions

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* reconcile-loop: add a controller

Listen to pod deletion, and for every deleted pod, assure their IPs
are gone.

The rough algorithm goes like this:
  - for every network-status in the pod's annotations:
    - read associated net-attach-def from the k8s API
    - extract the range from the net-attach-def
    - find the corresponding IP pool
    - look for allocations belonging to the deleted pod
    - delete them using `IPManagement(..., types.Deallocate, ...)`

All the API reads go through the informer cache, which is kept updated
whenever the objects are updated on the API.

The dockerfiles are also updated, to ship this new binary.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* e2e tests: remove manual cluster reconciliation

This would leave the `ip-control-loop` as the reconciliation tool.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* unit tests: assure stale IPAllocation cleanup

This commit adds a unit where it is checked that the pod deletion leads
to the cleanup of a stale IP address.

This commit features the automatic provisioning of the controller informer cache
with the data present on the fake clientset tracker (the "fake" datastore).

This way, users can just create the client with provisioned data, and
that'll trickle down to the informer cache of the pod controller.

Because the `network-attachment-definitions` resources feature dashes,
the heuristic function that guesses - yes, guesses. very deterministic
... - the name of the resource can't be used - [0]. As such, it was
needed to create an alternate `newFakeNetAttachDefClient` where it is
possible to specify the correct resource name.

[0] - 2fd7267afc/vendor/k8s.io/client-go/testing/fixture.go (L331)

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* unit tests: move helper funcs to other files

The helper files are tagged with the `test` build tag, to prevent them
from being shipped on the production code binary.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* control loop, queueing: use a rate-limiting queue

Using a queue allows us to re-queue errors.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* control loop: add IPAllocation cleanup related events

Adds two new events related to garbage collection of the whereabouts IP
addresses:
  - when an IP address is garbage collected
  - when a cleanup operation fails and is not re-queued

The former event looks like:
```
116s        Normal    IPAddressGarbageCollected   pod/macvlan1-worker1 \
            successful cleanup of IP address [192.168.2.1] from network \
            whereabouts-conf
```

The latter event looks like:
```
10s         Warning    IPAddressGarbageCollectionFailed    failed to garbage \
            collect addresses for pod default/macvlan1-worker1
```

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* e2e tests: check out statefulset scenarios

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* e2e tests: test different scale up/down order and instance deltas

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* ci: test e2e bash scripts last

These ugly tests do not cleanup after themselves; this way, the golang
based tests (which **do** cleanup after themselves) will not be impacted by
these left-overs.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* ip control loop, unit tests: test negative scenarios

Check the event thrown when a request is dropped from the queue, and
assure reconciling an allocation is impossible without having access to
the attachment configuration data.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* e2e tests: test fix for issue #182

Issue [0] reports an error when a pod associated to a `StatefulSet`
whose IPPool is already full is deleted. According to it, the new pod -
scheduled by the `StatefulSet` - cannot run because the IPPool is
already full, and the old pod's IP cannot be garbage collected because
we match by pod reference - and the "new" pod is stuck in `creating`
phase.

[0] - https://github.com/k8snetworkplumbingwg/whereabouts/issues/182

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* ip-control-loop: strip pod before queueing it

The ip reconcile loop only requires the pod metadata and its network
status annotatations to garbage collect the stale IP addresses.

As such, we remove the status and spec parameters from the pod before
queueing it.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

* reconcile-loop: focus on networks w/ whereabouts IPAM type

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-04-13 10:49:18 -04:00
Miguel Duarte Barroso
3049dcec3f lint: fix linting errors caught by staticcheck
These are the errors listed on the first `staticcheck` run:
```
+ staticcheck ./...
cmd/whereabouts.go:64:10: error strings should not be capitalized (ST1005)
cmd/whereabouts_test.go:613:17: unnecessary use of fmt.Sprintf (S1039)
pkg/allocate/allocate.go:59:28: error strings should not be capitalized (ST1005)
pkg/allocate/allocate.go:94:3: should merge variable declaration with assignment on next line (S1021)
pkg/allocate/allocate.go:266:20: error strings should not be capitalized (ST1005)
pkg/allocate/allocate_test.go:169:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:181:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:195:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:208:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:224:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:239:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:262:3: this value of err is never used (SA4006)
pkg/allocate/allocate_test.go:277:3: this value of err is never used (SA4006)
pkg/allocate/allocate_test.go:292:3: this value of err is never used (SA4006)
pkg/allocate/allocate_test.go:309:3: this value of err is never used (SA4006)
pkg/allocate/allocate_test.go:324:3: this value of err is never used (SA4006)
pkg/allocate/allocate_test.go:331:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:345:3: this value of ip is never used (SA4006)
pkg/allocate/allocate_test.go:359:3: this value of badip is never used (SA4006)
pkg/allocate/allocate_test.go:369:3: this value of badip is never used (SA4006)
pkg/config/config.go:73:21: error strings should not be capitalized (ST1005)
pkg/config/config.go:163:20: error strings should not be capitalized (ST1005)
pkg/reconciler/iploop_test.go:155:6: func generatePodRef is unused (U1000)
pkg/storage/etcd.go:175:17: error strings should not be capitalized (ST1005)
pkg/storage/kubernetes/client.go:112:2: should replace loop with podEntries = append(podEntries, podList.Items...) (S1011)
pkg/storage/kubernetes/client.go:125:2: should replace loop with clusterWiderReservations = append(clusterWiderReservations, overlappingIPsList.Items...) (S1011)
pkg/storage/kubernetes/ipam.go:415:17: error strings should not be capitalized (ST1005)
pkg/storage/kubernetes/ipam.go:499:7: should omit comparison to bool constant, can be simplified to !rl.IsAllocated (S1002)
```

All of these were fixed in this commit.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-02-28 12:49:04 +01:00
Miguel Duarte Barroso
bdbccad04a ip reconciler: use a different context per each list operation
These contexts are all cancellable, and have a timeout.

The timeout can be specified via the reconciler command line arguments,
and defaults to 30 seconds.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-02-04 14:31:07 +01:00
Miguel Duarte Barroso
46df4894cf golang convention: context
Follow the golang convention regarding `context.Context` - [0]

According to it:
"""
Do not store Contexts inside a struct type; instead, pass a Context explicitly
to each function that needs it. The Context should be the first parameter,
typically named ctx:
...
"""

[0] - https://pkg.go.dev/context

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-02-04 14:00:18 +01:00
Miguel Duarte Barroso
55be906b77 ip-reconciler: do not reconcile pods in Pending phase
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-11-17 11:37:38 +01:00
Miguel Duarte Barroso
74483162d2 ip-reconciler: run without a defined kubeconfig
With this change the reconciler can run in two different ways:
- when ran in a k8s pod, it does not require to be told how to
  connect to the cluster.
- when ran as a binary, it does require to know how to connect to
  the cluster, via the -kubeconfig config option.

The reconciler cron spec is updated to use the correct service
account name, and also is updated to run in the `kube-system`
namespace.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-10-20 18:09:27 +02:00
Ivan Kolodyazhny
9583b1d636 Use k8snetworkplumbingwg repo instead of dougbtv in sources 2021-10-07 17:29:56 +03:00
Miguel Duarte Barroso
0a8d8e835e build, ip reconciler: have configurable logging
The ip-reconciler tool currently feature the logging level
hard-coded to `debug`, which is too verbose.

This commit makes it configurable, defaulting it to error level.

The effective logging level is set to `verbose` in the daemonset
spec.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-10-07 16:19:41 +02:00
Doug Smith
043cf53bef
Merge pull request #142 from martinkennelly/fix_multi_instance
Add total Add/Del timeout
2021-09-29 13:45:58 -04:00
Martin Kennelly
334924dbd9 Add total Add/Del timeout
During scale testing, we found that if an instance
of whereabouts did not accomplish its task within a
period of time, the kubelet spawned an additional
whereabouts instance and the initial instance
remained active.

For pod creation, the Kubelet will try to
start a pod and wait for ~8 minutes before
attempting to kill it. See kubelets "runPod"
function.

For pod deletion, kubelet will attempt to kill a pod
and if it fails it will attempt after one minute one
more time. Unable to see this in kubelet code but
observed it using K8 1.22.

Signed-off-by: Martin Kennelly <mkennell@redhat.com>
2021-09-28 09:39:44 +01:00
Antonin Bas
ad343c7b45 Add support for CNI version
The hack/build-go.sh script is modified to pass version information
during the build through LDFLAGS, which is a pretty standard technique.

This information can then be used to report the version of the
whereabouts CNI plugin at runtime; before this change the plugin would
just print "TODO".
2021-08-27 17:45:43 -07:00
Miguel Duarte Barroso
3b01e1992f build: enable compilation & running tests
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-07-23 11:30:17 +02:00
Miguel Duarte Barroso
5f160d8c81 e2e test overlapping IPs
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-07-22 16:07:41 +02:00
Miguel Duarte Barroso
0154617a83 reconcile cluster wide IPs
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-07-22 16:07:40 +02:00
Miguel Duarte Barroso
66b75ad61a ip-reconciler: simplify ip looper code
By not returning an `IPReservation` entry - we now return an IP
address instead - we simplify the code.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-07-22 16:07:18 +02:00
Miguel Duarte Barroso
8fc04e458a ip-reconciler: also account for the pods IP addresses
Only mark a pod as "alive" when the pod's annotations feature the
IP being de-allocated.

This makes the reconciler binary *dependent* on multus, which adds
these `network-status` annotations into the pod.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-07-22 16:07:18 +02:00
Miguel Duarte Barroso
8f731970c9 reconciler: add e2e tests
Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2021-07-22 16:07:16 +02:00