TektonをHelm化する際のpipelineとpipelinerunの起動順序を制御

はじめに

TektonをHelm化するにあたり、pipelineとpipelinerunの起動順序を制御できず、起動に失敗することがあった。その解決方法を模索した。

helmのリソースの起動順序

次のソースでHelm適用時のKubernetesのリソースの起動順序が記載されている。

github.com

var InstallOrder KindSortOrder = []string{
    "PriorityClass",
    "Namespace",
    "NetworkPolicy",
    "ResourceQuota",
    "LimitRange",
    "PodSecurityPolicy",
    "PodDisruptionBudget",
    "ServiceAccount",
    "Secret",
    "SecretList",
    "ConfigMap",
    "StorageClass",
    "PersistentVolume",
    "PersistentVolumeClaim",
    "CustomResourceDefinition",
    "ClusterRole",
    "ClusterRoleList",
    "ClusterRoleBinding",
    "ClusterRoleBindingList",
    "Role",
    "RoleList",
    "RoleBinding",
    "RoleBindingList",
    "Service",
    "DaemonSet",
    "Pod",
    "ReplicationController",
    "ReplicaSet",
    "Deployment",
    "HorizontalPodAutoscaler",
    "StatefulSet",
    "Job",
    "CronJob",
    "IngressClass",
    "Ingress",
    "APIService",
}

ここに記載が行われていない、カスタムリソースのTektonのpipelineとpipelinerunは特に制御されていないようである。実際にHelmで起動した際、5回中2回はpipelinerunがpipelineより先に起動し、pipelinerunがうまく動かなかった。

対策

helm.sh

上記のChart Hooksを使用して制御する。こちらを使うことでリリースのライフサイクルを操作することができる。 Chart Hooksのpost-installをpipelinerunに適用する。通常で動作するpipelineより。pipelinerunの起動順序が後段になるように設定する。

https://gitlab.com/InoueRio/public/-/blob/main/kubernetes/helm/clone-read/templates/piplinerun-clone-read.yaml#L8

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: clone-read-run
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-install
(省略)

これでpipelineとpipelinerunの順序で起動し、pipelinerunが起動に失敗しなくなった(5回中5回は成功)。

#install
$ helm install clone-read-v1 ../clone-read/

#動作確認
$ kubectl get pod,taskrun,pipelinerun
NAME                                    READY   STATUS      RESTARTS   AGE
pod/clone-read-run-fetch-source-pod     0/1     Completed   0          41s
pod/clone-read-run-show-readme-pod      0/1     Completed   0          23s

NAME                                             SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
taskrun.tekton.dev/clone-read-run-fetch-source   True        Succeeded   41s         23s
taskrun.tekton.dev/clone-read-run-show-readme    True        Succeeded   23s         6s

NAME                                    SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
pipelinerun.tekton.dev/clone-read-run   True        Succeeded   42s         6s

$ kubectl logs pod/clone-read-run-show-readme-pod
Defaulted container "step-read" out of: step-read, prepare (init), place-scripts (init)
# Tekton Catalog
(省略)

参考

qiita.com

inorio.hatenablog.com