范围范围内的{{If}}子句看不到值

时间:2019-08-14 12:12:22

标签: kubernetes kubernetes-helm

任务是确定工作人员集合的范围,如果当前工作人员具有autoscaling.enabled = true,则为其创建一个hpa。

我试图将.autoscaling.enabled与“ true”进行比较,但返回“调用eq:不兼容的类型进行比较时出错”。 Here人说这实际上意味着.autoscaling.enabled为nil。因此,{{if .autoscaling.enabled}}不会以某种方式看到该变量并假定它不存在。

值:

...
workers:
  - name: worker1
    command: somecommand1
    memoryRequest: 500Mi
    memoryLimit: 1400Mi
    cpuRequest: 50m
    cpuLimit: 150m
    autoscaling:
      enabled: false
  - name: worker2
    command: somecommand2
    memoryRequest: 512Mi
    memoryLimit: 1300Mi
    cpuRequest: 50m
    cpuLimit: 150m
    autoscaling:
      enabled: false
  - name: workerWithAutoscaling
    command: somecommand3
    memoryRequest: 600Mi
    memoryLimit: 2048Mi
    cpuRequest: 150m
    cpuLimit: 400m
    autoscaling:
      enabled: true
      minReplicas: 1
      maxReplicas: 5
      targetCPUUtilization: 50
      targetMemoryUtilization: 50
...

模板:

...
{{- range .Values.workers }}
{{- if .autoscaling.enabled }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  labels:
    ...
  name: "hpa-{{ .name }}-{{ $.Realeas.Name }}"
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .name }}
  minReplicas: {{ .minReplicas }}
  maxReplicas: {{ .maxReplicas }}
  metrics:
{{- with .targetCPUUtilization}}
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: {{ . }}
{{- end }}
{{- with .targetMemoryUtilization}}
    - type: Resource
      resource:
        name: memory
        targetAverageUtilization: {{ . }}
{{- end }}
---
{{- end }}
{{- end }}

我希望针对一个hpa的清单针对workerWithAutoscaling,但实际输出完全为空。

1 个答案:

答案 0 :(得分:2)

您可以使用{{- range .Values.workers }}{{- if .autoscaling.enabled }}。您没有得到任何值,因为.minReplicas.maxReplicas等在.autoscaling范围内。

请参见Modifying scope using with

添加{{- with .autoscaling}}将解决此问题。

{{- range .Values.workers }}
{{- if .autoscaling.enabled }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  labels:
  name: "hpa-{{ .name }}-{{ $.Release.Name }}"
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .name }}
{{- with .autoscaling}}
  minReplicas: {{ .minReplicas }}
  maxReplicas: {{ .maxReplicas }}
  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: {{ .targetCPUUtilization}}
    - type: Resource
      resource:
        name: memory
        targetAverageUtilization: {{ .targetMemoryUtilization}}
{{- end }}
{{- end }}
{{- end }}

helm template .

---
# Source: templates/hpa.yaml

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  labels:
  name: "hpa-workerWithAutoscaling-release-name"
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: workerWithAutoscaling
  minReplicas: 1
  maxReplicas: 5
  metrics:
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: 50
    - type: Resource
      resource:
        name: memory
        targetAverageUtilization: 50