== Function Controller
The *function controller* runs inside kubernetes, watching for changes to *function* and *topic* resources.
It reacts by requesting the deployment of functions, and scales them according to activity on
their input topics.
Instance scaling may be capped to `maxReplicas` when that specified in the function configuration.
When Kafka is the message broker, scaling is capped to `maxReplicas` or the number of partitions in the input topic,
whichever is lowest.
The last replica of a function keeps running for at least `idleTimeoutMs` millseconds if this is specified
in the function configuration and for at least 10 seconds otherwise.
For example, the following function configuration limits the number of replicas to 3 even if the number of
topic partitions exceeds 3, and keeps the last replica running for at least 60 seconds.
```yaml
spec:
maxReplicas: 3
idleTimeoutMs: 60000
```
== Development
=== Prerequisites
The following tools are required to build this project:
- `make`
- Docker
- https://github.com/Masterminds/glide#install[Glide] for dependency management
If you intend to re-generate mocks for testing, install:
- https://github.com/vektra/mockery#installation[Mockery]
If you would like to run tests using the `ginkgo` command, install:
- http://onsi.github.io/ginkgo/[Ginkgo]
If you intend to run the autoscaler with a simulated workload:
- http://www.gnuplot.info/
=== Get the source
[source, bash]
----
go get github.com/projectriff/riff
cd $(go env GOPATH)/github.com/projectriff/riff/function-controller
----
=== Building
* To build locally (this will produce a binary named `function-controller` on _your_ machine):
+
[source, bash]
----
make build
----
* To build for docker (and deploy as part of the whole https://github.com/projectriff/riff#-manual-install-of-riff[riff]
installation):
+
[source, bash]
----
make dockerize
----
This assumes that your docker client is correctly configured to target the daemon where you want the image built.
* To run tests:
+
[source, bash]
----
make test
----
* To attach a https://github.com/derekparker/delve/blob/master/Documentation/EditorIntegration.md[delve capable] debugger (such as Goland)
to a `function-controller` running _inside_ k8s:
+
[source, bash]
----
make debug-dockerize
----
Then expose the `2345` port as a service, using `riff/config/delve/function-controller-debug.yaml`:
+
[source, bash]
----
kubectl apply -f config/debug-service.yaml
----
After restarting the `function-controller-...` pod, you can connect the debugger through `localhost:30102` (or `$(minikube ip):30102`).
=== Generating Mocks
Mocks can be regenerated as follows _and should then be checked in_:
[source, bash]
----
make gen-mocks
----
You can verify that generated mocks which are committed or staged are up to date using:
[source, bash]
----
make verify-mocks
----
=== Running the Autoscaler with a Simulated Workload
[source, bash]
----
make simulation
----