mirror of
https://github.com/zalando-incubator/kube-metrics-adapter.git
synced 2026-05-31 01:47:05 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6149308e6d | |||
| 62cc9c98f3 | |||
| e03222b12b | |||
| 063d3de6d2 | |||
| 9c8837c8ff | |||
| ec55cda1f8 | |||
| f7dfe8aedf | |||
| e0a1ca5de6 | |||
| 876074ab75 | |||
| 5da796fc9a | |||
| 5f6a683d64 | |||
| d416441688 | |||
| 4204daa44f | |||
| f02956ea35 | |||
| e580c5b5f5 | |||
| 58000627d6 | |||
| 8fefda5935 | |||
| 26baf4baed | |||
| 4168dae7dd | |||
| 6c95e44756 | |||
| 9f26d4b2ed | |||
| c26604c36b | |||
| ca9031228b | |||
| 48f0df44d9 | |||
| 964c4ecf04 | |||
| 498f85e7b1 | |||
| bdb374c4c1 | |||
| 802bf08508 |
@@ -10,10 +10,10 @@ jobs:
|
|||||||
tests:
|
tests:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v4
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: '^1.19'
|
go-version: '^1.23'
|
||||||
- run: go version
|
- run: go version
|
||||||
- run: go install github.com/mattn/goveralls@latest
|
- run: go install github.com/mattn/goveralls@latest
|
||||||
- run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
- run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ metadata:
|
|||||||
annotations:
|
annotations:
|
||||||
# metric-config.<metricType>.<metricName>.<collectorType>/<configKey>
|
# metric-config.<metricType>.<metricName>.<collectorType>/<configKey>
|
||||||
metric-config.pods.requests-per-second.json-path/json-key: "$.http_server.rps"
|
metric-config.pods.requests-per-second.json-path/json-key: "$.http_server.rps"
|
||||||
|
metric-config.pods.requests-per-second.json-path/json-eval: "ceil($['active processes'] / $['total processes'] * 100)" # cannot use both json-eval and json-key
|
||||||
metric-config.pods.requests-per-second.json-path/path: /metrics
|
metric-config.pods.requests-per-second.json-path/path: /metrics
|
||||||
metric-config.pods.requests-per-second.json-path/port: "9090"
|
metric-config.pods.requests-per-second.json-path/port: "9090"
|
||||||
metric-config.pods.requests-per-second.json-path/scheme: "https"
|
metric-config.pods.requests-per-second.json-path/scheme: "https"
|
||||||
@@ -158,6 +159,10 @@ The json-path query support depends on the
|
|||||||
See the README for possible queries. It's expected that the metric you query
|
See the README for possible queries. It's expected that the metric you query
|
||||||
returns something that can be turned into a `float64`.
|
returns something that can be turned into a `float64`.
|
||||||
|
|
||||||
|
The `json-eval` configuration option allows for more complex calculations to be
|
||||||
|
performed on the extracted metric. The `json-eval` expression is evaluated using
|
||||||
|
[ajson's script engine](https://github.com/spyzhov/ajson?tab=readme-ov-file#script-engine).
|
||||||
|
|
||||||
The other configuration options `path`, `port` and `scheme` specify where the metrics
|
The other configuration options `path`, `port` and `scheme` specify where the metrics
|
||||||
endpoint is exposed on the pod. The `path` and `port` options do not have default values
|
endpoint is exposed on the pod. The `path` and `port` options do not have default values
|
||||||
so they must be defined. The `scheme` is optional and defaults to `http`.
|
so they must be defined. The `scheme` is optional and defaults to `http`.
|
||||||
@@ -825,6 +830,7 @@ metadata:
|
|||||||
annotations:
|
annotations:
|
||||||
# metric-config.<metricType>.<metricName>.<collectorType>/<configKey>
|
# metric-config.<metricType>.<metricName>.<collectorType>/<configKey>
|
||||||
metric-config.external.unique-metric-name.json-path/json-key: "$.some-metric.value"
|
metric-config.external.unique-metric-name.json-path/json-key: "$.some-metric.value"
|
||||||
|
metric-config.external.unique-metric-name.json-path/json-eval: ceil($['active processes'] / $['total processes'] * 100) # cannot use both json-eval and json-key
|
||||||
metric-config.external.unique-metric-name.json-path/endpoint: "http://metric-source.app-namespace:8080/metrics"
|
metric-config.external.unique-metric-name.json-path/endpoint: "http://metric-source.app-namespace:8080/metrics"
|
||||||
metric-config.external.unique-metric-name.json-path/aggregator: "max"
|
metric-config.external.unique-metric-name.json-path/aggregator: "max"
|
||||||
metric-config.external.unique-metric-name.json-path/interval: "60s" # optional
|
metric-config.external.unique-metric-name.json-path/interval: "60s" # optional
|
||||||
@@ -852,6 +858,8 @@ The HTTP collector similar to the Pod Metrics collector. The following
|
|||||||
configuration values are supported:
|
configuration values are supported:
|
||||||
|
|
||||||
- `json-key` to specify the JSON path of the metric to be queried
|
- `json-key` to specify the JSON path of the metric to be queried
|
||||||
|
- `json-eval` to specify an evaluate string to [evaluate on the script engine](https://github.com/spyzhov/ajson?tab=readme-ov-file#script-engine),
|
||||||
|
cannot be used in conjunction with `json-key`
|
||||||
- `endpoint` the fully formed path to query for the metric. In the above example a Kubernetes _Service_
|
- `endpoint` the fully formed path to query for the metric. In the above example a Kubernetes _Service_
|
||||||
in the namespace `app-namespace` is called.
|
in the namespace `app-namespace` is called.
|
||||||
- `aggregator` is only required if the metric is an array of values and specifies how the values
|
- `aggregator` is only required if the metric is an array of values and specifies how the values
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
|
|||||||
kind: CustomResourceDefinition
|
kind: CustomResourceDefinition
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
controller-gen.kubebuilder.io/version: v0.15.0
|
controller-gen.kubebuilder.io/version: v0.16.5
|
||||||
name: clusterscalingschedules.zalando.org
|
name: clusterscalingschedules.zalando.org
|
||||||
spec:
|
spec:
|
||||||
group: zalando.org
|
group: zalando.org
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ apiVersion: v1
|
|||||||
metadata:
|
metadata:
|
||||||
name: kube-metrics-adapter
|
name: kube-metrics-adapter
|
||||||
namespace: {{ .Values.namespace }}
|
namespace: {{ .Values.namespace }}
|
||||||
|
annotations: {{- toYaml .Values.serviceAccountAnnotations | nindent 4 }}
|
||||||
|
|||||||
@@ -107,3 +107,5 @@ affinity: {}
|
|||||||
priorityClassName: ""
|
priorityClassName: ""
|
||||||
|
|
||||||
podAnnotations: {}
|
podAnnotations: {}
|
||||||
|
|
||||||
|
serviceAccountAnnotations: {}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
|
|||||||
kind: CustomResourceDefinition
|
kind: CustomResourceDefinition
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
controller-gen.kubebuilder.io/version: v0.15.0
|
controller-gen.kubebuilder.io/version: v0.16.5
|
||||||
name: scalingschedules.zalando.org
|
name: scalingschedules.zalando.org
|
||||||
spec:
|
spec:
|
||||||
group: zalando.org
|
group: zalando.org
|
||||||
|
|||||||
@@ -1,32 +1,33 @@
|
|||||||
module github.com/zalando-incubator/kube-metrics-adapter
|
module github.com/zalando-incubator/kube-metrics-adapter
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/argoproj/argo-rollouts v1.7.2
|
github.com/argoproj/argo-rollouts v1.8.0
|
||||||
github.com/aws/aws-sdk-go-v2 v1.32.2
|
github.com/aws/aws-sdk-go-v2 v1.36.1
|
||||||
github.com/aws/aws-sdk-go-v2/config v1.28.0
|
github.com/aws/aws-sdk-go-v2/config v1.29.6
|
||||||
github.com/aws/aws-sdk-go-v2/service/sqs v1.36.2
|
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.14
|
||||||
github.com/influxdata/influxdb-client-go v1.4.0
|
github.com/influxdata/influxdb-client-go v1.4.0
|
||||||
github.com/prometheus/client_golang v1.19.1
|
github.com/prometheus/client_golang v1.20.5
|
||||||
github.com/prometheus/common v0.55.0
|
github.com/prometheus/common v0.62.0
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.9.1
|
||||||
github.com/spyzhov/ajson v0.9.5
|
github.com/spyzhov/ajson v0.9.6
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.10.0
|
||||||
github.com/szuecs/routegroup-client v0.28.2
|
github.com/szuecs/routegroup-client v0.28.2
|
||||||
github.com/zalando-incubator/cluster-lifecycle-manager v0.0.0-20240619093047-7853f3386b71
|
github.com/zalando-incubator/cluster-lifecycle-manager v0.0.0-20240619093047-7853f3386b71
|
||||||
golang.org/x/net v0.30.0
|
golang.org/x/net v0.35.0
|
||||||
golang.org/x/oauth2 v0.23.0
|
golang.org/x/oauth2 v0.26.0
|
||||||
golang.org/x/sync v0.8.0
|
golang.org/x/sync v0.11.0
|
||||||
k8s.io/api v0.31.3
|
k8s.io/api v0.31.6
|
||||||
k8s.io/apimachinery v0.31.3
|
k8s.io/apimachinery v0.31.6
|
||||||
k8s.io/apiserver v0.31.3
|
k8s.io/apiserver v0.31.6
|
||||||
k8s.io/client-go v0.31.3
|
k8s.io/client-go v0.31.6
|
||||||
k8s.io/code-generator v0.31.3
|
k8s.io/code-generator v0.31.6
|
||||||
k8s.io/component-base v0.31.3
|
k8s.io/component-base v0.31.6
|
||||||
k8s.io/klog v1.0.0
|
k8s.io/klog v1.0.0
|
||||||
k8s.io/kube-openapi v0.0.0-20240620174524-b456828f718b
|
k8s.io/kube-openapi v0.0.0-20240620174524-b456828f718b
|
||||||
k8s.io/metrics v0.31.3
|
k8s.io/metrics v0.31.6
|
||||||
sigs.k8s.io/controller-tools v0.15.0
|
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
|
||||||
|
sigs.k8s.io/controller-tools v0.16.5
|
||||||
sigs.k8s.io/custom-metrics-apiserver v1.30.1-0.20241105195130-84dc8cfe2555
|
sigs.k8s.io/custom-metrics-apiserver v1.30.1-0.20241105195130-84dc8cfe2555
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,17 +42,17 @@ require (
|
|||||||
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
|
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
|
||||||
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
|
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
|
||||||
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
|
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.41 // indirect
|
github.com/aws/aws-sdk-go-v2/credentials v1.17.59 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.17 // indirect
|
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.28 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.21 // indirect
|
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.32 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.21 // indirect
|
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.32 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
|
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.2 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 // indirect
|
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.2 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.2 // indirect
|
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.13 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/service/sso v1.24.2 // indirect
|
github.com/aws/aws-sdk-go-v2/service/sso v1.24.15 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.2 // indirect
|
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.14 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/service/sts v1.32.2 // indirect
|
github.com/aws/aws-sdk-go-v2/service/sts v1.33.14 // indirect
|
||||||
github.com/aws/smithy-go v1.22.0 // indirect
|
github.com/aws/smithy-go v1.22.2 // indirect
|
||||||
github.com/aymerick/douceur v0.2.0 // indirect
|
github.com/aymerick/douceur v0.2.0 // indirect
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/blang/semver/v4 v4.0.0 // indirect
|
github.com/blang/semver/v4 v4.0.0 // indirect
|
||||||
@@ -66,7 +67,7 @@ require (
|
|||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||||
github.com/deepmap/oapi-codegen v1.16.3 // indirect
|
github.com/deepmap/oapi-codegen v1.16.3 // indirect
|
||||||
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
|
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
|
||||||
github.com/fatih/color v1.16.0 // indirect
|
github.com/fatih/color v1.18.0 // indirect
|
||||||
github.com/fatih/structs v1.1.0 // indirect
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||||
github.com/flosch/pongo2/v4 v4.0.2 // indirect
|
github.com/flosch/pongo2/v4 v4.0.2 // indirect
|
||||||
@@ -83,14 +84,14 @@ require (
|
|||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.22.0 // indirect
|
github.com/go-playground/validator/v10 v10.22.0 // indirect
|
||||||
github.com/gobuffalo/flect v1.0.2 // indirect
|
github.com/gobuffalo/flect v1.0.3 // indirect
|
||||||
github.com/goccy/go-json v0.10.3 // indirect
|
github.com/goccy/go-json v0.10.3 // indirect
|
||||||
github.com/gogo/protobuf v1.3.2 // indirect
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
github.com/golang/protobuf v1.5.4 // indirect
|
github.com/golang/protobuf v1.5.4 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 // indirect
|
github.com/gomarkdown/markdown v0.0.0-20241205020045-f7e15b2f3e62 // indirect
|
||||||
github.com/google/cel-go v0.20.1 // indirect
|
github.com/google/cel-go v0.20.1 // indirect
|
||||||
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
|
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
@@ -113,6 +114,7 @@ require (
|
|||||||
github.com/kataras/tunnel v0.0.4 // indirect
|
github.com/kataras/tunnel v0.0.4 // indirect
|
||||||
github.com/klauspost/compress v1.17.9 // indirect
|
github.com/klauspost/compress v1.17.9 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
||||||
|
github.com/kylelemons/godebug v1.1.0 // indirect
|
||||||
github.com/labstack/echo/v4 v4.12.0 // indirect
|
github.com/labstack/echo/v4 v4.12.0 // indirect
|
||||||
github.com/labstack/gommon v0.4.2 // indirect
|
github.com/labstack/gommon v0.4.2 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
@@ -131,7 +133,7 @@ require (
|
|||||||
github.com/prometheus/procfs v0.15.1 // indirect
|
github.com/prometheus/procfs v0.15.1 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||||
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
|
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.6 // indirect
|
||||||
github.com/stoewer/go-strcase v1.3.0 // indirect
|
github.com/stoewer/go-strcase v1.3.0 // indirect
|
||||||
github.com/tdewolff/minify/v2 v2.20.34 // indirect
|
github.com/tdewolff/minify/v2 v2.20.34 // indirect
|
||||||
github.com/tdewolff/parse/v2 v2.7.15 // indirect
|
github.com/tdewolff/parse/v2 v2.7.15 // indirect
|
||||||
@@ -158,30 +160,28 @@ require (
|
|||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
go.uber.org/zap v1.26.0 // indirect
|
go.uber.org/zap v1.26.0 // indirect
|
||||||
golang.org/x/arch v0.8.0 // indirect
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
golang.org/x/crypto v0.28.0 // indirect
|
golang.org/x/crypto v0.33.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
|
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
|
||||||
golang.org/x/mod v0.18.0 // indirect
|
golang.org/x/mod v0.21.0 // indirect
|
||||||
golang.org/x/sys v0.26.0 // indirect
|
golang.org/x/sys v0.30.0 // indirect
|
||||||
golang.org/x/term v0.25.0 // indirect
|
golang.org/x/term v0.29.0 // indirect
|
||||||
golang.org/x/text v0.19.0 // indirect
|
golang.org/x/text v0.22.0 // indirect
|
||||||
golang.org/x/time v0.5.0 // indirect
|
golang.org/x/time v0.5.0 // indirect
|
||||||
golang.org/x/tools v0.22.0 // indirect
|
golang.org/x/tools v0.26.0 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
|
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20240528184218-531527333157 // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
|
google.golang.org/grpc v1.68.1 // indirect
|
||||||
google.golang.org/grpc v1.65.0 // indirect
|
google.golang.org/protobuf v1.36.1 // indirect
|
||||||
google.golang.org/protobuf v1.34.2 // indirect
|
|
||||||
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
|
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
|
||||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
k8s.io/apiextensions-apiserver v0.30.0 // indirect
|
k8s.io/apiextensions-apiserver v0.31.2 // indirect
|
||||||
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect
|
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect
|
||||||
k8s.io/klog/v2 v2.130.1 // indirect
|
k8s.io/klog/v2 v2.130.1 // indirect
|
||||||
k8s.io/kms v0.31.3 // indirect
|
k8s.io/kms v0.31.6 // indirect
|
||||||
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
|
|
||||||
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3 // indirect
|
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3 // indirect
|
||||||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
|
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
|
||||||
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
|
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
|
||||||
|
|||||||
@@ -6,6 +6,13 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ScalingScheduler is an interface that represents a ScalingSchedule resource,
|
||||||
|
// namespaced or cluster wide.
|
||||||
|
type ScalingScheduler interface {
|
||||||
|
Identifier() string
|
||||||
|
ResourceSpec() ScalingScheduleSpec
|
||||||
|
}
|
||||||
|
|
||||||
// +genclient
|
// +genclient
|
||||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||||
// +kubebuilder:object:root=true
|
// +kubebuilder:object:root=true
|
||||||
@@ -25,6 +32,17 @@ type ScalingSchedule struct {
|
|||||||
Status ScalingScheduleStatus `json:"status"`
|
Status ScalingScheduleStatus `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Identifier returns the namespaced scalingScale Identifier in the format
|
||||||
|
// `<namespace>/<name>`.
|
||||||
|
func (s *ScalingSchedule) Identifier() string {
|
||||||
|
return s.ObjectMeta.Namespace + "/" + s.ObjectMeta.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResourceSpec returns the ScalingScheduleSpec of the ScalingSchedule.
|
||||||
|
func (s *ScalingSchedule) ResourceSpec() ScalingScheduleSpec {
|
||||||
|
return s.Spec
|
||||||
|
}
|
||||||
|
|
||||||
// +genclient
|
// +genclient
|
||||||
// +genclient:nonNamespaced
|
// +genclient:nonNamespaced
|
||||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||||
@@ -46,6 +64,17 @@ type ClusterScalingSchedule struct {
|
|||||||
Status ScalingScheduleStatus `json:"status"`
|
Status ScalingScheduleStatus `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Identifier returns the cluster scalingScale Identifier in the format
|
||||||
|
// `<name>`.
|
||||||
|
func (s *ClusterScalingSchedule) Identifier() string {
|
||||||
|
return s.ObjectMeta.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResourceSpec returns the ScalingScheduleSpec of the ClusterScalingSchedule.
|
||||||
|
func (s *ClusterScalingSchedule) ResourceSpec() ScalingScheduleSpec {
|
||||||
|
return s.Spec
|
||||||
|
}
|
||||||
|
|
||||||
// ScalingScheduleSpec is the spec part of the ScalingSchedule.
|
// ScalingScheduleSpec is the spec part of the ScalingSchedule.
|
||||||
// +k8s:deepcopy-gen=true
|
// +k8s:deepcopy-gen=true
|
||||||
type ScalingScheduleSpec struct {
|
type ScalingScheduleSpec struct {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ const (
|
|||||||
HTTPMetricNameLegacy = "http"
|
HTTPMetricNameLegacy = "http"
|
||||||
HTTPEndpointAnnotationKey = "endpoint"
|
HTTPEndpointAnnotationKey = "endpoint"
|
||||||
HTTPJsonPathAnnotationKey = "json-key"
|
HTTPJsonPathAnnotationKey = "json-key"
|
||||||
|
HTTPJsonEvalAnnotationKey = "json-eval"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPCollectorPlugin struct{}
|
type HTTPCollectorPlugin struct{}
|
||||||
@@ -31,14 +32,27 @@ func (p *HTTPCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2
|
|||||||
collector := &HTTPCollector{
|
collector := &HTTPCollector{
|
||||||
namespace: hpa.Namespace,
|
namespace: hpa.Namespace,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
value string
|
value string
|
||||||
ok bool
|
ok bool
|
||||||
|
|
||||||
|
jsonPath string
|
||||||
|
jsonEval string
|
||||||
)
|
)
|
||||||
if value, ok = config.Config[HTTPJsonPathAnnotationKey]; !ok {
|
|
||||||
return nil, fmt.Errorf("config value %s not found", HTTPJsonPathAnnotationKey)
|
if value, ok = config.Config[HTTPJsonPathAnnotationKey]; ok {
|
||||||
|
jsonPath = value
|
||||||
|
}
|
||||||
|
if value, ok = config.Config[HTTPJsonEvalAnnotationKey]; ok {
|
||||||
|
jsonEval = value
|
||||||
|
}
|
||||||
|
if jsonPath == "" && jsonEval == "" {
|
||||||
|
return nil, fmt.Errorf("config value %s or %s not found", HTTPJsonPathAnnotationKey, HTTPJsonEvalAnnotationKey)
|
||||||
|
}
|
||||||
|
if jsonPath != "" && jsonEval != "" {
|
||||||
|
return nil, fmt.Errorf("config value %s and %s cannot be used together", HTTPJsonPathAnnotationKey, HTTPJsonEvalAnnotationKey)
|
||||||
}
|
}
|
||||||
jsonPath := value
|
|
||||||
|
|
||||||
if value, ok = config.Config[HTTPEndpointAnnotationKey]; !ok {
|
if value, ok = config.Config[HTTPEndpointAnnotationKey]; !ok {
|
||||||
return nil, fmt.Errorf("config value %s not found", HTTPEndpointAnnotationKey)
|
return nil, fmt.Errorf("config value %s not found", HTTPEndpointAnnotationKey)
|
||||||
@@ -62,7 +76,7 @@ func (p *HTTPCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jsonPathGetter, err := httpmetrics.NewJSONPathMetricsGetter(httpmetrics.DefaultMetricsHTTPClient(), aggFunc, jsonPath)
|
jsonPathGetter, err := httpmetrics.NewJSONPathMetricsGetter(httpmetrics.DefaultMetricsHTTPClient(), aggFunc, jsonPath, jsonEval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,18 +16,22 @@ import (
|
|||||||
// the json path query.
|
// the json path query.
|
||||||
type JSONPathMetricsGetter struct {
|
type JSONPathMetricsGetter struct {
|
||||||
jsonPath string
|
jsonPath string
|
||||||
|
jsonEval string
|
||||||
aggregator AggregatorFunc
|
aggregator AggregatorFunc
|
||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewJSONPathMetricsGetter initializes a new JSONPathMetricsGetter.
|
// NewJSONPathMetricsGetter initializes a new JSONPathMetricsGetter.
|
||||||
func NewJSONPathMetricsGetter(httpClient *http.Client, aggregatorFunc AggregatorFunc, jsonPath string) (*JSONPathMetricsGetter, error) {
|
func NewJSONPathMetricsGetter(httpClient *http.Client, aggregatorFunc AggregatorFunc, jsonPath string, jsonEval string) (*JSONPathMetricsGetter, error) {
|
||||||
// check that jsonPath parses
|
// check that jsonPath parses
|
||||||
|
if jsonPath != "" {
|
||||||
_, err := ajson.ParseJSONPath(jsonPath)
|
_, err := ajson.ParseJSONPath(jsonPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &JSONPathMetricsGetter{client: httpClient, aggregator: aggregatorFunc, jsonPath: jsonPath}, nil
|
}
|
||||||
|
|
||||||
|
return &JSONPathMetricsGetter{client: httpClient, aggregator: aggregatorFunc, jsonPath: jsonPath, jsonEval: jsonEval}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultRequestTimeout = 15 * time.Second
|
var DefaultRequestTimeout = 15 * time.Second
|
||||||
@@ -67,10 +71,20 @@ func (g *JSONPathMetricsGetter) GetMetric(metricsURL url.URL) (float64, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes, err := root.JSONPath(g.jsonPath)
|
var nodes []*ajson.Node
|
||||||
|
if g.jsonPath != "" {
|
||||||
|
nodes, err = root.JSONPath(g.jsonPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
result, err := ajson.Eval(root, g.jsonEval)
|
||||||
|
nodes = append(nodes, result)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(nodes) == 0 {
|
if len(nodes) == 0 {
|
||||||
return 0, fmt.Errorf("unexpected json: expected single numeric or array value")
|
return 0, fmt.Errorf("unexpected json: expected single numeric or array value")
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ func TestJSONPathMetricsGetter(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
jsonResponse []byte
|
jsonResponse []byte
|
||||||
jsonPath string
|
jsonPath string
|
||||||
|
jsonEval string
|
||||||
result float64
|
result float64
|
||||||
aggregator AggregatorFunc
|
aggregator AggregatorFunc
|
||||||
err error
|
err error
|
||||||
@@ -58,6 +59,19 @@ func TestJSONPathMetricsGetter(t *testing.T) {
|
|||||||
result: 5,
|
result: 5,
|
||||||
aggregator: Average,
|
aggregator: Average,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "evaluated script",
|
||||||
|
jsonResponse: []byte(`{"active processes":1,"total processes":10}`),
|
||||||
|
jsonEval: "ceil($['active processes'] / $['total processes'] * 100)",
|
||||||
|
result: 10,
|
||||||
|
aggregator: Average,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid script should error",
|
||||||
|
jsonResponse: []byte(`{"active processes":1,"total processes":10}`),
|
||||||
|
jsonEval: "ceil($['active processes'] ) $['total processes'] * 100)",
|
||||||
|
err: errors.New("wrong request: formula has no left parentheses"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "json path not resulting in array or number should lead to error",
|
name: "json path not resulting in array or number should lead to error",
|
||||||
jsonResponse: []byte(`{"metric.value":5}`),
|
jsonResponse: []byte(`{"metric.value":5}`),
|
||||||
@@ -74,7 +88,7 @@ func TestJSONPathMetricsGetter(t *testing.T) {
|
|||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
server := makeTestHTTPServer(t, tc.jsonResponse)
|
server := makeTestHTTPServer(t, tc.jsonResponse)
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
getter, err := NewJSONPathMetricsGetter(DefaultMetricsHTTPClient(), tc.aggregator, tc.jsonPath)
|
getter, err := NewJSONPathMetricsGetter(DefaultMetricsHTTPClient(), tc.aggregator, tc.jsonPath, tc.jsonEval)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
url, err := url.Parse(fmt.Sprintf("%s/metrics", server.URL))
|
url, err := url.Parse(fmt.Sprintf("%s/metrics", server.URL))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ func NewPodMetricsJSONPathGetter(config map[string]string) (*PodMetricsJSONPathG
|
|||||||
getter := PodMetricsJSONPathGetter{}
|
getter := PodMetricsJSONPathGetter{}
|
||||||
var (
|
var (
|
||||||
jsonPath string
|
jsonPath string
|
||||||
|
jsonEval string
|
||||||
aggregator AggregatorFunc
|
aggregator AggregatorFunc
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
@@ -41,6 +42,16 @@ func NewPodMetricsJSONPathGetter(config map[string]string) (*PodMetricsJSONPathG
|
|||||||
jsonPath = v
|
jsonPath = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v, ok := config["json-eval"]; ok {
|
||||||
|
jsonEval = v
|
||||||
|
}
|
||||||
|
|
||||||
|
if jsonPath == "" && jsonEval == "" {
|
||||||
|
return nil, fmt.Errorf("config value json-key or json-eval must be set")
|
||||||
|
} else if jsonPath != "" && jsonEval != "" {
|
||||||
|
return nil, fmt.Errorf("config value json-key and json-eval are mutually exclusive")
|
||||||
|
}
|
||||||
|
|
||||||
if v, ok := config["scheme"]; ok {
|
if v, ok := config["scheme"]; ok {
|
||||||
getter.scheme = v
|
getter.scheme = v
|
||||||
}
|
}
|
||||||
@@ -93,7 +104,7 @@ func NewPodMetricsJSONPathGetter(config map[string]string) (*PodMetricsJSONPathG
|
|||||||
connectTimeout = d
|
connectTimeout = d
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonPathGetter, err := NewJSONPathMetricsGetter(CustomMetricsHTTPClient(requestTimeout, connectTimeout), aggregator, jsonPath)
|
jsonPathGetter, err := NewJSONPathMetricsGetter(CustomMetricsHTTPClient(requestTimeout, connectTimeout), aggregator, jsonPath, jsonEval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,17 @@ func TestNewPodJSONPathMetricsGetter(t *testing.T) {
|
|||||||
port: 9090,
|
port: 9090,
|
||||||
rawQuery: "foo=bar&baz=bop",
|
rawQuery: "foo=bar&baz=bop",
|
||||||
}, getterWithRawQuery)
|
}, getterWithRawQuery)
|
||||||
|
|
||||||
|
configErrorMixedPathEval := map[string]string{
|
||||||
|
"json-key": "{}",
|
||||||
|
"json-eval": "avg($.values)",
|
||||||
|
"scheme": "http",
|
||||||
|
"path": "/metrics",
|
||||||
|
"port": "9090",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err6 := NewPodMetricsJSONPathGetter(configErrorMixedPathEval)
|
||||||
|
require.Error(t, err6)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildMetricsURL(t *testing.T) {
|
func TestBuildMetricsURL(t *testing.T) {
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
package scheduledscaling
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||||
|
autoscalingv2 "k8s.io/api/autoscaling/v2"
|
||||||
|
apimeta "k8s.io/apimachinery/pkg/api/meta"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
cacheddiscovery "k8s.io/client-go/discovery/cached/memory"
|
||||||
|
"k8s.io/client-go/dynamic"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/rest"
|
||||||
|
"k8s.io/client-go/restmapper"
|
||||||
|
"k8s.io/client-go/scale"
|
||||||
|
scaleclient "k8s.io/client-go/scale"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TargetScaler is an interface for scaling a target referenced resource in an
|
||||||
|
// HPA to the desired replicas.
|
||||||
|
type TargetScaler interface {
|
||||||
|
Scale(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, replicas int32) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type hpaTargetScaler struct {
|
||||||
|
mapper apimeta.RESTMapper
|
||||||
|
scaleClient scaleclient.ScalesGetter
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHPATargetScaler creates a new TargetScaler that can scale resources
|
||||||
|
// targeted by HPAs. It takes a Kubernetes client and a REST config and uses a
|
||||||
|
// restmapper to resolve the target reference API.
|
||||||
|
func NewHPATargetScaler(ctx context.Context, kubeClient kubernetes.Interface, cfg *rest.Config) (TargetScaler, error) {
|
||||||
|
cachedClient := cacheddiscovery.NewMemCacheClient(kubeClient.Discovery())
|
||||||
|
restMapper := restmapper.NewDeferredDiscoveryRESTMapper(cachedClient)
|
||||||
|
go wait.Until(func() {
|
||||||
|
restMapper.Reset()
|
||||||
|
}, 30*time.Second, ctx.Done())
|
||||||
|
|
||||||
|
scaleKindResolver := scale.NewDiscoveryScaleKindResolver(kubeClient.Discovery())
|
||||||
|
scaleClient, err := scale.NewForConfig(cfg, restMapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create scale client: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &hpaTargetScaler{
|
||||||
|
mapper: restMapper,
|
||||||
|
scaleClient: scaleClient,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scale scales the target resource of the given HPA to the desired number of
|
||||||
|
// replicas.
|
||||||
|
func (s *hpaTargetScaler) Scale(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, replicas int32) error {
|
||||||
|
reference := fmt.Sprintf("%s/%s/%s", hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name)
|
||||||
|
|
||||||
|
targetGV, err := schema.ParseGroupVersion(hpa.Spec.ScaleTargetRef.APIVersion)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid API version '%s' in scale target reference: %w", hpa.Spec.ScaleTargetRef.APIVersion, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
targetGK := schema.GroupKind{
|
||||||
|
Group: targetGV.Group,
|
||||||
|
Kind: hpa.Spec.ScaleTargetRef.Kind,
|
||||||
|
}
|
||||||
|
|
||||||
|
mappings, err := s.mapper.RESTMappings(targetGK)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to determine resource for scale target reference: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
scale, targetGR, err := s.scaleForResourceMappings(ctx, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name, mappings)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get scale subresource for %s: %w", reference, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
scale.Spec.Replicas = replicas
|
||||||
|
_, err = s.scaleClient.Scales(hpa.Namespace).Update(ctx, targetGR, scale, metav1.UpdateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to rescale %s: %w", reference, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// scaleForResourceMappings attempts to fetch the scale for the
|
||||||
|
// resource with the given name and namespace, trying each RESTMapping
|
||||||
|
// in turn until a working one is found. If none work, the first error
|
||||||
|
// is returned. It returns both the scale, as well as the group-resource from
|
||||||
|
// the working mapping.
|
||||||
|
// from: https://github.com/kubernetes/kubernetes/blob/c9092f69fc0c099062dd23cd6ee226bcd52ec790/pkg/controller/podautoscaler/horizontal.go#L1326-L1353
|
||||||
|
func (s *hpaTargetScaler) scaleForResourceMappings(ctx context.Context, namespace, name string, mappings []*apimeta.RESTMapping) (*autoscalingv1.Scale, schema.GroupResource, error) {
|
||||||
|
var firstErr error
|
||||||
|
for i, mapping := range mappings {
|
||||||
|
targetGR := mapping.Resource.GroupResource()
|
||||||
|
scale, err := s.scaleClient.Scales(namespace).Get(ctx, targetGR, name, metav1.GetOptions{})
|
||||||
|
if err == nil {
|
||||||
|
return scale, targetGR, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// if this is the first error, remember it,
|
||||||
|
// then go on and try other mappings until we find a good one
|
||||||
|
if i == 0 {
|
||||||
|
firstErr = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure we handle an empty set of mappings
|
||||||
|
if firstErr == nil {
|
||||||
|
firstErr = fmt.Errorf("unrecognized resource")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, schema.GroupResource{}, firstErr
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -2,15 +2,24 @@ package scheduledscaling
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
v1 "github.com/zalando-incubator/kube-metrics-adapter/pkg/apis/zalando.org/v1"
|
v1 "github.com/zalando-incubator/kube-metrics-adapter/pkg/apis/zalando.org/v1"
|
||||||
scalingschedulefake "github.com/zalando-incubator/kube-metrics-adapter/pkg/client/clientset/versioned/fake"
|
scalingschedulefake "github.com/zalando-incubator/kube-metrics-adapter/pkg/client/clientset/versioned/fake"
|
||||||
|
zfake "github.com/zalando-incubator/kube-metrics-adapter/pkg/client/clientset/versioned/fake"
|
||||||
zalandov1 "github.com/zalando-incubator/kube-metrics-adapter/pkg/client/clientset/versioned/typed/zalando.org/v1"
|
zalandov1 "github.com/zalando-incubator/kube-metrics-adapter/pkg/client/clientset/versioned/typed/zalando.org/v1"
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
autoscalingv2 "k8s.io/api/autoscaling/v2"
|
||||||
|
v2 "k8s.io/api/autoscaling/v2"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
"k8s.io/utils/ptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -222,7 +231,7 @@ func TestRunOnce(t *testing.T) {
|
|||||||
err := applySchedules(client.ZalandoV1(), tc.schedules)
|
err := applySchedules(client.ZalandoV1(), tc.schedules)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
controller := NewController(client.ZalandoV1(), scalingSchedulesStore, clusterScalingSchedulesStore, now, 0, "Europe/Berlin")
|
controller := NewController(client.ZalandoV1(), fake.NewSimpleClientset(), nil, scalingSchedulesStore, clusterScalingSchedulesStore, now, 0, "Europe/Berlin", 0.10)
|
||||||
|
|
||||||
err = controller.runOnce(context.Background())
|
err = controller.runOnce(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -297,3 +306,162 @@ func checkSchedules(t *testing.T, client zalandov1.ZalandoV1Interface, schedules
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mockScaler struct {
|
||||||
|
client kubernetes.Interface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *mockScaler) Scale(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, replicas int32) error {
|
||||||
|
switch hpa.Spec.ScaleTargetRef.Kind {
|
||||||
|
case "Deployment":
|
||||||
|
deployment, err := s.client.AppsV1().Deployments(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
deployment.Spec.Replicas = &replicas
|
||||||
|
_, err = s.client.AppsV1().Deployments(hpa.Namespace).Update(ctx, deployment, metav1.UpdateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported kind %s", hpa.Spec.ScaleTargetRef.Kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAdjustScaling(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
msg string
|
||||||
|
currentReplicas int32
|
||||||
|
desiredReplicas int32
|
||||||
|
targetValue int64
|
||||||
|
scheduleTarget int64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
msg: "current less than 10%% below desired (target 10000)",
|
||||||
|
currentReplicas: 28, // 7.1% increase to desired
|
||||||
|
desiredReplicas: 31,
|
||||||
|
targetValue: 333, // 10000/333 ~= 31
|
||||||
|
scheduleTarget: 10000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
msg: "current less than 10%% below desired",
|
||||||
|
currentReplicas: 95, // 5.3% increase to desired
|
||||||
|
desiredReplicas: 100,
|
||||||
|
targetValue: 10, // 1000/10 = 100
|
||||||
|
scheduleTarget: 1000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
msg: "current more than 10%% below desired, no adjustment",
|
||||||
|
currentReplicas: 90, // 11% increase to desired
|
||||||
|
desiredReplicas: 90,
|
||||||
|
targetValue: 10, // 1000/10 = 100
|
||||||
|
scheduleTarget: 1000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
msg: "invalid HPA should not do any adjustment",
|
||||||
|
currentReplicas: 95,
|
||||||
|
desiredReplicas: 95,
|
||||||
|
targetValue: 0, // this is treated as invalid in the test, thus the HPA is ingored and no adjustment happens.
|
||||||
|
scheduleTarget: 1000,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(tc.msg, func(t *testing.T) {
|
||||||
|
kubeClient := fake.NewSimpleClientset()
|
||||||
|
scalingScheduleClient := zfake.NewSimpleClientset()
|
||||||
|
controller := NewController(
|
||||||
|
scalingScheduleClient.ZalandoV1(),
|
||||||
|
kubeClient,
|
||||||
|
&mockScaler{client: kubeClient},
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
time.Now,
|
||||||
|
time.Hour,
|
||||||
|
"Europe/Berlin",
|
||||||
|
0.10,
|
||||||
|
)
|
||||||
|
|
||||||
|
scheduleDate := v1.ScheduleDate(time.Now().Add(-10 * time.Minute).Format(time.RFC3339))
|
||||||
|
clusterScalingSchedules := []v1.ScalingScheduler{
|
||||||
|
&v1.ClusterScalingSchedule{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "schedule-1",
|
||||||
|
},
|
||||||
|
Spec: v1.ScalingScheduleSpec{
|
||||||
|
Schedules: []v1.Schedule{
|
||||||
|
{
|
||||||
|
Type: v1.OneTimeSchedule,
|
||||||
|
Date: &scheduleDate,
|
||||||
|
DurationMinutes: 15,
|
||||||
|
Value: tc.scheduleTarget,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
deployment := &appsv1.Deployment{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "deployment-1",
|
||||||
|
},
|
||||||
|
Spec: appsv1.DeploymentSpec{
|
||||||
|
Replicas: ptr.To(tc.currentReplicas),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := kubeClient.AppsV1().Deployments("default").Create(context.Background(), deployment, metav1.CreateOptions{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
hpa := &autoscalingv2.HorizontalPodAutoscaler{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "hpa-1",
|
||||||
|
},
|
||||||
|
Spec: v2.HorizontalPodAutoscalerSpec{
|
||||||
|
ScaleTargetRef: v2.CrossVersionObjectReference{
|
||||||
|
APIVersion: "apps/v1",
|
||||||
|
Kind: "Deployment",
|
||||||
|
Name: "deployment-1",
|
||||||
|
},
|
||||||
|
MinReplicas: ptr.To(int32(1)),
|
||||||
|
MaxReplicas: 1000,
|
||||||
|
Metrics: []v2.MetricSpec{
|
||||||
|
{
|
||||||
|
Type: v2.ObjectMetricSourceType,
|
||||||
|
Object: &v2.ObjectMetricSource{
|
||||||
|
DescribedObject: v2.CrossVersionObjectReference{
|
||||||
|
APIVersion: "zalando.org/v1",
|
||||||
|
Kind: "ClusterScalingSchedule",
|
||||||
|
Name: "schedule-1",
|
||||||
|
},
|
||||||
|
Target: v2.MetricTarget{
|
||||||
|
Type: v2.AverageValueMetricType,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.targetValue != 0 {
|
||||||
|
hpa.Spec.Metrics[0].Object.Target.AverageValue = resource.NewQuantity(tc.targetValue, resource.DecimalSI)
|
||||||
|
}
|
||||||
|
|
||||||
|
hpa, err = kubeClient.AutoscalingV2().HorizontalPodAutoscalers("default").Create(context.Background(), hpa, metav1.CreateOptions{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
hpa.Status.CurrentReplicas = tc.currentReplicas
|
||||||
|
_, err = kubeClient.AutoscalingV2().HorizontalPodAutoscalers("default").UpdateStatus(context.Background(), hpa, metav1.UpdateOptions{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = controller.adjustScaling(context.Background(), clusterScalingSchedules)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
deployment, err = kubeClient.AppsV1().Deployments("default").Get(context.Background(), "deployment-1", metav1.GetOptions{})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, tc.desiredReplicas, ptr.Deref(deployment.Spec.Replicas, 0))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+20
-1
@@ -138,6 +138,7 @@ func NewCommandStartAdapterServer(stopCh <-chan struct{}) *cobra.Command {
|
|||||||
flags.DurationVar(&o.DefaultScheduledScalingWindow, "scaling-schedule-default-scaling-window", 10*time.Minute, "Default rampup and rampdown window duration for ScalingSchedules")
|
flags.DurationVar(&o.DefaultScheduledScalingWindow, "scaling-schedule-default-scaling-window", 10*time.Minute, "Default rampup and rampdown window duration for ScalingSchedules")
|
||||||
flags.IntVar(&o.RampSteps, "scaling-schedule-ramp-steps", 10, "Number of steps used to rampup and rampdown ScalingSchedules. It's used to guarantee won't avoid reaching the max scaling due to the 10% minimum change rule.")
|
flags.IntVar(&o.RampSteps, "scaling-schedule-ramp-steps", 10, "Number of steps used to rampup and rampdown ScalingSchedules. It's used to guarantee won't avoid reaching the max scaling due to the 10% minimum change rule.")
|
||||||
flags.StringVar(&o.DefaultTimeZone, "scaling-schedule-default-time-zone", "Europe/Berlin", "Default time zone to use for ScalingSchedules.")
|
flags.StringVar(&o.DefaultTimeZone, "scaling-schedule-default-time-zone", "Europe/Berlin", "Default time zone to use for ScalingSchedules.")
|
||||||
|
flags.Float64Var(&o.HorizontalPodAutoscalerTolerance, "horizontal-pod-autoscaler-tolerance", 0.1, "The HPA tolerance also configured in the HPA controller.")
|
||||||
flags.StringVar(&o.ExternalRPSMetricName, "external-rps-metric-name", o.ExternalRPSMetricName, ""+
|
flags.StringVar(&o.ExternalRPSMetricName, "external-rps-metric-name", o.ExternalRPSMetricName, ""+
|
||||||
"The name of the metric that should be used to query prometheus for RPS per hostname.")
|
"The name of the metric that should be used to query prometheus for RPS per hostname.")
|
||||||
flags.BoolVar(&o.ExternalRPSMetrics, "external-rps-metrics", o.ExternalRPSMetrics, ""+
|
flags.BoolVar(&o.ExternalRPSMetrics, "external-rps-metrics", o.ExternalRPSMetrics, ""+
|
||||||
@@ -367,10 +368,25 @@ func (o AdapterServerOptions) RunCustomMetricsAdapterServer(stopCh <-chan struct
|
|||||||
return fmt.Errorf("failed to register ScalingSchedule object collector plugin: %v", err)
|
return fmt.Errorf("failed to register ScalingSchedule object collector plugin: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scaler, err := scheduledscaling.NewHPATargetScaler(ctx, client, clientConfig)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create HPA target scaler: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// setup ScheduledScaling controller to continuously update
|
// setup ScheduledScaling controller to continuously update
|
||||||
// status of ScalingSchedule and ClusterScalingSchedule
|
// status of ScalingSchedule and ClusterScalingSchedule
|
||||||
// resources.
|
// resources.
|
||||||
scheduledScalingController := scheduledscaling.NewController(scalingScheduleClient.ZalandoV1(), scalingSchedulesStore, clusterScalingSchedulesStore, time.Now, o.DefaultScheduledScalingWindow, o.DefaultTimeZone)
|
scheduledScalingController := scheduledscaling.NewController(
|
||||||
|
scalingScheduleClient.ZalandoV1(),
|
||||||
|
client,
|
||||||
|
scaler,
|
||||||
|
scalingSchedulesStore,
|
||||||
|
clusterScalingSchedulesStore,
|
||||||
|
time.Now,
|
||||||
|
o.DefaultScheduledScalingWindow,
|
||||||
|
o.DefaultTimeZone,
|
||||||
|
o.HorizontalPodAutoscalerTolerance,
|
||||||
|
)
|
||||||
|
|
||||||
go scheduledScalingController.Run(ctx)
|
go scheduledScalingController.Run(ctx)
|
||||||
}
|
}
|
||||||
@@ -501,6 +517,9 @@ type AdapterServerOptions struct {
|
|||||||
RampSteps int
|
RampSteps int
|
||||||
// Default time zone to use for ScalingSchedules.
|
// Default time zone to use for ScalingSchedules.
|
||||||
DefaultTimeZone string
|
DefaultTimeZone string
|
||||||
|
// The HPA tolerance also configured in the HPA controller.
|
||||||
|
// kube-controller-manager flag: --horizontal-pod-autoscaler-tolerance=
|
||||||
|
HorizontalPodAutoscalerTolerance float64
|
||||||
// Feature flag to enable external rps metric collector
|
// Feature flag to enable external rps metric collector
|
||||||
ExternalRPSMetrics bool
|
ExternalRPSMetrics bool
|
||||||
// Name of the Prometheus metric that stores RPS by hostname for external RPS metrics.
|
// Name of the Prometheus metric that stores RPS by hostname for external RPS metrics.
|
||||||
|
|||||||
Reference in New Issue
Block a user