Commit Graph

55 Commits

Author SHA1 Message Date
Dave Protasowski accfe36491
Satisfy linter (#3132)
* drop deprecated linter

* fix uint64=>int64 overflow

* fix unnecessary fmt.Sprintf

* ignore false positive fatcontext

* fix integer overflows

* update golangci-lint config - properly was moved

* fix formatting calls

* use new integer range syntax

* adjust nolint comments

* stop using deprecated k8s types
2025-01-10 15:06:18 +00:00
Dave Protasowski ee1db869c7
Update linter config and address lint warnings/failures (#3068)
* fix whitespace linter

* fix usestdlibvars

* fix staticheck

* ignore spancheck warning

* fix linter perfsprint

* fix nolintlint feedback

* fix nilerr lint checks

* fix misspell

* fix mirror lint

* fix intrange linter

* fix gofumpt linter

* fix gosec linter - ignore warning since default min tls version is 1.3

* fix gocritic linter

* fix whitespace

* fix fatcontext linter

* fix errorlint

* fix errname linter

* fix copyloopvar (go1.22) linter

* fix bodyclose linter

* update linter config

* add script to format code

* fix unit test
2024-06-25 14:49:36 +00:00
Dave Protasowski 8535fcc248
gofumpt the repo (#3067)
* gofumpt the repo

* don't prefix numbers with 0 - otherwise they're octal
2024-06-25 07:27:07 +00:00
Reto Lehmann 1860700f6f
update k8s min version to 1.26.x and go deps to 1.27.x (#2832)
* update k8s min version to 1.26.0

* bump k8s go deps to latest v0.26 version

* Fix version_test

* bump k8s go deps to latest v0.27 version
2023-10-02 12:57:59 +00:00
Kenjiro Nakayama c11003ae6d
Use go sync/atomic instead of go.uber.org/atomic (#2777)
* wip

* tiny fix

* Fix controller/controller_test.go

* fix metrics.go

* Fix profiling/server.go

* Fix reconciler/testing

* update ./test/spoof/spoof_test.go

* Fix ./test/zipkin/util.go

* update go.uber.org/atomic
2023-08-02 16:26:48 +00:00
Dave Protasowski 2daa86aa8f
Electors can provide an initial set of buckets (#2473)
The controller package now promotes those buckets
prior to starting reconciliation and leader election

The unopposed elector now seeds the univeral bucket

This helps avoid races in unit tests were the univeral
bucket isn't promoted in time when reconciling an resource
2022-03-25 11:21:49 -07:00
Matt Moore d60f1a4998
Avoid double-resyncs without leader election. (#2252)
* Avoid double-resyncs without leader election.

tl;dr Without leader election enabled, we've been suffering from double resyncs, this fixes that.

Essentially when the informers are started, prior to starting the controllers, they fill the controller's workqueue with ~the world.  Once we start the controllers, the leader election code calls `Promote` on a `Bucket` so that it starts processing them as their leader, and then is requeues every key that falls into that `Bucket`.

When leader election is disabled, we use the `UniversalBucket`, which always owns everything.  This means that the first pass through the workqueue was already hitting every key, so re-enqueuing every key results in superfluous processing of those keys.

This change makes the `LeaderAwareFuncs` elide the call to `PromoteFunc` when the `enq` function passed is `nil`, and makes the `unopposedElector` have a `nil` `enq` field when we explicitly pass it the `UniversalBucket` because leader election is disabled.

* Add more unit test coverage
2021-09-02 09:46:06 -07:00
Matt Moore e957ee54fa
Drop several deprecated APIs. (#2228) 2021-08-30 13:25:54 -07:00
Matt Moore 6ef763ddee
Remove long deprecated NewImplWithStats (#2223) 2021-08-21 14:43:34 -07:00
Matt Moore 3826bb2436
Add a new mechanism for requeuing a key. (#2201)
This is modelled after some of the semantics available in controller-runtime's `Result` type, which allows reconcilers to indicate a desire to reprocess a key either immediately or after a delay.

I worked around our lack of this when I reworked Tekton's timeout handling logic by exposing a "snooze" function to the Reconciler that wrapped `EnqueueAfter`, but this feels like a cleaner API for folks to use in general, and is consistent with our `NewPermanentError` and `NewSkipKey` functions in terms of influencing the queuing behaviors in our reconcilers via wrapped errors.

You can see some discussion of this here: https://knative.slack.com/archives/CA4DNJ9A4/p1627524921161100
2021-07-30 08:31:33 -07:00
Markus Thömmes d9c4e5c439
Fix a few more occurrences of divisive language (#1902) 2020-11-12 06:41:59 -08:00
Markus Thömmes 3d42810561
Make sure all controllers finish before ending test (#1818) 2020-10-19 10:02:58 -07:00
Victor Agababov a371418524
v2 (#1754) 2020-09-29 13:18:29 -07:00
Julian Friedman 6e0430fd94
Fix flakes in EnqueueAfter tests (#1710)
* Fix flakes in EnqueueAfter tests

* only call q.Len() once
2020-09-16 10:15:41 -07:00
Victor Agababov 55cef32af0
Remove code duplication by reusing funcs (#1669)
and consts
2020-08-31 12:11:15 -07:00
Victor Agababov dba58d1d78
Modernize the controller tests (#1661)
- remove mutexes where we can get by with an atomic
- make things private that are not public
- etc
2020-08-28 21:54:07 -07:00
Victor Agababov 34838c4559
Remove all ClearAll calls from pkg UTs (#1654)
We need this in order to deprecate the function.
Serving is already free of those.
2020-08-27 13:13:07 -07:00
Antoine Cotten c56f5e203b
Reduce delays in controller tests (#1603)
* TestEnqueue: remove unnecessary calls to Sleep

The rate limiter applies only when multiple items are put onto the
workqueue, which is not the case in those tests.

Execution: ~7.6s -> ~2.1s

* TestEnqueueAfter: remove assumptions on execution times

Instead of sleeping for a conservative amount of time, keep watching the
state of the workqueue in a goroutine, and notify the test logic as soon
as the item is observed.

Execution: ~1s -> ~0.05s

* TestEnqueueKeyAfter: remove assumptions on execution times

Instead of sleeping for a conservative amount of time, keep watching the
state of the workqueue in a goroutine, and notify the test logic as soon
as the item is observed.

Execution: ~1s -> ~0.05s

* TestStartAndShutdownWithErroringWork: remove sleep

Instead of sleeping for a conservative amount of time, keep watching the
state number of requeues in a goroutine, and notify the test logic as
soon as the expected threshold is reached.

Logs, for an idea of timings
----------------------------
Started workers
Processing from queue bar (depth: 0)
Reconcile error {"error": "I always error"}
Requeuing key bar due to non-permanent error (depth: 0)
Reconcile failed. Time taken: 104µs     {"knative.dev/key": "bar"}
Processing from queue bar (depth: 0)
Reconcile error {"error": "I always error"}
Requeuing key bar due to non-permanent error (depth: 0)
Reconcile failed. Time taken: 48.2µs    {"knative.dev/key": "bar"}

Execution: ~1s -> ~0.01s

* TestStart*/TestRun*: reduce sleep time

There is no need to sleep for that long. If an error was returned, it
would activate the second select case immediately.

Execution: ~1s -> ~0.05s

* TestImplGlobalResync: reduce sleep time

We know the fast lane is empty in this test, so we can safely assume
immediate enqueuing of all items on the slow lane.

Logs, for an idea of timings
----------------------------
Started workers
Processing from queue foo/bar (depth: 0)
Reconcile succeeded. Time taken: 11.5µs {"knative.dev/key": "foo/bar"}
Processing from queue bar/foo (depth: 1)
Processing from queue fizz/buzz (depth: 0)
Reconcile succeeded. Time taken: 9.7µs  {"knative.dev/key": "fizz/buzz"}
Reconcile succeeded. Time taken: 115µs  {"knative.dev/key": "bar/foo"}
Shutting down workers

Execution: ~4s -> ~0.05s

* review: Replace for/select with PollUntil

* review: Remove redundant duration multiplier

* review: Replace defer with t.Cleanup
2020-08-26 10:09:06 -07:00
Jon Donovan 7be5c0a87b
Allow creating controller with custom RateLimiter. (#1546)
* Allow creating controller with custom RateLimiter.

Which was possible before via field modification.
Not switching to a builder pattern mostly for speed of resolution.
Happy to consider alternatives.

* Add tests for new functionality.

Specifically, these test that the Wait() function is notified about
the item, and that the RateLimiter is passed through to the queue.

* Add Options. Gophers love Options.

* Even moar controller GenericOptions.

* Attempt to appease lint, don't create struct for typecheck.

* GenericOptions -> ControllerOptions

* Public struct fields.
2020-08-03 14:31:28 -07:00
Victor Agababov 08156c67f6
Use slow lane to do global resync (#1528)
* Use slow lane to do global resync

* cmt

* yolo

* yolo v2

* fix log str

* fixes

* publicize things

* renamemove
2020-07-21 13:11:54 -07:00
Victor Agababov 16eea5bd5b
remove the ResourceLock field from the pkg/LE (#1482)
we only use a single possible way, so no need to have, parse and validate
field that is in effectg a constant.
2020-07-14 09:15:18 -07:00
Matt Moore a81727701f
Enable leader election by default. (#1476)
* Enable HA by default.

This consolidates the core of sharedmain around the new leaderelection logic, which will now be **enabled by default**.

This can now be disabled with `--disable-ha` or by passing `sharedmain.WithHADisabled(ctx)` to `sharedmain.MainWithConfig`.

* vagababov comments, build failure

* Open an issue for enabledComponents removal.

* Move the configmap watcher startup.

This race was uncovered by the chaos duck on knative/serving!  When we have enabled a feature flag, e.g. multi-container, and the webhook pods are restarted, there is a brief window where the webhook is up and healthy before the configmaps have synchronized and the new webhook pod realizes the feature is enabled.

* Drop the import alias
2020-07-13 12:43:18 -07:00
Markus Thömmes 09d5e09da8
Assorted linting fixes. (#1443) 2020-06-24 12:11:27 -07:00
Matt Moore 7df8fc5d77
Implement the first wave of per-reconciler leaderelection. (#1301)
* Implement the first wave of per-reconciler leaderelection.

Detailed design: https://docs.google.com/document/d/1i_QHjQO2T3SNv49xjZLWlivcc0UvZN1Tbw2NKxThkyM/edit#
Issue: https://github.com/knative/pkg/issues/1181

* Feedback from vagababov

* Feedback from yanweiguo

* Drop IsLeaderFor from the LeaderAware interface.

* Moar vagababov nits

* dprotaso feedback

* Add issue comment, error return

* Incorporate dprotaso test feedback
2020-06-18 19:07:25 -07:00
Antoine Cotten 82fe339a5e
Implement Unwrap() and Is() for permanentErrors (#1363)
* Implement Unwrap() for permanentErrors

* Implement Is() for permanentErrors
2020-06-02 13:27:18 -07:00
Matt Moore 7b6e21a57a
Change StartAll to take context. (#1247)
* Change StartAll to take context.

This has bugged me since we started using `ctx`, which containers a `stopCh` of sorts as `Done()`.  This is somewhat for consistency, but by using `ctx` explicitly we enable ourselves to take advantage of more contextual information.

I did a quick scan of call sites and the good news is that the `sharedmain` change should be the place through which the vast majority of calls occur, however, the one outlier here is the KPA which calls this manually.  I will stage a PR to manually import pkg into serving to fix this once this lands.

* Add a Run shim for back-compat
2020-04-25 16:21:49 -07:00
Ville Aikas 4e57475bc8
add EnqueueNamespaceOf (#1217)
* add EnqueueNamespaceOf

* correct log error msg
2020-04-10 18:11:07 -07:00
Dave Protasowski e3d924ba00
allow filtering on schema.GroupKind (#1080)
* allow filtering on schema.GroupKind

In addition deprecated usage of Filter with the introduction of
FilterGroupVersionKind

* reduce nesting & simplify boolean logic
2020-02-12 12:12:18 -08:00
Maisem Ali 64ed9fcf84 add EnqueueSentinel to pkg/controller (#841)
* add EnqueueSentinel to pkg/controller

* address comments
2019-11-04 16:22:20 -08:00
Matt Moore 809ce573e4 Add FilterByName for cluster-scoped resources. (#816)
This is a precursor to reconciling named webhook configurations, and largely a copy of `FilterByNameAndNamespace`.
2019-10-28 10:41:42 -07:00
Slavomir Kaslev 29642b017b Add RunInformers function (#758)
Add RunInfomers which is similar to the StartInformers function but allows for
users to for informers to finish running.

This function will be mainly used in tests to fix the race described in
knative/serving#5351
2019-10-14 10:02:32 -07:00
Markus Thömmes de32ec136d Fix a subtle bug with cluster scoped entities. (#708) 2019-09-20 12:26:07 -07:00
Markus Thömmes 4a790dd36c Plumb through a structured key, keep current behavior. (#703)
* Plumb through a structured key, keep current behavior.

* Rename variable.
2019-09-20 06:52:05 -07:00
Matt Moore 1a3a36a996 Rework how we GlobalResync. (#597)
* Rework how we GlobalResync.

This consolidates the logic for how we do GlobalResyncs in a few ways:
1. Remove SendGlobalUpdates and replace it with FilteredGlobalResync.
2. Have GlobalResync go through the FilteredGlobalResync with a tautological predicate.
3. Change the way we enqueue things during a global resync to avoid flooding the system
   by using EnqueueAfter and wait.Jitter to stagger how things are queued.

The background for this is that when I started to benchmark reconciliation of a large
number of resources we'd see random large stalls like [this](https://mako.dev/run?run_key=4882560844824576&~dl=1&~cl=1&~rl=1&~rvl=1&~il=1&~sksl=1&~pal=1)
fairly consistently.  Looking at the logs, these stalls coincide with incredibly deep
work queues due to global resyncs triggers by configmap notifications. When I disabled
global resyncs the spikes [went away](https://mako.dev/run?run_key=4975897060835328&~dl=1&~cl=1&~rl=1&~rvl=1&~il=1&~sksl=1&~pal=1).

To mitigate the huge pile-up due to the global resync we use `wait.Jitter` to stagger how
things are enqueued with a `maxFactor` of the length of the store.  This also seems to
[keep things flowing](https://mako.dev/run?run_key=5701802099998720&~dl=1&~cl=1&~rl=1&~rvl=1&~il=1&~sksl=1&~pal=1),
although we will possibly need to tune things further.

* Update comment to mention the delay.
2019-08-24 21:23:13 -07:00
Matt Moore 222dd25986 Migrate pkg to use the knative.dev/pkg import path (#489)
* Manual changes.

* scripted changes.
2019-06-26 13:02:06 -07:00
Matt Moore 7538724784 Stabilize the controller unit tests. (#482) 2019-06-23 17:16:00 -07:00
Matt Moore c6f03fa600 Push the event recorder stuff into knative/pkg (#443) 2019-06-06 07:35:40 -07:00
Matt Moore 985bff446d Simplify the default controller.Impl constructor. (#427)
A while back we added a "StatsReporter" argument to `controller.NewImpl`,
but in serving every callsite of this is passing:
```
controller.NewImpl(r, logger, "my-string", MustNewStatsReporter("my-string", logger))
```

Where `MustNewStatsReporter` is just a form of knative/pkg's `controller.NewStatsReporter`
that logs fatally when an error is returned.  It is notable that Serving's logic has been
duplicated to both Build and Eventing.

There are a handful of changes here:
1. Move MustNewStatsReporter into knative/pkg
2. Expose the current interface as NewImplWithStats
3. Drop the StatsReporter from NewImpl and default to `MustNewStatsReporter()`

This is a breaking change for downstream repositories, but should make their callsites universally simpler.
2019-05-28 13:16:31 -07:00
Matt Moore d1ccfd6652 This upstreams a useful helper from serving. (#428)
In serving we have `reconciler.Handler` that wraps a handler function
(e.g. `Enqueue`) in the `cache.ResourceEventHandlerFuncs`.  This pattern
was becoming pervasive, so this simpler handler dramatically reduced our
boilerplate.
2019-05-27 23:21:29 -07:00
Matt Moore aae68ba9e0 Add methods for attaching resync duration to context. (#426)
Also adds a method for tracker leases, which should be a multiple of the resync period.
2019-05-26 23:44:29 -07:00
Ville Aikas 5e4512dcb2 Add Filter function by name/namespace (#414) 2019-05-14 13:53:32 -07:00
Victor Agababov c3f131538a add addafter method that permits enqueueing the objects themselves (#408) 2019-05-08 16:14:37 -07:00
Victor Agababov f23f58d373 Expose the EnqueueKeyAfter function on the controller (#402)
This is needed for the positive handoff, so that we can enqueue
the KPA after we received the positive answer from the activator
but still want to have buffer time to make sure the network
changes propagate everywhere.
2019-05-02 17:16:34 -07:00
Nghia Tran 590eb946f1 Add a version of GlobalResync that calls an event handler. (#381)
* Add a version of GlobalResync that calls an event handler.

This allows impl to apply filtering and queuing logic of their own,
instead of calling Enqueue() for everything.

* Fix typo in doc comment.
2019-04-10 13:33:59 -07:00
Matt Moore 7735348150 Add some helpers to reduce boilerplate in controller binaries. (#335)
* Add some helpers to reduce boilerplate in controller binaries.

* Incorporate feedback from Victor.

* Fix a couple test data races.
2019-03-23 10:57:48 -07:00
Brad Hoekstra b8c4664801 Don't rate-limit normal events (#242)
* Don't rate-limit normal events

* Fix test - first enqueue is not rate limited
2019-01-25 14:11:34 -08:00
Matt Moore 102237ce9b Simplify the core controller logic. (#184)
I was hoping to switch from a simple string key to a struct to include additional metadata into our workqueue item (in particular the time at which it was queued), but the great unit tests here reminded me that this would not properly deduplicate keys (they would have different queue times!), so I'm settling for simplifying the loop in a few ways:
1. Keys can only be strings, so remove some error checking and a test.
1. We were wrapping a block in a `func` because it contained `defer`, but the outer function had nothing in it.

I'd still like to try and find a way to track queue times for keys, so that we can surface two interesting metrics:
1. How long are things spending queued?
1. How long between events being observed and us responding? (above + reconcile latency)
2018-12-03 17:02:25 -08:00
mattmoor-sockpuppet d99eb0732f Run gofmt (#171)
Produced via: `gofmt -s -w $(find -name '*.go' | grep -v vendor)`
2018-11-26 07:18:22 -08:00
lichuqiang a5c260df2a Update EnqueueLabelOf to handle resources in same namespace (#166)
* update EnqueueLabelOf to handle resources in same namespace

* spit the EnqueueLabelOf func for namespaced/cluster-scoped resources
2018-11-19 19:04:20 -08:00
lichuqiang fcb0656514 requeue keys on error in controller (#149)
* requeue keys on error in controller

* remove maxRetries limit
2018-10-31 09:59:34 -07:00