在Istio VirtualService中配置HTTP重试时Helm range循环失败
Istio金丝雀部署Helm模板HTTP重试配置失败解决
问题描述
在金丝雀部署场景下,可正常添加多个Destination,但自定义Helm Chart配置HTTP重试时触发YAML解析错误,无法完成遍历。需求是将重试配置与每个Destination绑定,需调整模板的遍历逻辑。
错误的Helm模板代码
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: {{ .Values.virtualservice.name }} namespace: {{ .Values.namespace }} spec: hosts: - {{ .Values.virtualservice.hosts }} gateways: - {{ .Values.virtualservice.gateways }} http: - route: {{- range $key, $value := .Values.destination }} - destination: host: {{ $value.host }} subset: {{ $value.subset }} weight: {{ $value.weight }} retries: attempts: {{ $value.retries.attempts }} perTryTimeout: {{ $value.retries.perTryTimeout }} retryOn: {{ $value.retries.retryOn }} timeout: {{ $value.retries.timeout }} {{- end }}
错误日志
$ helm install asm-helm ./asm-svc-helm-chart -f values.yaml --dry-run Error: INSTALLATION FAILED: YAML parse error on asmvrtsvc/templates/retry-svc.yaml: error converting YAML to JSON: yaml: line 21: did not find expected key
版本信息
$ kubectl version --short Client Version: v1.24.0 Kustomize Version: v4.5.4 Server Version: v1.22.12-gke.300 $ helm version v3.9.4+gdbc6d8e
解决方案及修正后的模板
错误原因
原模板的YAML结构错误:retries和timeout被错误放置在route数组的元素列表中,不符合Istio VirtualService的结构规范。retries是HTTP规则(http数组元素)的属性,而非route数组元素的属性。若需为每个Destination绑定独立的重试配置,需将每个Destination包装为单独的HTTP规则。
修正后的Helm模板
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: {{ .Values.virtualservice.name }} namespace: {{ .Values.namespace }} spec: hosts: - {{ .Values.virtualservice.hosts }} gateways: - {{ .Values.virtualservice.gateways }} http: {{- range $key, $value := .Values.destination }} - route: - destination: host: {{ $value.host }} subset: {{ $value.subset }} weight: {{ $value.weight }} retries: attempts: {{ $value.retries.attempts }} perTryTimeout: {{ $value.retries.perTryTimeout }} retryOn: {{ $value.retries.retryOn }} timeout: {{ $value.retries.timeout }} {{- end }}
说明
调整后,range循环遍历destination列表时,为每个Destination生成一个独立的http规则条目,每个条目包含对应的route配置和专属的retries、timeout配置,既满足了重试与Destination绑定的需求,也符合YAML语法和Istio资源结构要求。
内容的提问来源于stack exchange,提问作者Ajinkya Bhabal
相关产品推荐
相关产品推荐

