我有一个项目要在
kube-system
命名空间中创建一个变异的webhook,它需要排除webhook服务器部署命名空间。
但是
kube-system
命名空间已经创建。如何使用 Helm 将所需的标签附加到它?
Helmfile 提供了非常简洁的钩子:
releases:
- name: istio-ingress
namespace: istio-ingress
chart: istio/gateway
wait: true
hooks:
- events:
- presync
showlogs: true
command: sh
args:
- -c
- "kubectl create namespace istio-ingress --dry-run=client -o yaml | kubectl apply -f -"
- events:
- presync
showlogs: true
command: sh
args:
- -c
- "kubectl label --dry-run=client -o yaml --overwrite namespace istio-ingress istio-injection=enabled | kubectl apply -f -"
由于
kube-system
命名空间是Kubernetes的核心部分(每个集群都预装了它,一些核心组件运行在那里)Helm无法管理它。
您可以做的一些可能的事情:
kube-system
作为代码中的特例。kubectl label namespace
。kubectl
命令(例如,如果你有一个安装 webhook 的 Jenkins 构建,也让它设置标签)。由于 helm 不支持直接管理命名空间(见:Helm 3 不创建命名空间#5753),“正确”的方法是使用 chart hook:
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "chart.serviceAccountName" . }}
labels:
{{- include "chart.labels" . | nindent 4 }}
annotations:
{{- toYaml .Values.serviceAccount.annotations | nindent 4 }}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: label-ns
rules:
- apiGroups: [""]
resources: ["namespaces"]
resourceNames: [{{ .Release.Namespace }}]
verbs: ["get", "patch"]
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ .Release.Namespace }}:label-ns
subjects:
- kind: ServiceAccount
name: {{ include "chart.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
roleRef:
kind: Role
name: label-ns
apiGroup: rbac.authorization.k8s.io
serviceAccountName
:apiVersion: batch/v1
kind: Job
metadata:
name: label-ns
labels:
app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
app.kubernetes.io/instance: {{ .Release.Name | quote }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-delete-policy": hook-succeeded
spec:
template:
metadata:
labels:
app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
app.kubernetes.io/instance: {{ .Release.Name | quote }}
helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
spec:
restartPolicy: Never
serviceAccountName: {{ include "chart.serviceAccountName" . }}
containers:
- name: label-ns
image: "bitnami/kubectl:latest"
command:
- kubectl
- label
- ns
- {{ .Release.Namespace }}
- foo=bar