Bird
0
0

You want to create a Helm chart template that sets the container port only if service.port is defined in values.yaml. Which template snippet correctly implements this conditional logic?

hard📝 Best Practice Q15 of 15
Kubernetes - Helm Package Manager
You want to create a Helm chart template that sets the container port only if service.port is defined in values.yaml. Which template snippet correctly implements this conditional logic?
A{{- if .Values.service.port }} containerPort: "{{ .Values.service.port }}" {{- else }} containerPort: 80 {{- end }}
B{{- if .service.port }} containerPort: {{ .service.port }} {{- end }}
C{{- if .Values.service.port }} containerPort: {{ .Values.service.port }} {{- end }}
D{{- if .Values.service.port != null }} containerPort: {{ .Values.service.port }} {{- end }}
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct value reference

    Use .Values.service.port to access the port value from values.yaml.
  2. Step 2: Use proper conditional syntax

    Helm templates use {{- if .Values.service.port }} to check if the value exists and is non-empty.
  3. Step 3: Evaluate options

    {{- if .Values.service.port }} containerPort: {{ .Values.service.port }} {{- end }} correctly uses the conditional and outputs the port only if defined. {{- if .service.port }} containerPort: {{ .service.port }} {{- end }} misses .Values. {{- if .Values.service.port }} containerPort: "{{ .Values.service.port }}" {{- else }} containerPort: 80 {{- end }} adds an else block which is not requested. {{- if .Values.service.port != null }} containerPort: {{ .Values.service.port }} {{- end }} uses invalid syntax (!= null is not valid in Helm templates).
  4. Final Answer:

    {{- if .Values.service.port }} containerPort: {{ .Values.service.port }} {{- end }} -> Option C
  5. Quick Check:

    Use if .Values.key for conditionals [OK]
Quick Trick: Check existence with if .Values.key, no need for != null [OK]
Common Mistakes:
  • Omitting .Values prefix
  • Using invalid comparison operators
  • Adding unnecessary else blocks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kubernetes Quizzes