Last active
March 20, 2022 12:59
-
-
Save MikaelElkiaer/2da7e96df4e3a2e07373623bc06fb3c5 to your computer and use it in GitHub Desktop.
Flux2 migrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
POSTFIX=${1:-"-dev"} | |
IMAGE_REGEX="^(?P<ts>[0-9]{4}\.[0-9]{2}\.[0-9]{2}\.[0-9]{2}\.[0-9]{2})_[a-f0-9]+${POSTFIX}\$" | |
IMAGE_EXTRACT='$ts' | |
for f in $(fd -g "**/*.yaml"); do | |
AUTOMATED=$(cat $f | yq -r 'select(.metadata.annotations?["fluxcd.io/automated"]=="true") | .metadata.name+"^"+.metadata.namespace') | |
for o in $AUTOMATED; do | |
if [[ -n "$o" ]]; then | |
NAME=$(echo $o | awk -F "^" '{print $1}') | |
NAMESPACE=$(echo $o | awk -F "^" '{print $2}') | |
IMAGEPOLICY="$(dirname $f)/${NAME}_imagepolicy.yaml" | |
echo "Creating policy: $IMAGEPOLICY" | |
flux create image policy "$NAME" -n "$NAMESPACE" --image-ref "$NAME" --select-alpha asc --filter-regex "$IMAGE_REGEX" --filter-extract "$IMAGE_EXTRACT" --export > "$IMAGEPOLICY" | |
fi | |
done | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo 'apiVersion: source.toolkit.fluxcd.io/v1beta1\nkind: HelmRepository\nmetadata:\n name: _NAME_\n namespace: _NAMESPACE_\nspec:\n interval: 1m\n url: _URL_\n' > helmrepository.template.yaml | |
&& for i in $(rg HelmRelease -l); do | |
cat $i | yq -r '.spec.chart.name+" "+.metadata.namespace+" "+.spec.chart.repository' | |
| read N NS R && cat helmrepository.yaml.template | |
| sed -e "s/_NAMESPACE_/$N/" -e "s/_NAME_/$NS/" -e "s,_URL_,$R," | |
> $(echo $i | sed "s/helmrelease/helmrepository/"); | |
done | |
&& rm helmrepository.template.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for f in $(fd -g '**/*.yaml'); do | |
IMAGES=$(cat $f | yq -r 'select(.kind|test("Deployment|DaemonSet|ReplicaSet|DaemonSet")) | .metadata.name as $n | .metadata.namespace as $ns | .spec.template.spec.containers[].image | $n+"^"+$ns+"^"+.') | |
for i in $IMAGES; do | |
echo "Image $i in file $f:" | |
NAME=$(echo $i | awk -F "^" '{print $1}') | |
NAMESPACE=$(echo $i | awk -F "^" '{print $2}') | |
IMAGE=$(echo $i | awk -F "^" '{print $3}' | sed "s,^\(.*\):\([^/]*\)\$,\1 \2,") | |
REPO=$(echo $IMAGE | awk '{print $1}') | |
TAG=$(echo $IMAGE | awk '{print $2}') | |
TAG=${TAG:-"latest"} | |
IMAGEREPO="$(dirname $f)/${NAME}_imagerepository.yaml" | |
echo "Creating file: $IMAGEREPO" | |
flux create image repository "$NAME" --image "$REPO" --secret-ref regcred-backend -n "$NAMESPACE" --export > "$IMAGEREPO" | |
sed -i "s,$(echo $TAG | sed "s,\([.]\),\\\\\1,g"),$TAG # {\"\$imagepolicy\": \"$NAMESPACE:$NAME\"}," $f | |
done | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for f in $(fd -g '**/*.yaml'); do | |
IMAGES=$(cat $f | yq -r 'select(.kind|test("CronJob")) | .metadata.name as $n | .metadata.namespace as $ns | .spec.jobTemplate.spec.template.spec.containers[].image | $n+"^"+$ns+"^"+.') | |
for i in $IMAGES; do | |
echo "Image $i in file $f:" | |
NAME=$(echo $i | awk -F "^" '{print $1}') | |
NAMESPACE=$(echo $i | awk -F "^" '{print $2}') | |
IMAGE=$(echo $i | awk -F "^" '{print $3}' | sed "s,^\(.*\):\([^/]*\)\$,\1 \2,") | |
REPO=$(echo $IMAGE | awk '{print $1}') | |
TAG=$(echo $IMAGE | awk '{print $2}') | |
TAG=${TAG:-"latest"} | |
IMAGEREPO="$(dirname $f)/${NAME}_imagerepository.yaml" | |
echo "Creating file: $IMAGEREPO" | |
flux create image repository "$NAME" --image "$REPO" --secret-ref regcred-backend -n "$NAMESPACE" --export > "$IMAGEREPO" | |
sed -i "s,$(echo $TAG | sed "s,\([.]\),\\\\\1,g"),$TAG # {\"\$imagepolicy\": \"$NAMESPACE:$NAME\"}," $f | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment