| Alex | f7c7863 | 2021-02-12 16:44:02 -0600 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # files | 
|  | 3 | ca_crt=$(pwd)/ca.crt | 
|  | 4 | client_crt=$(pwd)/client.crt | 
|  | 5 | client_key=$(pwd)/client.key | 
|  | 6 |  | 
|  | 7 | function show_help() { | 
|  | 8 | printf "\ngen_kubespec.sh <kubeconfig.yaml>\n" | 
|  | 9 | exit 1 | 
|  | 10 | } | 
|  | 11 |  | 
|  | 12 | # Check for a config file | 
|  | 13 | if [[ -z ${1+x} ]]; then | 
|  | 14 | show_help | 
|  | 15 | printf "\nERROR: No kubeconfig.yaml specified\n" | 
|  | 16 | exit 1 | 
|  | 17 | fi | 
|  | 18 |  | 
|  | 19 | # Check if file exists | 
|  | 20 | if [[ ! -f $1 ]]; then | 
|  | 21 | show_help | 
|  | 22 | printf "\nERROR: Supplied kubeconfig file not exists at '$1'\n" | 
|  | 23 | exit 1 | 
|  | 24 | fi | 
|  | 25 |  | 
|  | 26 | # extract data as variables | 
| Alex | 18fc381 | 2021-03-22 09:41:37 -0500 | [diff] [blame] | 27 | declare $(sed -e 's/:[^:\/\/,:443,:6443]/=/g;s/ *=/=/g;s/-/_/g' $1 | grep 'certificate\|key\|server' | tr -d ' ') | 
|  | 28 | echo "# Declared variable: server=$server" | 
| Alex | f7c7863 | 2021-02-12 16:44:02 -0600 | [diff] [blame] | 29 |  | 
|  | 30 | ### Uncomment if separate files needed | 
| Alex | 18fc381 | 2021-03-22 09:41:37 -0500 | [diff] [blame] | 31 | printf "# Creating 'ca.crt', 'client.crt' and 'client.key'\n" | 
| Alex | f7c7863 | 2021-02-12 16:44:02 -0600 | [diff] [blame] | 32 | echo "# '${ca_crt}'" | 
|  | 33 | echo $certificate_authority_data | base64 -d >${ca_crt} | 
|  | 34 | echo "# '${client_crt}'" | 
|  | 35 | echo $client_certificate_data | base64 -d >${client_crt} | 
|  | 36 | echo "# '${client_key}'" | 
|  | 37 | echo $client_key_data | base64 -d >${client_key} | 
|  | 38 |  | 
|  | 39 | printf "Generating 'kubespec.yaml'\n" | 
|  | 40 | cat << EOF >kubespec_generated.yaml | 
|  | 41 | --- | 
|  | 42 | existing@kubernetes: | 
|  | 43 | server: $server | 
|  | 44 | certificate-authority: ${ca_crt} | 
|  | 45 | client-certificate: ${client_crt} | 
|  | 46 | client-key: ${client_key} | 
|  | 47 | tls_insecure: True | 
|  | 48 | EOF |