PyPI Connector

PyPI Connector 是一个平台无关的连接器,您可以使用它连接到任何 PyPI 注册表。

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

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

Overview

本文档涵盖:

  • 集成要求:目标 PyPI 注册表的前提条件
  • 创建 PyPI Connector
  • 高级功能:关于 PyPI Connector 的代理功能和配置能力

Integration Requirements

PyPI 注册表前提条件

Creating a simple PyPI connector

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

# PyPI Connector
apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: pypi-connector
spec:
  connectorClassName: pypi
  address: https://pypi.org

Fields Reference

spec.connectorClassName:

pypi(固定值),指定用于 PyPI 集成的 ConnectorClass 名称。

spec.address:

目标 PyPI 注册表地址,例如:https://pypi.org。

spec.auth(可选):

指定 PyPI 注册表的认证方式

  • spec.auth.name:PyPI Connector 应为 basicAuth。

  • spec.auth.secretRef:指定包含 PyPI 注册表认证信息的 Secret,该 Secret 应创建在与 Connector 相同的命名空间中。如果您的 PyPI 注册表不需要认证,可以省略此字段。

可选元数据字段:

  • cpaas.io/description:PyPI Connector 的描述信息,例如:

    apiVersion: connectors.alauda.io/v1alpha1
    kind: Connector
    metadata:
      name: pypi-connector
      annotations:
        cpaas.io/description: "连接到团队开发 PyPI 注册表"

Capabilities of PyPI Connector

Authentication

PyPI Connector 支持以下认证类型:

  • basicAuth:基于用户名和密码的认证,对应的 Secret 类型为:kubernetes.io/basic-auth

使用基本认证

例如:

apiVersion: v1
stringData:
  username: your-pypi-registry-username
  password: your-pypi-registry-password
kind: Secret
metadata:
  name: pypi-secret
type: kubernetes.io/basic-auth

如果 Secret 不正确,PyPI Connector 中的 status.conditions 字段将显示错误信息。

apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: pypi-connector
spec: {}
status:
  conditions:
    - type: Ready
      status: False
      reason: "xxxxx"
      message: "xxxx"

有关完整状态信息,请参见 Connector Status Documentation。

如果 PyPI 注册表不需要认证,可以省略 secretRef 字段:

apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
  name: pypi-connector
spec:
  connectorClassName: pypi
  address: https://pypi.org
  auth:
    name: basicAuth

Credential Permissions Required

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

例如:

  • 安装软件包:如果您仅需使用 pip install 安装软件包,凭证只需对目标 PyPI 注册表具有读取权限。
  • 上传软件包:如果您需要使用 twine upload 或类似工具上传软件包,凭证必须对目标注册表具有读写权限。

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

Proxy and pip.conf, .pypirc Configuration

为了让客户端能够无凭证访问 PyPI 注册表,PyPI Connector 提供了一个代理服务器,用于自动注入认证信息。

客户端可以通过该代理服务器访问 PyPI 注册表,无需在客户端配置凭证。

为简化使用,PyPI ConnectorClass 提供了可通过 CSI 挂载到 Pod 中的 pip.conf 和 .pypirc 文件。在 Pod 中执行 PyPI 操作时,代理服务可自动注入认证信息。

Proxy Address

创建 Connector 后,系统会自动为目标 PyPI 注册表配置一个代理服务。

代理端点记录在 status.proxy.httpAddress 中:

例如:

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

pip.conf configuration file

PyPI Connector 提供以下配置:

pip.conf:

  • 提供一个 pip.conf 配置文件。结合 connector-csi-driver,该配置文件将挂载到 Pod 中,使得通过代理访问 PyPI 注册表时无需在客户端配置凭证。

Pod 中生成的配置文件示例:

[global]
index-url = http://connectors-pypi-demo-pypi-connector:eyJhbGciOiJEnEZaTQ@c-pypi-connector.connectors-pypi-demo.svc.cluster.local/simple/
timeout = 30

[install]
trusted-host = c-pypi-connector.connectors-pypi-demo.svc.cluster.local

.pypirc configuration file

  • 提供一个 .pypirc 配置文件。结合 connector-csi-driver,该配置文件将挂载到 Pod 中,使得通过代理访问 PyPI 注册表时无需在客户端配置凭证。

    [distutils]
    index-servers = connectors-pypi
    
    [connectors-pypi]
    repository = http://c-pypi-connector.connectors-pypi-demo.svc.cluster.local/
    username = connectors-pypi-demo-pypi-connector
    password = eyJhbGciOiJEnEZaTQ

有关代理机制的详细信息,请参见快速入门指南中的 How It Works。

Using Connectors CSI Driver to mount pip.conf and .pypirc file

PyPI Connector 提供了可通过 Connector CSI Driver 挂载到 Pod 中的 pip.conf 和 .pypirc 文件。

例如:

spec:
  volumes:
  - name: pip.conf
    csi:
      readOnly: true
      driver: connectors-csi
      volumeAttributes:
        connector.name: "pypi-connector"
        configuration.names: "pipconf"
  - name: pypirc
    csi:
      readOnly: true
      driver: connectors-csi
      volumeAttributes:
        connector.name: "pypi-connector"
        configuration.names: "pypirc"

参数说明:

  • csi.readOnly:固定值 true
  • csi.driver:Connector CSI Driver,固定为 connectors-csi
  • csi.volumeAttributes:CSI 卷属性
    • connector.name:PyPI Connector 名称
    • connector.namespace:PyPI Connector 所在命名空间;若未指定,则使用 Pod 所在命名空间
    • configuration.names:配置名称,由 PyPI Connector 提供。支持 pipconf 和 pypirc。

关于如何通过 connectors-csi-driver 在 Pod 中使用 pip.conf 和 .pypirc 文件的详细信息,请参见 Using PyPI Connectors in kubernetes jobs

Further Reading

References