Last active
February 8, 2024 10:55
-
-
Save lexfrei/9dbf5fa9bd8c1bfaab1288e00b629efe to your computer and use it in GitHub Desktop.
Script to find any images in your k8s based on CentOS 7
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 | |
| # This script will check all images used in the cluster and output the ones that are based on CentOS 7 to a file called centos7_images.txt | |
| # This is useful to check if you are using CentOS 7 images in your cluster, which will be EOL by the end of 2024 | |
| # Runs in parallel to speed up the process | |
| # On this step, we will check all images used in the cluster and will iterate over them to check if they are based on CentOS 7 | |
| kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" | tr -s '[:space:]' '\n' | sort | uniq | while read -r image; do | |
| { | |
| # Here we are generating a random string to use as a pod name | |
| # Needed to avoid pod name conflicts in parallel executions | |
| random_string=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 5 ; echo '') | |
| # We are using the /etc/redhat-release file to check if the image is based on CentOS 7 | |
| # If the file exists and contains "CentOS Linux release 7", we will output the image to centos7_images.txt | |
| kubectl run "tmp-check-pod-$random_string" --rm -i --tty --image "$image" --restart Never -- /bin/sh -c 'cat /etc/redhat-release || true' 2>/dev/null | grep -q "CentOS Linux release 7" && printf "CentOS 7 image: %s\n" "$image" >> centos7_images.txt | |
| } & | |
| done | |
| # Wait for all pods to finish | |
| wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment