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

Feat enhance helm chart #45

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
37 changes: 30 additions & 7 deletions charts/local-ai/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,39 @@ Common labels
*/}}
{{- define "local-ai.labels" -}}
helm.sh/chart: {{ include "local-ai.chart" . }}
app.kubernetes.io/name: {{ include "local-ai.name" . }}
app.kubernetes.io/instance: "{{ .Release.Name }}"
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{ include "local-ai.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

# Add defaults for global.labels and global.annotations
{{- define "local-ai.annotations" -}}
{}
{{- end -}}
{{/*
Selector labels
*/}}
{{- define "local-ai.selectorLabels" -}}
app.kubernetes.io/name: {{ include "local-ai.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "local-ai.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "local-ai.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Preprocess the models.galleries values into what is expected by LocalAI
*/}}
{{- define "local-ai.galleries" -}}
{{- $galleries := list -}}
{{- range $name, $url := .Values.models.galleries -}}
{{- $galleries = append $galleries (dict "name" $name "url" $url) -}}
{{- end -}}
{{- $galleries | toJson -}}
{{- end }}
76 changes: 76 additions & 0 deletions charts/local-ai/templates/_sidecars.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{{- define "local-ai.sidecars" }}
{{- range .Values.sidecarContainers }}
# Sidecar container from values.yaml
- name: {{ .name }}
image: {{ .image }}
imagePullPolicy: {{ .imagePullPolicy }}
{{- if .command }}
command:
{{- toYaml .command | nindent 12 }}
{{- end }}
{{- if .args }}
args:
{{- toYaml .args | nindent 12 }}
{{- end }}
{{- if .env }}
env:
{{- toYaml .env | nindent 12 }}
{{- end }}
{{- if .ports }}
ports:
{{- toYaml .ports | nindent 12 }}
{{- end }}
{{- if .resources }}
resources:
{{- toYaml .resources | nindent 12 }}
{{- end }}
{{- if or .volumeMounts .Values.persistence }}
volumeMounts:
{{- if .volumeMounts }}
{{- toYaml .volumeMounts | nindent 12 }}
{{- end }}
{{- range $key, $pvc := .Values.persistence }}
{{- if $pvc.enabled }}
- name: {{ $key }}
mountPath: {{ $pvc.globalMount | default (print "/" $key) }}
{{- end }}
{{- end }}
{{- end }}
{{- if .livenessProbe }}
livenessProbe:
{{- toYaml .livenessProbe | nindent 12 }}
{{- end }}
{{- if .readinessProbe }}
readinessProbe:
{{- toYaml .readinessProbe | nindent 12 }}
{{- end }}
{{- if .securityContext }}
securityContext:
{{- toYaml .securityContext | nindent 12 }}
{{- end }}
{{- end }}
{{ if .Values.enableModelSyncronizationSidecar }}
# Built in sidecar for model syncronization
- name: model-loader
image: quay.io/kiwigrid/k8s-sidecar:1.26.1
imagePullPolicy: IfNotPresent
env:
- name: LABEL
value: io.localai/model_source
- name: LABEL_VALUE
value: "1"
- name: METHOD
value: WATCH
- name: FOLDER
value: {{ .Values.deployment.modelsPath }}
- name: FOLDER_ANNOTATION
value: "io.localai/target-directory"
- name: SCRIPT
value: {{ .Values.deployment.modelsPath }}/gallery-models.sh
securityContext:
runAsGroup: 1000
volumeMounts:
- mountPath: {{ .Values.deployment.modelsPath }}
name: models
{{- end }}
{{- end }}
11 changes: 0 additions & 11 deletions charts/local-ai/templates/configmap-prompt-templates.yaml

This file was deleted.

34 changes: 34 additions & 0 deletions charts/local-ai/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{- $numModelConfigs := .Values.models.configs | keys | len -}}
{{- $numPromptTemplates := .Values.promptTemplates | keys | len -}}
{{- if lt ((concat (.Values.models.configs | keys) (.Values.promptTemplates | keys)) | uniq | len ) (add $numModelConfigs $numPromptTemplates) -}}
{{- fail "All object names within models.configs and promptTemplates must be unique" -}}
{{- end -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "local-ai.fullname" . }}-models
labels:
{{- include "local-ai.labels" . | nindent 4 }}
io.localai/model_source: "1"
{{- with .Values.annotations }}
annotations:
io.localai/target-directory: /models
{{- end }}
data:
{{- range $key, $data := .Values.models.configs}}
{{ if or (hasSuffix ".yml" $key) (hasSuffix ".yaml" $key) }}{{ $key }}{{ else }}{{ $key }}.yaml{{ end }}: |-
{{- $data | toYaml | nindent 4}}
{{- end }}
{{- range $key, $data := .Values.promptTemplates}}
{{ $key }}: |-
{{- $data | nindent 4}}
{{- end }}
gallery-models.sh: |-
#!/bin/sh
GALLERY_MODELS="{{ .Values.models.loadFromGalleries | join "," }}"

