2.3. 步骤资源定义
您可以为构建策略中的所有步骤包含资源的定义,如对 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 会按顺序执行所有策略步骤来构建镜像。