is there way to convert yaml object to multiline string in kustomize?

8/30/2019

I am trying to customize configmap in k8s by using kustomize, for example I need to customize 'title_link' for different systems prod, dev...(see example below) . Since 'data.alertmanager.yaml' in configmap is multiline string I have decided the approach where I defined extra yml object (config-object.yaml -> ConfigObject) in the base folder and then implemented ConfigObject customization in overlays/prod. I was able successfuly patch 'title_link' via patchesJson6902 but now I need to convert somehow resultig data from ConfigObject to multiline string in order to be able finally assign this string via variable $(CONFIG_DATA) in configmap. is it achievable in kustomize? If not please suggest other way to achieve this goal for customizing multiline strings . Thanks in advance.

#cat base/kustomization.yaml
resources:
 - config-object.yaml
 - configmap.yaml



#cat base/configmap.yaml
apiVersion: v1
data:
  alertmanager.yaml: |
    ''
kind: ConfigMap
metadata:
  name: alertmanager


#cat base/config-object.yaml 
apiVersion: v1
data:
  global:
   slack_api_url: 'https://slackurl'
   resolve_timeout: 5m
  route:
   group_wait: 30s
   group_interval: 5m
   repeat_interval: 12h
   receiver: default
   routes:
   - match_re:
       severity: critical
     receiver: ocp-alerts
  receivers:
  - name: default
  - name: ocp-alerts
    slack_configs:
    - channel: '#ocp-alerts'
      send_resolved: true
      title_link: ''
      title: >
        [{{ .Status | toUpper }}]
        {{ .CommonLabels.cluster }} - {{ .CommonLabels.alertname }}
        {{ if eq .Status "firing" }}({{ .Alerts.Firing | len }}){{ end }}
      footer: |
        {{ range .Alerts }}
        {{ .Annotations.description }}
        {{ .Annotations.message }}
        {{ end }}
kind: ConfigObject
metadata:
  name: alertmanager



#cat overlays/prod/kustomization.yaml
configurations:
- kustomizeconfig.yaml

bases:
 - ../../base

patchesStrategicMerge:
- configmap-patch.yaml

patchesJson6902:
- target:
    version: v1
    kind: ConfigObject
    name: alertmanager
  path: patch_title_link.yaml

vars:
- name: CONFIG_DATA
  objref:
    kind: ConfigObject
    name: alertmanager
    apiVersion: v1
  fieldref:
    fieldpath: data

#cat overlays/prod/configmap-patch.yaml
apiVersion: v1
data:
  alertmanager.yaml: |
    $(CONFIG_DATA)
kind: ConfigMap
metadata:
  name: 
    alertmanager

#cat overlays/prod/kustomizeconfig.yaml
varReference:
- path: data
  kind: ConfigMap

#cat overlays/prod/kconfigmap-patch.yaml
- op: replace
  path: /data/receivers/1/slack_configs/0/title_link
  value: 'https://customroute-production'






#This is what I want to achieve: 
apiVersion: v1
data:
  alertmanager.yaml: |
    global:
     slack_api_url: 'https://slackurl'
     resolve_timeout: 5m
    route:
     group_wait: 30s
     group_interval: 5m
     repeat_interval: 12h
     receiver: default
     routes:
     - match_re:
         severity: critical
       receiver: ocp-alerts
    receivers:
    - name: default
    - name: ocp-alerts
      slack_configs:
      - channel: '#ocp-alerts'
        send_resolved: true
        title_link: 'https://customroute-production'
        title: >
          [{{ .Status | toUpper }}]
          {{ .CommonLabels.cluster }} - {{ .CommonLabels.alertname }}
          {{ if eq .Status "firing" }}({{ .Alerts.Firing | len }}){{ end }}
        footer: |
          {{ range .Alerts }}
          {{ .Annotations.description }}
          {{ .Annotations.message }}
          {{ end }}
kind: ConfigMap
metadata:
  name: alertmanager
-- Rusey
kubernetes
kustomize
yaml

0 Answers