Harbor 连接器

Harbor 连接器是一个与平台无关的连接器,可用于连接到任意 Harbor registry。

你可以使用 Harbor Connector 在 CICD pipeline 中安全地执行容器镜像操作,也可以在 kubernetes 工作负载中使用它,在无需凭证的情况下执行镜像操作。

此外,你可以集中管理跨 namespace 的 Harbor 访问配置,从而避免在每个 namespace 中重复配置 Harbor 凭证。

概览

本文档涵盖以下内容:

  • 集成要求:目标 Harbor registry 的前提条件
  • 创建 Harbor 连接器
  • 高级功能:Harbor 连接器的代理能力和配置能力

集成要求

Harbor Registries 前提条件

  • 支持 Harbor 2.x 版本

创建一个简单的 Harbor 连接器

以下是创建基础 Harbor Connector 的方法:

# Harbor Connector
apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: harbor-connector
spec:
  connectorClassName: harbor
  address: https://harbor.example.com

字段参考

spec.connectorClassName

harbor(常量),指定 Harbor 集成所使用的 ConnectorClass 名称。

spec.address

目标 Harbor registry 地址,例如:https://harbor.example.com

spec.auth(optional)

指定 Harbor registry 的认证方式

  • spec.auth.name:对于 Harbor 连接器,应为 basicAuth

  • spec.auth.secretRef:指定包含 Harbor registry 认证信息的 secret,secret 应创建在与连接器相同的 namespace 中。如果你的 Harbor registry 不需要认证,可以省略此字段。secret 类型必须是 kubernetes.io/basic-auth

可选的 Metadata 字段

  • cpaas.io/description:Harbor 连接器的描述信息,例如:

    apiVersion: connectors.alauda.io/v1alpha1
    kind: Connector
    metadata:
      name: harbor-connector
      annotations:
        cpaas.io/description: "Connect to team development Harbor registry"

连接器能力

认证方式

Harbor 连接器支持以下认证方式:

  • Basic Authentication:用户名和密码认证,secret 类型必须是 kubernetes.io/basic-auth

if your Harbor registry does not require authentication, you can omit this field.

所需令牌权限

配置的凭证所需权限取决于你打算如何在 Pods/Pipelines 中使用它。

例如:

  • 镜像拉取和推送操作:如果你需要使用此连接器拉取和推送镜像,则凭证必须对目标 Harbor registry 具有读写权限。
  • API 操作:根据你需要执行的操作配置权限。在配置凭证时,确保该账户有权访问用户信息(/users/current)。

出于安全最佳实践,我们建议创建仅包含最低所需权限的凭证。当需要额外权限时,创建单独的、具有更高权限 secret 的 Connectors,并使用 namespace 隔离来控制哪些用户可以访问每个 Connector。

代理和配置能力

Harbor 连接器提供代理能力,以便安全访问 Harbor registries。

为了让客户端在不直接处理凭证的情况下访问 Harbor registries,Harbor ConnectorClass 提供了一个会自动注入认证信息的代理服务器。

有权访问该连接器的客户端可以使用此代理服务器访问 Harbor registries,而无需在客户端侧配置凭证。

代理地址

创建 Harbor 连接器时,系统会自动创建一个用于代理访问 Harbor registry 的 Service。

系统会将代理地址记录在 status.proxy.httpAddress 字段中。

例如:

apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: harbor-connector
spec:
  # . . .
status:
  conditions:
  # . . .
  proxy:
    httpAddress:
      url: http://c-harbor-connector.default.svc.cluster.local

正向代理

你可以使用 CSI 将代理信息挂载到 Pods 中,然后通过环境变量或配置文件使用这些代理信息。

volumes:
- name: proxyconfig
  csi:
    readOnly: true
    driver: connectors-csi
    volumeAttributes:
      connector.name: "harbor"

然后,在执行容器操作之前,通过环境变量或配置文件使用代理信息。

export http_proxy=$(cat /{mount-path}/http.proxy)
export https_proxy=$(cat /{mount-path}/https.proxy)
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export no_proxy=localhost,127.0.0.1
export NO_PROXY=$no_proxy
echo "Using proxy: http_proxy=$http_proxy, https_proxy=$https_proxy, no_proxy=$no_proxy"

Harbor CLI 配置

当你希望某个工作负载通过 Harbor 连接器运行 harbor-cli 命令,但又不想将原始 Harbor 用户名或密码放入 Pod 中时,可以使用 harbor-cli-config

挂载 harbor-cli-config 后,你的工作负载将获得一套可直接使用的 Harbor CLI 配置:

  • Harbor CLI 的 config.yaml
  • 连接器代理环境的 .env
  • HARBOR_ENCRYPTION_KEY,Harbor CLI 在读取生成的配置时需要它

对于单个 Harbor 连接器,按如下方式挂载:

volumes:
- name: harbor-cli
  csi:
    readOnly: true
    driver: connectors-csi
    volumeAttributes:
      connector.name: "harbor-connector"
      configuration.names: "harbor-cli-config"

对于多个 Harbor 连接器,可以在一次挂载中请求它们。生成的 config.yaml 会为每个选中的 Connector 包含一个 Harbor CLI context,因此你可以使用 harbor context switch 在它们之间切换。

volumes:
- name: harbor-cli
  csi:
    readOnly: true
    driver: connectors-csi
    volumeAttributes:
      connectors: "harbor-connector,harbor-connector-2"
      configuration.names: "harbor-cli-config"
Info

多连接器功能依赖 enable-multi-connector feature flag。有关该标志的详细信息,请参见 功能标志

在运行 harbor-cli 之前,请加载代理环境、信任代理 CA,并导出挂载的加密密钥:

set -a; source /mnt/harbor-cli/.env; set +a
export HARBOR_CLI_CONFIG=/mnt/harbor-cli/config.yaml
export HARBOR_ENCRYPTION_KEY=$(cat /mnt/harbor-cli/HARBOR_ENCRYPTION_KEY)

harbor-cli project list --name my-project

harbor-cli context switch default/harbor-connector-2
harbor-cli project list --name my-project

HARBOR_ENCRYPTION_KEY 仅用于让 Harbor CLI 读取生成的配置。它不是原始的 Harbor secret,且 config.yaml 中生成的密码值不会作为 Harbor 访问的真实凭证使用。真实访问仍然通过连接器代理进行。

反向代理

使用反向代理时,你需要将目标镜像地址修改为代理地址。

示例: harbor.example.com/test/abc:v1 → c-harbor-connector.default.svc.cluster.local/namespaces/harbor-connector-demo/connectors/harbor-connector/test/abc:v1

此外,你还需要将配置文件挂载到 Pod 中,并在 insecure-registries 中配置代理地址。系统提供默认配置文件 buildkitd.tomlconfig.json

基于 OCI Connector 类型创建的 OCI Connector 提供以下配置:

registry-config:OCI CLI 所需的配置信息,例如 buildkit、buildah 等。

  • 提供 config.json 配置文件。
  • 包含访问代理所需的认证信息。

例如:

// config.json

{
  "auths": {
      "<proxy address of the connector>": {
          "auth": "<authentication information required to access the connector proxy>"
      }
  }
}

buildkitd:BuildKit Daemon 所需的配置信息。

  • 提供 buildkitd.toml 配置文件。
  • 在配置文件中,当前连接器默认会被设置为 insecure-registries

例如:

insecure-entitlements = [ "network.host", "security.insecure" ]
[registry."<proxy address of the connector>"]
  http = true

延伸阅读