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 会按顺序执行构建镜像的所有策略步骤。