Skip to content

Commit b26910a

Browse files
tkdchenchmeliik
authored andcommitted
Make validate-migration.sh work with new task dir layout
STONEBLD-3903 According to ADR #54 Start versioning Tekton Tasks responsibly, a task directory will be simplified without versioned task directory: task └── hello └── hello.yaml This patch updates validate-migration.sh script to work with both current versioned-specific task directory layout and the new one. * Add a new function construct_task_file_path to build expected task file path from given migration file path. * Rewrite test check_migrations_is_in_task_version_specific_dir and rename it to check_migrations_dir_alongside_task_file. * Update check_version_match by calling the new function construct_task_file_path. * Update test check_oci_ta_migration. Signed-off-by: Chenxiong Qi <[email protected]>
1 parent 118dd79 commit b26910a

File tree

2 files changed

+82
-64
lines changed

2 files changed

+82
-64
lines changed

hack/validate-migration.sh

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,41 @@ resolve_migrations_parent_dir() {
197197
echo "${dir_path%/*}" # remove path component migrations/
198198
}
199199

200-
# Check a migration file is included in a task version-specific directory.
201-
# Each task version has its own migrations. Developers have to create
202-
# migrations/ directory under a specific task version directory, for example
203-
# task/buildah/0.2/migrations/.
204-
check_migrations_is_in_task_version_specific_dir() {
200+
# Construct expected task file path according to given migration file.
201+
# Arguments: a migration file path, e.g. task/foo/migrations/0.3.sh
202+
construct_task_file_path() {
205203
local -r migration_file=$1
206-
local -r parent_dir=$(resolve_migrations_parent_dir "$migration_file")
207-
local -r result=$(find task/*/* -type d -regex "$parent_dir" -print -quit)
208-
if [ -z "$result" ]; then
209-
info "${FUNCNAME[0]}: migrations/ directory is not created in a task version-specific directory. Current is under $parent_dir. To fix it, move it to a path like task/task-1/0.1/."
204+
local task_dir
205+
local task_file
206+
local task_name
207+
208+
task_name=${migration_file#task/}
209+
task_name=${task_name%%/*}
210+
task_dir=${migration_file%/migrations/*}
211+
printf "%s" "${task_dir}/${task_name}.yaml"
212+
}
213+
214+
# Directory migrations/ must be present alongside task file.
215+
# In version-specific task directory, it is:
216+
# task/<name>/<version>/<name>.yaml
217+
# task/<name>/<version>/migrations/
218+
# Otherwise, it is:
219+
# task/<name>/<name>.yaml
220+
# task/<name>/migrations/
221+
# Arguments: a migration file path, e.g. task/foo/migrations/0.3.sh
222+
check_migrations_dir_alongside_task_file() {
223+
local -r migration_file=$1
224+
local task_file
225+
226+
task_file=$(construct_task_file_path "$migration_file")
227+
228+
if [ ! -f "$task_file" ]; then
229+
error "Migration $migration_file is not present alongside task file $task_file."
210230
exit 1
211231
fi
212232
}
213233

234+
214235
# Check that version within the migration file name must match the task version
215236
# in task label .metadata.labels."app.kubernetes.io/version".
216237
# When a migration file is committed, the task version must be bumped properly
@@ -219,14 +240,15 @@ check_version_match() {
219240
local -r migration_file=$1
220241
local -r task_dir=$(resolve_migrations_parent_dir "$migration_file")
221242

222-
local task_name=${task_dir%/*} # remove version part
223-
task_name=${task_name##*/} # remove all path components before the name
243+
local task_name
244+
task_name=${migration_file#task/}
245+
task_name=${task_name%%/*}
224246

225247
local task_version=
226248
local -r label=".metadata.labels.\"${LABEL_TASK_VERSION}\""
227249

228250
if is_normal_task "$task_dir" "$task_name" ; then
229-
task_version=$(yq "$label" "${task_dir}/${task_name}.yaml")
251+
task_version=$(yq "$label" "$(construct_task_file_path "$migration_file")")
230252
elif is_kustomized_task "$task_dir" "$task_name"; then
231253
task_version=$(kubectl kustomize "$task_dir" | yq "$label")
232254
else
@@ -345,31 +367,18 @@ check_apply_in_real_cluster() {
345367
# Arguments: the normal-task migration file path
346368
check_oci_ta_migration() {
347369
local migration_file=$1
348-
# e.g. migration_file=task/foo/0.2/migrations/0.2.1.sh
349-
local parent_dir
350-
parent_dir=$(resolve_migrations_parent_dir "$migration_file") # => task/foo/0.2
370+
local oci_migration_file
351371

352-
# get the “foo” and “0.2”
353-
local task_dir="${parent_dir%/*}" # => task/foo
354-
local task_name
355-
task_name=$(basename "$task_dir") # => foo
356-
local version
357-
version=$(basename "$parent_dir") # => 0.2
358-
359-
# path to the OCI-TA variant and the migrations:
360-
local oci_task_dir="${task_dir}-oci-ta/${version}"
361-
local oci_mig_dir="${oci_task_dir}/migrations"
372+
# Add suffix oci-ta to task name
373+
oci_migration_file=$(sed -E 's|task/([^/]+)(.+)|task/\1-oci-ta\2|' <<<"$migration_file")
374+
oci_task_dir=${oci_migration_file%/migrations/*.sh}
362375

363376
if [ ! -d "$oci_task_dir" ]; then
364-
info "OCI-TA variant not found for task '${task_name}' and version '${version}', skipping check."
377+
info "OCI-TA variant ${oci_task_dir} is not found, skipping check."
365378
return 0
366379
fi
367380

368381
# Check if the corresponding migration file exists in the OCI-TA variant
369-
local migration_script_name
370-
migration_script_name=$(basename "$migration_file")
371-
local oci_migration_file="${oci_mig_dir}/${migration_script_name}"
372-
373382
if [ ! -f "$oci_migration_file" ]; then
374383
error "OCI-TA variant of task '${task_name}' is missing migration script:"
375384
error " ${oci_migration_file}"
@@ -403,8 +412,8 @@ main() {
403412
info "check pass shellcheck"
404413
check_pass_shellcheck "$migration_file"
405414

406-
info "check migrations/ is created in versioned-specific task directory"
407-
check_migrations_is_in_task_version_specific_dir "$migration_file"
415+
info "check migrations/ is present alongside task file."
416+
check_migrations_dir_alongside_task_file "$migration_file"
408417

409418
info "check OCI-TA variant has migration script"
410419
check_oci_ta_migration "$migration_file"

{{cookiecutter.repo_root}}/hack/validate-migration.sh

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,41 @@ resolve_migrations_parent_dir() {
197197
echo "${dir_path%/*}" # remove path component migrations/
198198
}
199199

200-
# Check a migration file is included in a task version-specific directory.
201-
# Each task version has its own migrations. Developers have to create
202-
# migrations/ directory under a specific task version directory, for example
203-
# task/buildah/0.2/migrations/.
204-
check_migrations_is_in_task_version_specific_dir() {
200+
# Construct expected task file path according to given migration file.
201+
# Arguments: a migration file path, e.g. task/foo/migrations/0.3.sh
202+
construct_task_file_path() {
205203
local -r migration_file=$1
206-
local -r parent_dir=$(resolve_migrations_parent_dir "$migration_file")
207-
local -r result=$(find task/*/* -type d -regex "$parent_dir" -print -quit)
208-
if [ -z "$result" ]; then
209-
info "${FUNCNAME[0]}: migrations/ directory is not created in a task version-specific directory. Current is under $parent_dir. To fix it, move it to a path like task/task-1/0.1/."
204+
local task_dir
205+
local task_file
206+
local task_name
207+
208+
task_name=${migration_file#task/}
209+
task_name=${task_name%%/*}
210+
task_dir=${migration_file%/migrations/*}
211+
printf "%s" "${task_dir}/${task_name}.yaml"
212+
}
213+
214+
# Directory migrations/ must be present alongside task file.
215+
# In version-specific task directory, it is:
216+
# task/<name>/<version>/<name>.yaml
217+
# task/<name>/<version>/migrations/
218+
# Otherwise, it is:
219+
# task/<name>/<name>.yaml
220+
# task/<name>/migrations/
221+
# Arguments: a migration file path, e.g. task/foo/migrations/0.3.sh
222+
check_migrations_dir_alongside_task_file() {
223+
local -r migration_file=$1
224+
local task_file
225+
226+
task_file=$(construct_task_file_path "$migration_file")
227+
228+
if [ ! -f "$task_file" ]; then
229+
error "Migration $migration_file is not present alongside task file $task_file."
210230
exit 1
211231
fi
212232
}
213233

234+
214235
# Check that version within the migration file name must match the task version
215236
# in task label .metadata.labels."app.kubernetes.io/version".
216237
# When a migration file is committed, the task version must be bumped properly
@@ -219,14 +240,15 @@ check_version_match() {
219240
local -r migration_file=$1
220241
local -r task_dir=$(resolve_migrations_parent_dir "$migration_file")
221242

222-
local task_name=${task_dir%/*} # remove version part
223-
task_name=${task_name##*/} # remove all path components before the name
243+
local task_name
244+
task_name=${migration_file#task/}
245+
task_name=${task_name%%/*}
224246

225247
local task_version=
226248
local -r label=".metadata.labels.\"${LABEL_TASK_VERSION}\""
227249

228250
if is_normal_task "$task_dir" "$task_name" ; then
229-
task_version=$(yq "$label" "${task_dir}/${task_name}.yaml")
251+
task_version=$(yq "$label" "$(construct_task_file_path "$migration_file")")
230252
elif is_kustomized_task "$task_dir" "$task_name"; then
231253
task_version=$(kubectl kustomize "$task_dir" | yq "$label")
232254
else
@@ -345,31 +367,18 @@ check_apply_in_real_cluster() {
345367
# Arguments: the normal-task migration file path
346368
check_oci_ta_migration() {
347369
local migration_file=$1
348-
# e.g. migration_file=task/foo/0.2/migrations/0.2.1.sh
349-
local parent_dir
350-
parent_dir=$(resolve_migrations_parent_dir "$migration_file") # => task/foo/0.2
370+
local oci_migration_file
351371

352-
# get the “foo” and “0.2”
353-
local task_dir="${parent_dir%/*}" # => task/foo
354-
local task_name
355-
task_name=$(basename "$task_dir") # => foo
356-
local version
357-
version=$(basename "$parent_dir") # => 0.2
358-
359-
# path to the OCI-TA variant and the migrations:
360-
local oci_task_dir="${task_dir}-oci-ta/${version}"
361-
local oci_mig_dir="${oci_task_dir}/migrations"
372+
# Add suffix oci-ta to task name
373+
oci_migration_file=$(sed -E 's|task/([^/]+)(.+)|task/\1-oci-ta\2|' <<<"$migration_file")
374+
oci_task_dir=${oci_migration_file%/migrations/*.sh}
362375

363376
if [ ! -d "$oci_task_dir" ]; then
364-
info "OCI-TA variant not found for task '${task_name}' and version '${version}', skipping check."
377+
info "OCI-TA variant ${oci_task_dir} is not found, skipping check."
365378
return 0
366379
fi
367380

368381
# Check if the corresponding migration file exists in the OCI-TA variant
369-
local migration_script_name
370-
migration_script_name=$(basename "$migration_file")
371-
local oci_migration_file="${oci_mig_dir}/${migration_script_name}"
372-
373382
if [ ! -f "$oci_migration_file" ]; then
374383
error "OCI-TA variant of task '${task_name}' is missing migration script:"
375384
error " ${oci_migration_file}"
@@ -403,8 +412,8 @@ main() {
403412
info "check pass shellcheck"
404413
check_pass_shellcheck "$migration_file"
405414

406-
info "check migrations/ is created in versioned-specific task directory"
407-
check_migrations_is_in_task_version_specific_dir "$migration_file"
415+
info "check migrations/ is present alongside task file."
416+
check_migrations_dir_alongside_task_file "$migration_file"
408417

409418
info "check OCI-TA variant has migration script"
410419
check_oci_ta_migration "$migration_file"

0 commit comments

Comments
 (0)