任务是范围内收集 worker ,如果当前 worker 具有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为目标的 list ,其目标对象为workerWithAutoscaling,但实际输出完全为空。

最佳答案

您可以使用{{- 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

关于kubernetes - 范围范围内的{{If}}子句看不到值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57494451/

10-10 04:29