# Istio Helm Chart 详解 - Ingress

> 这是《Istio Helm Chart 详解》系列的第三篇。开始逐个 Chart 进行阅读。

## 前言

全局变量之后，接下来就是 Ingress 一节了，这个 Chart 只是个兼容选项，为 Istio 提供了传统 Kubernetes Ingress 的功能。`ingress.enabled` 变量用于在 `requirements.yaml` 中控制该 Chart 是否启用。

## Chart.yaml

元数据文件，无需赘述。

## autoscale.yaml

该文件用于处理该模块的 [HPA 对象](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)。引用变量如下：

- **ingress.autoscaleMin**：水平伸缩的 Pod 数量下限，另外该变量被赋值的情况下才会渲染 HPA 对象。
- **ingress.autoscaleMax**：水平伸缩的 Pod 数量上限。
- **Release.Namespace**：HPA 对象所在的命名空间，使用 Istio 同一值。Release 是 Helm 的保留字，用于标识该 Release 所在的命名空间。

代码中我们看到，`targetAverageUtilization` 设置为固定值 `80`。

## serviceaccount.yaml

这个模板用于为 Pod 生成 Service Account——`istio-ingress-service-account`。

其中引用变量：

- **global.imagePullSecrets**：全局变量定义的数组，提供给 Service Account，在拉取镜像时使用。
- **Chart** 和 **Release** 都是 Helm 的内置对象。

## clusterrole.yaml 和 clusterrolebinding.yaml

Kubernetes [RBAC](https://kubernetes.io/docs/reference/access-authn-authz/rbac/) 系统使用，这是一个集群范围内生效的 ClusterRole，声明了 Ingress 中需要的两组权限，并最终和 Service Account 进行绑定：

- 对于 extensions.thirdpartyresources 和 extensions.ingresses 对象的读写权限。
- 对 configmaps、pods、endpoints 和 services 对象的读取权限。

其中引用变量：

- **istio.name**：定义在 istio 的 `_helpers.tpl` 中，如果没有使用 `nameOverride` 进行覆盖的话，会使用 Chart 名称。
- **Chart** 和 **Release** 都是 Helm 的内置对象。

## deployment.yaml

这一模板用于生成 Deployment，主体部分和 Ingress Gateway 类似，使用的都是 `istio/proxyv2` 镜像，[参数稍有区别](https://istio.io/docs/reference/commands/pilot-agent/#pilot-agent-proxy)：

- **istio-proxy**：`proxy sidecar`
- **ingress**：`proxy ingress`
- **gateways**：`proxy router`

其中引用变量：

- **istio.name**：定义在 istio 的 `_helpers.tpl` 中，如果没有使用 `nameOverride` 进行覆盖的话，会使用 Chart 名称。
- 使用 Chart 和 Release 数据生成标签。这里可以看到，固定使用 `istio: ingress` 提供给 Gateway  Selector 进行选择。
- **global.hub** 和 **global.tag**：生成镜像地址。
- **replicaCount**：控制 Pod 数量。
- **global.imagePullPolicy**：拉取策略。
- **global.priorityClassName**：[`PriorityClass`](https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/#priorityclass)
- **global.proxy.envoyStatsd.enabled**：如果启用，则加入 `--statsdUdpAddress` 参数。
- **global.controlPlaneSecurityEnabled**：根据这一设置确定 `controlPlaneAuthPolicy` 设置：

~~~yaml
# 如果启用
- --controlPlaneAuthPolicy
- MUTUAL_TLS
- --discoveryAddress
- istio-pilot:15005
# 如果没启用
- --controlPlaneAuthPolicy
- NONE
- --discoveryAddress
- istio-pilot:8080
~~~

- **resources** 和 **globale.defaultResources**：优先使用 Chart 自身的资源定义，如果没有则使用缺省定义。
- **nodeaffinity**：包含节点亲和性定义模板。

另外这里还包含了对两个 secret 的可选加载：

- **istio.istio-ingress-service-account**：用于 RBAC 的 Service Account 证书。
- **istio-ingress-certs**：提供 https 服务时使用这一组证书。

## service.yaml

这个模板用来为 Ingress 生成服务。

引用变量包括：

- **istio.name**：定义在 istio 的 `_helpers.tpl` 中，如果没有使用 `nameOverride` 进行覆盖的话，会使用 Chart 名称。
- 使用 Chart 和 Release 数据生成标签。
- **service.loadBalancerIP**：如果指定了 IP，则使用指定 IP。
- **service.type**：如果没有负载均衡支持，可以考虑采用 NodePort 方式。
- **service.annotations**：可以设置一系列的键值对，通过遍历的形式为 Service 对象生成注解。
- **service.ports**：可以定义其它的非标准端口，以数组形式供 Helm 进行遍历，例如：

~~~yaml
- port: 80
  name: http
  nodePort: 32000
- port: 443
  name: https
~~~

## 常用链接

1. Helm 内置对象：[https://docs.helm.sh/chart_template_guide/#built-in-objects](https://docs.helm.sh/chart_template_guide/#built-in-objects)
