|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +MAX_CALCULATED_LENGTH=47 |
| 4 | + |
| 5 | +print_explanation() { |
| 6 | + echo "--------------------------------------------------------------------------------" |
| 7 | + echo "Validation Explanation:" |
| 8 | + echo "This script ensures that generated Kubernetes resource names do not exceed the 63-character limit." |
| 9 | + echo "A DNS-compatible name is constructed in the 'clustergroup' Helm chart using the following pattern:" |
| 10 | + echo " -> {{ .Values.clusterGroup.name }}-gitops-server-{{ .Values.global.pattern }}-{{ .Values.clusterGroup.name }}" |
| 11 | + echo "" |
| 12 | + echo "The total length is calculated as:" |
| 13 | + echo " (2 * length of 'clusterGroup.name') + length of 'global.pattern' + 15 (for '-gitops-server-') + 1 (for the namespace separator '-')" |
| 14 | + echo "" |
| 15 | + echo "To stay under the 63-character limit, the variable part of the name must be less than $MAX_CALCULATED_LENGTH characters:" |
| 16 | + echo " (2 * length of 'clusterGroup.name') + length of 'global.pattern' < $MAX_CALCULATED_LENGTH" |
| 17 | + echo "--------------------------------------------------------------------------------" |
| 18 | +} |
| 19 | + |
| 20 | +if [ ! -f "values-global.yaml" ]; then |
| 21 | + echo "Error: Global values file 'values-global.yaml' not found." |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +global_pattern=$(yq .global.pattern "values-global.yaml") |
| 26 | + |
| 27 | +if [ "$global_pattern" == "null" ] || [ -z "$global_pattern" ]; then |
| 28 | + echo "Error: '.global.pattern' not found or is empty in 'values-global.yaml'." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | +pattern_length=${#global_pattern} |
| 32 | + |
| 33 | +echo "Validating that the pattern and clustergroup names don't exceed DNS limits after the pattern is installed." |
| 34 | +echo "" |
| 35 | + |
| 36 | +validation_failed=false |
| 37 | + |
| 38 | +for file in values-*.yaml; do |
| 39 | + group_name=$(yq .clusterGroup.name "$file") |
| 40 | + |
| 41 | + if [ "$group_name" != "null" ] && [ -n "$group_name" ]; then |
| 42 | + group_name_length=${#group_name} |
| 43 | + total_length=$(( (2 * group_name_length) + pattern_length )) |
| 44 | + |
| 45 | + echo "Checking file: $file" |
| 46 | + |
| 47 | + if [ "$total_length" -ge "$MAX_CALCULATED_LENGTH" ]; then |
| 48 | + echo " -> FAILED: Length of clustergroup '$group_name' and pattern '$global_pattern' will exceed DNS limits in clustergroup chart. Please shorten one or both." |
| 49 | + echo "" |
| 50 | + validation_failed=true |
| 51 | + else |
| 52 | + echo " -> PASSED: Length of clustergroup '$group_name' and pattern '$global_pattern' are within clustergroup chart limits." |
| 53 | + echo "" |
| 54 | + fi |
| 55 | + fi |
| 56 | +done |
| 57 | + |
| 58 | +if $validation_failed; then |
| 59 | + echo "One or more cluster group names failed the length validation." |
| 60 | + print_explanation |
| 61 | + exit 1 |
| 62 | +else |
| 63 | + echo "All names are within clustergroup chart limits." |
| 64 | + exit 0 |
| 65 | +fi |
0 commit comments