|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# List all unqualified images in all pods in all namespaces. |
| 4 | + |
| 5 | +strindex() { |
| 6 | + local x="${1%%$2*}" # find occurrence of $2 in $1 |
| 7 | + [[ "$x" == "$1" ]] && echo -1 || echo "${#x}" |
| 8 | +} |
| 9 | + |
| 10 | +# Finds any-of chars in string. Returns 0 on success, otherwise 1. |
| 11 | +strchr() { |
| 12 | + local str=$1 |
| 13 | + local chars=$2 |
| 14 | + for (( i=0; i<${#chars}; i++ )); do |
| 15 | + [[ $(strindex "$str" "${chars:$i:1}") -ge 0 ]] && return 0 |
| 16 | + done |
| 17 | + return 1 |
| 18 | +} |
| 19 | + |
| 20 | +split_image_at_domain() { |
| 21 | + local image=$1 |
| 22 | + local index=$(strindex "$image" "/") |
| 23 | + |
| 24 | + if [[ "$index" == -1 ]] || (! strchr "${image:0:$index}" ".:" && [[ "${image:0:$index}" != "localhost" ]]); then |
| 25 | + echo "" |
| 26 | + else |
| 27 | + echo "${image:0:$index}" |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +has_domain() { |
| 32 | + local image=$1 |
| 33 | + [[ -n $(split_image_at_domain "$image") ]] |
| 34 | +} |
| 35 | + |
| 36 | +die() { |
| 37 | + echo "$*" 1>&2 |
| 38 | + exit 1 |
| 39 | +} |
| 40 | + |
| 41 | +self_test() { |
| 42 | + strchr "foo/busybox" "Z" && die "self-test 1 failed" |
| 43 | + |
| 44 | + strchr "foo/busybox" "/" || die "self-test 2 failed" |
| 45 | + strchr "foo/busybox" "Zx" || die "self-test 3 failed" |
| 46 | + |
| 47 | + has_domain "foo" && die "self-test 4 failed" |
| 48 | + has_domain "foo/busybox" && die "self-test 5 failed" |
| 49 | + has_domain "repo/foo/busybox" && die "self-test 6 failed" |
| 50 | + has_domain "a/b/c/busybox" && die "self-test 7 failed" |
| 51 | + |
| 52 | + has_domain "localhost/busybox" || die "self-test 8 failed" |
| 53 | + has_domain "localhost:5000/busybox" || die "self-test 9 failed" |
| 54 | + has_domain "foo.com:5000/busybox" || die "self-test 10 failed" |
| 55 | + has_domain "docker.io/busybox" || die "self-test 11 failed" |
| 56 | + has_domain "a.b.c.io/busybox" || die "self-test 12 failed" |
| 57 | +} |
| 58 | + |
| 59 | +[[ -n ${SELF_TEST:-yes} ]] && self_test |
| 60 | + |
| 61 | +images="$(kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}")" |
| 62 | + |
| 63 | +for image in $images; do |
| 64 | + has_domain "$image" || echo "$image" |
| 65 | +done |
0 commit comments