Skip to content

Commit

Permalink
Refactor scaleHandler (kedacore#735)
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed ElSayed <ahmed@elsayed.io>
  • Loading branch information
ahmelsayed authored and Zbynek Roubalik committed Apr 27, 2020
1 parent 6a756ee commit b0c17f5
Show file tree
Hide file tree
Showing 27 changed files with 1,132 additions and 953 deletions.
16 changes: 8 additions & 8 deletions CREATE-NEW-SCALER.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ In order to develop a scaler, a developer should do the following:
2. Create the new scaler struct under the `pkg/scalers` folder.
3. Implement the methods defined in the [scaler interface](#scaler-interface) section.
4. Create a constructor according to [this](#constructor).
5. Change the `getScaler` function in `pkg/handler/scale_handler.go` by adding another switch case that matches your scaler.
5. Change the `getScaler` function in `pkg/scaling/scale_handler.go` by adding another switch case that matches your scaler.
6. Run `make build` from the root of KEDA and your scaler is ready.

If you want to deploy locally
If you want to deploy locally
1. Run `export VERSION=local`
2. Open the terminal and go to the root of the source code
3. Run `make build`
5. If you haven't done it yet clone the charts repository: `git clone git@github.com:kedacore/charts.git`
5. If you haven't done it yet clone the charts repository: `git clone git@github.com:kedacore/charts.git`
6. In the terminal, navigate to the `chart/keda` folder (the charts downloaded in step 3), and run the following command (don't forget to replace the placeholder text in the command) `helm install . --set image.keda=kedacore/keda:[tag used in step 1],image.pullPolicy=IfNotPresent`.

The last step assumes that you have `helm` already installed in the cluster. In this step we install the helm chart, and we substitute the image with the image we built in step 1. Notice that we are also overriding the image PullPolice to `IfNotPresent` since this is a local cluster.
Expand All @@ -29,12 +29,12 @@ The scalers in KEDA are implementations of a KEDA `Scaler` Go interface declared
This is the key function of a scaler; it returns a value that represents a current state of an external metric (e.g. length of a queue). The return type is an `ExternalMetricValue` struct which has the following fields:
- `MetricName`: this is the name of the metric that we are returning.
- `Timestamp`: indicates the time at which the metrics were produced.
- `WindowSeconds`: //TODO
- `WindowSeconds`: //TODO
- `Value`: A numerical value that represents the state of the metric. It could be the length of a queue, or it can be the amount of lag in a stream, but it can also be a simple representation of the state.

Kubernetes HPA (Horizontal Pod Autoscaler) will poll `GetMetrics` reulgarly through KEDA's metric server (as long as there is at least one pod), and compare the returned value to a configured value in the ScaledObject configuration. Kubernetes will use the following formula to decide whether to scale the pods up and down:
Kubernetes HPA (Horizontal Pod Autoscaler) will poll `GetMetrics` reulgarly through KEDA's metric server (as long as there is at least one pod), and compare the returned value to a configured value in the ScaledObject configuration. Kubernetes will use the following formula to decide whether to scale the pods up and down:

`desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]`.
`desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]`.

For more details check [Kubernetes HPA documentation](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/).

Expand All @@ -50,15 +50,15 @@ The return type of this function is `MetricSpec`, but in KEDA's case we will mos

### IsActive

For some reason, the scaler might need to declare itself as in-active, and the way it can do this is through implementing the function `IsActive`.
For some reason, the scaler might need to declare itself as in-active, and the way it can do this is through implementing the function `IsActive`.

KEDA polls ScaledObject object according to the `pollingInterval` confiugred in the ScaledObject; it checks the last time it was polled, it checks if the number of replicas is greater than 0, and if the scaler itself is active. So if the scaler returns false for `IsActive`, and if current number of replicas is greater than 0, and there is no configured minimum pods, then KEDA scales down to 0.

### Close
After each poll on the scaler to retrieve the metrics, KEDA calls this function for each scaler to give the scaler the opportunity to close any resources, like http clients for example.

### Constructor
What is missing from the `scaler` interface is a function that constructs the scaler itself. Up until the moment of writing this document, KEDA does not have a dynamic way to load scalers (at least not officially)[***]; instead scalers are part of KEDA's code-base, and they are shipped with KEDA's binary.
What is missing from the `scaler` interface is a function that constructs the scaler itself. Up until the moment of writing this document, KEDA does not have a dynamic way to load scalers (at least not officially)[***]; instead scalers are part of KEDA's code-base, and they are shipped with KEDA's binary.

Thus, each scaler should have a constructing function, KEDA will [explicitly invoke](https://github.com/kedacore/keda/blob/4d0cf5ef09ef348cf3a158634910f00741ae5258/pkg/handler/scale_handler.go#L565) the construction function based on the `trigger` property configured in the ScaledObject.

Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ ifndef GOROOT
@echo "WARNING: GOROOT is not defined"
endif

.PHONY: gofmt
gofmt:
go fmt ./...

.PHONY: build
build: checkenv build-adapter build-controller
build: gofmt checkenv build-adapter build-controller

.PHONY: build-controller
build-controller: generate-api pkg/scalers/liiklus/LiiklusService.pb.go
Expand All @@ -112,6 +116,9 @@ build-adapter: generate-api pkg/scalers/liiklus/LiiklusService.pb.go
generate-api:
$(GO_BUILD_VARS) operator-sdk generate k8s
$(GO_BUILD_VARS) operator-sdk generate crds
# withTriggers is only used for duck typing so we only need the deepcopy methods
# However operator-sdk generate doesn't appear to have an option for that
rm deploy/crds/keda.sh_withtriggers_crd.yaml

pkg/scalers/liiklus/LiiklusService.pb.go: hack/LiiklusService.proto
protoc -I hack/ hack/LiiklusService.proto --go_out=plugins=grpc:pkg/scalers/liiklus
Expand Down
13 changes: 6 additions & 7 deletions cmd/adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
"os"
"runtime"

"github.com/kedacore/keda/pkg/handler"
kedav1alpha1 "github.com/kedacore/keda/pkg/apis/keda/v1alpha1"
kedaprovider "github.com/kedacore/keda/pkg/provider"
"github.com/kedacore/keda/pkg/scaling"
"github.com/kedacore/keda/version"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
"k8s.io/klog/klogr"

kedav1alpha1 "github.com/kedacore/keda/pkg/apis/keda/v1alpha1"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
"k8s.io/klog/klogr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"

Expand Down Expand Up @@ -62,7 +61,7 @@ func (a *Adapter) makeProviderOrDie() provider.MetricsProvider {
os.Exit(1)
}

handler := handler.NewScaleHandler(kubeclient, nil, scheme)
handler := scaling.NewScaleHandler(kubeclient, nil, scheme)

namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
Expand Down
Loading

0 comments on commit b0c17f5

Please sign in to comment.