Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[airgap] Fix values processing for 2A extensions #659

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 61 additions & 52 deletions scripts/bundle-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,63 +162,72 @@ echo -e "\nPulling images for HMC extensions...\n"

# Next, we need to build a list of images used by k0s extensions. Walk the
# templates directory and extract the images used by the extensions.
for template in $(find ${templates_dir} -name 'k0s*.yaml');
for template in $(find ${TEMPLATES_DIR} -mindepth 2 -name Chart.yaml -type f -exec dirname {} \;);
do
if [[ $template == *"k0smotron"* ]]; then
extensions_path=".spec.k0sConfig.spec.extensions.helm"
else
extensions_path=".spec.k0sConfigSpec.k0s.spec.extensions.helm"
fi

repos=$(grep -vw "{{" ${template} | $YQ e "${extensions_path}.repositories[] | [.url, .name] | join(\";\")")
for repo in $repos
for k0sconfig_path in $(find "${template}" -name 'k0s*.yaml');
do
url=${repo%;*}
chartname=${repo#*;}
version=$(grep -vw "{{" ${template} |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .version")
name=$(grep -vw "{{" ${template} |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .name")
grep -vw "{{" $template | $YQ e "${extensions_path}.charts[] | select(.chartname == \"*$chartname*\") | .values" > ${name}-values.yaml

if [[ $url == "" ]] || [[ $name == "" ]] || [[ $version == "" ]]; then
echo "Error: Failed to get URL, name, or version from ${template}"
exit 1
fi

# Use 'helm template' to get the images used by the extension.
if [[ $name == "kube-vip" ]]; then
# FIXME: This is a temporary workaround for kube-vip, if we use
# a custom image tag in the future we'll need to update this.
# kube-vip is a special case where our yaml values result in invalid
# Helm template output, for now render the YAML without values.
template_output=$(${HELM} template --repo ${url} --version ${version} ${name})
if [[ k0sconfig_path == *"k0smotron"* ]]; then
extensions_path=".spec.k0sConfig.spec.extensions.helm"
else
template_output=$(${HELM} template --repo ${url} --version ${version} ${name} --values ${name}-values.yaml)
if [[ $? -ne 0 ]]; then
echo "Error: Failed to get images from Helm template for ${name}, trying to output values with debug..."

template_output=$(${HELM} template --repo ${url} --version ${version} ${name} --values ${name}-values.yaml --debug)
if [[ $? -ne 0 ]]; then
echo "Error: Failed to get images from Helm template for ${name} with debug output"
exit 1
fi
fi
extensions_path=".spec.k0sConfigSpec.k0s.spec.extensions.helm"
fi

for image in $(${HELM} template --repo ${url} --version ${version} ${name} --values ${name}-values.yaml | $YQ -N e .spec.template.spec.containers[].image);
do
docker pull ${image}
if [[ $? -ne 0 ]]; then
echo "Error: Failed to pull ${image}"
exit 1
fi

EXTENSION_IMAGES_BUNDLED="$EXTENSION_IMAGES_BUNDLED $image"
done

rm $name-values.yaml
done
repos=$(grep -vw "{{" ${k0sconfig_path} | $YQ e "${extensions_path}.repositories[] | [.url, .name] | join(\";\")")
for repo in $repos
do
url=${repo%;*}
chartname=${repo#*;}
version=$(grep -vw "{{" ${k0sconfig_path} |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .version")
name=$(grep -vw "{{" ${k0sconfig_path} |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .name")

if [[ $name != "kube-vip" ]]; then
k0sconfig="${k0sconfig_path#$template/}"
echo -e "Processing k0sconfig file: ${k0sconfig}\n"
${HELM} template "${template}" --show-only "${k0sconfig}" |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*$chartname*\") | .values" > ${name}-values.yaml
fi

if [[ $url == "" ]] || [[ $name == "" ]] || [[ $version == "" ]]; then
echo "Error: Failed to get URL, name, or version from ${k0sconfig_path}"
exit 1
fi

# Use 'helm template' to get the images used by the extension.
if [[ $name == "kube-vip" ]]; then
# FIXME: This is a temporary workaround for kube-vip, if we use
# a custom image tag in the future we'll need to update this.
# kube-vip is a special case where our yaml values result in invalid
# Helm template output, for now render the YAML without values.
template_output=$(${HELM} template --repo ${url} --version ${version} ${name})
else
template_output=$(${HELM} template --repo ${url} --version ${version} ${name} --values ${name}-values.yaml)
if [[ $? -ne 0 ]]; then
echo "Error: Failed to get images from Helm template for ${name}, trying to output values with debug..."

template_output=$(${HELM} template --repo ${url} --version ${version} ${name} --values ${name}-values.yaml --debug)
if [[ $? -ne 0 ]]; then
echo "Error: Failed to get images from Helm template for ${name} with debug output"
exit 1
fi
fi
fi

for image in $(echo "${template_output}" | $YQ -N e .spec.template.spec.containers[].image);
do
docker pull ${image}
if [[ $? -ne 0 ]]; then
echo "Error: Failed to pull ${image}"
exit 1
fi

EXTENSION_IMAGES_BUNDLED="$EXTENSION_IMAGES_BUNDLED $image"
done

rm -f $name-values.yaml
done
done
done

echo -e "\nSaving images...\n"
Expand Down