-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep-chart.sh
91 lines (83 loc) · 3.14 KB
/
prep-chart.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
helmChartName="kube-prometheus-stack"
helmChartUrl="https://prometheus-community.github.io/helm-charts"
helmChartVersion="41.4.1"
targetRegistry="myacr.azurecr.io"
get_chart(){
helm repo add "$helmChartName-repo" $helmChartUrl
if [ ! -f "$helmChartName-$helmChartVersion.tgz" ]; then
helm pull $helmChartName-repo/$helmChartName --version $helmChartVersion
tar xfz $helmChartName-$helmChartVersion.tgz
else
echo "[Info] The chart's tarball is already present. No need to pull"
tar xfz $helmChartName-$helmChartVersion.tgz
fi
grep -q $helmChartName .gitignore || echo $helmChartName >> .gitignore
grep -q $helmChartName .gitignore || echo $helmChartName >> .gitignore
}
create_list(){
helm template $helmChartName \
| yq '..|.image? | select(.)' \
| sort -u \
| sed 's/---//' \
| sed -r '/^\s*$/d' > image-list.txt
}
create_values_file(){
mkdir ./tmp
echo "---" > generated_values.yaml
loopCounter=0
while read i; do
loopCounter=$(($loopCounter+1))
imageRepo=${i%:*}
imageWithoutRegistry=$(echo $imageRepo | sed "s/^[^\/]*\///g" )
newTargetImage=$targetRegistry/$imageWithoutRegistry
imageTag=${i#*:}
yamlPath=$(yq '.. | select(. == '\"$imageRepo\"') | path' $helmChartName/values.yaml \
| sed 's/-//; s/ //; s/^/./' \
| tr -d '\n')
yamlPathToImage=$(sed "s/\.image.*//g" <<< $yamlPath)
if [ ! -z $yamlPathToImage ]; then
yq -n '('$yamlPath' = '\"$newTargetImage\"')' > temp.yaml
cat temp.yaml | yq $yamlPathToImage'.image += {"tag": '\"$imageTag\"'}' >> ./tmp/generated_values-$loopCounter.yaml
else
echo "[Info] couldnt find image $imageRepo in main chart. will look into subchart"
subValuesFile=$(grep -inrl --include \values.yaml $imageRepo)
subChart=$(sed "s/\/values\.yaml//" <<< $subValuesFile | sed "s:.*/::" )
subchartYamlPath=.$subChart$(yq '.. | select(. == '\"$imageRepo\"') | path' $subValuesFile \
| sed 's/-//; s/ //; s/^/./' \
| tr -d '\n')
yq -n '('$subchartYamlPath' = '\"$newTargetImage\"')' > temp.yaml
cat temp.yaml | yq ${subchartYamlPath%.*}' += {"tag": '\"$imageTag\"'}' >> ./tmp/generated_values-$loopCounter.yaml
fi
rm -rf temp.yaml
done < image-list.txt
yq eval-all '. as $item ireduce ({}; . * $item )' ./tmp/*.yaml >> generated_values.yaml
rm -rf ./tmp
echo "[Info] Success! Here is the content of your generated_values.yaml:"
cat generated_values.yaml
}
import_to_acr(){
if [[ $(wc -l < image-list.txt) -gt 1 ]]; then
while read source; do
tag="$targetRegistry/$(printf -- "%s" "${source#*/}")"
echo "Building $source with tag $tag"
dockerfile=$(cat Dockerfile-label.template | sed -e "s#%%SOURCE%%#$source#g")
echo "$dockerfile"
echo
echo "$dockerfile" | docker build -t $tag -
if [ $? -eq 0 ]; then
echo "Build successful, now pushing"
docker push $tag
else
echo "Error, stopping script"
exit 1
fi
done < image-list.txt
else
echo "The image-list.txt file is empty or inexistant. Couldn't run the script."
fi
}
# get_chart
# create_list
# import_to_acr
create_values_file