简体   繁体   中英

Helm - Configmap - Replace only the contents of file and not File Names

I have defined application.properties files in a config dir. Config dir is on the below structure.

config
  application.properties
  application-test.properties
  application-dev.properties
  application-prod.properties

I have created a helm chart to create the configmap. Helm chart is defined as below

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  {{- (.Files.Glob "config/*").AsConfig | nindent 2 }}

I see that the configmap is created.

We are consuming the ConfigMap via environment variables in a running container using the envFrom property. (This in my deployment yaml file)

spec:
   containers:
   - envFrom:
     - configMapRef:
         name: nginx-configmap

I see that the values are stored as environment variables. However the variables are in lower cases.

server.port=8080
server.name=localhost

Since these are env variables, they have to be uppercase and . should be replaced with _. So, I have modifed my chart as below

data:
  {{- (.Files.Glob "config/*").AsConfig | nindent 2 | upper | replace "." "_" }}

The generated configmap is as below

  APPLICATION_PROPERTIES: |
    SERVER_PORT = 8080
    SERVER_NAME = LOCALHOST

Below is the env variables inside container

APPLICATION_PROPERTIES=SERVER_PORT = 8080
SERVER_NAME = LOCALHOST

My requirement is that only the contents of the file should be upper case and . should be replaced with _. The filename should not be converted. The filename should be as is.

Can this be achieved?

Try this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  {{ $root := . }}
  {{ range $path, $bytes := .Files.Glob "config/*" }}
  {{ base $path }}: '{{ $root.Files.Get $path | nindent 2 | upper | replace "." "_"  }}'
  {{ end }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM