Harbor Connector

Harbor Connector 是一个平台无关的连接器,您可以使用它连接到任何 Harbor registry。

您可以使用 Harbor Connector 在 CICD 流水线中安全地执行容器镜像操作,或者在 kubernetes 工作负载中使用它来执行无凭证的镜像操作。

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

Overview

本文档涵盖:

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

Integration Requirements

Harbor Registries 先决条件

  • 支持 Harbor 2.x 版本

Creating a simple 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

Fields Reference

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 相同的命名空间中。如果您的 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: "Connect to team development Harbor registry"

Connector Capabilities

Authentication Methods

Harbor Connector 支持以下认证方式:

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

如果您的 Harbor registry 不需要认证,可以省略此字段。

Token Permissions Required

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

例如:

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

出于安全最佳实践,建议创建权限最小化的凭证。当需要额外权限时,创建具有更高权限的单独 Connector,并使用命名空间隔离控制哪些用户可以访问每个 Connector。

Proxy and Configuration Capabilities

Harbor Connector 提供代理能力,以实现对 Harbor registry 的安全访问。

为了使客户端无需直接处理凭证即可访问 Harbor registry,Harbor ConnectorClass 提供了一个代理服务器,自动注入认证信息。

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

Proxy Address

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

系统会将代理地址记录在 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

Forward Proxy

您可以通过 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"

Reverse 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 配置文件。
  • 配置文件中,当前连接器默认被设置为 insecure-registries

例如:

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

Further Reading