(O+P)ut

アウトプット



(O+P)ut

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

【Tekton/Kubernetes】Gitからコードを取得してビルドする"build-image-from-source"を読み解く

スポンサーリンク

はじめに

Tektonのサンプルコードとして提供される以下のコードを実際に動かし、ログを見ることで内部の動きを調査しました。

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: build-image-from-source
spec:
  inputs:
    resources:
      - name: git-source
        type: git
    params:
      - name: pathToContext
        description: The path to the build context, used by Kaniko - within the workspace
        default: .
      - name: pathToDockerfile
        description: The path to the dockerfile to build
        default: Dockerfile
      - name: imageUrl
        description: value should be like - us.icr.io/test_namespace/builtImageApp
      - name: imageTag
        description: Tag to apply to the built image
  steps:
    - name: list-src
      image: alpine
      command:
        - "ls"
      args:
        - "$(inputs.resources.git-source.path)"
    - name: build-and-push
      image: gcr.io/kaniko-project/executor
      command:
        - /kaniko/executor
      args:
        - "--dockerfile=$(inputs.params.pathToDockerfile)"
        - "--destination=$(inputs.params.imageUrl):$(inputs.params.imageTag)"
        - "--context=$(inputs.resources.git-source.path)/$(inputs.params.pathToContext)/"
環境情報
$ kubectl get node
NAME            STATUS   ROLES    AGE   VERSION
...   Ready    <none>   xx   v1.20.8+IKS

入力情報を確認する

パラメータは下記でimageUrl/imageTagは明示的に指定する必要があり、それぞれdocker buildを行うパス、Dockerfile名、イメージパス、イメージタグが対応します。

name: pathToContext
default: .

name: pathToDockerfile
default: Dockerfile

name: imageUrl

name: imageTag

またリソースとしてgit-sourceが指定されているので同名で用意する必要があり

$ kubectl get pipelineresource
NAME   AGE
git    23h

そのパイプラインリソースの中では以下を記載する必要があり、revisionにはツリー名、urlにはGithubのURLを指定します。

  Params:
name:   revision
name:   url

入力情報の与え方

imageURLとimageTag、そしてGitのリソースの与え方はTaskをキックするPipelineRunに記述します。
YAMLを一部抜粋すると

apiVersion: tekton.dev/v1alpha1
kind: PipelineRun
..
spec:
  pipelineRef:
    name: xx
  resources:
    - name: git-source
      resourceRef:
        name: git
  params:
    - name: pathToContext
      value: "src"
    - name: "imageUrl"
      value: "jp.icr.io/xx/xx"
    - name: "imageTag"
      value: "1.0"
...

入力情報の使われ方

最初のコンテナがurlとrevision情報を元に「/workspace/git-source」というディレクトリにgit pullをして

$ kubectl logs application-pipeline-run-build-image-from-source-dp62m-po-8j6cd -c step-git-source-git-source-z42xm
{"level":"info",..."msg":"Successfully initialized and updated submodules in path /workspace/git-source"}

そのディレクトリをlsコマンドで表示します。

$ kubectl logs application-pipeline-run-build-image-from-source-dp62m-po-8j6cd -c step-list-src
...
src
...

そしてsrcで指定したDockerfileをimageurl/imagetagで指定した場所にPushします。

$ kubectl logs application-pipeline-run-build-image-from-source-dp62m-po-8j6cd -c step-build-and-push
INFO[0000] GET KEYCHAIN
INFO[0000] running on kubernetes ....
...
INFO[0010] Executing 0 build triggers
...
INFO[0035] Pushing image to jp.icr.io/xx:1.0
INFO[0042] Pushed image to 1 destinations

終わりに

TaskRun自体は内容を把握してなくても実施できますが

$ kubectl get taskrun
NAME                                                     SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
application-pipeline-run-build-image-from-source-dp62m   True        Succeeded   76m         75m

各コンテナのログを読むことで中で何が起きているかが確認できました。

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