Skip to content
CNPE · Chapter 8 of 22 Day 6: Tekton – closing the loop

Day 6: Tekton – closing the loop

Goal: A Tekton pipeline in the cluster clones the code of shop, builds an image, pushes it to Gitea’s registry and writes the new tag into the platform repo – Argo CD rolls it out to staging · Duration: approx. 90 min · Prerequisite: Day 5

Briefing

  • So far you have changed image tags by hand. In a platform, the CI does that: it builds an image from every commit and reports the new tag – with a commit to the GitOps repo. Argo CD takes over from there. The CI needs no access to the application’s cluster.
shop-app (Code) ──Pipeline──▶ Registry (Image shop:<commit>)
                      └──────▶ plattform (overlays/staging: newTag <commit>) ──Argo CD──▶ team-green-staging
  • Tekton is CI as Kubernetes resources:
Resource What
Task Sequence of steps; each step is a container, all run in the same pod
Pipeline Tasks with an order (runAfter) and data flow (results, workspaces)
PipelineRun / TaskRun One run with concrete parameters
Workspace Shared directory, usually a PVC
Result Small value that a task passes on to later ones (here: commit hash)
  • Credentials are attached to a ServiceAccount. Tekton automatically writes Secrets with certain annotations into every step as Git and registry logins.

Drill 1: The registry for the nodes

Gitea ships a container registry at the same address as the web UI: 172.16.0.126:30300. It speaks HTTP, not HTTPS. containerd tries HTTPS by default and therefore needs a host configuration – on all three nodes:

# on cp1, worker1 and worker2
# One directory per registry: name = host:port
sudo mkdir -p /etc/containerd/certs.d/172.16.0.126:30300
# server: address of the registry; host section: what is allowed over HTTP (pull, resolve)
printf 'server = "http://172.16.0.126:30300"\n\n[host."http://172.16.0.126:30300"]\n  capabilities = ["pull", "resolve"]\n' \
  | sudo tee /etc/containerd/certs.d/172.16.0.126:30300/hosts.toml
# Point the CRI plugin at exactly this directory (the default is a list with two paths)
sudo sed -i "s#config_path = '/etc/containerd/certs.d:/etc/docker/certs.d'#config_path = '/etc/containerd/certs.d'#" \
  /etc/containerd/config.toml
grep -n "config_path = '/etc" /etc/containerd/config.toml
sudo systemctl restart containerd

Expected:

server = "http://172.16.0.126:30300"

[host."http://172.16.0.126:30300"]
  capabilities = ["pull", "resolve"]
54:      config_path = '/etc/containerd/certs.d'

While this course was being built, it did not work with the default list '/etc/containerd/certs.d:/etc/docker/certs.d': every pull ended with http: server gave HTTP response to HTTPS client, even though ctr images pull --hosts-dir /etc/containerd/certs.d … worked. With one path, containerd 2.2 finds the directory 172.16.0.126:30300 – presumably the colon in the folder name clashes with the colon used as the list separator. Remember the error text; it always means “containerd does not know the HTTP registry”.

Save your progress? Sign up for free

Drill 2: Installing Tekton

# on cp1
# Tekton Pipelines: one release manifest with CRDs, controller and webhook
k apply -f https://infra.tekton.dev/tekton-releases/pipeline/previous/v1.18.0/release.yaml
k -n tekton-pipelines rollout status deploy/tekton-pipelines-controller --timeout=200s
k -n tekton-pipelines rollout status deploy/tekton-pipelines-webhook --timeout=200s
# CLI tkn; the archive names the architecture after uname -m (x86_64, aarch64)
ARCH=$(uname -m)
cd /tmp
curl -sSfLO https://github.com/tektoncd/cli/releases/download/v0.47.0/tkn_0.47.0_Linux_$ARCH.tar.gz
tar xzf tkn_0.47.0_Linux_$ARCH.tar.gz tkn
sudo install -m 755 tkn /usr/local/bin/tkn
cd ~
tkn version

Expected (abridged):

deployment "tekton-pipelines-controller" successfully rolled out
deployment "tekton-pipelines-webhook" successfully rolled out
Client version: 0.47.0
Pipeline version: v1.18.0

Save your progress? Sign up for free

Drill 3: The code of shop

So far shop ran with the stock nginx image. Now it gets its own content – a repo of its own with a Dockerfile:

# on cp1
# Create the repo shop-app in Gitea (empty)
curl -s -u platform:platform-lab-123 -H 'Content-Type: application/json' \
  -d '{"name":"shop-app","default_branch":"main"}' \
  http://172.16.0.126:30300/api/v1/user/repos | jq -r .clone_url