if [ -n "$GALLERY_MODELS" ]; then
echo "$GALLERY_MODELS" | awk -F, '{for (i=1; i<=NF; i++) print $i}' | while read -r model; do
wget 127.0.0.1:8080/models/apply -q --header "Content-Type: application/json" --post-data "{\"id\":\"${model}\"}" -O -
done
fi
107 changes: 24 additions & 83 deletions charts/local-ai/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# yamllint disable rule:line-length
{{- $urls := "" -}}
{{- $urls := list -}}
{{- $rootPersistence := .Values.persistence }}
{{- range $idx, $model := .Values.models.list }}
{{- $urls = printf "%s%s %s," $urls $model.url ($model.basicAuth | default "") }}
{{- $urls = append $urls (printf "%s %s" $model.url ($model.basicAuth | default "")) }}
{{- end }}
{{- $urls = join "," $urls}}

apiVersion: apps/v1
kind: Deployment
Expand All @@ -15,19 +16,14 @@ metadata:
spec:
selector:
matchLabels:
app.kubernetes.io/name: {{ include "local-ai.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- include "local-ai.selectorLabels" . | nindent 6 }}
replicas: {{ .Values.replicaCount }}
template:
metadata:
name: {{ template "local-ai.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "local-ai.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
annotations:
{{- if .Values.promptTemplates }}
checksum/config-prompt-templates: {{ include (print $.Template.BasePath "/configmap-prompt-templates.yaml") . | sha256sum }}
{{- end }}
spec:
{{- with .Values.deployment.runtimeClassName }}
runtimeClassName: {{ . }}
Expand All @@ -36,6 +32,7 @@ spec:
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "local-ai.serviceAccountName" . }}
initContainers:
# Additional initContainers from values.yaml
{{- if .Values.initContainers }}
Expand Down Expand Up @@ -77,25 +74,6 @@ spec:
{{- end }}
{{- end }}
{{- end }}

{{- if .Values.promptTemplates }}
- name: prompt-templates
image: {{ .Values.deployment.prompt_templates.image }}
imagePullPolicy: {{ .Values.deployment.pullPolicy }}
command: ["/bin/sh", "-c"]
args:
- |
cp -fL /prompt-templates/* /models
volumeMounts:
- mountPath: /prompt-templates
name: prompt-templates
{{- range $key, $pvc := $rootPersistence }}
{{- if $pvc.enabled }}
- name: {{ $key }}
mountPath: {{ $pvc.globalMount | default (print "/" $key) }}
{{- end }}
{{- end }}
{{- end }}
- name: download-model
image: {{ .Values.deployment.download_model.image }}
imagePullPolicy: {{ .Values.deployment.pullPolicy }}
Expand All @@ -115,7 +93,7 @@ spec:

validate_url() {
local url=$1
local regex='^(https?|ftp)://[a-zA-Z0-9.-]+(:[a-zA-Z0-9.-]+)?(/[a-zA-Z0-9.-]*)*$'
local regex='^(https?|ftp)://[a-zA-Z0-9.-]+(:[a-zA-Z0-9.-]+)?(/[a-zA-Z0-9._-]*)*$'
if [[ $url =~ $regex ]]; then
return 0 # URL is valid
else
Expand Down Expand Up @@ -180,87 +158,50 @@ spec:
{{- end }}

containers:
# Sidecar containers from values.yaml
{{- range .Values.sidecarContainers }}
- name: {{ .name }}
image: {{ .image }}
imagePullPolicy: {{ .imagePullPolicy }}
{{- if .command }}
command:
{{- toYaml .command | nindent 12 }}
{{- end }}
{{- if .args }}
args:
{{- toYaml .args | nindent 6 }}
{{- end }}
{{- if .env }}
env:
{{- toYaml .env | nindent 6 }}
{{- end }}
{{- if .ports }}
ports:
{{- toYaml .ports | nindent 6 }}
{{- end }}
{{- if .resources }}
resources:
{{- toYaml .resources | nindent 6 }}
{{- end }}
{{- if or .volumeMounts $rootPersistence }}
volumeMounts:
{{- if .volumeMounts }}
{{- toYaml .volumeMounts | nindent 6 }}
{{- end }}
{{- range $key, $pvc := $rootPersistence }}
{{- if $pvc.enabled }}
- name: {{ $key }}
mountPath: {{ $pvc.globalMount | default (print "/" $key) }}
{{- end }}
{{- end }}
{{- end }}
{{- if .livenessProbe }}
livenessProbe:
{{- toYaml .livenessProbe | nindent 6 }}
{{- end }}
{{- if .readinessProbe }}
readinessProbe:
{{- toYaml .readinessProbe | nindent 6 }}
{{- end }}
{{- if .securityContext }}
securityContext:
{{- toYaml .securityContext | nindent 6 }}
{{- end }}
{{- end }}
- name: {{ template "local-ai.fullname" . }}
image: "{{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag }}"
imagePullPolicy: {{ .Values.deployment.pullPolicy }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
ports:
- containerPort: 8080
name: http
protocol: TCP
livenessProbe:
{{- toYaml .Values.livenessProbe | nindent 12 }}
readinessProbe:
{{- toYaml .Values.readinessProbe | nindent 12 }}
env:
{{- range $key, $value := .Values.deployment.env }}
- name: {{ $key | upper }}
value: {{ quote $value }}
{{- end }}
- name: GALLERIES
value: {{ include "local-ai.galleries" . | squote }}
- name: MODELS_PATH
value: {{ .Values.deployment.modelsPath }}
securityContext:
runAsGroup: 1000
volumeMounts:
{{- range $key, $pvc := $rootPersistence}}
{{- if $pvc.enabled }}
- name: {{ $key }}
mountPath: {{ $pvc.globalMount | default (print "/" $key) }}
{{- end }}
{{- end }}
{{ include "local-ai.sidecars" . | nindent 8 }}
{{- if $rootPersistence}}
volumes:
{{- range $key, $pvc := $rootPersistence}}
{{- if $pvc.enabled }}
{{- if $pvc.enabled }}
- name: {{ $key }}
persistentVolumeClaim:
claimName: {{ printf "%s-%s" (include "local-ai.fullname" $) $key }}
{{- end }}
{{- else }}
- name: {{ $key }}
emptyDir: {}
{{- end }}
{{- end }}
- name: prompt-templates
configMap:
name: {{ template "local-ai.fullname" . }}-prompt-templates
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
Expand Down
19 changes: 19 additions & 0 deletions charts/local-ai/templates/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- if and .Values.rbac.create (not .Values.rbac.useExistingRole) -}}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "local-ai.fullname" . }}
labels:
{{- include "local-ai.labels" . | nindent 4 }}
{{- with .Values.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["configmaps", "secrets"]
verbs: ["get", "watch", "list"]
{{- with .Values.rbac.extraRoleRules }}
{{- toYaml . | nindent 2 }}
{{- end}}
{{- end }}
Loading