11#! /usr/bin/env bash
22
3+ #
4+ # Taken from: https://wp.vpalos.com/537/uri-parsing-using-bash-built-in-features/
5+ #
6+
7+ #
8+ # URI parsing function
9+ #
10+ # The function creates global variables with the parsed results.
11+ # It returns 0 if parsing was successful or non-zero otherwise.
12+ #
13+ # [schema://][user[:password]@]host[:port][/path][?[arg1=val1]...][#fragment]
14+ #
15+ function uri_parser() {
16+ # uri capture
17+ uri=" $@ "
18+
19+ # safe escaping
20+ uri=" ${uri// \` /% 60} "
21+ uri=" ${uri// \" /% 22} "
22+
23+ # top level parsing
24+ pattern=' ^(([a-z]{3,5})://)?((([^:\/]+)(:([^@\/]*))?@)?([^:\/?]+)(:([0-9]+))?)(\/[^?]*)?(\?[^#]*)?(#.*)?$'
25+ [[ " $uri " =~ $pattern ]] || return 1;
26+
27+ # component extraction
28+ uri=${BASH_REMATCH[0]}
29+ uri_schema=${BASH_REMATCH[2]}
30+ uri_address=${BASH_REMATCH[3]}
31+ uri_user=${BASH_REMATCH[5]}
32+ uri_password=${BASH_REMATCH[7]}
33+ uri_host=${BASH_REMATCH[8]}
34+ uri_port=${BASH_REMATCH[10]}
35+ uri_path=${BASH_REMATCH[11]}
36+ uri_query=${BASH_REMATCH[12]}
37+ uri_fragment=${BASH_REMATCH[13]}
38+
39+ # path parsing
40+ count=0
41+ path=" $uri_path "
42+ pattern=' ^/+([^/]+)'
43+ while [[ $path =~ $pattern ]]; do
44+ eval " uri_parts[$count ]=\" ${BASH_REMATCH[1]} \" "
45+ path=" ${path: ${# BASH_REMATCH[0]} } "
46+ let count++
47+ done
48+
49+ # query parsing
50+ count=0
51+ query=" $uri_query "
52+ pattern=' ^[?&]+([^= ]+)(=([^&]*))?'
53+ while [[ $query =~ $pattern ]]; do
54+ eval " uri_args[$count ]=\" ${BASH_REMATCH[1]} \" "
55+ eval " uri_arg_${BASH_REMATCH[1]} =\" ${BASH_REMATCH[3]} \" "
56+ query=" ${query: ${# BASH_REMATCH[0]} } "
57+ let count++
58+ done
59+
60+ # return success
61+ return 0
62+ }
63+
364json=$( echo " $1 " )
465name=$( echo " $json " | jq -r ' .metadata.name' )
566namespace=$( echo " $json " | jq -r ' .metadata.namespace' )
667kind=$( echo " $json " | jq -r ' .kind' )
768secret=$( echo " $json " | jq -r ' .spec.secret.name' )
869read=$( echo " $json " | jq -r ' .spec.service.read' )
970write=$( echo " $json " | jq -r ' .spec.service.write' )
71+
72+ uri_parser " ${write} " || { echo " Malformed URI! ${write} " ; exit 1; }
73+ uri_host_port=" ${uri_host} :${uri_port} "
74+ write_dsn=" ${uri_schema} ://${uri_address} /"
75+ uri_parser " ${read} " || { echo " Malformed URI! ${read} " ; exit 1; }
76+ read_dsn=" ${uri_schema} ://${uri_address} /"
77+
1078echo " ${namespace} /${name} object is added"
1179echo " ${namespace} /${secret} is target secret"
1280echo " ${read} is read service"
@@ -15,24 +83,24 @@ echo "${write} is write service"
1583(kubectl get secret -n " ${namespace} " " ${secret} " )
1684if [[ " $? " == " 1" ]] ; then
1785 maxDatabases=$( redis-cli -u " ${write} " CONFIG GET databases | grep -v databases)
18- redisServerIsKnown=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r -c ' .data.dbs' | jq -r " .\" ${write } \" " | wc -l)
86+ redisServerIsKnown=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r -c ' .data.dbs' | jq -r " .\" ${uri_host_port } \" " | wc -l)
1987 if [[ " $redisServerIsKnown " == " 1" ]] ; then
20- kubectl create configmap redis-database-assignment-operator-in-use-dbs-list --from-literal=dbs=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq -r " . * {\" ${write } \" : {}}" | jq -c) --dry-run -o yaml | kubectl apply -f -
88+ kubectl create configmap redis-database-assignment-operator-in-use-dbs-list --from-literal=dbs=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq -r " . * {\" ${uri_host_port } \" : {}}" | jq -c) --dry-run -o yaml | kubectl apply -f -
2189 for i in $( seq 0 $maxDatabases ) ;
2290 do
23- kubectl create configmap redis-database-assignment-operator-in-use-dbs-list --from-literal=dbs=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq -r " . * {\" ${write } \" : {\" db${i} \" : \" free\" }}" | jq -c) --dry-run -o yaml | kubectl apply -f -
91+ kubectl create configmap redis-database-assignment-operator-in-use-dbs-list --from-literal=dbs=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq -r " . * {\" ${uri_host_port } \" : {\" db${i} \" : \" free\" }}" | jq -c) --dry-run -o yaml | kubectl apply -f -
2492 done
2593 fi
2694
2795 echo " Secret ${namespace} /${secret} doesn't exist"
2896
2997 for i in $( seq 0 $maxDatabases ) ;
3098 do
31- isDatabaseFree=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq " .[\" ${write } \" ].db${i} " | grep free | wc -l)
99+ isDatabaseFree=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq " .[\" ${uri_host_port } \" ].db${i} " | grep free | wc -l)
32100 if [[ " $isDatabaseFree " == " 1" ]] ; then
33101 echo " Database ${i} is available"
34- kubectl create secret generic " ${secret} " -n " ${namespace} " --from-literal=database=" ${i} " --from-literal=read=" ${read }${i} " --from-literal=write=" ${write }${i} "
35- kubectl create configmap redis-database-assignment-operator-in-use-dbs-list --from-literal=dbs=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq -r " . * {\" ${write } \" : {\" db${i} \" : \" ${namespace} /${secret} \" }}" | jq -c) --dry-run -o yaml | kubectl apply -f -
102+ kubectl create secret generic " ${secret} " -n " ${namespace} " --from-literal=database=" ${i} " --from-literal=read=" ${read_dsn }${i} " --from-literal=write=" ${write_dsn }${i} "
103+ kubectl create configmap redis-database-assignment-operator-in-use-dbs-list --from-literal=dbs=$( kubectl get configmap redis-database-assignment-operator-in-use-dbs-list -o json | jq -r ' .data.dbs' | jq -r " . * {\" ${uri_host_port } \" : {\" db${i} \" : \" ${namespace} /${secret} \" }}" | jq -c) --dry-run -o yaml | kubectl apply -f -
36104 echo " Database ${i} has now been claimed"
37105 break
38106 fi
0 commit comments