Skip to content

Commit

Permalink
Merge pull request #239 from camptocamp/autoscaling
Browse files Browse the repository at this point in the history
Add HPA
  • Loading branch information
jemacchi authored Sep 30, 2024
2 parents d358c55 + bb203f1 commit 6d1b269
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/spell-ignore-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ secretKey
1h
1m
1s
NotIn
HPA
cpu
Resource
14 changes: 8 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/sbrunner/jsonschema2md
rev: 1.2.3
rev: 1.3.0
hooks:
- id: jsonschema2md
files: values.schema.json
Expand All @@ -25,7 +25,7 @@ repos:
- id: trailing-whitespace
- id: mixed-line-ending
- repo: https://github.com/sbrunner/hooks
rev: 1.0.0
rev: 1.1.0
hooks:
- id: copyright
- id: workflows-require-timeout
Expand All @@ -47,11 +47,13 @@ repos:
- .
- tests/expected.yaml
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
rev: v2.3.0
hooks:
- id: codespell
args:
- --ignore-words=.github/spell-ignore-words.txt
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
rev: v4.0.0-alpha.8
hooks:
- id: prettier
additional_dependencies:
Expand All @@ -68,7 +70,7 @@ repos:
hooks:
- id: git-check
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.6
rev: 0.29.2
hooks:
- id: check-github-actions
- id: check-github-workflows
Expand Down Expand Up @@ -105,6 +107,6 @@ repos:
- --fields=description,title
- --spelling=.github/spell-ignore-words.txt
- repo: https://github.com/renovatebot/pre-commit-hooks
rev: 37.428.1
rev: 38.103.1
hooks:
- id: renovate-config-validator
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2022-2023, Camptocamp
Copyright (c) 2022-2024, Camptocamp
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
27 changes: 27 additions & 0 deletions templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{- range $serviceName, $serviceDefinition := .Values.services }}
{{- if and (eq $serviceDefinition.enabled true) (hasKey $serviceDefinition "hpa") (eq $serviceDefinition.hpa.enabled true) }}
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
{{- if hasKey $serviceDefinition.hpa "name" }}
name: {{ $serviceDefinition.hpa.name }}
{{- else }}
name: {{ include "common.fullname" (dict "root" $ "service" $serviceDefinition "serviceName" $serviceName ) }}-hpa
{{- end }}
{{- $hpaDict := deepCopy $serviceDefinition }}
{{- $hpaDict := merge $hpaDict (dict "annotations" $serviceDefinition.hpa.annotations "labels" $serviceDefinition.hpa.labels) }}
{{- include "common.metadata" (dict "root" $ "service" $hpaDict "serviceName" $serviceName) | nindent 2 }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "common.fullname" (dict "root" $ "service" $serviceDefinition "serviceName" $serviceName ) }}
minReplicas: {{ $serviceDefinition.hpa.minReplicas | default 1 }}
maxReplicas: {{ $serviceDefinition.hpa.maxReplicas | default 10 }}
metrics:
{{- with $serviceDefinition.hpa.metrics }}
{{- . | toYaml | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
146 changes: 146 additions & 0 deletions tests/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ spec:
# Source: application/templates/pdb.yaml
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: example-application-example5
labels:
helm.sh/chart: application
app.kubernetes.io/version: "1.0"
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
spec:
maxUnavailable: 1
selector:
matchLabels:
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
---
# Source: application/templates/pdb.yaml
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: example-application-internal
labels:
Expand Down Expand Up @@ -229,6 +249,8 @@ data:
SERVICE_EXAMPLE3_CONTAINER_DD_IMAGE_TAG: "latest"
SERVICE_EXAMPLE4_NAME: example-application-example4
SERVICE_EXAMPLE4_CONTAINER_MAIN_IMAGE_TAG: "latest"
SERVICE_EXAMPLE5_NAME: service-autoscaled
SERVICE_EXAMPLE5_CONTAINER_MAIN_IMAGE_TAG: "latest"
SERVICE_INTERNAL_NAME: example-application-internal
SERVICE_INTERNAL_CONTAINER_MAIN_IMAGE_TAG: "latest"
SERVICE_JOB-ALL_NAME: example-application-job-all
Expand Down Expand Up @@ -344,6 +366,28 @@ spec:
app.kubernetes.io/instance: example
app.kubernetes.io/component: example3
---
# Source: application/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: service-autoscaled
labels:
helm.sh/chart: application
app.kubernetes.io/version: "1.0"
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
selector:
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
---
# Source: application/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -979,6 +1023,58 @@ spec:
# Source: application/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-application-example5
labels:
helm.sh/chart: application
app.kubernetes.io/version: "1.0"
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
spec:
revisionHistoryLimit: 3
strategy:
type: RollingUpdate
selector:
matchLabels:
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
template:
metadata:
labels:
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
spec:
imagePullSecrets:
- name: example-application-docker-registry
serviceAccountName: example-application-myserviceaccount
securityContext:
{}
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: k1
operator: In
values:
- "v1"
topologyKey: "kubernetes.io/hostname"
containers:
- name: main
securityContext:
runAsNonRoot: true
runAsUser: 33
image: "camptocamp/image:latest"
imagePullPolicy: IfNotPresent
terminationMessagePolicy: FallbackToLogsOnError
---
# Source: application/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-application-internal
labels:
Expand Down Expand Up @@ -1045,6 +1141,33 @@ spec:
key: "hostname"
terminationMessagePolicy: FallbackToLogsOnError
---
# Source: application/templates/hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: example-application-example5-hpa
labels:
helm.sh/chart: application
app.kubernetes.io/version: "1.0"
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: example-application-example5
minReplicas: 2
maxReplicas: 5
metrics:
- resource:
name: cpu
target:
averageUtilization: 50
type: Utilization
type: Resource
---
# Source: application/templates/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
Expand Down Expand Up @@ -1581,3 +1704,26 @@ spec:
- port: prometheus
honorLabels: true
interval: 10s
---
# Source: application/templates/podmonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: example-application-example5
labels:
helm.sh/chart: application
app.kubernetes.io/version: "1.0"
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
spec:
selector:
matchLabels:
app.kubernetes.io/name: application
app.kubernetes.io/instance: example
app.kubernetes.io/component: example5
podMetricsEndpoints:
- port: prometheus
honorLabels: true
interval: 10s
37 changes: 37 additions & 0 deletions tests/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,43 @@ services:
interval: 10s
honorLabels: true

# Example with HPA and labelSelector in affinity
example5:
enabled: true
affinity:
podAntiAffinity:
labelSelector:
k1: v1
topologyKey: kubernetes.io/hostname
containers:
main:
image:
repository: camptocamp/image
tag: latest
podMonitor:
enabled: true
podMetricsEndpoints:
prometheus:
interval: 10s
honorLabels: true
service:
name: service-autoscaled
type: ClusterIP
ports:
- port: 80
targetPort: 80
hpa:
enabled: true
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50

# Minimal configuration of a deployment
deployment-min:
enabled: true
Expand Down
12 changes: 12 additions & 0 deletions values.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@
- **`labels`**: Refer to _[#/definitions/labels](#definitions/labels)_.
- **`annotations`**: Refer to _[#/definitions/annotations](#definitions/annotations)_.
- **`ports`** _(array)_
- **`hpa`** _(object)_
- **`enabled`** _(boolean, required)_: Enable or disable HPA for the service.
- **`minReplicas`** _(integer, required)_: Minimal number of replicas. Minimum: `1`.
- **`maxReplicas`** _(integer, required)_: Max number of replicas. Minimum: `1`.
- **`metrics`** _(array)_: Metrics definition.
- **Items** _(object)_
- **`type`** _(string, required)_: Metric types (Resource, Pods, External). Must be one of: `["Resource", "Pods", "External"]`.
- **`resource`** _(object)_
- **`name`** _(string, required)_: CPU or memory value. Must be one of: `["cpu", "memory"]`.
- **`target`** _(object, required)_
- **`type`** _(string, required)_: Metric target. Must be one of: `["Utilization", "Value", "AverageValue"]`.
- **`averageUtilization`** _(integer)_: % average use.
- **`podMonitor`** _(object)_: The Prometheus Pod monitor configuration. Cannot contain additional properties.
- **`enabled`** _(boolean)_: Enable the Pod monitor for this service (Pod).
- **`name`** _(string)_: The name of the Pod monitor.
Expand Down
Loading

0 comments on commit 6d1b269

Please sign in to comment.