在克隆任务中使用 Git Connector
目录
功能概述
Git Connector 允许普通用户在不直接处理凭证的情况下执行代码克隆操作。通过该 Connector,凭证信息由管理员集中管理,并在需要时自动注入到克隆过程中,从而提升安全性和便利性。
使用场景
- 多个团队共享代码仓库访问权限而无需共享凭证。
- DevOps 流水线中需要安全访问私有代码仓库。
- 环境中需要集中管理代码仓库访问权限。
- 避免在 Kubernetes 工作负载中硬编码或嵌入 Git 凭证。
前提条件
使用该功能前,请确保:
- 环境中已部署 Connectors Core 组件。
- 环境中已部署 Connectors Git 组件。
- 您拥有创建 Kubernetes 资源(Namespace、Secret、Connector 等)的权限。
操作步骤
按照以下步骤使用 Git Connector 完成代码克隆:
- 创建 Namespace
kubectl create ns connectors-git-demo
- 创建 Git Connector 及其凭证
cat <<EOF | kubectl apply -f -
kind: Secret
apiVersion: v1
metadata:
name: test-secret
namespace: connectors-git-demo
type: kubernetes.io/basic-auth
stringData:
username: username # 替换为您的 Git 服务器用户名
password: password # 替换为您的 Git 服务器密码
---
apiVersion: connectors.alauda.io/v1alpha1
kind: Connector
metadata:
name: test-connector
namespace: connectors-git-demo
spec:
connectorClassName: git
address: https://github.com # 替换为您的 Git 服务器地址
auth:
name: basicAuth
secretRef:
name: test-secret
params:
- name: repository
value: AlaudaDevops/connectors-git.git # 替换为当前凭证可访问的仓库路径
EOF
- 授权 Namespace 使用该 Connector
cat <<EOF | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: connectors-reader-binding
namespace: connectors-git-demo
subjects:
- kind: Group
name: system:serviceaccounts:connectors-git-demo
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: connectors-connector-viewer-role
apiGroup: rbac.authorization.k8s.io
EOF
- 使用 Connector 创建克隆任务
cat <<EOF | kubectl apply -f -
apiVersion: batch/v1
kind: Job
metadata:
name: git-clone
namespace: connectors-git-demo
spec:
template:
spec:
restartPolicy: Never
containers:
- name: git
image: bitnami/git:2.47.1
imagePullPolicy: IfNotPresent
command:
- "git"
args: [ "clone", "--progress", "https://github.com/AlaudaDevops/connectors-git.git", "/tmp/demo" ] # 替换为您的仓库地址
volumeMounts:
- name: gitconfig
mountPath: /root/
volumes:
- name: gitconfig
csi:
readOnly: true
driver: connectors-csi
volumeAttributes:
connector.name: "test-connector"
configuration.names: "gitconfig"
EOF
- 查看克隆任务执行结果
kubectl logs -f job/git-clone -n connectors-git-demo
参数说明如下:
参数 | 说明 |
---|
connector.name | 指定要使用的 Connector 名称 |
configuration.names | 指定要生成的配置文件类型;gitconfig 表示生成 Git 配置文件 |
mountPath | 指定配置文件的挂载路径;Git 操作时应挂载到 /root/ 目录 |
操作结果
配置成功后,克隆任务即可在不直接使用凭证的情况下完成代码仓库的克隆。您可以通过查看日志确认克隆操作是否成功。
工作原理
为了更好地理解 Git Connector 的工作原理,我们可以创建一个长时间运行的 Pod 来查看生成的配置:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: pod-git-clone
namespace: connectors-git-demo
spec:
restartPolicy: Never
containers:
- name: git
image: bitnami/git:2.47.1
imagePullPolicy: IfNotPresent
command:
- "sleep"
args: [ "3600" ]
volumeMounts:
- name: gitconfig
mountPath: /root/
volumes:
- name: gitconfig
csi:
readOnly: true
driver: connectors-csi
volumeAttributes:
connector.name: "test-connector"
configuration.names: "gitconfig"
EOF
使用以下命令查看生成的配置文件内容:
kubectl exec -it pod-git-clone -n connectors-git-demo -- cat /root/.gitconfig
生成的配置文件示例:
[http]
extraHeader = Authorization: Basic OmV5Smhixxxxxxxxx==
[url "http://connectors-proxy-service.connectors-system.svc/namespaces/default/connectors/test-connector"]
insteadOf = https://github.com
Git 克隆过程中的工作流程:
- 原始 Git 仓库地址被自动替换为
connectors-proxy
服务地址。
- 系统自动为代理请求注入认证信息(该信息 30 分钟后过期)。
connectors-proxy
在服务器端自动完成凭证信息注入,执行克隆操作。