@@ -344,24 +344,48 @@ validate_version() {
344344# future.
345345validate_config () {
346346 log_info " Validating $1 config"
347+
348+ check_conditionals () {
349+ merged_config=" ${1} "
350+ template_config=" ${2} "
351+
352+ # Loop all lines in ${template_config} and checks if same option has conditional set-me in ${merged_config}
353+ options=" $( yq_read_block " ${template_config} " " set-me-if-*" ) "
354+ for opt in ${options} ; do
355+ opt_value=" $( yq4 " ${opt} " " ${merged_config} " ) "
356+ opt_value_no_list=" $( yq4 " [.] | flatten | .[0]" <<< " ${opt_value}" ) "
357+
358+ if [[ " ${opt_value_no_list} " =~ ^set-me-if-.* $ ]]; then
359+ required_condition=" $( sed -rn ' s/^set-me-if-(.*)/\1/p' <<< " ${opt_value_no_list}" ) "
360+ if [[ " $( yq4 " ${required_condition} " " ${merged_config} " ) " == " true" ]]; then
361+ # If the option is a list, set the first element in the list
362+ if [[ " $( yq4 " ${opt} | tag" " ${merged_config} " ) " == " !!seq" ]]; then
363+ yq4 " ${opt} [0] = \" set-me\" " -i " ${merged_config} "
364+ yq4 " ${opt} [0] = \" set-me\" " -i " ${template_config} "
365+ log_info " Set-me condition matched for ${opt} "
366+ else
367+ yq4 " ${opt} = \" set-me\" " -i " ${merged_config} "
368+ yq4 " ${opt} = \" set-me\" " -i " ${template_config} "
369+ log_info " Set-me condition matched for ${opt} "
370+ fi
371+ fi
372+ fi
373+ done
374+ }
375+
347376 validate () {
348377 merged_config=" ${1} "
349378 template_config=" ${2} "
350379
351380 # Loop all lines in ${template_config} and warns if same option is not available in ${merged_config}
352381 options=$( yq_read_block " ${template_config} " " set-me" )
353- maybe_exit=" false"
354382 for opt in ${options} ; do
355383 compare=$( diff <( yq4 -oj " ${opt} " " ${template_config} " ) <( yq4 -oj " ${opt} " " ${merged_config} " ) || true)
356384 if [[ -z " ${compare} " ]]; then
357385 log_warning " WARN: ${opt} is not set in config"
358386 maybe_exit=" true"
359387 fi
360388 done
361-
362- if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE} ; then
363- ask_abort
364- fi
365389 }
366390
367391 schema_validate () {
@@ -376,46 +400,50 @@ validate_config() {
376400 sed -r ' s/^.*_(..-config\.yaml): fail: (.*)/\1: \2/; / failed validation$/q' < " ${schema_validation_result} "
377401 grep -oP ' (?<=fail: )[^:]+' " ${schema_validation_result} " | sort -u |
378402 while read -r jpath; do
379- echo -n " .$jpath = "
380- yq4 -oj " .$jpath " " ${merged_config} "
403+ if [[ $jpath != " (root)" ]]; then
404+ echo -n " .$jpath = "
405+ yq4 -oj " .$jpath " " ${merged_config} "
406+ fi
381407 done
382408 maybe_exit=" true"
383409 fi
384-
385- if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE} ; then
386- ask_abort
387- fi
388410 }
389411
390412 template_file=$( mktemp --suffix=" -tpl.yaml" )
391413 append_trap " rm ${template_file} " EXIT
392414
415+ maybe_exit=" false"
393416 if [[ $1 == " sc" ]]; then
394417 check_config " ${config_template_path} /common-config.yaml" \
395418 " ${config_template_path} /sc-config.yaml" \
396419 " ${config_template_path} /secrets.yaml"
397420 yq_merge " ${config_template_path} /common-config.yaml" \
398421 " ${config_template_path} /sc-config.yaml" \
399422 > " ${template_file} "
400- validate " ${config[config_file_sc]} " " ${template_file} "
401- schema_validate " ${config[config_file_sc]} " " ${config_template_path} /schemas/config.yaml"
402- validate " ${secrets[secrets_file]} " " ${config_template_path} /secrets.yaml"
403- schema_validate " ${secrets[secrets_file]} " " ${config_template_path} /schemas/secrets.yaml"
423+ config_to_validate=" ${config[config_file_sc]} "
404424 elif [[ $1 == " wc" ]]; then
405425 check_config " ${config_template_path} /common-config.yaml" \
406426 " ${config_template_path} /wc-config.yaml" \
407427 " ${config_template_path} /secrets.yaml"
408428 yq_merge " ${config_template_path} /common-config.yaml" \
409429 " ${config_template_path} /wc-config.yaml" \
410430 > " ${template_file} "
411- validate " ${config[config_file_wc]} " " ${template_file} "
412- schema_validate " ${config[config_file_wc]} " " ${config_template_path} /schemas/config.yaml"
413- validate " ${secrets[secrets_file]} " " ${config_template_path} /secrets.yaml"
414- schema_validate " ${secrets[secrets_file]} " " ${config_template_path} /schemas/secrets.yaml"
431+ config_to_validate=" ${config[config_file_wc]} "
415432 else
416433 log_error " ERROR: usage validate_config <sc|wc>"
417434 exit 1
418435 fi
436+
437+ check_conditionals " ${config_to_validate} " " ${template_file} "
438+ validate " ${config_to_validate} " " ${template_file} "
439+ schema_validate " ${config_to_validate} " " ${config_template_path} /schemas/config.yaml"
440+ check_conditionals " ${secrets[secrets_file]} " " ${config_template_path} /secrets.yaml"
441+ validate " ${secrets[secrets_file]} " " ${config_template_path} /secrets.yaml"
442+ schema_validate " ${secrets[secrets_file]} " " ${config_template_path} /schemas/secrets.yaml"
443+
444+ if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE} ; then
445+ ask_abort
446+ fi
419447}
420448
421449validate_sops_config () {
0 commit comments