Skip to content

Commit

Permalink
chore: add Helm Chart
Browse files Browse the repository at this point in the history
  • Loading branch information
machine424 committed Jul 15, 2022
1 parent 09f9fa0 commit 93941c7
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ RUN pip install -r requirements.txt

COPY main.py main.py

CMD ["python", "main.py"]
ENTRYPOINT ["python", "main.py"]
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ or

`python main.py --help`

Needs Python `>=3.10`

### Deploy

Docker images are published [here](https://hub.docker.com/r/machine424/kube-hpa-scale-to-zero).

A Helm chart is also available [here](). (Coming soon)
A Helm chart is also available [here](./helm-chart).

`helm upgrade --install RELEASE_NAME helm-chart/ -n RELEASE_NAMESPACE`
23 changes: 23 additions & 0 deletions helm-chart/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
6 changes: 6 additions & 0 deletions helm-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: kube-hpa-scale-to-zero
description: See https://github.com/machine424/kube-hpa-scale-to-zero
type: application
version: 0.2.0
appVersion: "0.2.0"
Empty file added helm-chart/templates/NOTES.txt
Empty file.
62 changes: 62 additions & 0 deletions helm-chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "kube-hpa-scale-to-zero.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "kube-hpa-scale-to-zero.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "kube-hpa-scale-to-zero.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "kube-hpa-scale-to-zero.labels" -}}
helm.sh/chart: {{ include "kube-hpa-scale-to-zero.chart" . }}
{{ include "kube-hpa-scale-to-zero.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "kube-hpa-scale-to-zero.selectorLabels" -}}
app.kubernetes.io/name: {{ include "kube-hpa-scale-to-zero.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "kube-hpa-scale-to-zero.serviceAccountName" -}}
{{- if .Values.rbac.create }}
{{- default (include "kube-hpa-scale-to-zero.fullname" .) .Values.rbac.serviceAccountName }}
{{- else }}
{{- default "default" .Values.rbac.serviceAccountName }}
{{- end }}
{{- end }}
39 changes: 39 additions & 0 deletions helm-chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "kube-hpa-scale-to-zero.fullname" . }}
labels:
{{- include "kube-hpa-scale-to-zero.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "kube-hpa-scale-to-zero.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "kube-hpa-scale-to-zero.selectorLabels" . | nindent 8 }}
spec:
serviceAccountName: {{ include "kube-hpa-scale-to-zero.serviceAccountName" . }}
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- with .Values.args }}
args:
{{- toYaml . | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
46 changes: 46 additions & 0 deletions helm-chart/templates/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{- if .Values.rbac.create -}}

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "kube-hpa-scale-to-zero.serviceAccountName" . }}
labels:
{{- include "kube-hpa-scale-to-zero.labels" . | nindent 4 }}
rules:
- apiGroups: ["apps"]
resources: ["deployments/scale"]
verbs: ["get", "patch"]
- apiGroups: ["autoscaling"]
resources: ["horizontalpodautoscalers"]
verbs: ["get", "list", "watch"]
- apiGroups: ["custom.metrics.k8s.io"]
resources: ["*"]
verbs: ["get"]


---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "kube-hpa-scale-to-zero.serviceAccountName" . }}
labels:
{{- include "kube-hpa-scale-to-zero.labels" . | nindent 4 }}
subjects:
- kind: ServiceAccount
name: {{ include "kube-hpa-scale-to-zero.serviceAccountName" . }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "kube-hpa-scale-to-zero.serviceAccountName" . }}

---

apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "kube-hpa-scale-to-zero.serviceAccountName" . }}
labels:
{{- include "kube-hpa-scale-to-zero.labels" . | nindent 4 }}

{{- end }}
24 changes: 24 additions & 0 deletions helm-chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
replicaCount: 1

image:
repository: machine424/kube-hpa-scale-to-zero
pullPolicy: IfNotPresent
tag: "0.2.0"

nameOverride: ""
fullnameOverride: ""

# args: ["--hpa-namespace", "foo"]
args: []

rbac:
create: true
serviceAccountName: ""

podAnnotations: {}

resources: {}

tolerations: []

affinity: {}
48 changes: 26 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@
)
LOGGER = logging.getLogger(__name__)

parser = argparse.ArgumentParser(
description="kube-hpa-scale-to-zero. Check https://github.com/machine424/kube-hpa-scale-to-zero"
)
parser.add_argument(
"--hpa-label-selector",
dest="hpa_label_selector",
default="",
help="label_selector to get HPA to watch, 'foo=bar,bar=foo' e.g. (default: empty string to select all)",
)
parser.add_argument(
"--hpa-namespace",
dest="hpa_namespace",
default="default",
help="namespace where the HPA live. (default: 'default' namespace)",
)
ARGS = parser.parse_args()


def load_kubernetes_config() -> None:
try:
Expand Down Expand Up @@ -73,17 +56,17 @@ def _watch():
threading.Thread(target=_watch, daemon=True).start()


def watch_hpa() -> None:
def watch_hpa(args) -> None:
LOGGER.info(
f"Will watch HPA with label selector '{ARGS.hpa_label_selector}' in {ARGS.hpa_namespace}."
f"Will watch HPA with label selector '{args.hpa_label_selector}' in {args.hpa_namespace}."
)
while True:
try:
w = watch.Watch()
for event in w.stream(
AUTOSCALING_V1.list_namespaced_horizontal_pod_autoscaler,
ARGS.hpa_namespace,
label_selector=ARGS.hpa_label_selector,
args.hpa_namespace,
label_selector=args.hpa_label_selector,
):
update_hpa(event["object"].metadata)
except kubernetes.client.exceptions.ApiException as exc:
Expand Down Expand Up @@ -207,6 +190,27 @@ def update_deployment(*, namespace, name, needed_replicas) -> None:
raise exc


def parse_cli_args():
parser = argparse.ArgumentParser(
description="kube-hpa-scale-to-zero. Check https://github.com/machine424/kube-hpa-scale-to-zero"
)
parser.add_argument(
"--hpa-namespace",
dest="hpa_namespace",
default="default",
help="namespace where the HPA live. (default: 'default' namespace)",
)
parser.add_argument(
"--hpa-label-selector",
dest="hpa_label_selector",
default="",
help="label_selector to get HPA to watch, 'foo=bar,bar=foo' e.g. (default: empty string to select all)",
)

return parser.parse_args()


if __name__ == "__main__":
cli_args = parse_cli_args()
watch_metrics()
watch_hpa()
watch_hpa(cli_args)

0 comments on commit 93941c7

Please sign in to comment.