N Avi
N Avi

Reputation: 1

Use ENV variables in flux helmrelease as variable substitution

I am looking for a solution to update the env variable in flux helmrelease. I have tried multiple ways but nothing is working. I tried passing env as configmaps and tried to pull it into helmrelease for installing prometheus but it didn't work. Below are the changes I made:

kubectl create configmap my-app-config -n monitoring \
    --from-literal="server_number=${POD_HOSTNAME}" \
    --from-literal="environment=${cloud_environment}"

---
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
  name: kube-prometheus-stack
  namespace: flux-system
spec:
  releaseName: kube-prometheus-stack
  chart:
    spec:
      chart: kube-prometheus-stack
      reconcileStrategy: ChartVersion
      sourceRef:
        kind: HelmRepository
        name: prometheus-community
  interval: 10s
  targetNamespace: monitoring
  valuesFrom:
    - kind: ConfigMap
      name: my-app-config
      valuesKey: server_number
    - kind: ConfigMap
      name: my-app-config
      valuesKey: environment
  values:
    alertmanager:
      enabled: false
    prometheus:
      prometheusSpec:
        externalLabels:
          cluster: "{{ .Values.server_number | quote }}"
          environment: "{{ .Values.environment | quote }}"

Error: "could not resolve ConfigMap chart values reference 'monitoring/my-app-config' with key 'location_number': error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type chartutil.Values"

I also tried passing them as follows:
---
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
  name: kube-prometheus-stack
  namespace: flux-system
spec:
  releaseName: kube-prometheus-stack
  chart:
    spec:
      chart: kube-prometheus-stack
      reconcileStrategy: ChartVersion
      sourceRef:
        kind: HelmRepository
        name: prometheus-community
  interval: 10s
  targetNamespace: monitoring
  valuesFrom:
    - kind: ConfigMap
      name: my-app-config
      valuesKey: server_number
    - kind: ConfigMap
      name: my-app-config
      valuesKey: environment
  values:
    alertmanager:
      enabled: false
    prometheus:
      prometheusSpec:
        externalLabels:
          cluster: ${server_number}
          environment: ${environment}

Also tried

prometheus:
  prometheusSpec:
    externalLabels:
      cluster: $(kubectl get secret my-app-config -n monitoring -o jsonpath="{.data.cluster_name}" | base64 --decode)
      environment: $(kubectl get secret my-app-config -n monitoring -o jsonpath="{.data.cloud_environment}" | base64 --decode)

this approach didn't work because Helm and Kubernetes YAML files do not support inline command execution. I am expected the cluster and environment name to get updated with the env variable Nothing seems to be woking. Please suggest some solution

Upvotes: 0

Views: 185

Answers (0)

Related Questions