From 926b34c39e2e4930ebfe860a3a362326b6f0d24b Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Wed, 31 Jul 2024 16:50:03 -0600 Subject: [PATCH] feat: Helm Chart (#63) Signed-off-by: Alfredo Gutierrez --- .github/workflows/helm-charts.yaml | 65 ++++++++++++++ charts/.gitignore | 2 + charts/hedera-block-node/.helmignore | 23 +++++ charts/hedera-block-node/Chart.yaml | 16 ++++ charts/hedera-block-node/README.md | 75 +++++++++++++++++ charts/hedera-block-node/templates/NOTES.txt | 22 +++++ .../hedera-block-node/templates/_helpers.tpl | 80 ++++++++++++++++++ .../templates/configmap.yaml | 9 ++ .../templates/deployment.yaml | 65 ++++++++++++++ .../hedera-block-node/templates/ingress.yaml | 61 ++++++++++++++ .../hedera-block-node/templates/secret.yaml | 9 ++ .../hedera-block-node/templates/service.yaml | 15 ++++ .../templates/serviceaccount.yaml | 12 +++ charts/hedera-block-node/values.yaml | 84 +++++++++++++++++++ ct.yaml | 3 + 15 files changed, 541 insertions(+) create mode 100644 .github/workflows/helm-charts.yaml create mode 100644 charts/.gitignore create mode 100644 charts/hedera-block-node/.helmignore create mode 100644 charts/hedera-block-node/Chart.yaml create mode 100644 charts/hedera-block-node/README.md create mode 100644 charts/hedera-block-node/templates/NOTES.txt create mode 100644 charts/hedera-block-node/templates/_helpers.tpl create mode 100644 charts/hedera-block-node/templates/configmap.yaml create mode 100644 charts/hedera-block-node/templates/deployment.yaml create mode 100644 charts/hedera-block-node/templates/ingress.yaml create mode 100644 charts/hedera-block-node/templates/secret.yaml create mode 100644 charts/hedera-block-node/templates/service.yaml create mode 100644 charts/hedera-block-node/templates/serviceaccount.yaml create mode 100644 charts/hedera-block-node/values.yaml create mode 100644 ct.yaml diff --git a/.github/workflows/helm-charts.yaml b/.github/workflows/helm-charts.yaml new file mode 100644 index 000000000..7f71396e8 --- /dev/null +++ b/.github/workflows/helm-charts.yaml @@ -0,0 +1,65 @@ +## +# Copyright (C) 2024 Hedera Hashgraph, LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## +name: Lint and Test Charts + +on: pull_request + +jobs: + lint-test: + runs-on: ubuntu-latest + steps: + - name: Harden Runner + uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0 + with: + egress-policy: audit + + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Helm + uses: Azure/setup-helm@fe7b79cd5ee1e45176fcad797de68ecaf3ca4814 # v4.2.0 + with: + version: v3.14.4 + + - uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 + with: + python-version: '3.x' + check-latest: true + + - name: Set up chart-testing + uses: helm/chart-testing-action@e6669bcd63d7cb57cb4380c33043eebe5d111992 # v2.6.1 + + - name: Run chart-testing (list-changed) + id: list-changed + run: | + changed=$(ct list-changed --target-branch ${{ github.event.repository.default_branch }}) + if [[ -n "$changed" ]]; then + echo "changed=true" >> "$GITHUB_OUTPUT" + fi + + - name: Run chart-testing (lint) + if: steps.list-changed.outputs.changed == 'true' + run: ct lint --config ct.yaml + + - name: Create kind cluster + if: steps.list-changed.outputs.changed == 'true' + uses: helm/kind-action@v1.10.0 + + - name: Run chart-testing (install) + if: steps.list-changed.outputs.changed == 'true' + run: ct install --config ct.yaml diff --git a/charts/.gitignore b/charts/.gitignore new file mode 100644 index 000000000..a6f151d7a --- /dev/null +++ b/charts/.gitignore @@ -0,0 +1,2 @@ +*/out +out diff --git a/charts/hedera-block-node/.helmignore b/charts/hedera-block-node/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/charts/hedera-block-node/.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/hedera-block-node/Chart.yaml b/charts/hedera-block-node/Chart.yaml new file mode 100644 index 000000000..19c33f981 --- /dev/null +++ b/charts/hedera-block-node/Chart.yaml @@ -0,0 +1,16 @@ +apiVersion: v2 +appVersion: "0.1.0-SNAPSHOT" +description: A Helm chart for Hedera Block Node +home: https://github.com/hashgraph/hedera-block-node +keywords: + - blocknode + - dlt + - hedera + - hashgraph +maintainers: + - name: Hedera Block Node Team + email: blocknode@hashgraph.com +name: hedera-block-node +sources: + - https://github.com/hashgraph/hedera-block-node +version: 0.1.0-SNAPSHOT diff --git a/charts/hedera-block-node/README.md b/charts/hedera-block-node/README.md new file mode 100644 index 000000000..1a938661b --- /dev/null +++ b/charts/hedera-block-node/README.md @@ -0,0 +1,75 @@ +# Helm Chart +Installs the Hedera Block Node on a Kubernetes cluster. + +## Prerequisites +- Helm 3+ +- Kubernetes 1.29+ + +Set environment variables that will be used for the remainder of the document: + +```bash +export RELEASE="bn1" +``` + +## Template +To generate the K8 manifest files without installing the chart: +```bash +helm template --name-template my-bn hedera-block-node/ --dry-run --output-dir out +``` + +## Install +To install the chart: +```bash +helm repo add hedera-block-node https://hashgraph.github.io/hedera-block-node/charts +helm install "${RELEASE}" hedera-block-node/hedera-block-node -f +``` +*Note:* If using the chart directly after cloning the github repo, there is no need to add the repo. and install can be directly. +Assuming you are at the root folder of the repo. +```bash +helm install "${RELEASE}" charts/hedera-block-node -f +``` + +## Configure + +There are several ways to configure the Hedera Block Node. The following is the most common and recommended way to configure the Hedera Block Node. + +1. Create an override values file, `values.yaml`: +2. Add the necessary environment configuration variables to the following section: +```yaml +blockNode: + config: + # Add any additional env configuration here + # key: value + BLOCKNODE_STORAGE_ROOT_PATH: "/app/storage" + +``` +3. Secrets should be set at the following structure: +```yaml +blockNode: + secret: + PRIVATE_KEY: "" +``` +or passed at the command line: +```bash +helm install "${RELEASE}" hedera-block-node/hedera-block-node --set blockNode.secret.PRIVATE_KEY="" +``` + +## Using +Follow the `NOTES` instructions after installing the chart to perform `port-forward` to the Hedera Block Node and be able to use it. + +## Uninstall +To uninstall the chart: +```bash +helm uninstall "${RELEASE}" +``` + +## Upgrade +To upgrade the chart: +```bash +# update the helm repo +helm repo update +# save current user supplied values +helm get values "${RELEASE}" > user-values.yaml +# upgrade the chart +helm upgrade "${RELEASE}" hedera-block-node/hedera-block-node -f user-values.yaml +``` diff --git a/charts/hedera-block-node/templates/NOTES.txt b/charts/hedera-block-node/templates/NOTES.txt new file mode 100644 index 000000000..ae9ee2067 --- /dev/null +++ b/charts/hedera-block-node/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 "hedera-block-node.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 "hedera-block-node.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "hedera-block-node.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 "hedera-block-node.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/hedera-block-node/templates/_helpers.tpl b/charts/hedera-block-node/templates/_helpers.tpl new file mode 100644 index 000000000..42d688e1a --- /dev/null +++ b/charts/hedera-block-node/templates/_helpers.tpl @@ -0,0 +1,80 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "hedera-block-node.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 "hedera-block-node.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 "hedera-block-node.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "hedera-block-node.labels" -}} +helm.sh/chart: {{ include "hedera-block-node.chart" . }} +{{ include "hedera-block-node.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "hedera-block-node.selectorLabels" -}} +app.kubernetes.io/name: {{ include "hedera-block-node.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "hedera-block-node.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "hedera-block-node.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} + +{{/* +This function returns the image tag from the values.yaml file if provided. +If the tag is not provided, it defaults to the AppVersion specified in the Chart.yaml file. +Usage: {{ include "image.AppVersion" . }} +*/}} +{{- define "hedera-block-node.image.tag" -}} +{{- default .Chart.AppVersion .Values.image.tag -}} +{{- end -}} + +{{/* +This function returns the image tag from the values.yaml file if provided. +If the tag is not provided, it defaults to the AppVersion specified in the Chart.yaml file. +Usage: {{ include "hedera-block-node.app.version" . }} +*/}} +{{- define "hedera-block-node.appVersion" -}} +{{- default .Chart.AppVersion .Values.blockNode.version -}} +{{- end -}} diff --git a/charts/hedera-block-node/templates/configmap.yaml b/charts/hedera-block-node/templates/configmap.yaml new file mode 100644 index 000000000..b101617b6 --- /dev/null +++ b/charts/hedera-block-node/templates/configmap.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "hedera-block-node.fullname" . }}-config +data: + VERSION: {{ include "hedera-block-node.appVersion" . | quote }} +{{- range $key, $value := .Values.blockNode.config }} + {{ $key }}: {{ $value | quote }} +{{- end }} diff --git a/charts/hedera-block-node/templates/deployment.yaml b/charts/hedera-block-node/templates/deployment.yaml new file mode 100644 index 000000000..16327512f --- /dev/null +++ b/charts/hedera-block-node/templates/deployment.yaml @@ -0,0 +1,65 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "hedera-block-node.fullname" . }} + labels: + {{- include "hedera-block-node.labels" . | nindent 4 }} +spec: + replicas: 1 + selector: + matchLabels: + {{- include "hedera-block-node.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "hedera-block-node.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "hedera-block-node.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: {{ .Values.service.port }} + protocol: TCP + envFrom: + - configMapRef: + name: {{ include "hedera-block-node.fullname" . }}-config + - secretRef: + name: {{ include "hedera-block-node.fullname" . }}-secret + + # TODO: Uncomment them once we have the health and readiness probes in our application + #livenessProbe: + # httpGet: + # path: /health # we need to define this in our application + # port: 8080 # will be the same as server port? + #readinessProbe: + # httpGet: + # path: /ready # we need to define this in our application + # port: 8080 # will be the same as server port? + {{- 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/charts/hedera-block-node/templates/ingress.yaml b/charts/hedera-block-node/templates/ingress.yaml new file mode 100644 index 000000000..893b8ada9 --- /dev/null +++ b/charts/hedera-block-node/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "hedera-block-node.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "hedera-block-node.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- 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 }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/charts/hedera-block-node/templates/secret.yaml b/charts/hedera-block-node/templates/secret.yaml new file mode 100644 index 000000000..908ab7bb8 --- /dev/null +++ b/charts/hedera-block-node/templates/secret.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "hedera-block-node.fullname" . }}-secret +type: Opaque +data: +{{- range $key, $value := .Values.blockNode.secret }} + {{ $key }}: {{ $value | b64enc }} +{{- end }} diff --git a/charts/hedera-block-node/templates/service.yaml b/charts/hedera-block-node/templates/service.yaml new file mode 100644 index 000000000..6661f0691 --- /dev/null +++ b/charts/hedera-block-node/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "hedera-block-node.fullname" . }} + labels: + {{- include "hedera-block-node.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "hedera-block-node.selectorLabels" . | nindent 4 }} diff --git a/charts/hedera-block-node/templates/serviceaccount.yaml b/charts/hedera-block-node/templates/serviceaccount.yaml new file mode 100644 index 000000000..32b8b9a16 --- /dev/null +++ b/charts/hedera-block-node/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "hedera-block-node.serviceAccountName" . }} + labels: + {{- include "hedera-block-node.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/charts/hedera-block-node/values.yaml b/charts/hedera-block-node/values.yaml new file mode 100644 index 000000000..ad0bae35a --- /dev/null +++ b/charts/hedera-block-node/values.yaml @@ -0,0 +1,84 @@ +# Default values for hedera-block-node. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +image: + repository: ghcr.io/hashgraph/hedera-block-node + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "main" + +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 + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +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: {} + +blockNode: + # if blank will use same as AppVersion of chart. + version: "" + config: + # Add any additional env configuration here + # key: value + BLOCKNODE_STORAGE_ROOT_PATH: "/app/storage" + secret: + # if blank will use same as AppVersion of chart. + PRIVATE_KEY: "fake_private_key" diff --git a/ct.yaml b/ct.yaml new file mode 100644 index 000000000..c14bbedaf --- /dev/null +++ b/ct.yaml @@ -0,0 +1,3 @@ +check-version-increment: false +target-branch: main +validate-maintainers: false