Authentication
如果在tekton中使用git或者docker需要用户名密码登录,需要怎么设置呢?
我们知道kubernetes中是使用Secret
,使用volumes
或者env
将这个配置文件直接mount到pod中。
类似tekton也沿用了这一思路
Tekton Authentication
Tekton 可以直接使用了Kubernetes Secret
定义的类型,处理Git和Docker登录
Git | Docker |
---|---|
kubernetes.io/basic-auth kubernetes.io/ssh-auth | kubernetes.io/basic-auth kubernetes.io/dockercfg kubernetes.io/dockerconfigjson |
以下演示一个以docker basic-auth authentication为例的设置
-
先创建一个包含密码的
Secret
。并加上tekton
的annotations
,这个非常重要。apiVersion: v1 kind: Secret metadata: name: basic-user-pass annotations: tekton.dev/docker-0: https://gcr.io # Described below type: kubernetes.io/basic-auth stringData: username: <cleartext username> password: <cleartext password>
annotations
必须要是要以tekton.dev/docker-
开头 -
创建
ServiceAccount
绑定这个Secret
apiVersion: v1 kind: ServiceAccount metadata: name: build-bot secrets: - name: basic-user-pass
-
在运行配置中绑定这个
ServiceAccount
如TaskRun
apiVersion: tekton.dev/v1beta1 kind: TaskRun metadata: name: build-push-task-run-2 spec: serviceAccountName: build-bot taskRef: name: build-push
再如
PipelineRun
apiVersion: tekton.dev/v1beta1 kind: PipelineRun metadata: name: demo-pipeline namespace: default spec: serviceAccountName: build-bot pipelineRef: name: demo-pipeline
-
最后执行即可
kubectl apply --filename secret.yaml serviceaccount.yaml run.yaml
至于具体发生了什么,其实就是tekton自动将Secret
mount到了HOME目录,原理跟kubernetes是相似的。
其中的关键的关键就是Secret
中的这个annotations
,必须是tekton.dev/git-
or tekton.dev/docker-
开头
tekton就是根据这个annotations
,判断对应是docker还是git,找到对应的用户名密码。
避免全局使用credentials
细心的读者会发现serviceAccount
绑定在PipelineRun
或TaskRun
上,那么对整个Pipeline
或Task
都生效的
所有步骤不管有没有需要登录认证,pod里都会有credentials
如果想避免这里情况,那么就不能用上文所述的这种做法了。
一个变通的办法是单独将不同的Secret
放到不同的workspace
中,
对应的task
可以根据需要挂载一个或者多个workspace
使用credentials。
skopeo
接下来我们实际使用skopeo演示这两种认证方法
skopeo是一个给docker image打标签的工具,不需要把镜像完整拖下来,直接在registry上操作。
集中认证方式
-
先创建docker登录Secret,注意annotations中定义了对应docker.io这个仓库
apiVersion: v1 kind: Secret metadata: name: docker-creds annotations: tekton.dev/docker-0: https://docker.io type: kubernetes.io/basic-auth stringData: username: massivezh password: xxxx-xxxx-xxxx-xxxx-xxxx
-
创建
ServiceAccount
apiVersion: v1 kind: ServiceAccount metadata: name: secret-service-account secrets: - name: docker-creds # 这里可以一次性绑定多个Secret,对应不同的repo # - name: quay-creds
-
最后在
TaskRun
中绑定serviceAccountName
apiVersion: tekton.dev/v1beta1 kind: TaskRun metadata: name: skopeo-run spec: serviceAccountName: secret-service-account params: - name: srcImageURL value: docker://docker.io/massivezh/kaniko-nocode:latest - name: destImageURL value: docker://docker.io/massivezh/kaniko-nocode:1 taskRef: name: skopeo-copy workspaces: - name: images-url emptyDir: {}
-
查看运行结果
# kubectl get taskrun NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME skopeo-run True Succeeded 29m 29m
docker hub已经有了
tag:1
的镜像
独立认证方式
这个使用workspace
的方法,可能已经在其他地方看到过了
-
改造skopeo,增加一个
workspace
,将dockerconfig
挂载到HOME目录# diff -u skopeo-copy.yaml workspaces-cred/skopeo-copy-mod.yaml --- skopeo-copy.yaml 2022-09-18 11:30:18.692333792 +0800 +++ workspaces-cred/skopeo-copy-mod.yaml 2022-09-18 11:53:23.459643040 +0800 @@ -22,6 +22,10 @@ workspaces: - name: images-url + - name: dockerconfig + description: Includes a docker `config.json` + optional: true + mountPath: /tekton/home/.docker
-
docker login
手动登录产生/root/.docker/config.json
-
将
config.json
转化成secret
# kubectl create secret generic dockerhub-secret --from-file=/root/.docker/config.json
-
在
TaskRun
中设置workspaces
apiVersion: tekton.dev/v1beta1 kind: TaskRun metadata: name: skopeo-run-mod spec: params: - name: srcImageURL value: docker://docker.io/massivezh/kaniko-nocode:latest - name: destImageURL value: docker://docker.io/massivezh/kaniko-nocode:2 taskRef: name: skopeo-copy-mod workspaces: - name: images-url emptyDir: {} - name: dockerconfig secret: secretName: dockerhub-secret
-
查看运行结果
# kubectl get taskrun NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME skopeo-run-mod True Succeeded 3m26s 3m13s
docker hub已经有了
tag:2
的镜像
一次修改多个tag
前面说的都是一次只打一个tag,如果需要批量处理打tag,需要设置images-url
这个参数
首先创建一个configmap
,里面对应的文件名必须是url.txt
把需要修改的tag列表全部写在这里面
#cat url.txt
docker://quay.io/temp/kubeconfigwriter:v1 docker://quay.io/skopeotest/kube:v1
docker://quay.io/temp/kubeconfigwriter:v2 docker://quay.io/skopeotest/kube:v2
kubectl create configmap image-configmap --from-file=url.txt
默认一次只处理一个,这个值是空的
workspaces:
- name: images-url
emptyDir: {}
我们替换成configmap
包含url.txt
文件
workspaces:
- name: images-url
configmap:
name: image-configmap
运行过程中这个url.txt
会mount到pod里供skopeo使用。
参考:
https://hub.tekton.dev/tekton/task/skopeo-copy
https://github.com/tektoncd/catalog/tree/main/task/skopeo-copy/0.2
https://tekton.dev/docs/pipelines/auth/