Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Helm chart #18

Merged
merged 11 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added a Helm chart

### Fixed
- Fixed container image being deployed to `ghcr.io/gravitational/gha-exporter/gha-exporter` instead of `ghcr.io/gravitational/gha-exporter`

## [0.0.3] - 2024-03-07
### Fixed
Expand Down
31 changes: 27 additions & 4 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ container-image:
# Build args
ARG TARGETARCH
ARG NATIVEARCH
ARG CONTAINER_REGISTRY
ARG OCI_REGISTRY

# Setup for build
# `IF` statements essentially run as shell `if` statements, so a build context must be declared
Expand All @@ -115,7 +115,7 @@ container-image:
ENTRYPOINT [ "/gha-exporter" ]

# Process the outputs
SAVE IMAGE --push "$CONTAINER_REGISTRY$IMAGE_NAME:$IMAGE_TAG"
SAVE IMAGE --push "$OCI_REGISTRY$IMAGE_NAME:$IMAGE_TAG"

# Same as `binary`, but wraps the output in a tarball.
tarball:
Expand All @@ -128,10 +128,30 @@ tarball:
RUN tar -czvf "$TARBALL_NAME" *
SAVE ARTIFACT $TARBALL_NAME AS LOCAL "outputs/$GOOS/$GOARCH/$TARBALL_NAME"

helm:
ARG OCI_REGISTRY

FROM alpine/helm:3.14.2
WORKDIR /helm
COPY ./helm .

IF [ -n "$GIT_TAG" ]
LET VERSION_FLAGS="--version ${GIT_TAG#v} --app-version ${GIT_TAG#v}"
END

RUN helm package . $VERSION_FLAGS
LET ARTIFACT_NAME=$(find . -name '*.tgz' -exec basename {} \\; | head -n 1)

SAVE ARTIFACT $ARTIFACT_NAME AS LOCAL "outputs/helm/$ARTIFACT_NAME"
RUN --push --secret GH_TOKEN \
echo "$GH_TOKEN" | helm registry login "$OCI_REGISTRY" --username gravitational --password-stdin && \
helm push "$ARTIFACT_NAME" "oci://$OCI_REGISTRY/charts"

all:
BUILD +binary
BUILD +tarball
BUILD +container-image
BUILD +helm

# Runs the project's Go tests.
test:
Expand Down Expand Up @@ -255,7 +275,7 @@ create-release-pr:
# Cuts a new GH release and pushes file assets to it. Also pushes container images.
release:
ARG --required GIT_TAG # This global var is redeclared here to ensure that it is set via `--required`
ARG CONTAINER_REGISTRY="ghcr.io/$REPO_NAME/"
ARG OCI_REGISTRY="ghcr.io/gravitational/"
ARG EARTHLY_PUSH
ARG NATIVEARCH

Expand Down Expand Up @@ -291,4 +311,7 @@ release:
./*

# Build container images and push them
BUILD --platform=linux/amd64 --platform=linux/arm64 +container-image --CONTAINER_REGISTRY="$CONTAINER_REGISTRY"
BUILD --platform=linux/amd64 --platform=linux/arm64 --pass-args +container-image

# Build the helm chart and push it
BUILD --pass-args +helm
22 changes: 22 additions & 0 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
apiVersion: v2
name: gha-exporter
description: GitHub Actions metrics exporter for Prometheus
# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
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.0.0-dev
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.0.0-dev"
65 changes: 65 additions & 0 deletions helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "gha-exporter.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 "gha-exporter.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 "gha-exporter.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

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

{{/*
Selector labels
*/}}
{{- define "gha-exporter.selectorLabels" -}}
app.kubernetes.io/name: {{ include "gha-exporter.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Determine whether a new secret should be deployed
*/}}
{{- define "gha-exporter.secret.enabled" -}}
{{ ne .Values.exporter.githubApp.privateKey "" }}
{{- end }}

{{/*
Determine secret name
*/}}
{{- define "gha-exporter.secret.name" -}}
{{- default (include "gha-exporter.fullname" .) .Values.exporter.githubApp.privateKeySecretName -}}
{{- end }}
85 changes: 85 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "gha-exporter.fullname" . }}
labels:
{{- include "gha-exporter.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
{{- include "gha-exporter.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "gha-exporter.labels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
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 }}
args:
- --app-id
- {{ required "Missing .Values.exporter.githubApp.id" .Values.exporter.githubApp.id | quote }}
- --owner
- {{ required "Missing .Values.exporter.repoOwner" .Values.exporter.repoOwner | quote}}
- --repos
- {{ join "," (required "Missing .Values.exporter.repos" .Values.exporter.repos) | quote}}
{{- if .Values.exporter.pollingTime }}
- --sleep
- {{ .Values.exporter.pollingTime | quote }}
{{- end }}
{{- if .Values.exporter.backfill.enabled }}
- --backfill
- "true"
{{- if .Values.exporter.backfill.initialWindow }}
- --initial-window
- {{ .Values.exporter.backfill.initialWindow | quote }}
{{- end }}
{{- end }}
envFrom:
- secretRef:
name: {{ include "gha-exporter.secret.name" . }}
optional: false
ports:
- name: metrics
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /metrics
port: metrics
readinessProbe:
httpGet:
path: /metrics
port: metrics
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
17 changes: 17 additions & 0 deletions helm/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{- if eq (include "gha-exporter.secret.enabled" .) "true" }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ include "gha-exporter.secret.name" . }}
labels:
{{- include "gha-exporter.labels" . | nindent 4 }}
type: Opaque
stringData:
GHA_APP_KEY: {{ .Values.exporter.githubApp.privateKey }}
{{- else }}
{{- $_ := required
".Values.exporter.githubApp.privateKey or .Values.exporter.githubApp.privateKeySecretName must be set"
.Values.exporter.githubApp.privateKeySecretName
-}}
{{- end }}
16 changes: 16 additions & 0 deletions helm/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: v1
kind: Service
metadata:
name: {{ include "gha-exporter.fullname" . }}
labels:
{{- include "gha-exporter.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: metrics
protocol: TCP
name: metrics
selector:
{{- include "gha-exporter.selectorLabels" . | nindent 4 }}
23 changes: 23 additions & 0 deletions helm/templates/servicemonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{- if .Values.serviceMonitor.enabled }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ include "gha-exporter.fullname" . }}
labels:
{{- include "gha-exporter.labels" . | nindent 4 }}
spec:
jobLabel: app.kubernetes.io/name
selector:
matchLabels:
{{- include "gha-exporter.selectorLabels" . | nindent 6 }}
namespaceSelector:
matchNames:
- {{ .Release.Namespace | quote }}
endpoints:
- honorLabels: true
path: /metrics
port: metrics
scheme: http
scrapeTimeout: 30s
{{- end }}
70 changes: 70 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# Default values for gha-exporter.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

image:
repository: ghcr.io/gravitational/gha-exporter/gha-exporter
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: ""

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

exporter:
githubApp:
# id: 1234
privateKey: ""
privateKeySecretName: ""
# repoOwner: "gravitational"
# repos:
# - teleport
# - teleport.e
# pollingTime: 1m
backfill:
enabled: false
# initialWindow: 2h

podAnnotations: {}
podLabels: {}

podSecurityContext:
{}
# fsGroup: 2000

securityContext:
{}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000

service:
type: ClusterIP
port: 10013 # Next available port from https://github.com/prometheus/prometheus/wiki/Default-port-allocations

serviceMonitor:
enabled: false

resources:
{}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}
Loading