You want to create a Helm chart that conditionally creates a Service only if service.enabled is true in values.yaml. Which template snippet correctly implements this?
hard📝 Workflow Q8 of 15
Kubernetes - Helm Package Manager
You want to create a Helm chart that conditionally creates a Service only if service.enabled is true in values.yaml. Which template snippet correctly implements this?
A{{- if .service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }}
B{{- with .Values.service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }}
C{{- if .Values.service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }}
D{{- if .Values.service.enabled == true }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }}
Step-by-Step Solution
Solution:
Step 1: Identify correct value reference and conditional
Use .Values.service.enabled with if to conditionally render.
Step 2: Evaluate options
{{- if .Values.service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }} uses correct syntax; {{- with .Values.service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }} uses with which changes context; {{- if .service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }} misses .Values; {{- if .Values.service.enabled == true }}
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
{{- end }} uses invalid == operator.
Final Answer:
{{- if .Values.service.enabled }} -> Option C
Quick Check:
Use if with .Values for conditionals [OK]
Quick Trick:Use if .Values.key for conditional template blocks [OK]
Common Mistakes:
Omitting .Values prefix
Using with which changes context unexpectedly
Overcomplicating condition with eq
Master "Helm Package Manager" in Kubernetes
9 interactive learning modes - each teaches the same concept differently