搜索

2.3. 步骤资源定义

download PDF

您可以包括资源的定义,如构建策略中所有步骤对 CPU、内存和磁盘用量的限制。对于具有多个步骤的策略,步骤可能需要的资源多于其他步骤。作为策略管理员,您可以定义对每个步骤的最佳资源值。

例如,您可以使用相同的步骤安装策略,但在集群中使用不同的名称和步骤资源,以便用户可以创建具有较小的或更大资源要求的构建。

2.3.1. 使用不同资源的策略

定义多个同一策略类型,但资源的不同限制。以下示例使用与为资源定义的小和中等限制相同的 buildah 策略。这些示例提供了对步骤资源定义的更多控制。

2.3.1.1. Buildah 策略具有小限制

buildah 策略定义带有小资源限制的 spec.steps[].resources 字段,如下例所示:

示例:带有小限制的 buildah 策略

apiVersion: shipwright.io/v1beta1
kind: ClusterBuildStrategy
metadata:
  name: buildah-small
spec:
  steps:
    - name: build-and-push
      image: quay.io/containers/buildah:v1.31.0
      workingDir: $(params.shp-source-root)
      securityContext:
        capabilities:
          add:
          - "SETFCAP"
      command:
        - /bin/bash
      args:
        - -c
        - |
          set -euo pipefail
          # Parse parameters
        # ...
        # That's the separator between the shell script and its args
        - --
        - --context
        - $(params.shp-source-context)
        - --dockerfile
        - $(build.dockerfile)
        - --image
        - $(params.shp-output-image)
        - --build-args
        - $(params.build-args[*])
        - --registries-block
        - $(params.registries-block[*])
        - --registries-insecure
        - $(params.registries-insecure[*])
        - --registries-search
        - $(params.registries-search[*])
      resources:
        limits:
          cpu: 250m
          memory: 65Mi
        requests:
          cpu: 250m
          memory: 65Mi
  parameters:
    - name: build-args
      description: "The values for the args in the Dockerfile. Values must be in the format KEY=VALUE."
      type: array
      defaults: []
    # ...

2.3.1.2. Buildah 策略带有介质限制

使用 buildah 策略的中等资源限值定义 spec.steps[].resources 字段,如下例所示:

示例:使用中等限制的 buildah 策略

apiVersion: shipwright.io/v1beta1
kind: ClusterBuildStrategy
metadata:
  name: buildah-medium
spec:
  steps:
    - name: build-and-push
      image: quay.io/containers/buildah:v1.31.0
      workingDir: $(params.shp-source-root)
      securityContext:
        capabilities:
          add:
          - "SETFCAP"
      command:
        - /bin/bash
      args:
        - -c
        - |
          set -euo pipefail
          # Parse parameters
        # ...
        # That's the separator between the shell script and its args
        - --
        - --context
        - $(params.shp-source-context)
        - --dockerfile
        - $(build.dockerfile)
        - --image
        - $(params.shp-output-image)
        - --build-args
        - $(params.build-args[*])
        - --registries-block
        - $(params.registries-block[*])
        - --registries-insecure
        - $(params.registries-insecure[*])
        - --registries-search
        - $(params.registries-search[*])
      resources:
        limits:
          cpu: 500m
          memory: 1Gi
        requests:
          cpu: 500m
          memory: 1Gi
  parameters:
    - name: build-args
      description: "The values for the args in the Dockerfile. Values must be in the format KEY=VALUE."
      type: array
      defaults: []
    # ...

为策略配置资源定义后,您必须在 Build CR 中引用策略,如下例所示:

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: buildah-medium
spec:
  source:
    git:
      url: https://github.com/shipwright-io/sample-go
    contextDir: docker-build
  strategy:
    name: buildah-medium
    kind: ClusterBuildStrategy
  # ...

2.3.2. Tekton 管道中的资源管理

构建控制器可用于 Tekton 管道控制器,以便它能够调度 pod 来执行策略步骤。在运行时,构建控制器会创建一个 Tekton TaskRun 资源,而 TaskRun 资源会在特定命名空间中创建新 pod。然后,此 pod 会按顺序执行构建镜像的所有策略步骤。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.