Skip to content

Commit

Permalink
Refactoring of code
Browse files Browse the repository at this point in the history
  • Loading branch information
Senjuti256 authored and vdemeester committed Nov 21, 2023
1 parent 87c1a15 commit d84f42c
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
name: test

on:
push:
tags-ignore:
- "*"
branches:
- main
pull_request:
branches:
- main

jobs:
test-e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true

- uses: azure/setup-helm@v3

# instantiate a KinD (Kubernetes in Docker) cluster, installs `kubectl` and configures the
# `kubeconfig` to reach the local cluster
- uses: helm/[email protected]
with:
cluster_name: kind
wait: 120s

# installs Tekton Pipelines and `tkn` command line, including a local Container-Registry with
# settings to reach it during testing
- uses: openshift-pipelines/setup-tektoncd@v1
with:
pipeline_version: v0.41.3

# running end-to-end test target
- name: test-e2e
run: make test-e2e
34 changes: 34 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

# tekton's home directory
declare -rx TEKTON_HOME="${TEKTON_HOME:-/tekton/home}"

#
# Functions
#

function fail() {
echo "ERROR: ${*}" 2>&1
exit 1
}

function phase() {
echo "---> Phase: ${*}..."
}

# assert local variables are exporeted on the environment
function exported_or_fail() {
declare -a _required_vars="${@}"

for v in ${_required_vars[@]}; do
[[ -z "${!v}" ]] &&
fail "'${v}' environment variable is not set!"
done

return 0
}





24 changes: 24 additions & 0 deletions scripts/oc-client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

shopt -s inherit_errexit
set -eu -o pipefail

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

[[ "$(workspaces.manifest_dir.bound)" == "true" ]] && \
cd $(workspaces.manifest_dir.path)

[[ "$(workspaces.kubeconfig_dir.bound)" == "true" ]] && \
[[ -f $(workspaces.kubeconfig_dir.path)/kubeconfig ]] && \
export KUBECONFIG=$(workspaces.kubeconfig_dir.path)/kubeconfig

"${params.SCRIPT}"








25 changes: 25 additions & 0 deletions scripts/oc-common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash


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

declare -rx WORKSPACES_MANIFEST_DIR_PATH="${WORKSPACES_MANIFEST_DIR_PATH:-}"
declare -rx WORKSPACES_MANIFEST_DIR_BOUND="${WORKSPACES_MANIFEST_DIR_BOUND:-}"
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_MANIFEST_DIR_PATH \
WORKSPACES_KUBECONFIG_DIR_PATH \
PARAMS_SCRIPT \
PARAMS_VERSION





21 changes: 21 additions & 0 deletions templates/environment.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{- /*
This template is meant to translate the Tekton placeholder utilized by the shell scripts, thus the
scripts can rely on a pre-defined and repetable way of consuming Tekton attributes.
Example:
The placeholder `workspaces.a.b` becomes `WORKSPACES_A_B`
*/ -}}

{{- define "environment" -}}
{{- range $v := index . 0 }}
- name: {{ $v | upper | replace "." "_" | replace "-" "_" }}
value: "$({{ $v }})"
{{- end }}
{{- end -}}





33 changes: 33 additions & 0 deletions templates/load-scripts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{- /*
Loads all script files into the "/scripts" mount point.
*/ -}}

{{- define "load_scripts" -}}
{{- $global := index . 0 -}}
- name: load-scripts
image: {{ $global.Values.images.bash }}
workingDir: /scripts
script: |
set -e
{{- range $i, $prefix := . -}}
{{- if gt $i 0 }}
{{- range $path, $content := $global.Files.Glob "scripts/*.sh" }}
{{- $name := trimPrefix "scripts/" $path }}
{{- if or ( hasPrefix $prefix $name ) ( hasPrefix "common" $name ) }}
printf '%s' "{{ $content | toString | b64enc }}" |base64 -d >{{ $name }}
{{- end }}
{{- end }}
chmod +x {{ $prefix }}*.sh
{{- end }}
{{- end }}
volumeMounts:
- name: scripts-dir
mountPath: /scripts
{{- end -}}





