如何从 IBM Cloud Delivery Pipeline(Tekton、dockerconfigjson)访问私有容器注册表

问题描述 投票:0回答:3

我正在尝试在我的一项任务中使用来自私有容器注册表的容器映像。

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: echo-hello-world
spec:
  steps:
    - name: echo
      image: de.icr.io/reporting/status:latest
      command:
        - echo
      args:
        - "Hello World"

但是当我在 IBM Cloud Delivery Pipeline (Tekton) 中运行此任务时,无法拉取映像

 message: 'Failed to pull image "de.icr.io/reporting/status:latest": rpc error: code = Unknown desc = failed to pull and unpack image "de.icr.io/reporting/status:latest": failed to resolve reference "de.icr.io/reporting/status:latest": failed to authorize: failed to fetch anonymous token: unexpected status: 401 Unauthorized'

我阅读了一些教程和博客,但到目前为止找不到解决方案。这可能就是我需要完成的任务,以便 IBM Cloud Delivery Pipeline (Tekton) 可以访问我的私有容器注册表:https://tekton.dev/vault/pipelines-v0.15.2/auth/#basic-authentication-码头工人

到目前为止,我已经在 .tekton 目录中创建了一个 Secret.yaml 文件:

apiVersion: v1
kind: Secret
metadata:
 name: basic-user-pass
 annotations:
   tekton.dev/docker-0: https://de.icr.io # Described below
type: kubernetes.io/basic-auth
stringData:
  username: $(params.DOCKER_USERNAME)
  password: $(params.DOCKER_PASSWORD)

我也在创建一个ServiceAccount

apiVersion: v1
kind: ServiceAccount
metadata:
 name: default-runner
secrets:
 - name: basic-user-pass

在我的触发器定义中,我告诉管道使用

default-runner
ServiceAccount:

apiVersion: tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: theTemplateTrigger
spec:
  resourcetemplates:
  - apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:
      name: pipelinerun-$(uid)
    spec:
      serviceAccountName: default-runner
      pipelineRef:
        name: hello-goodbye
ibm-cloud tekton tekton-pipelines delivery-pipeline
3个回答
4
投票

我找到了一种将 API 密钥传递到 IBM Cloud Delivery Pipeline (Tekton) 的方法,管道中的任务现在能够从我的私有容器注册表中提取容器映像。

这是我的工作触发器模板:

apiVersion: tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: theTemplateTrigger
spec:
  params:
    - name: pipeline-dockerconfigjson
      description: dockerconfigjson for images used in .pipeline-config.yaml
      default: "eyJhdXRocyI6e319" # ie. {"auths":{}} base64 encoded
  resourcetemplates:
    - apiVersion: v1
      kind: Secret
      data:
        .dockerconfigjson: $(tt.params.pipeline-dockerconfigjson)
      metadata:
        name: pipeline-pull-secret
      type: kubernetes.io/dockerconfigjson      
    - apiVersion: tekton.dev/v1beta1
      kind: PipelineRun
      metadata:
        name: pipelinerun-$(uid)
      spec:
        pipelineRef:
          name: hello-goodbye
        podTemplate:
          imagePullSecrets:
            - name: pipeline-pull-secret

它首先定义了一个名为

pipeline-dockerconfigjson
的参数:

  params:
    - name: pipeline-dockerconfigjson
      description: dockerconfigjson for images used in .pipeline-config.yaml
      default: "eyJhdXRocyI6e319" # ie. {"auths":{}} base64 encoded

第二部分将传入此参数的值转换为 Kubernetes 秘密:

    - apiVersion: v1
      kind: Secret
      data:
        .dockerconfigjson: $(tt.params.pipeline-dockerconfigjson)
      metadata:
        name: pipeline-pull-secret
      type: kubernetes.io/dockerconfigjson

然后这个秘密被推送到 PodTemplate 的

imagePullSecrets
字段中。

最后一步是使用有效的

dockerconfigjson
填充参数,这可以在 Delivery Pipeline UI (IBM Cloud UI) 中完成。

要为我的注册表创建有效的

dockerconfigjson
de.icr.io
,我必须使用以下 kubectl 命令:

kubectl create secret docker-registry mysecret \
 --dry-run=client \
 --docker-server=de.icr.io  \
 --docker-username=iamapikey \                     
 --docker-password=<MY_API_KEY> \
 --docker-email=<MY_EMAIL> \
 -o yaml

然后在输出中有一个有效的 base64 编码的

.dockerconfigjson
字段。


2
投票

© www.soinside.com 2019 - 2024. All rights reserved.