Skip to content

Commit

Permalink
feat(template): update resource naming, automate secret csi volume cr…
Browse files Browse the repository at this point in the history
…eation (#3)
  • Loading branch information
browol authored Nov 26, 2023
1 parent 8808123 commit 62e994c
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 105 deletions.
2 changes: 1 addition & 1 deletion charts/general-purpose/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: general-purpose
description: A Helm chart for Kubernetes to deploy an Application
type: application
version: 0.2.10
version: 0.3.0-rc.1
maintainers:
- name: browol
url: https://github.com/browol
2 changes: 1 addition & 1 deletion charts/general-purpose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ ingress:
port:
number: 8080

ConfigMap:
configMap:
enabled: false
data:
- name: NODE_ENV
Expand Down
10 changes: 5 additions & 5 deletions charts/general-purpose/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{- if .Values.ConfigMap.enabled -}}
{{- if .Values.configMap.enabled -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.app.name | default .Release.Name }}--configmap
name: {{ .Values.app.name | default .Release.Name }}
data:
{{- if .Values.ConfigMap.data -}}
{{- range $index, $value := .Values.ConfigMap.data }}
{{- if .Values.configMap.data -}}
{{- range $index, $value := .Values.configMap.data }}
{{ $value.name }}: {{ $value.value | quote }}
{{- end }}
{{- end -}}
Expand All @@ -21,7 +21,7 @@ data:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $value.spec.name }}-{{ $releaseName }}--configmap
name: {{ $value.spec.name }}
data:
{{ $value.spec.fileName }}: |-
{{ if $value.spec.fromFile }}
Expand Down
59 changes: 32 additions & 27 deletions charts/general-purpose/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,34 +118,38 @@ spec:
{{- toYaml .Values.resources | nindent 10 }}

# Defined variable configMapMeta.
{{- $configMapMeta := printf "%s--%s" $releaseName "configmap" -}}
{{- $configMapMeta := $releaseName -}}
# Defined variable secretMeta.
{{ $secretMeta := printf "%s-%s" "keyvault" $releaseName }}

# Use envFrom to define all of the ConfigMap's data as container environment variables
# Use envFrom to define all of the configMap's data as container environment variables
envFrom:
{{ if .Values.envFrom.enabled }}
{{- toYaml .Values.envFrom.data | nindent 10 }}
{{ end }}

# Environment variable injection
env:

# Check Env is enabled
{{ if .Values.env.enabled }}
# then inject env to the application.
{{- toYaml .Values.env.data | nindent 10 }}
{{ end }}
# Check ConfigMap is enabled
{{ if .Values.ConfigMap.enabled }}

# Check configMap is enabled
{{ if .Values.configMap.enabled }}

# Inject env to the application.
{{ range $key, $value := .Values.ConfigMap.data }}
{{ range $key, $value := .Values.configMap.data }}
- name: {{ $value.name }}
valueFrom:
configMapKeyRef:
name: {{ $configMapMeta }}
key: {{ $value.name }}
{{ end }}
{{ end }}

# Env variable from Secret (Azure Keyvault)
# Check Secret is enabled
{{ if .Values.keyVault.enabled }}
Expand All @@ -159,17 +163,17 @@ spec:
{{ end }}
{{ end }}
{{ end }}

# Env variable from volumeMounts (configMap)
{{ if .Values.volumeMounts.enabled }}
{{ if .Values.volumeMounts.volumes }}
{{- $configMapSuffix := printf "%s--%s" $releaseName "configmap" -}}
{{ range $key, $value := .Values.volumeMounts.volumes }}
{{ if eq $value.type "configMap" }}
{{ if $value.spec.asEnvVar }}
- name: {{ $value.spec.asEnvVar }}
valueFrom:
configMapKeyRef:
name: {{ $value.spec.name }}-{{ $configMapSuffix }}
name: {{ $value.spec.name }}
key: {{ $value.spec.fileName }}
{{ end }}
{{ end }}
Expand All @@ -186,45 +190,46 @@ spec:
{{ if or .Values.volumeMounts.enabled .Values.keyVault.enabled }}
{{- $secret := .Values.keyVault -}}
volumes:
{{ if .Values.volumeMounts.enabled }}
{{ if .Values.volumeMounts.volumes }}
{{ range $key, $value := .Values.volumeMounts.volumes }}
{{ if $value }}

{{ if eq $value.type "emptyDir" }}
# Empty Dir
- name: {{ $value.name }}
# Empty Dir
{{ if eq $value.type "emptyDir" }} # check for create volume as empty (empty volume)
emptyDir: {}
# ConfigMap
{{ else if eq $value.type "configMap" }} # check for create volume from configMap (configMap volume)

{{ else if eq $value.type "configMap" }}
# configMap
- name: {{ $value.name }}
configMap:
name: {{ $value.spec.name }}-{{ $releaseName }}--configmap
name: {{ $value.spec.name }}
items:
- key: {{ $value.spec.fileName }}
path: {{ $value.spec.fileName }}
# CSI Driver for Key Vault
{{ else if and (eq $value.type "csi") (eq $secret.enabled true) }}
csi:
driver: {{ $secret.config.driver | default "secrets-store.csi.k8s.io" }}
readOnly: true
volumeAttributes:
secretProviderClass: secret-provider-{{ $releaseName }}
nodePublishSecretRef:
name: {{ $secret.config.secretRef | quote }} # secret name that create in namespace before helm install
# Azure Disk
{{ else if eq $value.type "azureDisk" }}

{{ else if eq $value.type "azureDisk" }}
# Azure Disk
- name: {{ $value.name }}
azureDisk:
kind: Managed
diskName: {{ $value.spec.diskName }}
diskURI: {{ $value.spec.diskURI }}
# Existing PVC
{{ else if eq $value.type "persistentVolumeClaim" }}

{{ else if eq $value.type "persistentVolumeClaim" }}
# Existing PVC
- name: {{ $value.name }}
persistentVolumeClaim:
claimName: {{ $value.spec.claimName }}
{{ end }}

{{ end }}
{{ end }}
{{ end }}
{{ end }}

{{ if .Values.keyVault.enabled }}
{{ if .Values.keyVault.config.autoVolume }} # auto create volume option for Key Vault
- name: vault
csi:
driver: {{ $secret.config.driver | default "secrets-store.csi.k8s.io" }}
Expand All @@ -234,7 +239,7 @@ spec:
nodePublishSecretRef:
name: {{ $secret.config.secretRef | quote }} # secret name that create in namespace before helm install
{{ end }}
{{ end }}

{{ end }}

# Deployment strategy
Expand Down
2 changes: 1 addition & 1 deletion charts/general-purpose/templates/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spec:
{{- range .http }}
{{ if .tls }}
{{ if .tls.cert }}
{{ $tlssecret := printf "%s--%s" .tls.cert.name "tls" }}
{{ $tlssecret := printf "%s-%s" "tls" .tls.cert.name }}
- secretName: {{ $tlssecret }}
hosts:
{{- toYaml .domainName | nindent 8 }}
Expand Down
2 changes: 1 addition & 1 deletion charts/general-purpose/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
kind: Secret
apiVersion: v1
metadata:
name: {{ printf "%s--%s" .tls.cert.name "tls" }}
name: {{ printf "%s-%s" "tls" .tls.cert.name }}
type: kubernetes.io/tls
data:
{{ ($file.Glob $path).AsSecrets | indent 2 }}
Expand Down
Loading

0 comments on commit 62e994c

Please sign in to comment.