Kubernetes - How to define ConfigMap built using a file in a yaml?

前端 未结 3 2123
失恋的感觉
失恋的感觉 2020-12-14 08:49

At present I am creating a configmap from the file config.json by executing:

kubectl create configmap jksconfig --from-file=config.json

I w

相关标签:
3条回答
  • 2020-12-14 09:10

    Soln 01:

    • insert your config.json file content into a template
    • then use this template into your data against config.json
    • then run $ helm install command

    finally,

    {{define "config"}}
    {
        "a": "A",
        "b": {
            "b1": 1
        }
    }
    {{end}}
    
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: jksconfig
      labels:
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        app: "my-app"
        heritage: "{{ .Release.Service }}"
        release: "{{ .Release.Name }}"
    data:
      config.json: {{ (include "config" .) | trim | quote }}
    
    0 讨论(0)
  • 2020-12-14 09:27

    Your config.json file should be inside your mychart/ directory, not inside mychart/templates

    Chart Template Guide

    configmap.yaml

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      config.json: |-
    {{ .Files.Get "config.json" | indent 4}}
    

    config.json

    {
        "val": "key"
    }
    

    helm install --dry-run --debug mychart

    [debug] Created tunnel using local port: '52091'     
    
    [debug] SERVER: "127.0.0.1:52091"                    
    
    ...           
    
    NAME:   dining-saola                                 
    REVISION: 1                                          
    RELEASED: Fri Nov 23 15:06:17 2018                   
    CHART: mychart-0.1.0                                 
    USER-SUPPLIED VALUES:                                
    {}                                                   
    
    ...
    
    ---                                                  
    # Source: mychart/templates/configmap.yaml           
    apiVersion: v1                                       
    kind: ConfigMap                                      
    metadata:                                            
      name: dining-saola-configmap                       
    data:                                                
      config.json: |-                                    
        {                                                
            "val": "key"                                 
        }     
    

    EDIT:

    But I want it the values in the config.json file to be taken from values.yaml. Is that possible?

    configmap.yaml

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      config.json: |-
        {
    {{- range $key, $val := .Values.json }}
    {{ $key | quote | indent 6}}: {{ $val | quote }}
    {{- end}}
        }
    

    values.yaml

    json:
      key1: val1
      key2: val2
      key3: val3
    

    helm install --dry-run --debug mychart

    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: mangy-hare-configmap
    data:
      config.json: |-
        {
          "key1": "val1"
          "key2": "val2"
          "key3": "val3"
        }
    
    0 讨论(0)
  • 2020-12-14 09:27

    Here is an example of a ConfigMap that is attached to a Deployment:

    ConfigMap:

    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: jksconfig
    data:
      config.json: |-
    {{ .Files.Get "config.json" | indent 4 }}
    

    Deployment:

    ---
    apiVersion: apps/v1beta2
    kind: Deployment
    metadata:
      name: jksapp
      labels:
        app: jksapp
    spec:
      selector:
        matchLabels:
          app: jksapp
      template:
        metadata:
          labels:
            app: jksapp
          containers:
            - name: jksapp
              image: jksapp:1.0.0
              ports:
                - containerPort: 8080
              volumeMounts:
                - name: config #The name(key) value must match pod volumes name(key) value 
                  mountPath: /path/to/config.json
          volumes:
            - name: config
              configMap:
                name: jksconfig
    
    0 讨论(0)
提交回复
热议问题