004_CreateGatewayRoute.sh 2.74 KB
Newer Older
据说甜蜜呢 committed
1 2 3 4
#!/bin/bash
set -e
IFS=$'\n\n'

据说甜蜜呢 committed
5 6 7 8 9
# Import external functions
chmod +x ./devops/PipeLines/Functions.core.sh
source ./devops/PipeLines/Functions.core.sh

GetAppName appName
据说甜蜜呢 committed
10

据说甜蜜呢 committed
11
echo "Start dynamically building the gateway route for ${appName}..."
据说甜蜜呢 committed
12

据说甜蜜呢 committed
13 14 15 16 17
GetMajor major
GetNameSpace namespace
declare fdnOfK8s="${namespace}.svc.cluster.local"
declare kongServiceBaseUrl="${KongApiServer}/services"
declare kongRouteBaseUrl="${KongApiServer}/routes"
据说甜蜜呢 committed
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
declare releaseVersion="v${major}"

# resilience handle
# Maximum time in seconds that you allow the whole operation to take.
declare maxTime=5 
# Maximum time in seconds that you allow the connection to the server to take.
declare maxConnectTime=2
declare retryCount=5

## add services
function createService()
{
	curl -X POST $1 \
	--connect-timeout $2 \
	--max-time $3 \
	--retry $4 \
	-H  "accept: application/json" \
	-H  "Content-Type: application/json" \
	-d "{ \"name\": \"$5\",  \"url\": \"$6\"}";
}

## add routes
function createRoute()
{
    declare svcResponse=$(curl -X GET ${kongServiceBaseUrl}/$5 --connect-timeout $2 --max-time $3 --retry $4)
    declare JQ_EXEC=`which jq`
    declare svcId=$(echo $svcResponse | ${JQ_EXEC} .id | sed 's/\"//g')
	declare defMethods="[\"GET\",\"POST\"]"

	set +e
	if [ -n "$8" ]; then
	   defMethods=$8
	fi

	if [ -z "$svcId" ]; then
	  echo "Warnning, failed to get the service[$5] identifier, route cannot be created.";
    else
	  # idempotent
	  declare routesAdded=$(curl -X GET ${kongServiceBaseUrl}/$5/routes)
	  declare routeid=$(echo $routesAdded | ${JQ_EXEC} .data[0].id | sed 's/\"//g')
	  if [ "$routeid" == "null" ]; then
        curl -X POST $1 \
	    --connect-timeout $2 \
	    --max-time $3 \
	    --retry $4 \
	    -H  "accept: application/json" \
	    -H  "Content-Type: application/json" \
据说甜蜜呢 committed
65
	    -d "{ \"service\": "{\"id\":\"$svcId\"}",\"paths\": "[\"$6\"]",\"methods\": "$defMethods",\"strip_path\":$7,\"hosts\": "[\"${KongRouteDomain}\"]"}";
据说甜蜜呢 committed
66 67 68 69 70
      fi
	fi
	set -e
}

据说甜蜜呢 committed
71
for servicePrefix in `ls ./src/Services|xargs -d '/'`
据说甜蜜呢 committed
72
do
据说甜蜜呢 committed
73
  GetServiceName ${servicePrefix} serviceName
据说甜蜜呢 committed
74
  # replace . to -, compatible with k8s.
据说甜蜜呢 committed
75
  Replace ${serviceName} '.' '-' serviceName
据说甜蜜呢 committed
76
  ToLower "${serviceName}-${releaseVersion}" serviceNameWithVersion
据说甜蜜呢 committed
77
  ToLower "http://${serviceFdn}/api/${prefix}" serviceUrl
据说甜蜜呢 committed
78 79

  echo "Begin creating service[${serviceNameWithVersion}]"
据说甜蜜呢 committed
80
  createService ${kongServiceBaseUrl} ${maxConnectTime} ${maxTime} ${retryCount} ${serviceNameWithVersion} ${serviceUrl}
据说甜蜜呢 committed
81 82

  echo "Begin creating route of service[${serviceNameWithVersion}]" 
据说甜蜜呢 committed
83
  ToLower "/api/${releaseVersion}/${servicePrefix}" serviceRouteUrl
据说甜蜜呢 committed
84
  createRoute ${kongRouteBaseUrl} ${maxConnectTime} ${maxTime} ${retryCount} ${serviceNameWithVersion} ${serviceRouteUrl} true
据说甜蜜呢 committed
85 86 87 88
done

echo ""
echo "Dynamicly building gateway route successfully !!!"