(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【Tekton/Kubernetes】TriggerTemplateとTriggerBindingをカスタマイズする際の考え方

スポンサーリンク

はじめに

Tektonのサンプルプログラムを動かせばTrigger経由でPipelineが起動できますが、TriggerTemplateの記載方法などが分かりにくいです。本記事では初学者の方向けに、PipelineをPilelineRunで起動できた後に、同PiplelineをEventListener経由で起動するための流れについて記載しました。

環境情報
  • v1.20.8+IKS
  • tektoncd:v0.14.2

Pipelineを動かすために与える情報を確認する

元々PipelineRunで与えていた情報をCurlコマンドのヘッダーに付与することでPipelineをキックします。

よって定義済のPipelineRunを確認すると

apiVersion: tekton.dev/v1alpha1
kind: PipelineRun
metadata:
  name: application-pipeline-run
spec:
  pipelineRef:
    name: application-pipeline
  resources:
    - name: git-source
      resourceRef:
        name: git
  params:
    - name: pathToContext
      value: "src"
    - name: pathToYamlFile
      value: "deploy.yaml"
    - name: "imageUrl"
      value: "jp.icr.io/xx"
    - name: "imageTag"
      value: "1.1"
  serviceAccountName: test-sa
  • パイプラインリソース(PipelineResource)で定義しているgit情報
  • Dockerfileの位置を指定するpathToContext
  • YAMLのファイル名を指定するdeploy.yaml
  • イメージのPush先のimageUrl
  • イメージをPushする際のタグimageUrl

が同パイプラインを起動するために必要なことが分かります。

TriggerTemplate/TriggerBinding/EventListenerを作成する

全体のイメージですが、EventListenerを担うPodに紐づくServiceに対してcurlを打ってパイプラインが起動されます。具体的にはEventListenerが受け取った情報はTriggerBindingに記載された構文に従う必要があり、その情報をTriggerTemplateにわたすことでPilelineRun相当の処理が走ります。

それらの情報の記載方法ですが、TriggerTemplateにて受け取る全ての情報をspec.paramsに箇条書きにし

apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerTemplate
metadata:
  name: build-deploy-pipeline-template
spec:
  params:
    - name: pathToContext
    - name: pathToYamlFile
    - name: imageUrl
    - name: imageTag
    - name: revision
    - name: url

それぞれの詳細をresourcetemplates.spec.params/resourcetemplates.spec.resourcesで定義します。
今回はGitのみresourcesなので以下のような記載となります。

  resourcetemplates:
    - apiVersion: tekton.dev/v1beta1
      kind: PipelineRun
      metadata:
        generateName: test-pipeline-run-
      spec:
        pipelineRef:
          name: test-pipeline
        params:
          - name: pathToContext
            value: $(tt.params.pathToContext)
          - name: pathToYamlFile
            value: $(tt.params.pathToYamlFile)
          - name: imageUrl
            value: $(tt.params.imageUrl)
          - name: imageTag
            value: $(tt.params.imageTag)
        resources:
        - name: git-source
          resourceSpec:
            type: git
            params:
            - name: revision
              value: $(tt.params.revision)
            - name: url
              value: $(tt.params.url)

あとは同情報をTriggerBindingでどのように受け取るかを記載すれば

apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
metadata:
  name: test-pipeline-binding
spec:
  params:
    - name: revision
      value: $(body.head_commit.id)
    - name: url
      value: $(body.repository.url)
    - name: pathToContext
      value: $(body.filepath.context)
    - name: pathToYamlFile
      value: $(body.yamlpath.yaml)
    - name: imageUrl
      value: $(body.image.url)
    - name: imageTag
      value: $(body.thisimage.tag)

そのルールに則ってCurlコマンドを押下することでパイプラインを起動できます。

$ curl -X POST -H 'Content-Type: application/json' xx -d '{"head_commit":{"id": "main"},"repository":{"url": "https://github.com/xx"},"filepath":{"context": "src"},"yamlpath":{"yaml": "deploy.yaml"},"image":{"url": "jp.icr.io/xx"},"thisimage":{"tag": "1.3"}}'

終わりに

Tektonはサンプルアプリケーションはあるものの、その読み解き方の情報が少ないので初学者の方向けに内容を整理しました。

以上、ご参考になれば幸いです。