1

Looking for some help / guidance on writing a shell script to generate a configmap to use in my cluster. I wanted to write a shell script to loop through a map or dictionary of values to fill out a configmap.

Example:

apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: prometheus-adapter
    meta.helm.sh/release-namespace: prometheus
  labels:
    app.kubernetes.io/component: metrics
    app.kubernetes.io/instance: prometheus-adapter
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: prometheus-adapter
    app.kubernetes.io/part-of: prometheus-adapter
    app.kubernetes.io/version: v0.10.0
    helm.sh/chart: prometheus-adapter-3.4.1
  name: prometheus-adapter
  namespace: prometheus
data:
  config.yaml: |
    rules:
      - seriesQuery: 'haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="pod1"}'
        metricsQuery: 'sum(rate(haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="pod1"}[2m])) by (<<.GroupBy>>)'
        resources:
          overrides:
            namespace: {resource: "namespace"}
        name:
          matches: "^(.*)_total$"
          # The metric we want the individual pod to match
          as: "dfs_requests_per_second"

I wanted to write a shell script that adds everything from -seriesQuery and below replacing values such as the proxy="value" and the [2m] metric interval wherever that assignment variable exists. And for each key / value pair, it would add the same block with indents. Kind of like this

pod names: [pod1, pod2] interval: [2m, 3m]

      - seriesQuery: 'haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="pod1"}'
        metricsQuery: 'sum(rate(haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="pod1"}[2m])) by (<<.GroupBy>>)'
        resources:
          overrides:
            namespace: {resource: "namespace"}
        name:
          matches: "^(.*)_total$"
          # The metric we want the individual pod to match
          as: "dfs_requests_per_second"
          - seriesQuery: 'haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="pod2"}'
        metricsQuery: 'sum(rate(haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="pod2"}[3m])) by (<<.GroupBy>>)'
        resources:
          overrides:
            namespace: {resource: "namespace"}
        name:
          matches: "^(.*)_total$"
          # The metric we want the individual pod to match
          as: "dfs_requests_per_second"

And would append that to everything below

apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: prometheus-adapter
    meta.helm.sh/release-namespace: prometheus
  labels:
    app.kubernetes.io/component: metrics
    app.kubernetes.io/instance: prometheus-adapter
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: prometheus-adapter
    app.kubernetes.io/part-of: prometheus-adapter
    app.kubernetes.io/version: v0.10.0
    helm.sh/chart: prometheus-adapter-3.4.1
  name: prometheus-adapter
  namespace: prometheus
data:
  config.yaml: |
    rules:

1 Answer 1

1

It would be preferable to use a template engine that's YAML-aware but, provided that your pod names and intervals don't contain any problematic character, then you can generate your config file easily with bash:

#!/bin/bash

pods=( pod1 pod2 )
intervals=( 2m 3m )

cat <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: prometheus-adapter
    meta.helm.sh/release-namespace: prometheus
  labels:
    app.kubernetes.io/component: metrics
    app.kubernetes.io/instance: prometheus-adapter
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: prometheus-adapter
    app.kubernetes.io/part-of: prometheus-adapter
    app.kubernetes.io/version: v0.10.0
    helm.sh/chart: prometheus-adapter-3.4.1
  name: prometheus-adapter
  namespace: prometheus
data:
  config.yaml: |
    rules:
EOF

for i in "${!pods[@]}"
do
cat <<EOF
      - seriesQuery: 'haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="${pods[i]}"}'
        metricsQuery: 'sum(rate(haproxy_backend_http_requests_total{namespace!="", pod!="", proxy="${pods[i]}"}[${intervals[i]}])) by (<<.GroupBy>>)'
        resources:
          overrides:
            namespace: {resource: "namespace"}
        name:
          matches: "^(.*)_total$"
          # The metric we want the individual pod to match
          as: "dfs_requests_per_second"
EOF
done
Sign up to request clarification or add additional context in comments.

1 Comment

Oh that is way easier that what I was trying to do with arrays. I'm really not good at shell scripts. Primarily Python and GO. I'll mess with this some more. I was trying to use multiple arrays and for loops, and I totally forgot about using EOF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.