-
Notifications
You must be signed in to change notification settings - Fork 64
/
kubectl-image
executable file
·87 lines (77 loc) · 2.71 KB
/
kubectl-image
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
#!/bin/bash
usage() { echo -e "Search for docker images.\nUsage: kubectl img <options> -i <image>" && grep " .)\ #" $0; exit 0; }
[ $# -eq 0 ] && echo -e "\nERROR: Missing image name [-i].\n" && usage
while getopts ":i:cph" arg; do
case $arg in
i) # Image name to search.
IMAGE=${OPTARG}
;;
c) # Configure your Google Registry. Uses Docker Hub if not provided.
echo -e "\nType the name of your Google registry (i.e: us.gcr.io/myProject): "
read REGISTRY
read -p "Save ${REGISTRY} for future reference? [y/n]: " save
if [[ "$save" == "y" ]]; then
# Overwrite or add default registry
ex '+g/IMG_REGISTRY=/d' -cwq ~/.bash_profile
echo 'export IMG_REGISTRY="'$REGISTRY'"' >> ~/.bash_profile
echo -e "\nSaved to ~/.bash_profile. Source or open a new terminal to take effect."
fi
IMG_REGISTRY="$REGISTRY"
;;
p) # Search Docker hub. Default, useful only if you've configured a default registry with [-c].
IMG_REGISTRY=""
;;
h) # Display help.
usage
exit 0
;;
*)
;;
esac
done
if [ "$#" -eq 1 ] && [[ ! "$*" =~ "^-" ]]; then
IMAGE="$*"
fi
REGISTRY=${IMG_REGISTRY:-}
RESTORE=$(echo -en '\033[0m')
LGREYBG=$(echo -en '\033[38;5;00m \033[48;5;7m')
DGREYBG=$(echo -en '\033[38;5;15m \033[48;5;8m')
BLUEBG=$(echo -en '\033[38;5;15m \033[48;5;27m')
if [[ "$IMAGE" ]]; then
IFS=$'\n'
case "$REGISTRY" in
*"gcr.io"*)
echo -e "\nSearching $REGISTRY for $IMAGE."
available_images=$(gcloud container images list-tags ${REGISTRY}/${IMAGE} --limit=20 --format='(digest,tags,timestamp.datetime)' | grep -v TAGS)
[ $? -ne 0 ] && echo -e "\nNo images found for ${REGISTRY}/${IMAGE}.\n" && exit 1
count=1
;;
*)
REGISTRY=''
echo -e "\nSearching Docker Hub for $IMAGE."
available_images=($(docker search --limit=25 "${IMAGE}" | sed -e 's|TAG||g; s|\[OK\]||g; s|AUTOMATED||g; s|OFFICIAL||g' | sed 's/[[:blank:]]*$//'))
[ "${#available_images[@]}" -le 1 ] && echo -e "\nNo images found for $IMAGE.\n" && exit 1
count=0
;;
esac
COLOR="$LGREYBG"
echo -e "\nLatest tags for ${IMAGE}:\n"
index=' '
for i in ${available_images[@]}; do
if [ ${count} -gt 0 ]; then
index='['${count}']'
fi
if [ $count -lt 10 ]; then
PAD=' '
elif [ $count -ge 10 ]; then
PAD=''
fi
let count=count+1
echo "${COLOR}${index}${PAD} $i${RESTORE}"
[[ "$COLOR" == "$LGREYBG" ]] && COLOR="$DGREYBG" || COLOR="$LGREYBG"
done
echo
else
echo -e "\n\nMust provide the image name [-i] if passing other flags.\n"
exit 1
fi