Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable ports based on the config value in the kubernates #12965

Open
nagraj321 opened this issue Apr 19, 2024 · 15 comments
Open

enable ports based on the config value in the kubernates #12965

nagraj321 opened this issue Apr 19, 2024 · 15 comments

Comments

@nagraj321
Copy link

What happened?

I have the following daemonset.yaml my requirement is we need to enable the debug port only if services-debug is set to true.

apiVersion: apps/v1
   kind: DaemonSet
   metadata:
     name: your-daemonset
   spec:
     selector:
       matchLabels:
         app: your-app
     template:
       metadata:
         labels:
           app: your-app
       spec:
         containers:
         - name: your-container
           image: your-image
           env:
             - name: SERVICES_DEBUG
               valueFrom:
                 configMapKeyRef:
                   name: test-config
                   key: services-debug
                   optional: true
           ports:
             - name: http-port
               containerPort: 80  # Adjust as needed
               hostPort: 80        # Adjust as needed
              - name: debug-port
               containerPort: 81  # Adjust as needed
               hostPort: 81        # Adjust as needed

i have tried the following option

{{- if eq (index .Values.configMap.test-config "services-debug") "true" }}
- name: debug-port
  containerPort: {{ .Values.image.debug-port }}
  hostPort: {{ .Values.image.debug-port }}
{{- end }}

but when we use it helm is throwing

Error: parse error at (test/templates/daemonset.yaml:59): bad character U+002D '-'

also tried

{{- if eq (index .Values.configMap "test-config" "services-debug") "true" }}
- name: debug-port
  containerPort: {{ .Values.image.debug-port }}
  hostPort: {{ .Values.image.debug-port }}
{{- end }}

but giving

templates/daemonset.yaml:59:20: executing "test/templates/daemonset.yaml" at <index .Values.configMap "test-config" "services-debug">: error calling index: index of untyped nil

What did you expect to happen?

it should procced without any errors

How can we reproduce it (as minimally and precisely as possible)?

crated a config map and key which contains the hyphen in it.

Try to use the configmap key in the if condition.

Kubernetes version

$ kubectl version
Client Version: v1.29.0+rke2r1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.29.0+rke2r1

OS version

# On Linux:
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$ uname -a
Linux pamutil46-32 6.1.73+pam01 #1 SMP PREEMPT_DYNAMIC Thu Jan 18 10:16:13 PST 2024 x86_64 GNU/Linux

Output of helm version:
version.BuildInfo{Version:"v3.4.1", GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", GitTreeState:"clean", GoVersion:"go1.14.11"}

@sabre1041
Copy link
Contributor

The issue is that a dash is included in {{ .Values.image.debug-port }} which is not supported. If you change it to {{ index .Values "image" "debug-port" }}, Helm will no longer produce en error

@nchitimilla
Copy link

@sabre1041

we have changed the "debug-port" in the values.ymal to "debugport" and updated the daemonset.yaml respectively. But still getting the same error.

{{- if eq (index .Values.configMap.test-config "services-debug") "true" }}

  • name: debug-port
    containerPort: {{ .Values.image.debugport }}
    hostPort: {{ .Values.image.debugport }}
    {{- end }}

Error: parse error at (test/templates/daemonset.yaml:59): bad character U+002D '-'

{{- if eq (index .Values.configMap "test-config" "services-debug") "true" }}

  • name: debug-port
    containerPort: {{ .Values.image.debugport }}
    hostPort: {{ .Values.image.debugport }}
    {{- end }}

templates/daemonset.yaml:59:20: executing "test/templates/daemonset.yaml" at <index .Values.configMap "test-config" "services-debug">: error calling index: index of untyped nil

@sabre1041
Copy link
Contributor

 {{- if eq (index .Values.configMap "test-config" "services-debug" | toString) "true" }}

Values:

configMap:
  test-config:
    services-debug: false
image:
  debug-port: 81

@nchitimilla
Copy link

nchitimilla commented Apr 23, 2024

Still getting the same error with {{- if eq (index .Values.configMap "test-config" "services-debug" | toString) "true" }}

test/templates/daemonset.yaml:59:20: executing "test/templates/daemonset.yaml" at <index .Values.configMap "test-config" "services-debug">: error calling index: index of untyped nil

Note: the we dont have the services-debug value in values.yaml instead we have a config map where we define services-debug value.

e.g.

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Chart.Name }}
data:
    services-debug: true

This is deployed a test-config

@sabre1041
Copy link
Contributor

@nchitimilla Can you please share as much information as possible. Your values.yaml and template files

@nchitimilla
Copy link

Provided all the necessary filles

daemonset.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Chart.Name }}
annotations:
reloader.stakater.com/match: "true"
data:
services-debug: {{ .Values.servicesdebug | quote }}

values.yaml
servicesdebug: false

daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: {{ .Chart.Name }}
annotations:
reloader.stakater.com/auto: "true"
spec:
hostNetwork: yes
containers:
ports:
- name: http-port
containerPort: {{ .Values.image.porthttp }}
hostPort: {{ .Values.image.porthttp }}
- name: debug-port
containerPort: {{ .Values.image.debugport }}
hostPort: {{ .Values.image.debugport }}

values.yaml
image:
porthttp: 8080
debugport: 8081

@sabre1041
Copy link
Contributor

@nchitimilla This structure looks totally different than the original issue.

Are you testing against the servicesdebug property in the values.yaml file and then will enable the debug-port based upon it?

@nagraj321
Copy link
Author

@nchitimilla This structure looks totally different than the original issue.

Are you testing against the servicesdebug property in the values.yaml file and then will enable the debug-port based upon it?

Yes. If the servicesdebug is true we would like to enable the debug port

@sabre1041
Copy link
Contributor

@nagraj321 @nchitimilla why are there 2 values.yaml files? A since this is a relatively flat structure, you can just perform a simple check and it should work without issue. You can remove all of the complex index logic

@nagraj321
Copy link
Author

First daemons.yaml and values.yaml are deployed as a one service i.e. test-config which will have the common details required by the other services.

Other daemonset.yaml and values.yaml are related to some other service.

We can’t club together as this test-config service is being used by nearly 10 other services.

Now we need to look up if we have a debug enabled in the test-config service then we need to enable the debug port.

@sabre1041
Copy link
Contributor

so we can assume there is a ConfigMap where you want to look up a value already on the cluster?

@nchitimilla
Copy link

so we can assume there is a ConfigMap where you want to look up a value already on the cluster?

Yes

@sabre1041
Copy link
Contributor

sabre1041 commented Apr 29, 2024

You would want to use the lookup filter

@nagraj321
Copy link
Author

You would want to use the lookup filter

If you can give some example or link to documentation will be helpful.

@sabre1041
Copy link
Contributor

You would want to use the lookup filter

If you can give some example or link to documentation will be helpful.

https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants