Skip to content

Commit

Permalink
import extraFiles wholesale from z2jh
Browse files Browse the repository at this point in the history
more complex, but also more general
  • Loading branch information
minrk committed Apr 25, 2022
1 parent d92f718 commit f8d35fa
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 26 deletions.
35 changes: 29 additions & 6 deletions helm-chart/binderhub/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ required:
- service
- config
- extraConfig
- templates
- jupyterhub
- deployment
- dind
- imageCleaner
- ingress
- initContainers
- lifecycle
- extraFiles
- extraVolumes
- extraVolumeMounts
- extraEnv
Expand Down Expand Up @@ -261,13 +261,36 @@ properties:
manifest as either the binder pod going into `Error` or `CrashLoopBackoff` states, or in
some special cases, the binder pod running but... just doing very random things. Be careful!
templates:
extraFiles:
type: object
additionalProperties:
type: string
additionalProperties: false
description: |
Custom HTML page templates for BinderHub,
e.g. page.html.
A dictionary with extra files to be injected into the binder pod's container
on startup. This can for example be used to inject: configuration
files, custom user interface templates, images, and more.
See zero-to-jupyterhub's extraFiles documentation for reference.
patternProperties:
".*":
type: object
additionalProperties: false
required: [mountPath]
oneOf:
- required: [data]
- required: [stringData]
- required: [binaryData]
properties:
mountPath:
type: string
data:
type: object
additionalProperties: true
stringData:
type: string
binaryData:
type: string
mode:
type: number

jupyterhub:
type: object
Expand Down
74 changes: 74 additions & 0 deletions helm-chart/binderhub/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,77 @@ Render docker config.json for the registry-publishing secret and other docker co

{{- $dockerConfig | toPrettyJson }}
{{- end }}

{{- /*
binderhub.extraFiles.data:
Renders content for a k8s Secret's data field, coming from extraFiles with
binaryData entries.
*/}}
{{- define "binderhub.extraFiles.data.withNewLineSuffix" -}}
{{- range $file_key, $file_details := . }}
{{- include "binderhub.extraFiles.validate-file" (list $file_key $file_details) }}
{{- if $file_details.binaryData }}
{{- $file_key | quote }}: {{ $file_details.binaryData | nospace | quote }}{{ println }}
{{- end }}
{{- end }}
{{- end }}
{{- define "binderhub.extraFiles.data" -}}
{{- include "binderhub.extraFiles.data.withNewLineSuffix" . | trimSuffix "\n" }}
{{- end }}
{{- /*
binderhub.extraFiles.stringData:
Renders content for a k8s Secret's stringData field, coming from extraFiles
with either data or stringData entries.
*/}}
{{- define "binderhub.extraFiles.stringData.withNewLineSuffix" -}}
{{- range $file_key, $file_details := . }}
{{- include "binderhub.extraFiles.validate-file" (list $file_key $file_details) }}
{{- $file_name := $file_details.mountPath | base }}
{{- if $file_details.stringData }}
{{- $file_key | quote }}: |
{{- $file_details.stringData | trimSuffix "\n" | nindent 2 }}{{ println }}
{{- end }}
{{- if $file_details.data }}
{{- $file_key | quote }}: |
{{- if or (eq (ext $file_name) ".yaml") (eq (ext $file_name) ".yml") }}
{{- $file_details.data | toYaml | nindent 2 }}{{ println }}
{{- else if eq (ext $file_name) ".json" }}
{{- $file_details.data | toJson | nindent 2 }}{{ println }}
{{- else if eq (ext $file_name) ".toml" }}
{{- $file_details.data | toToml | trimSuffix "\n" | nindent 2 }}{{ println }}
{{- else }}
{{- print "\n\nextraFiles entries with 'data' (" $file_key " > " $file_details.mountPath ") needs to have a filename extension of .yaml, .yml, .json, or .toml!" | fail }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- define "binderhub.extraFiles.stringData" -}}
{{- include "binderhub.extraFiles.stringData.withNewLineSuffix" . | trimSuffix "\n" }}
{{- end }}

