Chapter 6. Debugging and Cleaning Up Pipelines and Tasks


Делаю:
2025.12.11


Debugging pipelines


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: invalid-image
spec:
  steps:
    - name: log
      image: invaliduser/nonexistingimage
      command:
        - /bin/bash
      args: ['-c', 'echo "this task will fail"']
EOF


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: failing
spec:
  tasks:
    - name: fail
      taskRef:
        name: invalid-image
EOF


$ tkn pipeline start failing --showlog


$ tkn pipelinerun ls


$ tkn pipelinerun logs failing-run-jp2nj


$ kubectl get pods


$ kubectl describe pod/failing-run-jldmw-fail-58ng8-pod-9rtx5


$ tkn pipelinerun cancel failing-run-jldmw


Установить TimeOut


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: failing
spec:
  tasks:
    - name: fail
      timeout: "0h0m30s"
      taskRef:
        name: invalid-image
EOF


Running a halting task


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: log-and-exit
spec:
  params:
    - name: text
      type: string
    - name: exitcode
      type: string
  steps:
    - name: log
      image: registry.access.redhat.com/ubi8/ubi
      command:
        - /bin/bash
      args: ['-c', 'echo $(params.text)']
    - name: exit
      image: registry.access.redhat.com/ubi8/ubi
      command:
        - /bin/bash
      args: [
        "-c",
        "echo 'Exiting with code $(params.exitcode)' && exit $(params.exitcode)"
      ]
EOF


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: exitcodes
spec:
  tasks:
    - name: clone
      taskRef:
        name: log-and-exit
      params:
        - name: text
          value: "Simulating git clone"
        - name: exitcode
          value: "0"
    - name: unit-tests
      taskRef:
        name: log-and-exit
      params:
        - name: text
          value: "Simulating unit testing"
        - name: exitcode
          value: "1"
      runAfter:
        - clone
    - name: deploy
      taskRef:
        name: log-and-exit
      params:
        - name: text
          value: "Simulating deployment"
        - name: exitcode
          value: "0"
      runAfter:
        - unit-tests
EOF


$ tkn pipeline start exitcodes --showlog


Нужно руками прописывать значение отличное от 0, чтобы поймать ошибку.


$ kubectl get pipelinerun


$ tkn pipelinerun describe exitcodes-run-sl94w


$ kubectl logs exitcodes-run-sl94w-unit-tests-pod -c step-exit
Exiting with code 1


Adding a finally task


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: cleanup
spec:
  steps:
    - name: clean
      image: registry.access.redhat.com/ubi8/ubi
      command:
        - /bin/bash
      args: ['-c', 'echo Cleaning up!']
EOF


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: exitcodes
spec:
  tasks:
    - name: clone
      taskRef:
        name: log-and-exit
      params:
        - name: text
          value: "Simulating git clone"
        - name: exitcode
          value: "0"
    - name: unit-tests
      taskRef:
        name: log-and-exit
      params:
        - name: text
          value: "Simulating unit testing"
        - name: exitcode
          value: "1"
      runAfter:
        - clone
    - name: deploy
      taskRef:
        name: log-and-exit
      params:
        - name: text
          value: "Simulating deployment"
        - name: exitcode
          value: "0"
      runAfter:
        - unit-tests
  finally:
    - name: cleanup-task
      taskRef:
        name: cleanup
EOF


$ tkn pipeline start exitcodes --showlog


Assessments


Fail if root


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: fail-if-root
spec:
  steps:
    - name: fail-if-root
      image: registry.access.redhat.com/ubi8/ubi
      script: |
        if [ $(whoami) == "root" ]
          then
            echo "User is root"
            exit 1
        fi
        exit 0
---
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: check-root
spec:
  tasks:
    - name: is-root
      taskRef:
        name: fail-if-root
EOF


$ tkn pipeline start check-root --showlog


PipelineRun started: check-root-run-vqv8p
Waiting for logs to be available...
task is-root has failed: "step-fail-if-root" exited with code 1: Error
[is-root : fail-if-root] User is root


Make your bets


Task logger должна быть создана.


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: draw-card
spec:
  results:
    - name: card
      description: Card value
  steps:
    - name: randomize
      image: node:14
      script: |
        #!/usr/bin/env node
        const fs = require("fs");
        const cardValue = Math.floor(Math.random() * 10) + 1;
        fs.writeFileSync("$(results.card.path)", cardValue.toString());
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: check-result
spec:
  params:
    - name: new-card
      type: string
  steps:
    - name: add-card
      image: registry.access.redhat.com/ubi8/ubi
      script: |
        HAND=17
        NEWCARD=$(params.new-card)
        NEWHAND=$(($HAND+$NEWCARD))
        echo "New hand value is $NEWHAND"
        if (($NEWHAND > 21))
          then
            echo "Busted"
            exit 1
        fi
        echo "You won"
        exit 0
---
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: make-your-bets
spec:
  tasks:
    - name: draw
      taskRef:
        name: draw-card
    - name: check
      taskRef:
        name: check-result
      params:
        - name: new-card
          value: $(tasks.draw.results.card)
      runAfter:
        - draw
  finally:
    - name: clean
      taskRef:
        name: logger
      params:
        - name: text
          value: "Cleaning up the table"
EOF


$ tkn pipeline start make-your-bets --showlog


$ kubectl get pipelinerun


[check : add-card] New hand value is 20
[check : add-card] You won

[clean : log] [12/12/2025 00:03:21] - Cleaning up the table