diff --git a/README.md b/README.md index b5c0b22..3c8e153 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,22 @@ spec: - name: mc ``` +You can also opt-out of auto-scaling per server by setting the following annotations on the `Service` object: +- `mc-router.itzg.me/autoScaleUp=false` +- `mc-router.itzg.me/autoScaleDown=false` + +Example server with auto-scaling disabled explicitly: +```yaml +apiVersion: v1 +kind: Service +metadata: + name: mc-forge + annotations: + "mc-router.itzg.me/externalServerName": "external.host.name" + "mc-router.itzg.me/autoScaleUp": "false" + "mc-router.itzg.me/autoScaleDown": "false" +``` + ## REST API * `GET /routes` (with `Accept: application/json`) diff --git a/go.mod b/go.mod index 3cab3c1..9cdf608 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/prometheus/client_golang v1.23.2 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.11.1 - golang.ngrok.com/ngrok v1.13.0 + golang.ngrok.com/ngrok v1.12.1 golang.org/x/text v0.28.0 k8s.io/api v0.33.4 k8s.io/apimachinery v0.33.4 diff --git a/go.sum b/go.sum index 5f0dd00..8c06dd2 100644 --- a/go.sum +++ b/go.sum @@ -200,8 +200,8 @@ go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= golang.ngrok.com/muxado/v2 v2.0.1 h1:jM9i6Pom6GGmnPrHKNR6OJRrUoHFkSZlJ3/S0zqdVpY= golang.ngrok.com/muxado/v2 v2.0.1/go.mod h1:wzxJYX4xiAtmwumzL+QsukVwFRXmPNv86vB8RPpOxyM= -golang.ngrok.com/ngrok v1.13.0 h1:6SeOS+DAeIaHlkDmNH5waFHv0xjlavOV3wml0Z59/8k= -golang.ngrok.com/ngrok v1.13.0/go.mod h1:BKOMdoZXfD4w6o3EtE7Cu9TVbaUWBqptrZRWnVcAuI4= +golang.ngrok.com/ngrok v1.12.1 h1:fjPyPr/R5/Et02x52iIJD2XqukwYeafsHNvM1ndJDAI= +golang.ngrok.com/ngrok v1.12.1/go.mod h1:BKOMdoZXfD4w6o3EtE7Cu9TVbaUWBqptrZRWnVcAuI4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/server/k8s.go b/server/k8s.go index ec401d5..3eae748 100644 --- a/server/k8s.go +++ b/server/k8s.go @@ -23,6 +23,8 @@ import ( const ( AnnotationExternalServerName = "mc-router.itzg.me/externalServerName" AnnotationDefaultServer = "mc-router.itzg.me/defaultServer" + AnnotationAutoScaleUp = "mc-router.itzg.me/autoScaleUp" + AnnotationAutoScaleDown = "mc-router.itzg.me/autoScaleDown" ) // K8sWatcher is a RouteFinder that can find routes from kubernetes services. @@ -279,11 +281,32 @@ func (w *K8sWatcher) buildDetails(service *core.Service, externalServiceName str } func (w *K8sWatcher) buildScaleFunction(service *core.Service, from int32, to int32) ScalerFunc { - if from <= to && !w.autoScaleUp { - return nil + // Currently, annotations can only be used to opt-out of auto-scaling. + // However, this logic is prepared also for opt-in, as it returns a `ScalerFunc` when flags are false but annotations are set to `enabled`. + if from <= to { + enabled, exists := service.Annotations[AnnotationAutoScaleUp] + if exists { + if enabled == "false" { + return nil + } + } else { + if !w.autoScaleUp { + return nil + } + } } - if from >= to && !w.autoScaleDown { - return nil + if from >= to { + enabled, exists := service.Annotations[AnnotationAutoScaleDown] + if exists { + if enabled == "false" { + return nil + } + } else { + if !w.autoScaleDown { + return nil + } + } + } return func(ctx context.Context) error { serviceName := service.Name