mkdir -p ~/cnpe/tag06 && cd ~/cnpe/tag06
#   -b main   branch is called main, as in Gitea
git init -q -b main shop-app && cd shop-app
cat > Dockerfile <<'EOF'
FROM nginx:1.29-alpine
COPY index.html /usr/share/nginx/html/index.html
EOF
echo '<h1>Shop von Team Green – Version 1</h1>' > index.html
git add -A && git commit -qm 'Shop Version 1'
git remote add origin http://172.16.0.126:30300/platform/shop-app.git
git push -q -u origin main
git log --oneline

Expected:

http://172.16.0.126:30300/platform/shop-app.git
32ee6c8 Shop Version 1

Save your progress? Sign up for free

Drill 4: Credentials for the pipeline

The pipeline runs in the platform’s ci namespace. It needs read access to shop-app, write access to plattform and push rights for the registry.

# on cp1
cd ~/cnpe/tag06
k create namespace ci
# Git login as a basic-auth Secret ...
k -n ci create secret generic gitea-zugang --type=kubernetes.io/basic-auth \
  --from-literal=username=platform --from-literal=password=platform-lab-123
# ... with an annotation: use it for this Git server
k -n ci annotate secret gitea-zugang tekton.dev/git-0=http://172.16.0.126:30300
# Registry login in the format of ~/.docker/config.json
k -n ci create secret docker-registry registry-zugang --docker-server=172.16.0.126:30300 \
  --docker-username=platform --docker-password=platform-lab-123
# ServiceAccount that both Secrets are attached to
k -n ci create serviceaccount pipeline
k -n ci patch serviceaccount pipeline -p '{"secrets":[{"name":"gitea-zugang"},{"name":"registry-zugang"}]}'
k -n ci get sa pipeline -o jsonpath='{.secrets}{"\n"}'

Expected:

namespace/ci created
secret/gitea-zugang created
secret/gitea-zugang annotated
secret/registry-zugang created
serviceaccount/pipeline created
serviceaccount/pipeline patched
[{"name":"gitea-zugang"},{"name":"registry-zugang"}]

Save your progress? Sign up for free

Drill 5: Three tasks

# on cp1
cat > tasks.yaml <<'EOF'
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: klonen
  namespace: ci
spec:
  params:
  - name: url
  - name: revision
    default: main
  workspaces:
  - name: quelle                       # clone into here
  results:
  - name: commit
    description: kurzer Hash des geklonten Commits
  steps:
  - name: clone
    image: alpine/git:2.54.1
    workingDir: $(workspaces.quelle.path)
    script: |
      set -e
      git clone -q --depth 1 --branch $(params.revision) $(params.url) .
      # Write the result: a file whose content Tekton passes on
      git rev-parse --short HEAD | tr -d "\n" | tee $(results.commit.path)
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: bauen
  namespace: ci
spec:
  params:
  - name: image
  - name: tag
  workspaces:
  - name: quelle
  steps:
  - name: build-push
    image: quay.io/buildah/stable:v1.46.1
    workingDir: $(workspaces.quelle.path)
    securityContext:
      privileged: true                 # in the lab, Buildah needs full privileges (see Enterprise Note)
    script: |
      set -e
      # vfs: simple storage without overlay-in-overlay; bud = build using Dockerfile
      buildah --storage-driver=vfs bud -q -t $(params.image):$(params.tag) .
      # The registry speaks HTTP
      buildah --storage-driver=vfs push --tls-verify=false $(params.image):$(params.tag)
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: ausrollen
  namespace: ci
spec:
  params:
  - name: repo
  - name: datei                        # Kustomization whose image tag gets set
  - name: image
  - name: tag
  workspaces:
  - name: plattform
  steps:
  - name: clone
    image: alpine/git:2.54.1
    workingDir: $(workspaces.plattform.path)
    script: |
      git clone -q --depth 1 $(params.repo) .
  - name: tag-setzen
    image: mikefarah/yq:4.54.3
    workingDir: $(workspaces.plattform.path)
    securityContext:
      runAsUser: 0                     # files belong to root (from the git step); otherwise yq runs as 1000
    script: |
      yq -i ".images[0].newName = \"$(params.image)\" | .images[0].newTag = \"$(params.tag)\"" $(params.datei)
      cat $(params.datei)
  - name: commit-push
    image: alpine/git:2.54.1
    workingDir: $(workspaces.plattform.path)
    script: |
      set -e
      git config user.name "Tekton"
      git config user.email [email protected]
      git commit -qam "staging: shop auf $(params.tag) (Pipeline)"
      git push -q origin HEAD:main
      git log --oneline -1
