community icon indicating copy to clipboard operation
community copied to clipboard

Feature: Create a Template.BaseFolder function

Open sei-areuter opened this issue 7 months ago • 0 comments

Hi,

I am interested in creating a feature request for being able to determine which folder a file is in. Helm already has pretty similar functionality to this built-in with {{ Template.BasePath }} which tells you the path to your file. I think that having something similar with {{ Template.BaseFolder }} could buy users a lot of advantages when it comes to creating reusable code. I can provide a pretty quick example below.

If my directory was formatted as follows..

Chart.yaml
values.yaml
README.md
templates/
    backend/
        deployment.yaml
    frontend/
        deployment.yaml

Then I could setup my one deployment like this and have 2 unique deployments

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ Template.BaseFolder }}-deployment
  labels:
  {{- include "chart.labels" . | indent 4 }}
    app: {{ Template.BaseFolder }}
spec:
  replicas: {{ .Values.frontend.replicaCount }}  # even this could be templated using index and setting your values.yaml up. But that is a little overkill
  selector:
    matchLabels:
      app: {{ Template.BaseFolder }}
  template: 
    metadata:
      labels:
        {{- include "chart.labels" . | indent 8 }}
         app: {{ Template.BaseFolder }}
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        ....

Then the output of these 2 deployments would be

frontend-deployment
backend-deployment

It should look something like the following from a python perspective

{{ Template.BasePath}}.split('/')[-2]

sei-areuter avatar Jun 12 '25 15:06 sei-areuter