Chapter 8. Adding when Expressions


Using when expressions with parameters


$ vi ~/tmp/logger.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
    name: logger
spec:
    params:
        - name: text
          type: string
    steps:
        - name: log
          image: registry.access.redhat.com/ubi8/ubi-minimal
          script: |
              DATE=$(date +%d/%m/%Y\ %T)
              echo [$DATE] - $(params.text)


$ kubectl create -f ~/tmp/logger.yaml


$ vi ~/tmp/guess.yaml


apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
    name: guess-game
spec:
    params:
        - name: number
          description: Pick a number
          type: string
    tasks:
        - name: win
          params:
              - name: text
                value: You win
          taskRef:
              name: logger
          when:
              - input: $(params.number)
                operator: in
                values: ['3']


$ kubectl create -f ~/tmp/guess.yaml


$ tkn pipeline start guess-game --showlog


? Value for param `number` of type `string`? 3

В общем только на 3 выводит лог.


Using the notin operator


$ vi ~/tmp/guess-notin.yaml


apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
    name: guess-game-notin
spec:
    params:
        - name: number
          description: Pick a number
          type: string
    tasks:
        - name: win
          params:
              - name: text
                value: You win
          taskRef:
              name: logger
          when:
              - input: $(params.number)
                operator: in
                values: ['3']
        - name: lose
          params:
              - name: text
                value: You lose
          taskRef:
              name: logger
          when:
              - input: $(params.number)
                operator: notin
                values: ['3']


$ kubectl create -f ~/tmp/guess-notin.yaml


$ tkn pipeline start guess-game-notin --showlog

Теперь 3 и не 3.


Using when expressions with results


$ vi ~/tmp/guess-result.yaml


apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
    name: random-number-generator
spec:
    results:
        - name: random-number
          description: random number
    steps:
        - name: generate-number
          image: registry.access.redhat.com/ubi8/ubi-minimal
          script: |
              NUMBER=$((1 + $RANDOM % 3))
              echo Random number picked, result is $NUMBER
              echo -n $NUMBER > $(results.random-number.path)
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
    name: guess-result
spec:
    tasks:
        - name: generate
          taskRef:
              name: random-number-generator
        - name: win
          params:
              - name: text
                value: You win
          taskRef:
              name: logger
          when:
              - input: $(tasks.generate.results.random-number)
                operator: in
                values: ['3']
          runAfter:
              - generate
        - name: lose
          params:
              - name: text
                value: You lose
          taskRef:
              name: logger
          when:
              - input: $(tasks.generate.results.random-number)
                operator: notin
                values: ['3']
          runAfter:
              - generate


$ kubectl create -f ~/tmp/guess-result.yaml


$ tkn pipeline start guess-result --showlog


Assessments


Hello Admin


Build a pipeline that will take a username as a parameter. If the username is admin, log the Hello Admin text. For any other username, output a simple Hello message.


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: logger
spec:
  params:
    - name: text
      type: string
  steps:
    - name: log
      image: registry.access.redhat.com/ubi8/ubi-minimal
      script: |
        DATE=$(date +%d/%m/%Y\ %T)
        echo [$DATE] - $(params.text)
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: hello-admin
spec:
  params:
    - name: username
      type: string
  tasks:
    - name: hello-admin
      taskRef:
        name: logger
      params:
        - name: text
          value: "Hello Admin"
      when:
        - input: $(params.username)
          operator: in
          values: ["admin"]
    - name: hello-other
      taskRef:
        name: logger
      params:
        - name: text
          value: "Hello User"
      when:
        - input: $(params.username)
          operator: notin
          values: ["admin"]
EOF


$ tkn pipeline start hello-admin --showlog
? Value for param `username` of type `string`? a3333333
PipelineRun started: hello-admin-run-j8b6v
Waiting for logs to be available...
[hello-other : log] ++ date '+%d/%m/%Y %T'
[hello-other : log] + DATE='24/10/2021 13:03:08'
[hello-other : log] + echo '[24/10/2021' '13:03:08]' - Hello User
[hello-other : log] [24/10/2021 13:03:08] - Hello User


