Not able to use the patch parameter of the k8s module

Hi team,
We are using the K8s module to patch our config map file.
The config map file which we are patching has 2 data.

The data which we pass its replacing the actual data.
We want to append the data in the current data field
Our code:

- name: Configure prometheus-server-conf
  kubernetes.core.k8s:
    state: patched
    kind: ConfigMap
    name: prometheus-server-conf
    namespace: monitoring
    definition:
      data:
        prometheus.yml: |-
          scrape_configs:
            - job_name: "ceph"
              static_configs:
                - targets: [' 10.0.1.34:9283', ' 10.0.1.35:9283', ' 10.0.1.36:9283']

Example:
we have a following file:

apiVersion: v1
data:
prometheus.yml: |-
    scrape_configs:
      - job_name: 'kubernetes-apiservers'
        kubernetes_sd_configs:
        - role: endpoints
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        relabel_configs:
        - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
          action: keep
          regex: default;kubernetes;https

We want the following:

apiVersion: v1
data:
prometheus.yml: |-
    scrape_configs:
      - job_name: 'kubernetes-apiservers'
        kubernetes_sd_configs:
        - role: endpoints
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        relabel_configs:
        - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
          action: keep
          regex: default;kubernetes;https
      - job_name: "ceph"
        static_configs:
          - targets: [' 10.0.1.34:9283', ' 10.0.1.35:9283', ' 10.0.1.36:9283']

The following is happening:

apiVersion: v1
data:
prometheus.yml: |-
    scrape_configs:
      - job_name: "ceph"
        static_configs:
          - targets: [' 10.0.1.34:9283', ' 10.0.1.35:9283', ' 10.0.1.36:9283']

Kindly help.

Hi,

Perhaps try to explicitly specify you’d like to perform a “strategic-merge” patch, although description states it should be the default.

Edit: I highly doubt it, but I’m also wondering if stripping last newline with a literal block scalar (|-) as you do in your patch definition could invalidate the key you’re trying to patch in some way. Just a thought.

I didn’t make any investigation or test on my side, but since data in ConfigMap is string type, in the first place, your prometheus.yml is just one big string data instead of well-structured data.

I mean,

prometheus.yml: |-
    scrape_configs:
      - job_name: 'kubernetes-apiservers'
        kubernetes_sd_configs:
        ...

almost equals to:

prometheus.yml: "scrape_configs:\n  - job_name: 'kubernetes-apiservers'\n    kubernetes_sd_configs:\n    ..."

So AFAIK I don’t think a merge strategy will work :thinking:

If we want to patch an existing ConfigMap that has any keys with the data in YAML format, it would be difficult unless we get the existing ConfigMap, convert it to dict with from_yaml, patch it, and then update the ConfigMap with to_nice_yaml. If there is a better way, I’d like to know too.

2 Likes

There is a similar KEP for kustomize: kustomize/proposals/22-03-value-in-the-structured-data.md at master · kubernetes-sigs/kustomize · GitHub

1 Like