{{- define "binderhub.extraFiles.validate-file" -}}
{{- $file_key := index . 0 }}
{{- $file_details := index . 1 }}

{{- /* Use of mountPath. */}}
{{- if not ($file_details.mountPath) }}
{{- print "\n\nextraFiles entries (" $file_key ") must contain the field 'mountPath'." | fail }}
{{- end }}

{{- /* Use one of stringData, binaryData, data. */}}
{{- $field_count := 0 }}
{{- if $file_details.data }}
{{- $field_count = add1 $field_count }}
{{- end }}
{{- if $file_details.stringData }}
{{- $field_count = add1 $field_count }}
{{- end }}
{{- if $file_details.binaryData }}
{{- $field_count = add1 $field_count }}
{{- end }}
{{- if ne $field_count 1 }}
{{- print "\n\nextraFiles entries (" $file_key ") must only contain one of the fields: 'data', 'stringData', and 'binaryData'." | fail }}
{{- end }}
{{- end }}
21 changes: 14 additions & 7 deletions helm-chart/binderhub/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ spec:
- name: config
secret:
secretName: binder-secret
- name: templates
{{- if .Values.extraFiles }}
- name: files
secret:
secretName: binder-secret
items:
{{- range $filename, $contents := .Values.templates }}
- key: "templates-{{ $filename }}"
path: {{ $filename | quote }}
{{- range $file_key, $file_details := .Values.extraFiles }}
- key: {{ $file_key | quote }}
path: {{ $file_key | quote }}
{{- with $file_details.mode }}
mode: {{ . }}
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.config.BinderHub.use_registry }}
- name: docker-secret
secret:
Expand Down Expand Up @@ -80,9 +85,11 @@ spec:
- mountPath: /etc/binderhub/config/
name: config
readOnly: true
- mountPath: /etc/binderhub/templates/
name: templates
readOnly: true
{{- range $file_key, $file_details := .Values.extraFiles }}
- mountPath: {{ $file_details.mountPath }}
subPath: {{ $file_key | quote }}
name: files
{{- end }}
{{- if .Values.config.BinderHub.use_registry }}
- mountPath: /root/.docker
name: docker-secret
Expand Down
15 changes: 9 additions & 6 deletions helm-chart/binderhub/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ stringData:
values.yaml: |
{{- pick .Values "config" "cors" "dind" "extraConfig" | toYaml | nindent 4 }}
{{- /* Custom HTML templates */ -}}
{{- range $filename, $contents := .Values.templates }}
templates-{{ $filename }}: |
{{ $contents | nindent 4 }}
{{- end }}

{{- /* Glob files to allow them to be mounted by the binderhub pod */}}
{{- /* key=filename: value=content */}}
{{- (.Files.Glob "files/*").AsConfig | nindent 2 }}

{{- with include "binderhub.extraFiles.stringData" .Values.extraFiles }}
{{- . | nindent 2 }}
{{- end }}

{{- with include "binderhub.extraFiles.data" .Values.extraFiles }}
data:
{{- . | nindent 2 }}
{{- end }}
---
{{- if or .Values.config.BinderHub.use_registry .Values.config.BinderHub.buildDockerConfig }}
kind: Secret
Expand Down
16 changes: 9 additions & 7 deletions testing/k8s-binder-k8s-hub/binderhub-chart-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ config:
log_level: 10
cors_allow_origin: "*"

templates:
page.html: |
{% extends "templates/page.html" %}
{% block footer %}
{{ super() }}
<span>test-template</span>
{% endblock %}
extraFiles:
page.html:
mountPath: /etc/binderhub/templates/page.html
stringData: |
{% extends "templates/page.html" %}
{% block footer %}
{{ super() }}
<span>test-template</span>
{% endblock %}
ingress:
# Enabled to test the creation/update of the k8s Ingress resource, but not
Expand Down

0 comments on commit f8d35fa

Please sign in to comment.