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

device-agent: helm chart for deployment in kubernetes #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions flowforge-device-helm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
device.yml
23 changes: 23 additions & 0 deletions flowforge-device-helm/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
24 changes: 24 additions & 0 deletions flowforge-device-helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: flowforge-device
description: Helm chart which deploys FlowForge devices in Kubernetes cluster

# 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.1.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: "0.0.1"
35 changes: 35 additions & 0 deletions flowforge-device-helm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.PHONY: install upgrade uninstall

NAMESPACE ?= devices

DEV_FILE ?= device.yml

install-azure upgrade-azure template-azure: $(DEV_FILE)
helm $(subst -azure,,$@) \
flowforge-device \
. \
--values values-azure.yaml \
--set provisioningToken=$(shell base64 -i $(DEV_FILE)) \
--create-namespace \
--namespace=$(NAMESPACE)

install-aws upgrade-aws template-aws: $(DEV_FILE)
helm $(subst -aws,,$@) \
flowforge-device \
. \
--values values-aws.yaml \
--set provisioningToken=$(shell base64 -i $(DEV_FILE)) \
--create-namespace \
--namespace=$(NAMESPACE)

uninstall:
helm $@ \
flowforge-device

template-debug-azure: device.yml
helm template \
flowforge-device \
. \
--values values-azure.yaml \
--set provisioningToken=$(shell base64 -i $(DEV_FILE)) \
--debug
36 changes: 36 additions & 0 deletions flowforge-device-helm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Helm chart for FlowForge device in Kubernetes

This chart deploys FlowForge devices to a Kubernetes cluster. Devices can be assigned to a team or to a project during deployment.

Device instances are deployed as `Pods` in a `StatefulSet`. Every pod has own storage volume to keep variable data.

### Device installation

The procedure has been tested for Azure and AWS Kubernetes services.

1. Get provisioning token. In FlowForge Web-UI go to `Team` -> `Settings` -> `Devices` -> `Add Token` and save a token.
2. Save provisioning token to a file `device.yml` in the current (README.md) directory
3. Install `make` utility.
4. Make sure you have `base64` utility. Should be available on any Linux or MacOSX system.
5. Configure `kubectl` to access target K8S cluster with `default` context.
6. Edit `values-aws.yaml` or `values-azure.yaml` files. Fill with your custom settings.
7. Install the chart with command `make install-aws` or `make install-azure`. To choose namespace and device file name use command `make install-aws NAMESPACE=team-b DEV_FILE=device.yml`
8. Verify if pods are running in `devices` namespace.
9. Verify if devices are visible on FlowForge Web-UI.

### Upgrade after HELM modifications
`make upgrade-{vendor}`

### Uninstall the chart
`make uninstall`

Note that persistent volume claims and volumes will remain existing.

### Token update procedure

In case if device token has been updated, it is not enough just update the Helm.

1. Uninstall Helm chart
2. Manually remove `PersistenVolumeClaims` `flowforge-data-flowforge-device-*`.
3. Manually remove related `PersistenVolumes`
4. Install the chart with new device token.
22 changes: 22 additions & 0 deletions flowforge-device-helm/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -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 "flowforge-device.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 "flowforge-device.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "flowforge-device.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 "flowforge-device.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 }}
62 changes: 62 additions & 0 deletions flowforge-device-helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "flowforge-device.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 "flowforge-device.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 "flowforge-device.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

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

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

{{/*
Create the name of the service account to use
*/}}
{{- define "flowforge-device.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "flowforge-device.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
28 changes: 28 additions & 0 deletions flowforge-device-helm/templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "flowforge-device.fullname" . }}
labels:
{{- include "flowforge-device.labels" . | nindent 4 }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "flowforge-device.fullname" . }}
minReplicas: {{ .Values.autoscaling.minReplicas }}
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
metrics:
{{- if .Values.autoscaling.targetCPUUtilizationPercentage }}
- type: Resource
resource:
name: cpu
targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }}
{{- end }}
{{- if .Values.autoscaling.targetMemoryUtilizationPercentage }}
- type: Resource
resource:
name: memory
targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }}
{{- end }}
{{- end }}
75 changes: 75 additions & 0 deletions flowforge-device-helm/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{{- $replicaCount := .Values.replicaCount | int -}}


{{- if $.Values.ingress.enabled -}}

{{- $fullName := include "flowforge-device.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 }}

{{- $context := . -}}

{{- range $index := until $replicaCount -}}

{{- if semverCompare ">=1.19-0" $context.Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1
{{- else if semverCompare ">=1.14-0" $context.Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
name: {{ $fullName }}-{{ $index }}
labels:
{{- include "flowforge-device.labels" $context | nindent 4 }}
{{- with $context.Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- if and $context.Values.ingress.className (semverCompare ">=1.18-0" $context.Capabilities.KubeVersion.GitVersion) }}
ingressClassName: {{ $context.Values.ingress.className }}
{{- end }}
{{- if $context.Values.ingress.tls }}
tls:
{{- range $context.Values.ingress.tls }}
- hosts:
{{- range $context.Values.ingress.hosts }}
- ff-device-{{ $index }}.{{ .host }}
{{- end }}
secretName: ff-device-{{ $index }}.{{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range $context.Values.ingress.hosts }}
# - host: {{ .host }}
- host: ff-device-{{ $index }}.{{ .host }}
http:
paths:
{{- range .paths }}
- path: {{ .path }}
# - path: {{ .path }}ff-device-{{ $index }}
{{- if and .pathType (semverCompare ">=1.18-0" $context.Capabilities.KubeVersion.GitVersion) }}
pathType: {{ .pathType }}
{{- end }}
backend:
{{- if semverCompare ">=1.19-0" $context.Capabilities.KubeVersion.GitVersion }}
service:
name: {{ $fullName }}-{{ $index }}
port:
number: {{ $svcPort }}
{{- else }}
serviceName: {{ $fullName }}-{{ $index }}
servicePort: {{ $svcPort }}
{{- end }}
{{- end }}
{{- end }}

---
{{- end }}
{{- end }}
6 changes: 6 additions & 0 deletions flowforge-device-helm/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Secret
metadata:
name: provisioning-token
data:
device.yml: {{ .Values.provisioningToken }}
24 changes: 24 additions & 0 deletions flowforge-device-helm/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{- $replicaCount := .Values.replicaCount | int -}}

{{- $context := . -}}

{{- range $index := until $replicaCount -}}

apiVersion: v1
kind: Service
metadata:
name: {{ include "flowforge-device.fullname" $context }}-{{ $index }}
labels:
{{- include "flowforge-device.labels" $context | nindent 4 }}
spec:
type: {{ $context.Values.service.type }}
ports:
- port: {{ $context.Values.service.port }}
targetPort: {{ $context.Values.service.port }}
protocol: TCP
name: http
selector:
{{- include "flowforge-device.selectorLabels" $context | nindent 4 }}
statefulset.kubernetes.io/pod-name: flowforge-device-{{ $index }}
---
{{- end }}
12 changes: 12 additions & 0 deletions flowforge-device-helm/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "flowforge-device.serviceAccountName" . }}
labels:
{{- include "flowforge-device.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
Loading