Skip to content

Commit a1b23c8

Browse files
committed
add script to validate length of clustergroup chart and pattern names
1 parent 5330bd9 commit a1b23c8

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

Makefile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
NAME ?= $(shell basename "`pwd`")
1+
NAME ?= $(shell yq .global.pattern values-global.yaml)
22

33
ifneq ($(origin TARGET_SITE), undefined)
44
TARGET_SITE_OPT=--set main.clusterGroupName=$(TARGET_SITE)
@@ -189,13 +189,7 @@ validate-schema: ## validates values files against schema in common/clustergroup
189189

190190
.PHONY: validate-prereq
191191
validate-prereq: ## verify pre-requisites
192-
$(eval GLOBAL_PATTERN := $(shell yq -r .global.pattern values-global.yaml))
193-
@if [ $(NAME) != $(GLOBAL_PATTERN) ]; then\
194-
echo "";\
195-
echo "WARNING: folder directory is \"$(NAME)\" and global.pattern is set to \"$(GLOBAL_PATTERN)\"";\
196-
echo "this can create problems. Please make sure they are the same!";\
197-
echo "";\
198-
fi
192+
@common/scripts/validate-names-length.sh
199193
@if [ ! -f /run/.containerenv ]; then\
200194
echo "Checking prerequisites:";\
201195
echo -n " Check for python-kubernetes: ";\

scripts/validate-names-length.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)