【问题标题】:Adding helm hooks to RBAC resources将 helm hooks 添加到 RBAC 资源
【发布时间】:2021-10-18 01:02:49
【问题描述】:

我想创建一个post-install,post-upgradehelm 钩子(更准确地说是Job)。

这将需要以下RBAC资源(我已经添加了对应的helm-hook注解)

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: "{{ .Release.Name }}-post-install-role"
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded,hook-failed
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: "{{ .Release.Name }}-post-install-rolebinding"
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded,hook-failed
subjects:
  - kind: ServiceAccount
    namespace: {{ .Release.Namespace }}
    name: "{{ .Release.Name }}-post-install-sa"
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: "{{ .Release.Name }}-post-install-role"
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: "{{ .Release.Name }}-post-install-sa"
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded,hook-failed

在我对应的Job 规范中:

  annotations:
    "helm.sh/hook": post-install,post-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation

...
serviceAccountName: "{{ .Release.Name }}-post-install-sa"

尽管通过将 pre- 添加到 RBAC 资源,我会确保这些是在实际的 Job 之前创建的,这是 post- 的事情。

通过还将hook-delete-policy 设置为before-hook-creation,hook-succeeded,hook-failed,这些也将在所有情况下(无论作业失败还是成功)都将被删除,以避免出于安全考虑让它们闲置。

但是Job创建错误提示找不到ServiceAccount

error looking up service account elastic/elastic-stack-post-install-sa: serviceaccount "elastic-stack-post-install-sa" not found

这是为什么呢?

【问题讨论】:

  • PGS的解决方案对您有帮助吗?
  • 还没来得及测试。将通过在此处发布并返回;听起来很有希望
  • 你有什么进展吗?
  • 您好,您找到这种行为的原因了吗?通过将升级后添加到 rbac 它可以工作!即使我面临同样的问题。对于升级后作业,虽然 rbac 是在升级前创建的,但它是不可访问的。

标签: kubernetes kubernetes-helm


【解决方案1】:

尝试使用挂钩权重来确保确定性顺序。Helm 首先加载权重最低的挂钩(从负到正)

"helm.sh/hook-weight": "0"

示例: 创建权重最低的服务帐号。

【讨论】:

    【解决方案2】:

    正如 PGS 建议的那样,“helm.sh/hook-weight”注释是这里的解决方案。

    重要提示:

    1. Hook 权重可以是正数、零或负数,但必须表示为 字符串示例: "helm.sh/hook-weight": "-5" (注意:双引号内的-5)

    2. 当 Helm 开始执行特定种类的钩子时,它会按升序对这些钩子进行排序。

    挂钩重量确保如下:

    1. 按正确的权重顺序执行(从负到正升序)
    2. 互相阻止(对你的场景很重要)
    3. 所有区块主 K8s 资源正在启动

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多