I am trying to set terminationgraceperiodseconds in my deployment.yaml. I am performing two steps
checking if lifecycle hook is enabled and shutdowndelay is present in values.yaml then add value of delay in sleep to terminationgraceperiod
{{- $delay := hasKey .Values "shutdownDelay" | ternary .Values.shutdownDelay 30 }} {{- $graceperiod := hasKey .Values.service "terminationGracePeriodSeconds" | ternary .Values.service.terminationGracePeriodSeconds 120 }} {{- if or .Values.lifecycleHooks.enabled (gt ( int $delay ) 0 )}} terminationGracePeriodSeconds: {{ add $graceperiod $delay }} {{- else }} terminationGracePeriodSeconds: {{ int $graceperiod }} {{- end }}
but everytime i am running the code it gives me 150 no matter what the value of lifecycle enable is
values.yaml
#shutdownDelay: 40
lifecycleHooks:
enabled: true
service:
terminationGracePeriodSeconds: 120
What am I doing wrong?
CodePudding user response:
You add the value if lifecycleHooks.enabled
is true or if $delay
is positive
{{- if or .Values.lifecycleHooks.enabled (gt ( int $delay ) 0 )}}
{{/* ^^ */}}
It sounds like you do not want to do the addition if the enabled
flag is false; you only want to do it if the flag is true and $delay
is positive
{{- if and .Values.lifecycleHooks.enabled (gt ( int $delay ) 0 )}}
{{/* ^^^ */}}
If you believe the delay will never be negative, then you can simplify this by removing the gt 0
case; adding zero to something leaves it unchanged.
{{- if .Values.lifecycleHooks.enabled }}
terminationGracePeriodSeconds: {{ add $graceperiod $delay }}
{{- else }}
terminationGracePeriodSeconds: {{ int $graceperiod }}
{{- end }}
This is also another place you can do tricks with ternary
, again taking advantage of adding zero being a no-op. You could set another variable to be a copy of $delay
if the enabled
flag is true, or zero if not, and then it's safe to just add
it without another conditional.
{{- $extraDelay := .Values.lifecycleHooks.enabled | ternary $delay 0 }}
terminationGracePeriodSeconds: {{ add $graceperiod $extraDelay }}