Created
September 7, 2020 12:39
-
-
Save hassenius/9c686068da9555faa78d42ce50536362 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
function show_help() { | |
echo "Usage: " | |
echo $0 | |
echo "-u <username>" | |
echo "-p <password>" | |
echo "-r <list of registries>" | |
echo "-d <dockerconfig.json>" | |
echo "" | |
echo "Either use ${0} -d <location of dockerconfig.json> to add all auths from your docker config json to the clusters global pull secret" | |
echo "or specify the username and password together with a list of registries to add to the global pull secrets" | |
echo "" | |
echo "Examples: " | |
echo "${0} -d ~/.docker/config.json" | |
echo "" | |
echo "${0} -u [email protected] -p some_password -r myregistry1.somewhere,myregistry2.somewhereelse,myregistry3.somewhereelseagain" | |
} | |
if [[ -z "${1}" ]]; then | |
show_help | |
fi | |
while getopts "h?d:u:p:r:" opt; do | |
case "$opt" in | |
h|\?) | |
show_help | |
exit 0 | |
;; | |
d) | |
DOCKERCONFIG=$OPTARG | |
;; | |
u) | |
USERNAME=$OPTARG | |
;; | |
p) | |
PASSWORD=$OPTARG | |
;; | |
r) | |
declare -a REGISTRIES | |
IFS=',' read -r -a REGISTRIES <<< "$OPTARG" | |
;; | |
*) | |
show_help | |
exit 0 | |
;; | |
esac | |
done | |
if ! which jq &>/dev/null ; then | |
echo "Please ensure that which and jq is installed" | |
exit 1 | |
fi | |
if ! which oc &>/dev/null ; then | |
echo "Please ensure that which and oc command line tool is installed" | |
exit 1 | |
fi | |
if ! which base64 &>/dev/null ; then | |
echo "Please ensure that which and base64 tool is installed" | |
exit 1 | |
fi | |
if [[ ! -z "${DOCKERCONFIG}" ]]; then | |
echo "Attempting to add auth from ${DOCKERCONFIG}" | |
if [[ ! -f ${DOCKERCONFIG} ]]; then | |
echo "ERROR: Could not find file ${DOCKERCONFIG}" | |
exit 1 | |
fi | |
AUTH_INPUT=${DOCKERCONFIG} | |
fi | |
if [[ ! -z "${USERNAME}" && ! -z "${PASSWORD}" && ! -z "${REGISTRIES}" ]]; then | |
echo "Attempting to generate dockerconfig based on ${USERNAME}, ${PASSWORD}, ${REGISTRIES} " | |
AUTH_INPUT=dockerconfig.tmp.json | |
echo '{"auths": {' >dockerconfig.tmp.json | |
for registry in ${REGISTRIES[@]}; do | |
cat <<EOF >>dockerconfig.tmp.json | |
"$registry": { | |
"auth": "$(echo ${USERNAME}:${PASSWORD} | base64 )", | |
"email": "${USERNAME}" | |
} | |
EOF | |
if [[ ! ${registry} == ${REGISTRIES[-1]} ]]; then | |
echo "," >> dockerconfig.tmp.json | |
fi | |
done | |
echo '}}' >>dockerconfig.tmp.json | |
fi | |
if [[ -z "${AUTH_INPUT}" ]]; then | |
echo "Please provide valid input" | |
exit 1 | |
fi | |
# First get the json of the current global pull secret | |
orgdc=$(oc -n openshift-config get secret/pull-secret -o jsonpath='{.data.\.dockerconfigjson}') | |
echo "Backing up original pull secrets to ./originaldockerconfig.json" | |
echo ${orgdc} | base64 --decode > originaldocerconfig.json | |
# Add existing json and new json together | |
echo ${orgdc} | base64 --decode | jq -s '.[0] * .[1]' ${AUTH_INPUT} - > newdockerconfig.json | |
# Update the global docker config | |
oc set data secret/pull-secret -n openshift-config --from-file=.dockerconfigjson=newdockerconfig.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment