diff --git a/.github/workflows/helm-release.yaml b/.github/workflows/helm-release.yaml new file mode 100644 index 00000000..0c65ed53 --- /dev/null +++ b/.github/workflows/helm-release.yaml @@ -0,0 +1,95 @@ +name: Package Helm Chart and publish to GitHub Packages + +on: + workflow_dispatch: {} + push: + branches: + - main + paths: + - .github/workflows/helm-release.yaml + - charts/heureka + + +permissions: + contents: write + packages: write + +env: + REGISTRY: ghcr.io + ACTIONS_RUNNER_DEBUG: false + +jobs: + helm-release: + runs-on: [ default ] + strategy: + fail-fast: false + matrix: + include: + - chartDir: charts/heureka + chartName: heureka + + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Configure Git + run: | + git config user.name "$GITHUB_ACTOR" + git config user.email "$GITHUB_ACTOR@users.noreply.github.com" + + - name: Set up Helm + uses: azure/setup-helm@v4.2.0 + + - uses: actions/setup-python@v5 + with: + python-version: 3.9 + check-latest: true + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v44 + with: + files: | + ${{ matrix.chartDir }}/** + + - name: Check if Helm chart with same version already exists + id: check-chart + if: steps.changed-files.outputs.all_changed_files != '' + env: + ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + run: | + # List all changed + echo "All changed files: $ALL_CHANGED_FILES" + chartName=$(yq .name "${{ matrix.chartDir }}/Chart.yaml") + chartVersion=$(yq .version "${{ matrix.chartDir }}/Chart.yaml") + echo "chart_version=${chartVersion}" >> "$GITHUB_OUTPUT" + if $(helm pull "oci://${{ env.REGISTRY }}/${{ github.repository }}/charts/${{ matrix.chartName }}" --version $chartVersion); then + echo "bump=true" >> "$GITHUB_OUTPUT" + fi + + - name: Chart needs version bump + if: steps.check-chart.outputs.bump == 'true' + env: + CHART_VERSION: ${{ steps.check-chart.outputs.chart_version }} + run: | + echo "Chart ${{ matrix.chartDir }}:${{ env.CHART_VERSION }} already exists in OCI registry. Skipping upload. Please increment the chart version." + exit 1 + + - name: Push Charts to GHCR + if: steps.changed-files.outputs.all_changed_files != '' && steps.check-chart.outputs.bump != 'true' + run: | + helm package ${{ matrix.chartDir }} -d ${{ matrix.chartDir }} + PKG_NAME=`ls ${{ matrix.chartDir }}/*.tgz` + helm push ${PKG_NAME} oci://${{ env.REGISTRY }}/${{ github.repository }}/charts/ diff --git a/.gitignore b/.gitignore index f5070af1..233077af 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ # Heureka specifics build/ .db/ -.env \ No newline at end of file +.env +*.tgz \ No newline at end of file diff --git a/charts/heureka/.helmignore b/charts/heureka/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/charts/heureka/.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/charts/heureka/Chart.lock b/charts/heureka/Chart.lock new file mode 100644 index 00000000..63d0e5a0 --- /dev/null +++ b/charts/heureka/Chart.lock @@ -0,0 +1,6 @@ +dependencies: +- name: mariadb + repository: https://charts.bitnami.com/bitnami + version: 19.0.3 +digest: sha256:7ee2a1a9bea682f5bef8d7847ddd962bc67f76721ed3911f39aef82d1a9f67c4 +generated: "2024-08-14T14:08:20.758927+02:00" diff --git a/charts/heureka/Chart.yaml b/charts/heureka/Chart.yaml new file mode 100644 index 00000000..47a0bca8 --- /dev/null +++ b/charts/heureka/Chart.yaml @@ -0,0 +1,29 @@ +apiVersion: v2 +name: heureka +description: A Helm chart for heureka + +# 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.3.0 + +# 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: "1.16.0" + +dependencies: + - name: mariadb + repository: https://charts.bitnami.com/bitnami + version: 19.0.3 \ No newline at end of file diff --git a/charts/heureka/files/schema.sql b/charts/heureka/files/schema.sql new file mode 120000 index 00000000..b869a97f --- /dev/null +++ b/charts/heureka/files/schema.sql @@ -0,0 +1 @@ +../../../internal/database/mariadb/init/schema.sql \ No newline at end of file diff --git a/charts/heureka/templates/NOTES.txt b/charts/heureka/templates/NOTES.txt new file mode 100644 index 00000000..a49629a0 --- /dev/null +++ b/charts/heureka/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "heureka.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 "heureka.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "heureka.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 "heureka.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/charts/heureka/templates/_helpers.tpl b/charts/heureka/templates/_helpers.tpl new file mode 100644 index 00000000..c8d34f9b --- /dev/null +++ b/charts/heureka/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "heureka.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 "heureka.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 "heureka.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "heureka.labels" -}} +helm.sh/chart: {{ include "heureka.chart" . }} +{{ include "heureka.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "heureka.selectorLabels" -}} +app.kubernetes.io/name: {{ include "heureka.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "heureka.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "heureka.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/charts/heureka/templates/database-schema-configmap.yaml b/charts/heureka/templates/database-schema-configmap.yaml new file mode 100644 index 00000000..55ebb83a --- /dev/null +++ b/charts/heureka/templates/database-schema-configmap.yaml @@ -0,0 +1,13 @@ +{{ if .Files.Get "files/schema.sql" }} +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "heureka.fullname" . }}-mariadb-schema + labels: + {{- include "heureka.labels" . | nindent 4 }} +data: + schema.sql: |- +{{ .Files.Get "files/schema.sql" | indent 6 }} +{{ else }} +{{ fail "Error: schema.sql file not found in files/ directory" }} +{{ end }} \ No newline at end of file diff --git a/charts/heureka/templates/deployment.yaml b/charts/heureka/templates/deployment.yaml new file mode 100644 index 00000000..dc0398b1 --- /dev/null +++ b/charts/heureka/templates/deployment.yaml @@ -0,0 +1,86 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "heureka.fullname" . }} + labels: + {{- include "heureka.labels" . | nindent 4 }} + app.kubernetes.io/managed-by: Helm + annotations: + prometheus.io/scrape: "true" + prometheus.io/targets: "kubernetes" +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "heureka.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "heureka.selectorLabels" . | nindent 8 }} + annotations: + prometheus.io/scrape: "true" + prometheus.io/targets: kubernetes + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "heureka.serviceAccountName" . }} + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.serverTag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + volumeMounts: + - name: mariadb-schema-init-volume + mountPath: /app/schema-init/schema.sql + subPath: schema.sql + env: + - name: ENVIRONMENT + valueFrom: + secretKeyRef: + name: {{ include "heureka.fullname" . }} + key: environment + - name: SERVER_PORT + value: ":{{ .Values.service.port }}" + - name: SEED_MODE + value: "{{ .Values.seedMode }}" + - name: DB_NAME + value: "heureka" + - name: DB_PORT + value: "3306" + - name: DB_SCHEMA + value: "/app/schema-init/schema.sql" + - name: DB_USER + value: "my_username" + - name: DB_PASSWORD + valueFrom: + secretKeyRef: + name: {{ include "heureka.fullname" . }}-mariadb + key: mariadb-password + - name: DB_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: {{ include "heureka.fullname" . }}-mariadb + key: mariadb-root-password + - name: DB_ADDRESS + value: "heureka-mariadb.heureka.svc.cluster.local" + ports: + - name: http + containerPort: {{ .Values.service.port }} + protocol: TCP + - name: metrics + containerPort: {{ .Values.service.port }} + livenessProbe: + httpGet: + path: /status + port: http + readinessProbe: + httpGet: + path: /ready + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumes: + - name: mariadb-schema-init-volume + configMap: + name: {{ include "heureka.fullname" . }}-mariadb-schema diff --git a/charts/heureka/templates/ingress.yaml b/charts/heureka/templates/ingress.yaml new file mode 100644 index 00000000..3085c49c --- /dev/null +++ b/charts/heureka/templates/ingress.yaml @@ -0,0 +1,44 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "heureka.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "heureka.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- if and $.Values.global.linkerd_enabled $.Values.global.linkerd_requested }} + ingress.kubernetes.io/service-upstream: "true" + nginx.ingress.kubernetes.io/service-upstream: "true" + {{- end }} + {{- end }} +spec: +{{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} +{{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + pathType: Prefix + backend: + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- end }} + {{- end }} +{{- end }} diff --git a/charts/heureka/templates/secret.yaml b/charts/heureka/templates/secret.yaml new file mode 100644 index 00000000..ea339fab --- /dev/null +++ b/charts/heureka/templates/secret.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "heureka.fullname" . }} + labels: + {{- include "heureka.labels" . | nindent 4 }} +data: + environment: {{ .Values.environment | b64enc | quote }} + mariadb-password: {{ .Values.mariadb.auth.password | b64enc }} + mariadb-root-password: {{ .Values.mariadb.auth.rootPassword | b64enc }} \ No newline at end of file diff --git a/charts/heureka/templates/service.yaml b/charts/heureka/templates/service.yaml new file mode 100644 index 00000000..7525cf38 --- /dev/null +++ b/charts/heureka/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "heureka.fullname" . }} + labels: + {{- include "heureka.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "heureka.selectorLabels" . | nindent 4 }} diff --git a/charts/heureka/templates/serviceaccount.yaml b/charts/heureka/templates/serviceaccount.yaml new file mode 100644 index 00000000..72f66dee --- /dev/null +++ b/charts/heureka/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "heureka.serviceAccountName" . }} + labels: + {{- include "heureka.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/charts/heureka/values.yaml b/charts/heureka/values.yaml new file mode 100644 index 00000000..ddef27a5 --- /dev/null +++ b/charts/heureka/values.yaml @@ -0,0 +1,82 @@ +# Default values for heureka. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +serverTag: main + +image: + repository: ghcr.io/cloudoperators/heureka + pullPolicy: Always + +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: 8080 + +ingress: + enabled: false + +environment: staging + +seedMode: true + +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 + +# autoscaling: +# enabled: false +# minReplicas: 1 +# maxReplicas: 100 +# targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +mariadb: + global: + imageRegistry: docker.io + auth: + database: heureka + username: my_username + rootPassword: my_password + password: my_password