$ tkn pipeline start hello-admin --showlog
? Value for param `username` of type `string`? admin
PipelineRun started: hello-admin-run-8d7jv
Waiting for logs to be available...
[hello-admin : log] ++ date '+%d/%m/%Y %T'
[hello-admin : log] [24/10/2021 13:04:31] - Hello Admin
[hello-admin : log] + DATE='24/10/2021 13:04:31'
[hello-admin : log] + echo '[24/10/2021' '13:04:31]' - Hello Admin


Critical Hit


In role-playing games using dice, rolling a 20 on a 20-sided dice is sometimes referred to as rolling a critical hit. For this exercise, build a pipeline that would log Critical Hit when the result of a dice roll is 20 . To do so, use a task that will generate a random number between 1 and 20 and produce a result that the when expression of a second task can pick up.


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: dice-roll-result
spec:
  params:
    - name: sides
      type: string
  results:
    - name: dice-roll
      description: Random number generated by the dice roll
  steps:
    - name: generate-random-number
      image: node:14
      script: |
        #!/usr/bin/env node
        const fs = require("fs");
        const max = $(params.sides)
        let randomNumber =  Math.floor(Math.random() * Math.floor(max)) + 1;
        fs.writeFile("$(results.dice-roll.path)", randomNumber.toString(), () => {
          console.log("Dice rolled");
        });
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: results
spec:
  params:
    - name: sides
      default: "20"
      type: "string"
  tasks:
    - name: intro
      params:
        - name: text
          value: "Preparing to roll the $(params.sides)-sided dice"
      taskRef:
        name: logger
    - name: roll
      params:
        - name: sides
          value: $(params.sides)
      taskRef:
        name: dice-roll-result
      runAfter:
        - intro
    - name: critical
      taskRef:
        name: logger
      params:
        - name: text
          value: "Critical hit!"
      when:
        - input: $(tasks.roll.results.dice-roll)
          operator: in
          values: ["20"]
      runAfter:
        - roll
    - name: result
      params:
        - name: text
          value: "Result from dice roll was $(tasks.roll.results.dice-roll)"
      taskRef:
        name: logger
      runAfter:
        - roll
EOF


$ tkn pipeline start results --showlog
? Value for param `sides` of type `string`? (Default is `20`) 20
PipelineRun started: results-run-xwnfq
Waiting for logs to be available...
[intro : log] ++ date '+%d/%m/%Y %T'
[intro : log] + DATE='24/10/2021 13:06:42'
[intro : log] + echo '[24/10/2021' '13:06:42]' - Preparing to roll the 20-sided dice
[intro : log] [24/10/2021 13:06:42] - Preparing to roll the 20-sided dice

[roll : generate-random-number] Dice rolled

[critical : log] ++ date '+%d/%m/%Y %T'
[critical : log] + DATE='24/10/2021 13:06:55'
[critical : log] + echo '[24/10/2021' '13:06:55]' - Critical 'hit!'
[critical : log] [24/10/2021 13:06:55] - Critical hit!

[result : log] ++ date '+%d/%m/%Y %T'
[result : log] + DATE='24/10/2021 13:06:56'
[result : log] [24/10/2021 13:06:56] - Result from dice roll was 20
[result : log] + echo '[24/10/2021' '13:06:56]' - Result from dice roll was 20


Not working on weekends


Even your servers deserve a break. Build a pipeline with a task that Tekton will only execute on weekdays. The task should log a Working message to simulate some work.


$ cat << 'EOF' | kubectl apply -f -
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: get-day
spec:
  results:
    - name: daynumber
      description: day of week, 0 is Sunday
  steps:
    - name: get-day
      image: registry.access.redhat.com/ubi8/ubi
      script: |
        date +%w | tr -d '\n' > $(results.daynumber.path)
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: not-on-weekends
spec:
  tasks:
    - name: getday
      taskRef:
        name: get-day
    - name: work
      taskRef:
        name: logger
      params:
        - name: text
          value: Working...
      when:
        - input: $(tasks.getday.results.daynumber)
          operator: in
          values: ["1", "2", "3", "4", "5"]
      runAfter:
        - getday
EOF


$ tkn pipeline start not-on-weekends --showlog
PipelineRun started: not-on-weekends-run-rpblg
Waiting for logs to be available...
[getday : get-day] + date +%w
[getday : get-day] + tr -d '\n'


$ tkn pr logs -f not-on-weekends-run-rpblg