12.5. 使用 scorecard 验证 Operator
Operator 作者应验证其 Operator 已正确打包且没有语法错误。作为 Operator 作者,您可以使用 Operator SDK 的 scorecard 工具验证 Operator 打包并运行测试。
OpenShift Container Platform 4.4 支持 Operator SDK v0.15.0。
12.5.1. 关于 scorecard 工具
为验证 Operator,Operator SDK 的 scorecard 工具将首先创建任何相关的自定义资源 (CR) 和 Operator 所需的所有资源。然后,scorecard 在 Operator 的 Deployment 中创建一个代理容器,用于记录对 API 服务器的调用并运行一些测试。执行的测试还会检查 CR 中的部分参数。
12.5.2. Scorecard 配置
Scorecard 工具使用一个配置文件来供您配置内部插件以及几个全局配置选项。
12.5.2.1. 配置文件
Scorecard 工具配置的默认位置为 <project_dir>/.osdk-scorecard.*
。以下是 YAML 格式的配置文件示例:
Scorecard 配置文件
scorecard: output: json plugins: - basic: 1 cr-manifest: - "deploy/crds/cache.example.com_v1alpha1_memcached_cr.yaml" - "deploy/crds/cache.example.com_v1alpha1_memcachedrs_cr.yaml" - olm: 2 cr-manifest: - "deploy/crds/cache.example.com_v1alpha1_memcached_cr.yaml" - "deploy/crds/cache.example.com_v1alpha1_memcachedrs_cr.yaml" csv-path: "deploy/olm-catalog/memcached-operator/0.0.3/memcached-operator.v0.0.3.clusterserviceversion.yaml"
全局选项的配置方法采用以下优先级,从最高到最低依次为:
命令参数(如果可用)→ 配置文件
配置文件必须为 YAML 格式。由于配置文件将来可能会扩展为允许配置所有 operator-sdk
子命令,因此 scorecard 的配置必须在 scorecard
子部分下。
配置文件支持由 viper
软件包提供。如需有关 viper 配置如何工作的更多信息,请参阅 viper
软件包 README。
12.5.2.2. 命令参数
虽然大多数的 scorecard 工具配置都是使用配置文件完成的,但您也可以使用以下参数:
标记 | 类型 | 描述 |
---|---|---|
| 字符串 | 用于捆绑包验证测试的捆绑包目录的路径。 |
| 字符串 |
Scorecard 配置文件的路径。默认为 |
| 字符串 |
输出格式。有效选项为 |
| 字符串 |
|
| 字符串 |
要运行的 scorecard 版本。默认和唯一有效选项为 |
| 字符串 | 用于过滤测试的标签选择器。 |
| bool |
如果为 |
12.5.2.3. 配置文件选项
Scorecard 配置文件提供以下选项:
选项 | 类型 | 描述 |
---|---|---|
| 字符串 |
等同于 |
| 字符串 |
等同于 |
| 字符串 |
等同于 |
| 数组 | 插件名称的数组。 |
12.5.2.3.1. 基本和 OLM 插件
Scorecard 支持内部 basic
和 olm
插件,这些插件由配置文件中的 plugins
部分配置。
选项 | 类型 | 描述 |
---|---|---|
| []string |
待测试 CR 的路径。如果 |
| 字符串 |
Operator 的 CSV 路径。对于 OLM 测试或者当 |
| bool | 指示 OLM 已将 CSV 和相关 CRD 部署到集群中。 |
| 字符串 |
|
| 字符串 |
在其中运行插件的命名空间。如果未设置,则会使用由 |
| int | Operator 初始化过程中的超时时间(以秒为单位)。 |
| 字符串 | 包含必须部署到集群中的 CRD 的目录路径。 |
| 字符串 |
包含了在一个命名空间内运行的所有资源的清单文件。默认情况下,scorecard 将 |
| 字符串 |
包含全局运行(不具有命名空间)的所需资源的清单。默认情况下,scorecard 将 |
目前,将 scorecard 与 CSV 搭配使用时不允许通过 CLI、配置文件或 CSV 注解设置多个 CR 清单。您必须在集群中停止 Operator,重新部署,并为每个经过测试的 CR 重新运行 scorecard。
其他资源
-
您可以将
cr-manifest
或 CSV 的metadata.annotations['alm-examples']
设置为向 scorecard 提供 CR,但不能同时设置两者。详情请参阅 CRD 模板。
12.5.3. 执行的测试
默认情况下,scorecard 工具在两个内部插件之间提供八个可以运行的内部测试。如果为插件指定多个 CR,在每个 CR 后会完全清理测试环境,以便每个 CR 获取一个干净的测试环境。
每个测试都有一个唯一标识测试的简短名称。这在选择一个特定测试或多个测试来运行时很有用。例如:
$ operator-sdk scorecard -o text --selector=test=checkspectest $ operator-sdk scorecard -o text --selector='test in (checkspectest,checkstatustest)'
12.5.3.1. Basic 插件
Basic
插件提供了以下基本的 Operator 测试:
测试 | 描述 | 短名称 |
---|---|---|
Spec Block Exists |
此测试会检查集群中创建的自定义资源,以确保所有 CR 都有一个 |
|
Status Block Exists |
此测试会检查集群中创建的自定义资源,以确保所有 CR 都有一个 |
|
Writing Into CRs Has An Effect |
此测试会读取 scorecard 代理的日志,以验证 Operator 是否正在向 API 服务器发出 |
|
12.5.3.2. OLM 插件
Olm
插件提供了以下 OLM 集成测试:
测试 | 描述 | 短名称 |
---|---|---|
OLM 捆绑包验证 | 此测试会验证捆绑包目录中找到的 OLM 捆绑包清单,通过捆绑包标记来指定。如果捆绑包内容包含错误,那么测试结果输出中将包括验证器日志以及验证库中的错误消息。 |
|
Provided APIs Have Validation |
此测试会验证提供的 CR 的 CRD 是否包含一个验证部分,且 CR 中检测到的每个 |
|
Owned CRDs Have Resources Listed |
此测试确保 |
|
Spec Fields With Descriptors |
此测试会验证自定义资源的 |
|
Status Fields With Descriptors |
此测试会验证自定义资源的 |
|
其他资源
12.5.4. 在 scorecard 中运行
先决条件
Operator 项目的以下先决条件由 scorecard 工具检查:
- 访问运行 Kubernetes 1.11.3 或更高版本的集群。
-
如果要使用 scorecard 检查 Operator 项目与 Operator Lifecycle Manager (OLM) 的集成,则还需要 ClusterServiceVersion (CSV) 文件。这是使用
olm-deployed
选项时的一项要求。 对于不是使用 Operator SDK 生成的 Operator(非 SDK Operator):
- 用于安装和配置 Operator 和 CR 的资源清单。
-
支持从
KUBECONFIG
环境变量读取的配置 getter,例如clientcmd
或controller-runtime
配置 getter。这是 scorecard 代理正常工作所需的。
流程
-
在 Operator 项目中定义
.osdk-scorecard.yaml
配置文件。 -
创建在 RBAC 文件中定义的命名空间 (
role_binding
)。 从 Operator 项目的根目录运行 scorecard:
$ operator-sdk scorecard
如果任何执行的测试都未通过,则 scorecard 返回代码为
1
,如果所有选定的测试都已通过,则为0
。
12.5.5. 使用 OLM 管理的 Operator 运行 scorecard
可使用 ClusterServiceVersion (CSV) 运行 scorecard,提供测试集群就绪和非 SDK Operator 的方法。
流程
Scorecard 需要 Operator 的
Deployment
Pod 中的代理容器来读取 Operator 日志。在使用 OLM 部署 Operator 之前,需要对 CSV 进行一些修改并创建一个额外的对象来运行代理。这一步可以手动执行或使用 bash 功能自动执行。选择以下任一方法。
手动方法:
创建包含本地 Kubeconfig 的代理服务器 secret:
使用 scorecard 代理具有命名空间的所有者引用生成用户名。
$ echo '{"apiVersion":"","kind":"","name":"scorecard","uid":"","Namespace":"'<namespace>'"}' | base64 -w 0 1
- 1
- 将
<namespace>
替换为 Operator 将部署到的命名空间。
使用以下模板编写
Config
清单scorecard-config.yaml
,将<username>
替换为上一步中生成的 base64 用户名:apiVersion: v1 kind: Config clusters: - cluster: insecure-skip-tls-verify: true server: http://<username>@localhost:8889 name: proxy-server contexts: - context: cluster: proxy-server user: admin/proxy-server name: <namespace>/proxy-server current-context: <namespace>/proxy-server preferences: {} users: - name: admin/proxy-server user: username: <username> password: unused
将
Config
编码为 base64:$ cat scorecard-config.yaml | base64 -w 0
创建
Secret
清单scorecard-secret.yaml
:apiVersion: v1 kind: Secret metadata: name: scorecard-kubeconfig namespace: <namespace> 1 data: kubeconfig: <kubeconfig_base64> 2
应用 secret:
$ oc apply -f scorecard-secret.yaml
将一个指向
Secret
的卷插入到 Operator 的 Deployment 中:spec: install: spec: deployments: - name: memcached-operator spec: ... template: ... spec: containers: ... volumes: - name: scorecard-kubeconfig 1 secret: secretName: scorecard-kubeconfig items: - key: kubeconfig path: config
- 1
- Scorecard
kubeconfig
卷。
在 Operator 的 Deployment 中,将卷挂载和
KUBECONFIG
环境变量插入到每个容器中:spec: install: spec: deployments: - name: memcached-operator spec: ... template: ... spec: containers: - name: container1 ... volumeMounts: - name: scorecard-kubeconfig 1 mountPath: /scorecard-secret env: - name: KUBECONFIG 2 value: /scorecard-secret/config - name: container2 3 ...
将 scorecard 代理容器插入到 Operator 的 Deployment 中:
spec: install: spec: deployments: - name: memcached-operator spec: ... template: ... spec: containers: ... - name: scorecard-proxy 1 command: - scorecard-proxy env: - name: WATCH_NAMESPACE valueFrom: fieldRef: apiVersion: v1 fieldPath: metadata.namespace image: quay.io/operator-framework/scorecard-proxy:master imagePullPolicy: Always ports: - name: proxy containerPort: 8889
- 1
- Scorecard 代理容器。
自动方法:
Community-operators
存储库有几个 bash 功能,可为您执行流程中的前面步骤:$ curl -Lo csv-manifest-modifiers.sh \ https://raw.githubusercontent.com/operator-framework/community-operators/master/scripts/lib/file $ . ./csv-manifest-modifiers.sh $ create_kubeconfig_secret_file scorecard-secret.yaml "<namespace>" 1 $ oc apply -f scorecard-secret.yaml $ insert_kubeconfig_volume "<csv_file>" 2 $ insert_kubeconfig_secret_mount "<csv_file>" $ insert_proxy_container "<csv_file>" "quay.io/operator-framework/scorecard-proxy:master"
- 插入代理容器后,按照 Operator SDK 入门指南中的步骤捆绑 CSV 和 CRD,并在 OLM 上部署 Operator。
-
在 OLM 上部署了 Operator 后,请在 Operator 项目中定义
.osdk-scorecard.yaml
配置文件,并确保csv-path: <csv_manifest_path>
和olm-deployed
选项都已设置。 在
csv-path: <csv_manifest_path>
和olm-deployed
均已在 scorecard 配置文件中设置的情况下运行 scorecard:$ operator-sdk scorecard