diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1c9469f..373ec75 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,6 +3,7 @@ repos: rev: "v3.0.0-alpha.4" # Use the sha / tag you want to point at hooks: - id: prettier + exclude: "deploy/helm" - repo: https://github.com/hadolint/hadolint rev: "v2.10.0" @@ -13,6 +14,7 @@ repos: rev: "v4.3.0" hooks: - id: check-yaml + exclude: "deploy/helm" - id: check-merge-conflict - id: check-executables-have-shebangs - id: check-shebang-scripts-are-executable diff --git a/deploy/helm/.helmignore b/deploy/helm/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/deploy/helm/.helmignore @@ -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/ diff --git a/deploy/helm/Chart.yaml b/deploy/helm/Chart.yaml new file mode 100644 index 0000000..b0d1c3b --- /dev/null +++ b/deploy/helm/Chart.yaml @@ -0,0 +1,11 @@ +apiVersion: v2 +name: alertmanager-discord +description: A Helm chart to deploy alertmanager-discord to Kubernetes +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 +# This is the version number of the application being deployed. +appVersion: "0.1.0" diff --git a/deploy/helm/templates/NOTES.txt b/deploy/helm/templates/NOTES.txt new file mode 100644 index 0000000..c3fce50 --- /dev/null +++ b/deploy/helm/templates/NOTES.txt @@ -0,0 +1,16 @@ +1. Get the application URL by running these commands: +{{- if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "alertmanager-discord.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "alertmanager-discord.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "alertmanager-discord.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "alertmanager-discord.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/deploy/helm/templates/_helpers.tpl b/deploy/helm/templates/_helpers.tpl new file mode 100644 index 0000000..7150890 --- /dev/null +++ b/deploy/helm/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "alertmanager-discord.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 "alertmanager-discord.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 "alertmanager-discord.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "alertmanager-discord.labels" -}} +helm.sh/chart: {{ include "alertmanager-discord.chart" . }} +{{ include "alertmanager-discord.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "alertmanager-discord.selectorLabels" -}} +app.kubernetes.io/name: {{ include "alertmanager-discord.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "alertmanager-discord.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "alertmanager-discord.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/deploy/helm/templates/ciliumnetworkpolicy.yaml b/deploy/helm/templates/ciliumnetworkpolicy.yaml new file mode 100644 index 0000000..9df5ea4 --- /dev/null +++ b/deploy/helm/templates/ciliumnetworkpolicy.yaml @@ -0,0 +1,24 @@ +{{- if .Values.ciliumNetworkPolicy.enabled -}} +apiVersion: cilium.io/v2 +kind: CiliumNetworkPolicy +metadata: + name: alertmanager-discord +spec: + endpointSelector: + matchLabels: + {{- include "alertmanager-discord.selectorLabels" . | nindent 6 }} + ingress: + - fromEndpoints: + - matchLabels: + {{- .Values.ciliumNetworkPolicy.alertManagerSelectorLabels | toYaml | nindent 12 }} + toPorts: + - ports: + - port: "{{ .Values.service.port }}" + egress: + - toFQDNs: + - matchPattern: "discord.com" + - matchPattern: "discordapp.com" + toPorts: + - ports: + - port: "443" +{{- end -}} diff --git a/deploy/helm/templates/deployment.yaml b/deploy/helm/templates/deployment.yaml new file mode 100644 index 0000000..31f118e --- /dev/null +++ b/deploy/helm/templates/deployment.yaml @@ -0,0 +1,66 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "alertmanager-discord.fullname" . }} + labels: + {{- include "alertmanager-discord.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "alertmanager-discord.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "alertmanager-discord.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "alertmanager-discord.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 9094 + protocol: TCP + livenessProbe: + httpGet: + path: /liveness + port: http + readinessProbe: + httpGet: + path: /readiness + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + env: + - name: DISCORD_WEBHOOK + valueFrom: + secretKeyRef: + name: {{ .Values.server.discordWebhookSecret.name }} + key: {{ .Values.server.discordWebhookSecret.key }} + optional: false + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/deploy/helm/templates/service.yaml b/deploy/helm/templates/service.yaml new file mode 100644 index 0000000..e80e600 --- /dev/null +++ b/deploy/helm/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "alertmanager-discord.fullname" . }} + labels: + {{- include "alertmanager-discord.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "alertmanager-discord.selectorLabels" . | nindent 4 }} diff --git a/deploy/helm/templates/serviceaccount.yaml b/deploy/helm/templates/serviceaccount.yaml new file mode 100644 index 0000000..ac52551 --- /dev/null +++ b/deploy/helm/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "alertmanager-discord.serviceAccountName" . }} + labels: + {{- include "alertmanager-discord.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deploy/helm/templates/tests/test-connection.yaml b/deploy/helm/templates/tests/test-connection.yaml new file mode 100644 index 0000000..f8d7742 --- /dev/null +++ b/deploy/helm/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "alertmanager-discord.fullname" . }}-test-connection" + labels: + {{- include "alertmanager-discord.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "alertmanager-discord.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/deploy/helm/values.yaml b/deploy/helm/values.yaml new file mode 100644 index 0000000..d027a04 --- /dev/null +++ b/deploy/helm/values.yaml @@ -0,0 +1,60 @@ +replicaCount: 1 + +image: + repository: speckle/alertmanager-discord + pullPolicy: Always + # Overrides the image tag whose default is the chart appVersion. + tag: "latest" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: + fsGroup: 2000 + +securityContext: + capabilities: + drop: + - ALL + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 1000 + +service: + type: ClusterIP + port: 9094 + +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 50m + memory: 64Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +server: + discordWebhookSecret: + name: discord-webhook # name of the Kubernetes Secret containing the Discord webhook url. Must be in the same namespace as this helm chart is deployed. + key: "discord-uri" # key within the Opaque Kubernetes Secret + +ciliumNetworkPolicy: + enabled: false + alertManagerSelectorLabels: {} # the labels applied to the alertmanager which will send data to this service. If Cilium Network Policy is enabled, ingress to this service is only allowed from a pod matching these labels. diff --git a/main.go b/main.go index 1d3e4a2..1284444 100644 --- a/main.go +++ b/main.go @@ -176,6 +176,21 @@ func main() { http.ListenAndServe(*listenAddress, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf("%s - [%s] %s", r.Host, r.Method, r.URL.RawPath) + if r.URL.Path == "/readiness" { + log.Print("Readiness probe encountered.") + return + } + + if r.URL.Path == "/liveness" { + log.Print("Liveness probe encountered.") + return + } + + if r.URL.Path == "/favicon.ico" { + log.Print("Favicon request encountered.") + return + } + b, err := ioutil.ReadAll(r.Body) if err != nil { panic(err)