Skip to content

Commit

Permalink
Tkn task working as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
Senjuti256 authored and vdemeester committed Dec 8, 2023
1 parent ee5a865 commit 80b7d66
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ BATS_FLAGS ?= --print-output-on-failure --show-output-of-passing-tests --verbose
E2E_TESTS ?= ./test/e2e/*.bats
E2E_OPENSHIFT_PARAMS_SCRIPT ?= oc version

E2E_TKN_PARAMS_SCRIPT ?= tkn version

# generic arguments employed on most of the targets
ARGS ?=

Expand Down Expand Up @@ -47,10 +49,15 @@ test-e2e: install
.PHONY: prepare-e2e-openshift
prepare-e2e-openshift:
./hack/install-osp.sh $(OSP_VERSION)

.PHONY: test-e2e-openshift
test-e2e-openshift: prepare-e2e-openshift
test-e2e-openshift: test-e2e

.PHONY: test-e2e-tkn
test-e2e-tkn: prepare-e2e-openshift
test-e2e-tkn: test-e2e

# act runs the github actions workflows, so by default only running the test workflow (integration
# and end-to-end) to avoid running the release workflow accidently
act: ARGS = --workflows=./.github/workflows/test.yaml
Expand Down
13 changes: 13 additions & 0 deletions scripts/tkn-client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

shopt -s inherit_errexit
set -eu -o pipefail

source "$(dirname ${BASH_SOURCE[0]})/common.sh"
source "$(dirname ${BASH_SOURCE[0]})/tkn-common.sh"

[[ "${WORKSPACES_KUBECONFIG_DIR_BOUND}" == "true" ]] && \
[[ -f ${WORKSPACES_KUBECONFIG_DIR_PATH}/kubeconfig ]] && \
export KUBECONFIG=${WORKSPACES_KUBECONFIG_DIR_PATH}/kubeconfig

${PARAMS_SCRIPT}
15 changes: 15 additions & 0 deletions scripts/tkn-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

declare -rx PARAMS_SCRIPT="${PARAMS_SCRIPT:-}"

declare -rx WORKSPACES_KUBECONFIG_DIR_PATH="${WORKSPACES_KUBECONFIG_DIR_PATH:-}"
declare -rx WORKSPACES_KUBECONFIG_DIR_BOUND="${WORKSPACES_KUBECONFIG_DIR_BOUND:-}"

#
# Asserting Environment
#

exported_or_fail \
WORKSPACES_KUBECONFIG_DIR_BOUND \
PARAMS_SCRIPT \

63 changes: 63 additions & 0 deletions templates/task-tkn.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: tkn
labels:
app.kubernetes.io/version: {{ .Chart.Version }}
{{- if .Values.annotations }}
annotations:
{{- .Values.annotations | toYaml | nindent 4 }}
{{- end }}
spec:
description: >-
This task performs operations on Tekton resources using tkn.
workspaces:
- name: kubeconfig_dir
optional: true
description: >-
An optional workspace that allows you to provide a .kube/config
file for tkn to access the cluster. The file should be placed at
the root of the Workspace with name kubeconfig.
params:
- name: SCRIPT
description: tkn CLI script to execute
type: string
default: "tkn $@"
- name: ARGS
type: array
description: tkn CLI arguments to run
default: ["--help"]

stepTemplate:
env:
{{- $variables := list
"params.SCRIPT"
"workspaces.kubeconfig_dir.bound"
"workspaces.kubeconfig_dir.path"
}}
{{- include "environment" ( list $variables ) | nindent 6 }}

steps:
{{- include "load_scripts" ( list . "tkn-" ) | nindent 4 }}

- name: tkn
image: {{ .Values.images.tkn }}
env:
- name: HOME
value: /tekton/home
command:
- /scripts/tkn-client.sh
args: ["$(params.ARGS)"]
securityContext:
runAsNonRoot: true
runAsUser: 65532
volumeMounts:
- name: scripts-dir
mountPath: /scripts

volumes:
- name: scripts-dir
emptyDir: {}
34 changes: 34 additions & 0 deletions test/e2e/resources/tkn-pipeline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
labels:
name: task-tkn
name: task-tkn
spec:
workspaces:
- name: kubeconfig_dir
optional: true

params:
- name: SCRIPT
type: string
default: "tkn $@"
- name: ARGS
type: array
default: ["--help"]

tasks:
- name: task-tkn
taskRef:
name: task-tkn
workspaces:
- name: kubeconfig_dir
workspace: kubeconfig_dir
params:
- name: SCRIPT
value: "$(params.SCRIPT)"
- name: ARGS
value: "$(params.ARGS)"


13 changes: 13 additions & 0 deletions test/e2e/resources/tkn-pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
name: task-tkn
name: task-tkn
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 250Mi
43 changes: 43 additions & 0 deletions test/e2e/tkn-e2e.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bats

source ./test/helper/helper.sh

# E2E tests parameters for the test pipeline

# Testing the tkn task,
@test "[e2e] tkn task" {
[ -n "${E2E_TKN_PARAMS_SCRIPT}" ]

run kubectl delete taskrun --all
assert_success

run tkn task start tkn \
--param="SCRIPT=${E2E_TKN_PARAMS_SCRIPT}" \
--use-param-defaults \
--skip-optional-workspace \
--showlog >&3
assert_success

# waiting a few seconds before asserting results
sleep 15

# assering the taskrun status, making sure all steps have been successful
declare tmpl_file="${BASE_DIR}/go-template.tpl"
# the following template is able to extract information from TaskRun and PipelineRun resources,
# and as well supports the current Tekton Pipeline version using a different `.task.results`
# attribute
cat >${tmpl_file} <<EOS
{{- range .status.conditions }}
{{- if and (eq .type "Succeeded") (eq .status "True") }}
{{- printf "%s\n" .message -}}
{{- end -}}
{{- end }}
{{- range .status.results }}
{{- printf "%s=%s\n" .name .value -}}
{{- end -}}
EOS

run tkn taskrun describe --last --output=go-template-file --template=${tmpl_file}
assert_success
assert_output "All Steps have completed executing"
}
3 changes: 2 additions & 1 deletion values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
images:
bash: registry.access.redhat.com/ubi8-minimal:latest
openshiftCli: registry.redhat.io/openshift4/ose-cli@sha256:3d5b31cc3fbf878015e5c3ed1d48379d74b15b77a1a823024a7a2b7cd5e2e86d
tkn: registry.redhat.io/openshift-pipelines/pipelines-cli-tkn-rhel8@sha256:72394dfaed68c4b6b490c3c971fb1d9f0139f8656f6672b55b8e02ea98d1298d

annotations:
tekton.dev/pipelines.minVersion: "0.17.0"
tekton.dev/tags: cli
tekton.dev/displayName: "openshift client"
tekton.dev/displayName: "CLI"

0 comments on commit 80b7d66

Please sign in to comment.