Skip to content

Commit 7e92e29

Browse files
authored
Add script to validate length of clustergroup chart and pattern names (#643)
* add script to validate length of clustergroup chart and pattern names * enforce that pattern name is set in values-global.yaml * update ci workflow to test common updates on MCG * reference utility container from validatedpatterns quay org * add arm runner for common scripts test
1 parent 5330bd9 commit 7e92e29

File tree

4 files changed

+85
-18
lines changed

4 files changed

+85
-18
lines changed

.github/workflows/pattern-sh-ci.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
matrix:
2020
# Fedora is not an option yet
21-
os: [ubuntu-latest, ubuntu-22.04]
21+
os: [ubuntu-latest, ubuntu-22.04, ubuntu-24.04-arm]
2222
runs-on: ${{ matrix.os }}
2323
permissions:
2424
contents: read
@@ -29,12 +29,6 @@ jobs:
2929
with:
3030
persist-credentials: false
3131

32-
- name: Install Podman on Ubuntu
33-
if: contains(matrix.os, 'ubuntu')
34-
run: |
35-
sudo apt-get update
36-
sudo apt-get install -y podman
37-
3832
# Currently we do not do MacOSX as it is not free, maybe in the future
3933
# - name: Install Podman on macOS
4034
# if: contains(matrix.os, 'macos')
@@ -46,7 +40,14 @@ jobs:
4640
- name: Verify Podman Installation
4741
run: podman --version
4842

43+
- name: Clone MCG and update common
44+
run: |
45+
git clone --depth 1 https://github.com/hybrid-cloud-patterns/multicloud-gitops mcg
46+
cp -r scripts/ mcg/common/scripts
47+
cp Makefile mcg/common
48+
4949
- name: Run pattern.sh script
5050
run: |
51-
export TARGET_BRANCH=main
52-
./scripts/pattern-util.sh make validate-origin
51+
cd mcg
52+
./pattern.sh make validate-origin
53+
./pattern.sh make show

Makefile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
NAME ?= $(shell basename "`pwd`")
1+
NAME ?= $(shell yq .global.pattern values-global.yaml)
2+
3+
ifeq ($(NAME),)
4+
$(error Pattern name MUST be set in values-global.yaml with the value .global.pattern)
5+
endif
6+
ifeq ($(NAME),null)
7+
$(error Pattern name MUST be set in values-global.yaml with the value .global.pattern)
8+
endif
29

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

190197
.PHONY: validate-prereq
191198
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
199+
@common/scripts/validate-names-length.sh
199200
@if [ ! -f /run/.containerenv ]; then\
200201
echo "Checking prerequisites:";\
201202
echo -n " Check for python-kubernetes: ";\

scripts/pattern-util.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function version {
99
}
1010

1111
if [ -z "$PATTERN_UTILITY_CONTAINER" ]; then
12-
PATTERN_UTILITY_CONTAINER="quay.io/hybridcloudpatterns/utility-container"
12+
PATTERN_UTILITY_CONTAINER="quay.io/validatedpatterns/utility-container"
1313
fi
1414
# If PATTERN_DISCONNECTED_HOME is set it will be used to populate both PATTERN_UTILITY_CONTAINER
1515
# and PATTERN_INSTALL_CHART automatically

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)