Harbor Connector

Harbor connector 是一种与平台无关的 connector,可用于连接任意 Harbor registry。

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

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

概述

本文档涵盖以下内容:

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

集成要求

Harbor Registries 前提条件

  • 支持 Harbor 2.x 版本

创建一个简单的 Harbor connector

以下是创建基础 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 connector 应设置为 basicAuth

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

可选元数据字段

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

    apiVersion: connectors.alauda.io/v1alpha1
    kind: Connector
    metadata:
      name: harbor-connector
      annotations:
        cpaas.io/description: "连接到团队开发 Harbor registry"

Connector 能力

认证方式

Harbor Connector 支持以下认证方式:

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

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

所需令牌权限

已配置凭证所需的权限取决于您打算如何在 Pod/Pipeline 中使用它。

例如:

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

出于安全最佳实践,我们建议使用仅具备最小必要权限的凭证。当需要额外权限时,请创建使用更高权限 secret 的独立 Connector,并使用 namespace 隔离来控制哪些用户可以访问每个 Connector。

代理和配置能力

Harbor Connector 提供代理能力,以便安全访问 Harbor registry。

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

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

代理地址

创建 Harbor connector 时,系统会自动创建一个用于代理访问 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 将代理信息挂载到 Pod 中,然后通过环境变量或配置文件使用这些代理信息。

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.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 提供以下配置:

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 配置文件。
  • 在配置文件中,当前 connector 会默认设置为 insecure-registries

例如:

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

延伸阅读