资源组

使用选择器对不同的 PipelineRuns/TaskRuns 集合应用不同的清理策略。

重要: 选择器仅适用于 namespace 级别的 ConfigMapstekton-pruner-namespace-spec)。全局 ConfigMaps 中的选择器会被 pruner 忽略。

WARNING

namespace 级别的 ConfigMaps 不属于 TektonConfig 生命周期的一部分。如果你之后需要备份或恢复 Tekton 配置,请单独保存这些 ConfigMaps。

工作原理

  • 按标签或注解匹配 PipelineRuns/TaskRuns
  • 首个匹配生效:按顺序评估各组
  • 回退:未匹配的资源使用 namespace 或全局默认值
  • 位置:必须位于 namespace ConfigMap 中,而不是全局 ConfigMap 中

选择器类型

Label selectors:

apiVersion: v1
kind: ConfigMap
metadata:
  name: tekton-pruner-namespace-spec
  namespace: my-app
  labels:
    app.kubernetes.io/part-of: tekton-pruner
    pruner.tekton.dev/config-type: namespace
data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchLabels:
            environment: production
            tier: frontend
        ttlSecondsAfterFinished: 604800
        successfulHistoryLimit: 10

Annotation selectors:

data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchAnnotations:
            tekton.dev/release: "true"
        ttlSecondsAfterFinished: 2592000

混合 selectors(标签和注解都必须匹配):

data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchLabels:
            app: myapp
          matchAnnotations:
            critical: "true"
        successfulHistoryLimit: 50

常见模式

按 Pipeline 类型:

apiVersion: v1
kind: ConfigMap
metadata:
  name: tekton-pruner-namespace-spec
  namespace: my-app
  labels:
    app.kubernetes.io/part-of: tekton-pruner
    pruner.tekton.dev/config-type: namespace
data:
  ns-config: |
    ttlSecondsAfterFinished: 3600
    pipelineRuns:
      - selector:
          matchLabels:
            pipeline-type: build
        ttlSecondsAfterFinished: 300
      - selector:
          matchLabels:
            pipeline-type: test
        ttlSecondsAfterFinished: 3600
      - selector:
          matchLabels:
            pipeline-type: release
        ttlSecondsAfterFinished: 604800
        successfulHistoryLimit: 20

按环境:

data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchLabels:
            env: dev
        ttlSecondsAfterFinished: 300
      - selector:
          matchLabels:
            env: staging
        ttlSecondsAfterFinished: 86400
      - selector:
          matchLabels:
            env: prod
        ttlSecondsAfterFinished: 604800

按关键性:

data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchLabels:
            critical: "true"
        ttlSecondsAfterFinished: 2592000
        successfulHistoryLimit: 50
      - selector:
          matchLabels:
            critical: "false"
        ttlSecondsAfterFinished: 3600
        successfulHistoryLimit: 3

顺序很重要

首个匹配生效 - 按从最具体到最不具体的顺序排列 selectors:

data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchLabels:
            env: prod
            critical: "true"
        ttlSecondsAfterFinished: 2592000
      - selector:
          matchLabels:
            env: prod
        ttlSecondsAfterFinished: 604800
      - selector:
          matchLabels:
            app: myapp
        ttlSecondsAfterFinished: 3600

最佳实践

  1. 使用 namespace ConfigMaps 来配置基于 selector 的分组
  2. 按从最具体到最不具体的顺序排列 selectors(首个匹配生效)
  3. 使用一致的标签appcomponentenvtier
  4. 在 selectors 上方添加注释 来记录各组用途
  5. 在生产环境前先使用示例运行进行测试

高级配置

多层应用

data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchLabels:
            tier: frontend
        ttlSecondsAfterFinished: 604800
        successfulHistoryLimit: 10
      - selector:
          matchLabels:
            tier: backend
        ttlSecondsAfterFinished: 1209600
        successfulHistoryLimit: 15
      - selector:
          matchLabels:
            tier: database
        ttlSecondsAfterFinished: 2592000
        successfulHistoryLimit: 30

发布类型

data:
  ns-config: |
    pipelineRuns:
      - selector:
          matchLabels:
            release-type: feature
        ttlSecondsAfterFinished: 604800
      - selector:
          matchLabels:
            release-type: hotfix
        ttlSecondsAfterFinished: 2592000
      - selector:
          matchLabels:
            release-type: major
        ttlSecondsAfterFinished: 7776000

为你的 Pipelines 添加标签

为 PipelineRuns 添加标签以便分组:

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  generateName: my-pipeline-
  labels:
    pipeline-type: release
    env: prod
    critical: "true"
spec:
  pipelineRef:
    name: my-pipeline

相关内容