Skip to content

Commit d0fcb93

Browse files
authored
feat: opt-out of auto-scaling per server (k8s) (#454)
1 parent 534afd4 commit d0fcb93

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ spec:
366366
- name: mc
367367
```
368368

369+
You can also opt-out of auto-scaling per server by setting the following annotations on the `Service` object:
370+
- `mc-router.itzg.me/autoScaleUp=false`
371+
- `mc-router.itzg.me/autoScaleDown=false`
372+
373+
Example server with auto-scaling disabled explicitly:
374+
```yaml
375+
apiVersion: v1
376+
kind: Service
377+
metadata:
378+
name: mc-forge
379+
annotations:
380+
"mc-router.itzg.me/externalServerName": "external.host.name"
381+
"mc-router.itzg.me/autoScaleUp": "false"
382+
"mc-router.itzg.me/autoScaleDown": "false"
383+
```
384+
369385
## REST API
370386

371387
* `GET /routes` (with `Accept: application/json`)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/prometheus/client_golang v1.23.2
1515
github.com/sirupsen/logrus v1.9.3
1616
github.com/stretchr/testify v1.11.1
17-
golang.ngrok.com/ngrok v1.13.0
17+
golang.ngrok.com/ngrok v1.12.1
1818
golang.org/x/text v0.28.0
1919
k8s.io/api v0.33.4
2020
k8s.io/apimachinery v0.33.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
200200
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
201201
golang.ngrok.com/muxado/v2 v2.0.1 h1:jM9i6Pom6GGmnPrHKNR6OJRrUoHFkSZlJ3/S0zqdVpY=
202202
golang.ngrok.com/muxado/v2 v2.0.1/go.mod h1:wzxJYX4xiAtmwumzL+QsukVwFRXmPNv86vB8RPpOxyM=
203-
golang.ngrok.com/ngrok v1.13.0 h1:6SeOS+DAeIaHlkDmNH5waFHv0xjlavOV3wml0Z59/8k=
204-
golang.ngrok.com/ngrok v1.13.0/go.mod h1:BKOMdoZXfD4w6o3EtE7Cu9TVbaUWBqptrZRWnVcAuI4=
203+
golang.ngrok.com/ngrok v1.12.1 h1:fjPyPr/R5/Et02x52iIJD2XqukwYeafsHNvM1ndJDAI=
204+
golang.ngrok.com/ngrok v1.12.1/go.mod h1:BKOMdoZXfD4w6o3EtE7Cu9TVbaUWBqptrZRWnVcAuI4=
205205
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
206206
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
207207
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

server/k8s.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
const (
2424
AnnotationExternalServerName = "mc-router.itzg.me/externalServerName"
2525
AnnotationDefaultServer = "mc-router.itzg.me/defaultServer"
26+
AnnotationAutoScaleUp = "mc-router.itzg.me/autoScaleUp"
27+
AnnotationAutoScaleDown = "mc-router.itzg.me/autoScaleDown"
2628
)
2729

2830
// K8sWatcher is a RouteFinder that can find routes from kubernetes services.
@@ -279,11 +281,32 @@ func (w *K8sWatcher) buildDetails(service *core.Service, externalServiceName str
279281
}
280282

281283
func (w *K8sWatcher) buildScaleFunction(service *core.Service, from int32, to int32) ScalerFunc {
282-
if from <= to && !w.autoScaleUp {
283-
return nil
284+
// Currently, annotations can only be used to opt-out of auto-scaling.
285+
// However, this logic is prepared also for opt-in, as it returns a `ScalerFunc` when flags are false but annotations are set to `enabled`.
286+
if from <= to {
287+
enabled, exists := service.Annotations[AnnotationAutoScaleUp]
288+
if exists {
289+
if enabled == "false" {
290+
return nil
291+
}
292+
} else {
293+
if !w.autoScaleUp {
294+
return nil
295+
}
296+
}
284297
}
285-
if from >= to && !w.autoScaleDown {
286-
return nil
298+
if from >= to {
299+
enabled, exists := service.Annotations[AnnotationAutoScaleDown]
300+
if exists {
301+
if enabled == "false" {
302+
return nil
303+
}
304+
} else {
305+
if !w.autoScaleDown {
306+
return nil
307+
}
308+
}
309+
287310
}
288311
return func(ctx context.Context) error {
289312
serviceName := service.Name

0 commit comments

Comments
 (0)