Skip to content

Commit

Permalink
Merge pull request #8 from evgkrsk/add-promrule
Browse files Browse the repository at this point in the history
Add PrometheusRule support and tests
  • Loading branch information
evgkrsk authored Jun 13, 2022
2 parents 063781c + af6c699 commit 70d651f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 18 deletions.
3 changes: 3 additions & 0 deletions charts/universal-chart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## 3.3.0 - Jun 13, 2022
* feat: add PrometheusRule support and tests

## 3.2.0 - May 2, 2022

* feat: add HPA support and tests
Expand Down
2 changes: 1 addition & 1 deletion charts/universal-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
description: Nixys universal Helm chart for deploy your apps to Kubernetes
name: universal-chart
version: 3.2.0
version: 3.3.0
maintainers:
- name: Roman Andreev
email: [email protected]
Expand Down
57 changes: 41 additions & 16 deletions charts/universal-chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,25 +424,50 @@ Secret `data` object is a map where value can be a string, json or base64 encode

`hpas` is map of HPA parameters, where key is a name

| Name | Description | Value |
|------------------|-------------------------------------------------------------------------|-------------------------|
| `labels` | Extra HPA labels | `{}` |
| `annotations` | Extra HPA annotations | `{}` |
| `apiVersion` | apiVersion for HPA object | `"autoscaling/v2beta1"` |
| `minReplicas` | minimum replicas for HPA | `2` |
| `maxReplicas` | maximum replicas for HPA | `3` |
| `scaleTargetRef` | Required [scaleTargetRef](#hpa-scaletargetref-object-parameters) object | |
| `targetCPU` | target CPU utilization percentage | `""` |
| `targetMemory` | target memory utilization percentage | `""` |
| `metrics` | list of custom metrics | `[]` |
| Name | Description | Value |
|------------------|---------------------------------------------------------------------------|-------------------------|
| `labels` | Extra HPA labels | `{}` |
| `annotations` | Extra HPA annotations | `{}` |
| `apiVersion` | apiVersion for HPA object | `"autoscaling/v2beta1"` |
| `minReplicas` | minimum replicas for HPA | `2` |
| `maxReplicas` | maximum replicas for HPA | `3` |
| `scaleTargetRef` | (REQUIRED) [scaleTargetRef](#hpa-scaletargetref-object-parameters) object | |
| `targetCPU` | target CPU utilization percentage | `""` |
| `targetMemory` | target memory utilization percentage | `""` |
| `metrics` | list of custom metrics | `[]` |

### HPA `scaleTargetRef` object parameters

| Name | Description | Value |
|------------|----------------------------------|--------------|
| apiVersion | apiVersion for target HPA object | "apps/v1" |
| kind | kind for target HPA object | "Deployment" |
| name | Required name of target object | "" |
| Name | Description | Value |
|--------------|----------------------------------|--------------|
| `apiVersion` | apiVersion for target HPA object | "apps/v1" |
| `kind` | kind for target HPA object | "Deployment" |
| `name` | (REQUIRED) name of target object | "" |

### PrometheusRule parameters

`prometheusrules` is map of PrometheusRule-s, where key is a name

| Name | Description | Value |
|----------|-----------------------------------------------------------------------------------------------------|-------|
| `labels` | Extra PrometheusRule labels | `{}` |
| `groups` | (REQUIRED) map of [PrometheusRuleGroup](#prometheusrulegroup-object-parameters) where key is a name | `{}` |

### PrometheusRuleGroup object parameters

| Name | Description | Value |
|------------|--------------------------------------------------------------|-------|
| `interval` | period check for alerts | `5m` |
| `rules` | map of [Alert](#alert-object-parameters) where key is a name | `{}` |

### Alert object parameters

| Name | Description | Value |
|---------------|------------------------------|-------|
| `expr` | (REQUIRED) PromQL expression | `""` |
| `for` | Duration for alert | `5m` |
| `labels` | Map of rule labels | `{}` |
| `annotations` | Map of rule annotations | `{}` |

## Configuration and installation details

Expand Down
25 changes: 25 additions & 0 deletions charts/universal-chart/results/prometheusrules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# Source: universal-chart/templates/prometheusrule.yml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: test-promrule
namespace: "default"
labels:
app.kubernetes.io/name: test
app.kubernetes.io/instance: test
app.kubernetes.io/managed-by: Helm
foo: bar
spec:
groups:
- name: "job_rules"
interval: 1m
rules:
- alert: "RedisDown"
expr: redis_up == 0
for: 2m
labels:
severity: critical
annotations:
description: Redis instance "{{ $labels.instance }}" is down.
summary: Redis instance "{{ $labels.instance }}" down
16 changes: 16 additions & 0 deletions charts/universal-chart/samples/prometheusrules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
prometheusrules:
promrule:
labels:
foo: bar
groups:
job_rules:
interval: 1m
rules:
RedisDown:
expr: redis_up == 0
for: 2m
labels:
severity: critical
annotations:
summary: Redis instance "{{ $labels.instance }}" down
description: Redis instance "{{ $labels.instance }}" is down.
31 changes: 31 additions & 0 deletions charts/universal-chart/templates/prometheusrule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{{- range $name, $rule := .Values.prometheusrules }}
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $name "context" $) }}
namespace: {{ $.Release.Namespace | quote }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
{{- with .labels }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
spec:
groups:
{{- range $gname, $group := $rule.groups }}
- name: {{ $gname | quote }}
interval: {{ .interval }}
rules:
{{- range $aname, $alert := .rules }}
- alert: {{ $aname | quote }}
expr: {{ .expr }}
for: {{ .for }}
labels:
{{- range $key, $value := .labels }}
{{ $key }}: {{ $value }}
{{- end }}
annotations:
{{- range $key, $value := .annotations }}
{{ $key }}: {{ $value }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion charts/universal-chart/testit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ for filename in "$dirname"/samples/*
do
echo "Testing $filename"
basename=$(basename "$filename")
helm template test "$dirname" --values "$filename" |grep -Ev '^ +helm.sh/chart:' > "$tests"/"$basename"
helm --kube-context="notexisting" template test "$dirname" --values "$filename" |grep -Ev '^ +helm.sh/chart:' > "$tests"/"$basename"
diff -u "$dirname"/results/"$basename" "$tests"/"$basename"
echo "OK"
done
17 changes: 17 additions & 0 deletions charts/universal-chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,23 @@ servicemonitors: {}
# labels:
# foo: foo

prometheusrules: {}
# promrule:
# labels:
# foo: bar
# groups:
# job_rules:
# interval: 1m
# rules:
# RedisDown:
# expr: redis_up == 0
# for: 2m
# labels:
# severity: critical
# annotations:
# summary: Redis instance "{{ $labels.instance }}" down
# description: Redis instance "{{ $labels.instance }}" is down.

diagnosticMode:
enabled: false
command: ["sleep"]
Expand Down

0 comments on commit 70d651f

Please sign in to comment.