77 changes: 77 additions & 0 deletions templates/task-openshift-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: openshift-client
labels:
app.kubernetes.io/version: {{ .Chart.Version }}
{{- if .Values.annotations }}
annotations:
{{- .Values.annotations | toYaml | nindent 4 }}
{{- end }}
spec:
description: >-
This task runs commands against the cluster provided by user
and if not provided then where the Task is being executed.
OpenShift is a Kubernetes distribution from Red Hat which provides oc,
the OpenShift CLI that complements kubectl for simplifying deployment
and configuration applications on OpenShift.
workspaces:
- name: manifest_dir
optional: true
description: >-
The workspace which contains kubernetes manifests which we want to apply on the cluster.
- name: kubeconfig_dir
optional: true
description: >-
The workspace which contains the the kubeconfig file if in case we want to run the oc command on another cluster.
params:
- name: SCRIPT
description: The OpenShift CLI arguments to run
type: string
default: "oc help"
- name: VERSION
description: The OpenShift Version to use
type: string
default: "latest"

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

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

- name: oc
image: {{ .Values.images.openshift-cli }}
env:
- name: HOME
value: /tekton/home
command:
- /scripts/oc-client.sh
securityContext:
runAsNonRoot: true
runAsUser: 65532
volumeMounts:
- name: scripts-dir
mountPath: /scripts

volumes:
- name: scripts-dir
emptyDir: {}





Empty file added test/e2e/e2e.bats
Empty file.
38 changes: 38 additions & 0 deletions test/e2e/resources/pipeline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
labels:
name: task-openshift-client
name: task-openshift-client
spec:
workspaces:
- name: manifest-dir
optional: true
- name: kubeconfig-dir
optional: true

params:
- name: SCRIPT
type: string
default: "oc help"
- name: VERSION
type: string
default: "latest"

tasks:
- name: task-openshift-client
taskRef:
name: task-openshift-client
workspaces:
- name: manifest-dir
workspace: manifest-dir
- name: kubeconfig-dir
workspace: kubeconfig-dir
params:
- name: SCRIPT
value: "$(params.SCRIPT)"
- name: VERSION
value: "$(params.VERSION)"


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

load '../.bats/bats-support/load'
load '../.bats/bats-assert/load'
load '../.bats/bats-file/load'

# root directory for the test cases
export BASE_DIR=""

function setup() {
# creating a temporary directory before each test run below
BASE_DIR="$(mktemp -d ${BATS_TMPDIR}/bats.XXXXXX)"

chmod -R 777 ${BASE_DIR} >/dev/null

# making sure the tests will "fail fast", when a given test fails the suite stops
[ -n "${BATS_TEST_COMPLETED}" ] || touch ${BATS_PARENT_TMPNAME}.skip
}

function teardown() {
rm -rfv ${BASE_DIR} || true
}

# Assert elements of a Tekton resource (taskrun or pipelinerun) using bats framework, the first
# function argument represents the Tekton resource and the rest is passed to "assert_output".
function assert_tekton_resource () {
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.taskResults }}
{{- printf "%s=%s\n" .name .value -}}
{{- end -}}
{{- range .status.results }}
{{- printf "%s=%s\n" .name .value -}}
{{- end -}}
EOS

run tkn ${1} describe --last --output=go-template-file --template=${tmpl_file}
shift
assert_success
assert_output ${*}
}
2 changes: 1 addition & 1 deletion values.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
images:
bash: registry.access.redhat.com/ubi8-minimal:latest
openshift-cli: registry.redhat.io/openshift-pipelines/pipelines-cli-tkn-rhel8
openshift-cli: registry.redhat.io/openshift3/ose-cli

annotations:
tekton.dev/pipelines.minVersion: "0.17.0"
Expand Down

0 comments on commit d84f42c

Please sign in to comment.