Skip to main content

Mi Primera Task

Vamos directo a nuestro hello world. Como todo en Kubernetes es un manifiesto, vamos a desarrollar nuestro manifiesto para una task hello world.

Todo se mantiene como siempre en Kubernetes...

# --- BLOQUE ESTÁNDAR
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello-task
spec: # Aquí vamos a definir lo que queremos
# ---
cat <<EOF > taskHW.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello-task
spec:
steps: # Nuestra lista de steps
- name: hello
image: ubuntu
script: >
echo "Hello World"
EOF

kubectl create -f taskHW.yaml
task.tekton.dev/hello-task created

kubectl get tasks
NAME AGE
hello-task 4s

kubectl get tasks -n tekton-pipelines
No resources found in tekton-pipelines namespace.

kubectl describe task hello-task
Name: hello-task
Namespace: default
Labels: <none>
Annotations: <none>
API Version: tekton.dev/v1
Kind: Task
Metadata:
Creation Timestamp: 2024-07-31T11:24:23Z
Generation: 1
Resource Version: 1995385
UID: 8f1b52d9-b2a1-4df4-8b7a-1d3ee1c898c1
Spec:
Steps:
Compute Resources:
Image: ubuntu
Name: hello
Script: echo "Hello World"

Events: <none>

Como no definimos el namespace fue creado en default y podemos observar que Tekton separa estos recursos por namespace.

Esto es solo la definición de una task pero para ejecutar realmente una task es necesario un taskRun.

cat <<EOF > taskRunHW.yaml
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
# En este caso estamos declarando un prefijo para que genere el nombre
generateName: hello-task-
spec:
taskRef:
name: hello-task # Qué task se ejecutará
EOF

kubectl create -f taskRunHW.yaml
taskrun.tekton.dev/hello-task-46z7v created

kubectl get taskruns
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
hello-task-46z7v True Succeeded 49s 31s

kubectl logs hello-task-46z7v-pod
Defaulted container "step-hello" out of: step-hello, prepare (init), place-scripts (init)
Hello World

Vamos a crear con un step más para probar lo que tenemos.

cat <<EOF > taskHW2.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello-task2
spec:
steps:
- name: hello
image: ubuntu
script: >
echo "Hello World"
- name: world
image: ubuntu
script: >
echo "World hello"
EOF

kubectl create -f taskHW2.yaml
task.tekton.dev/hello-task2 created

kubectl get tasks
hello-task 19m
hello-task2 3s

cat <<EOF > taskRunHW2.yaml
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
generateName: hello-task2-
spec:
taskRef:
name: hello-task2
EOF

kubectl create -f taskRunHW2.yaml
taskrun.tekton.dev/hello-task2-fg8gh created

kubectl logs hello-task2-fg8gh-pod
Defaulted container "step-hello" out of: step-hello, step-world, prepare (init), place-scripts (init)
Hello World

# Observa en la salida anterior que trajo el log del primer step porque no pasamos cuál step.
# Pasando el step del que queremos el log
kubectl logs hello-task2-fg8gh-pod step-hello
Hello World

kubectl logs hello-task2-fg8gh-pod step-world
World hello

La CLI de Tekton viene en realidad como un facilitador que aplica filtros al comando kubectl.

tkn task list
NAME DESCRIPTION AGE
hello-task 1 hour ago
hello-task2 1 hour ago

kubectl get task
NAME AGE
hello-task 103m
hello-task2 83m

tkn task describe hello-task
Name: hello-task
Namespace: default

🦶 Steps

∙ hello

🗂 Taskruns

NAME STARTED DURATION STATUS
hello-pipeline-q5c4f-hello 14 minutes ago 9s Succeeded
hello-task-46z7v 1 hour ago 18s Succeeded

kubectl describe task hello-task
Name: hello-task
Namespace: default
Labels: <none>
Annotations: <none>
API Version: tekton.dev/v1
Kind: Task
Metadata:
Creation Timestamp: 2024-07-31T11:24:23Z
Generation: 1
Resource Version: 1995385
UID: 8f1b52d9-b2a1-4df4-8b7a-1d3ee1c898c1
Spec:
Steps:
Compute Resources:
Image: ubuntu
Name: hello
Script: echo "Hello World"

Events: <none>