EOF
k apply -f tasks.yaml
tkn task list -n ci

Expected:

task.tekton.dev/klonen created
task.tekton.dev/bauen created
task.tekton.dev/ausrollen created
NAME        DESCRIPTION   AGE
ausrollen                 1 second ago
bauen                     1 second ago
klonen                    1 second ago

Without runAsUser: 0 in the yq step, the run fails with open …/.yq-inplace-…: permission denied: the yq image runs as user 1000, the git step before it as root. Steps in a task share the file system, but not the user.

Save your progress? Sign up for free

Drill 6: Pipeline and first run

# on cp1
cat > pipeline.yaml <<'EOF'
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: shop
  namespace: ci
spec:
  params:
  - name: image
    default: 172.16.0.126:30300/platform/shop   # registry/owner/name
  workspaces:
  - name: arbeit
  tasks:
  - name: klonen
    taskRef: {name: klonen}
    params:
    - {name: url, value: http://172.16.0.126:30300/platform/shop-app.git}
    workspaces:
    - {name: quelle, workspace: arbeit, subPath: quelle}       # subfolder in the workspace
  - name: bauen
    runAfter: [klonen]
    taskRef: {name: bauen}
    params:
    - {name: image, value: $(params.image)}
    - {name: tag, value: $(tasks.klonen.results.commit)}        # result from klonen
    workspaces:
    - {name: quelle, workspace: arbeit, subPath: quelle}
  - name: ausrollen
    runAfter: [bauen]
    taskRef: {name: ausrollen}
    params:
    - {name: repo, value: http://172.16.0.126:30300/platform/plattform.git}
    - {name: datei, value: apps/team-green/shop/overlays/staging/kustomization.yaml}
    - {name: image, value: $(params.image)}
    - {name: tag, value: $(tasks.klonen.results.commit)}
    workspaces:
    - {name: plattform, workspace: arbeit, subPath: plattform}
EOF
k apply -f pipeline.yaml
cat > lauf.yaml <<'EOF'
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  generateName: shop-                  # every run gets a name of its own
  namespace: ci
spec:
  pipelineRef: {name: shop}
  taskRunTemplate:
    serviceAccountName: pipeline       # credentials from Drill 4
  workspaces:
  - name: arbeit
    volumeClaimTemplate:               # a fresh PVC per run
      spec:
        accessModes: [ReadWriteOnce]
        resources:
          requests: {storage: 1Gi}
EOF
# create instead of apply: generateName only works with create
k create -f lauf.yaml
# Follow the logs of the last run (-f), hide the lines with layer copies
tkn pipelinerun logs -n ci --last -f | grep -v -E 'Copying blob|^$'
tkn pipelinerun list -n ci

Expected (abridged):

pipeline.tekton.dev/shop created
pipelinerun.tekton.dev/shop-m9jx8 created
[klonen : clone] 32ee6c8
[bauen : build-push] 105acc37f3e183d3a615eaa7b20f3beed7a50efd8ddb8b846379d025c6c3a957
[bauen : build-push] Getting image source signatures
[bauen : build-push] Copying config sha256:105acc37f3e1...
[bauen : build-push] Writing manifest to image destination
[ausrollen : tag-setzen] namespace: team-green-staging
...
[ausrollen : tag-setzen] images:
[ausrollen : tag-setzen]   - name: nginx
[ausrollen : tag-setzen]     newTag: "32ee6c8"
[ausrollen : tag-setzen]     newName: 172.16.0.126:30300/platform/shop
[ausrollen : commit-push] 2103c3e staging: shop auf 32ee6c8 (Pipeline)
NAME         STARTED          DURATION   STATUS
shop-m9jx8   49 seconds ago   48s        Succeeded

newName replaces the image name nginx from the base with your own image – the base stays unchanged. yq adjusted the indentation of the lists; for Kustomize that is equivalent.

Save your progress? Sign up for free

Drill 7: Version 2 – full circle

# on cp1
cd ~/cnpe/tag06/shop-app
echo '<h1>Shop von Team Green – Version 2</h1>' > index.html
git commit -qam 'Shop Version 2' && git push -q
cd ..
k create -f lauf.yaml
tkn pipelinerun logs -n ci --last -f | grep -E 'klonen|commit-push'
# Make Argo CD check right away
argocd app get team-green-shop-staging --refresh >/dev/null
sleep 40
argocd app get team-green-shop-staging | grep -E 'Sync Status|Health Status'
k -n team-green-staging get deploy shop -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
k -n team-green-staging run probe --rm -i --restart=Never --image=curlimages/curl:8.17.0 \
  --command -- curl -s -m 5 http://shop/ 2>/dev/null | head -1

Expected:

pipelinerun.tekton.dev/shop-dslrx created
[klonen : clone] 820f70f
[ausrollen : commit-push] 73c7720 staging: shop auf 820f70f (Pipeline)
Sync Status:        Synced to main (73c7720)
Health Status:      Healthy
172.16.0.126:30300/platform/shop:820f70f
<h1>Shop von Team Green – Version 2</h1>

From the commit in the code to the new page in staging without a single kubectl command against team-green-staging. prod stays untouched – changes go there via pull request (Day 4).

Save your progress? Sign up for free

Exercise: What did the pipeline build?

Time limit: 5 minutes. List the images of shop in the registry and the duration of each task of the last run.

Solution

# on cp1
# Packages (container images) of the user platform via the Gitea API
curl -s -u platform:platform-lab-123 'http://172.16.0.126:30300/api/v1/packages/platform?type=container' \
  | jq -r '.[] | "\(.name) \(.version)"' | sort
# TaskRuns, newest first
tkn taskrun list -n ci | head -4

Expected (abridged):

shop 32ee6c8
shop 820f70f
NAME                   STARTED          DURATION   STATUS
shop-dslrx-ausrollen   1 minute ago     17s        Succeeded
shop-dslrx-bauen       2 minutes ago    1m8s       Succeeded
shop-dslrx-klonen      3 minutes ago    36s        Succeeded

Building takes the longest – vfs copies every layer in full. Cloning is slow because the pod first has to get the PVC and pull the image.

Save your progress? Sign up for free

Challenge

Time limit: 10 minutes. Every run leaves behind a PVC of 1 GiB. Find them and make sure that old runs disappear along with their PVCs.

Solution

# on cp1
k -n ci get pvc
# Delete PipelineRuns except the last two (--keep); their PVCs belong to them and go with them
tkn pipelinerun delete -n ci --keep 2 -f
k -n ci get pvc

Expected (abridged):

NAME             STATUS   VOLUME          CAPACITY   ...
pvc-cc01e5f634   Bound    pvc-a1a57890…   1Gi        ...
pvc-d68066583f   Bound    pvc-7868aa70…   1Gi        ...
pvc-f6f5177cc6   Bound    pvc-fe457380…   1Gi        ...
All but 2 PipelineRuns(Completed) deleted in namespace "ci"
NAME             STATUS   VOLUME          CAPACITY   ...
pvc-d68066583f   Bound    pvc-7868aa70…   1Gi        ...
pvc-f6f5177cc6   Bound    pvc-fe457380…   1Gi        ...

The PVCs from volumeClaimTemplate have the PipelineRun as their owner: if you delete the run, garbage collection cleans up the PVC. A permanent fix is the pruner of the Tekton Operator or a CronJob with tkn pipelinerun delete --keep.

Save your progress? Sign up for free

Checkpoint

# on cp1
# last run succeeded
tkn pipelinerun list -n ci --limit 1
# staging runs with its own image, prod still with nginx
k -n team-green-staging get deploy shop -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
k -n team-green get deploy shop -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
# on your machine
# containerd on every node with exactly one config_path
for ip in 126 228 83; do
  ssh -i ~/.ssh/cnpe [email protected].$ip "grep -c \"config_path = '/etc/containerd/certs.d'\$\" /etc/containerd/config.toml"
done

Expected (abridged):

NAME         STARTED          DURATION   STATUS
shop-dslrx   5 minutes ago    2m3s       Succeeded
172.16.0.126:30300/platform/shop:820f70f
nginx:1.29-alpine
1
1
1

Interview Note

  • Why does the CI write to Git instead of running kubectl set image? Git remains the single source of truth, Argo CD would otherwise revert the change, and the CI needs no cluster rights.
  • How do credentials get into a Tekton step? Through Secrets on the ServiceAccount; basic-auth with a tekton.dev/git-* annotation and dockerconfigjson become the Git and registry logins.
  • Why privileged for Buildah – and what instead? Building needs namespaces and mounts. Without privileges: rootless Buildah with user namespaces, Kaniko or BuildKit rootless.

Enterprise Note

  • Don’t start pipelines by hand: use Tekton Triggers with an EventListener that a Gitea webhook calls on every push.
  • Builds without privileged (rootless, a dedicated node pool for CI with taints) and a registry with TLS.
  • Manage the pipeline definitions themselves via GitOps; take tasks from a vetted catalog (Tekton Hub or Artifact Hub) instead of your own scripts.
  • Signing the image and generating an SBOM belong in the same pipeline – Day 14.

Contents

More